Running the metering system

Metering system main event loop

Synopsis

This script is main loop which handles the system flow

Authors

Steffen, Thomas, Janus

Latest update

17 November 2020

Version

0.92

Version history

  • Ver. 0.1: Build main loop with queue and Mqtt startup.

  • Ver. 0.9: Implement mqtt to get command from ReCalc, dispatcher, and mqtt to send data to ReCalc.

  • Ver. 0.91: Implement (1) gw-id from settings into topics, (2) mqtt pub rc check (log on err), (3) Use DEBUG instead of print.

  • Ver. 0.92: Move functions out of __main__ section to document them.

Starting and stopping the system

  • Full system is started using shell script start_stop_system.sh.
    • Start: ./start_stop_system.sh start

    • Stop: ./start_stop_system.sh stop

  • Driver will be running as daemon and will create a FIFO as driver/IM871A_pipe.

  • This script will visibly run in terminal (debug).

Stopping the system over MQTT

  • Send message with topic: STOP to end program. Example:
    • mosquitto_pub -h <INSERT IP> -p 1883 -t STOP -m ‘anything’ -u <INSERT USER> -P <INSERT PWD>

Data flows

  • Config flow: ReCalc command (mqtt) -> Update Dispatcher

  • Data flow: Driver -> (FIFO) -> C1-parser -> Dispatcher -> Handler (OmniPower) -> Mqtt publish data

  • Error logging flow: On errors -> Logger -> SysLog

Testing

  • Test message to start monitoring our OmniPower (fill in username and password):
    • mosquitto_pub -h <INSERT IP> -t “v2/706462169/sensors/set” -m ‘[{“deviceId”: “32666857”,”manufacturerKey”: “kam”,”encryptionKey”: “9A25139E3244CC2E391A8EF6B915B697”, “manufacturerDeviceKey”: “OmniPower1”}]’ -u <INSERT USER> -P <INSERT PWD>

  • Test to receive published data (fill in username and password):
    • mosquitto_sub -h <INSERT IP> -t “#” -u <INSERT USER> -P <INSERT PWD>

Handling data from ReCalc

Monitored list is built based on serial numbers from ReCalc messages:

  • Keeps object (dispatcher) to keep track of monitored meters.

  • No method (or way) to report if a serial numbers is invalid.

  • Invalid serial numbers will be monitored, but no data will ever be sent.

  • Consider expanding ReCalc Cloud API to receive messages about invalid commands.

  • Currently only able to send config messages for OmniPower devices.

Future implementation of more meters

  • Expand the api.config_json() function to take as arguments the name/type/manufacturer of a device.

  • Return a config string based on these properties.

  • Base configs on yaml files or other files that can be updated on the fly without restarting code.

Main event loop

run.run_system.run_system()[source]

Implements the main loop to run the entire system.

Support functions

run.run_system.on_command_callback(client, userdata, message)[source]

On every message from ReMoni ReCalc, the message is put into an atomic, threadsafe queue. Atomic deque, dq, is from global scope from __main__ section.

run.run_system.end_loop()[source]

Function to cleanly exit loop and end threads, disconnect. From __main__ section: FIFO queue, fifo; Mqtt subscriber, recalc; Mqtt publisher.

run.run_system.DEBUG(message: str)[source]

Prints out if global DEBUG_ON set to True.