diff options
Diffstat (limited to 'src/mqtt_node_util.py')
-rwxr-xr-x | src/mqtt_node_util.py | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/src/mqtt_node_util.py b/src/mqtt_node_util.py index 674b60c..0352c8f 100755 --- a/src/mqtt_node_util.py +++ b/src/mqtt_node_util.py @@ -1,4 +1,6 @@ #!/usr/bin/env python3 +import os.path +from time import sleep from typing import Optional from argparse import ArgumentParser, ArgumentError @@ -25,34 +27,47 @@ if __name__ == '__main__': help='mqtt modules to include') parser.add_argument('--switch-relay', choices=[0, 1], type=int, help='send relay state') - parser.add_argument('--switch-relay-secret', type=str, - help='secret password to switch relay') + parser.add_argument('--push-ota', type=str, metavar='OTA_FILENAME', + help='push ota, argument receives filename') + parser.add_argument('--node-secret', type=str, + help='node admin password') config.load('mqtt_util', parser=parser) arg = parser.parse_args() - if (arg.switch_relay is not None or arg.switch_relay_secret is not None) and 'relay' not in arg.modules: + if (arg.switch_relay is not None or arg.node_secret is not None) and 'relay' not in arg.modules: raise ArgumentError(None, '--relay is only allowed when \'relay\' module included in --modules') - if (arg.switch_relay is not None and arg.switch_relay_secret is None) or (arg.switch_relay is None and arg.switch_relay_secret is not None): - raise ArgumentError(None, 'both --switch-relay and --switch-relay-secret are required') - mqtt = MqttNode(node_id=arg.node_id) # must-have modules - add_module('ota') + ota_module = add_module('ota') add_module('diagnostics') if arg.modules: for m in arg.modules: module_instance = add_module(m) if m == 'relay' and arg.switch_relay is not None: + if not arg.node_secret: + raise ArgumentError(None, '--switch-relay requires --node-secret') module_instance.switchpower(mqtt, arg.switch_relay == 1, - arg.switch_relay_secret) + arg.node_secret) mqtt.configure_tls() try: - mqtt.connect_and_loop() + mqtt.connect_and_loop(loop_forever=False) + + if arg.push_ota: + if not os.path.exists(arg.push_ota): + raise OSError(f'--push-ota: file \"{arg.push_ota}\" does not exists') + if not arg.node_secret: + raise ArgumentError(None, 'pushing OTA requires --node-secret') + + ota_module.push_ota(arg.node_secret, arg.push_ota, 1) + + while True: + sleep(0.1) + except KeyboardInterrupt: mqtt.disconnect() |