def run(args): logging.basicConfig( format="%(asctime)s [%(threadName)-15s] %(levelname)-6s %(message)s", #filename=os.path.join('./', "app.out"), level=max(3 - args.verbose_count, 0) * 10) try: config = parseConfig(args.conf_file) except Exception as e: logging.error("Failure while reading configuration file '%s': %r" % (args.conf_file, e)) return m = mqtt.Mqtt(config) m.connect() f = fritzbox.Fritzbox(config) f.connect() stopEvent = threading.Event() t = threading.Thread(target=processFritzboxMessages, args=[m, f, stopEvent], name="processFritzboxMessages") t.daemon = True t.start() try: while True: stopEvent.wait(60) except (SystemExit, KeyboardInterrupt): # Normal exit getting a signal from the parent process pass except: # Something unexpected happened? logging.exception("Exception") finally: logging.info("Shutting down ...") stopEvent.set() f.disconnect() t.join() m.disconnect() logging.shutdown()
t1.join() if __name__ == "__main__": _LOGGER.info("Loading config file...") config = yamlparser.load_yaml('config/config.yaml') gateway_pass = yamlparser.get_gateway_password(config) polling_interval = config['gateway'].get("polling_interval", 2) polling_models = config['gateway'].get("polling_models", ['motion']) gateway_ip = config['gateway'].get("ip", None) signal.signal(signal.SIGINT, exit_handler) signal.signal(signal.SIGTERM, exit_handler) _LOGGER.info("Init mqtt client.") client = mqtt.Mqtt(config) client.connect() # only this devices can be controlled from MQTT client.subscribe("gateway", "+", "+", "set") client.subscribe("gateway", "+", "write", None) client.subscribe("plug", "+", "status", "set") gateway = XiaomiHub(gateway_pass, gateway_ip, config) stop_event = threading.Event() t1 = threading.Thread(target=process_gateway_messages, args=[gateway, client, stop_event]) t1.daemon = True t1.start() t2 = threading.Thread(target=process_mqtt_messages, args=[gateway, client, stop_event])
#!/usr/bin/python # -*- coding: utf-8 -*- import signal import sys import time import traceback # user modules from gw_message import parse_gateway_message, GatewayMessageError, GatewayMessageIgnored from monitoring import MonLog import mqtt import settings MQTT_CLIENT = mqtt.Mqtt() MON_LOG = MonLog() def shutdown(): ''' Shutdown the process gracefully. ''' MQTT_CLIENT.client.loop_stop() settings.logger.debug("MQTT client loop stopped.") settings.logger.info("Client disconnecting.") MQTT_CLIENT.client.disconnect() def terminate_signal_handler(_signal, _frame):
import command import environment as env import instruction import led import os import mqtt import tlc5947 import queuehandler mqtt = mqtt.Mqtt() mqtt.connect() led.mqtt = mqtt tlc5947 = tlc5947.tlc5947() def on_instruction(instruction): if (env.logLevel == env.DEBUG): print(f"on_instruction: {instruction}") tlc5947.handle(instruction) instructionHandler = queuehandler.handler() instructionHandler.set_callback(on_instruction) instructionHandler.loop_start() ledController = led.controller(tlc5947.numberOfLeds, instructionHandler) def on_command(command): if (env.logLevel == env.DEBUG):