Smarthome Functionen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smart_brain.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import config
  2. import function
  3. import json
  4. import logging
  5. import mqtt
  6. import os
  7. import report
  8. import subprocess
  9. import time
  10. logger = logging.getLogger(config.APP_NAME)
  11. # TODO: implement garland (incl. day events like sunset, sunrise, ...)
  12. VERS_MAJOR = 1
  13. VERS_MINOR = 2
  14. VERS_PATCH = 0
  15. INFO_TOPIC = "__info__"
  16. INFO_DATA = {
  17. "app_name": os.path.splitext(os.path.basename(__file__))[0],
  18. "version": {
  19. "readable": "%d.%d.%d" % (VERS_MAJOR, VERS_MINOR, VERS_PATCH),
  20. "major": VERS_MAJOR,
  21. "minor": VERS_MINOR,
  22. "patch": VERS_PATCH
  23. },
  24. "git": {
  25. "url": subprocess.check_output(["git", "config", "--get", "remote.origin.url"])[:-1].decode("utf-8"),
  26. "ref": subprocess.check_output(["git", "log", "-1", '--format="%H"'])[1:-2].decode("utf-8")
  27. }
  28. }
  29. def __info_publisher__(client, userdata, message):
  30. data = json.loads(message.payload)
  31. if data != INFO_DATA:
  32. client.publish(INFO_TOPIC, json.dumps(INFO_DATA))
  33. if __name__ == "__main__":
  34. if config.DEBUG:
  35. report.appLoggingConfigure(None, None, ((config.APP_NAME, logging.DEBUG), ), fmt=report.SHORT_FMT, host='localhost', port=19996)
  36. else:
  37. report.stdoutLoggingConfigure(((config.APP_NAME, logging.WARNING), ), report.SHORT_FMT)
  38. #
  39. mc = mqtt.mqtt_client(host=config.MQTT_SERVER, port=config.MQTT_PORT, username=config.MQTT_USER,
  40. password=config.MQTT_PASSWORD, name=config.APP_NAME)
  41. mc.add_callback(INFO_TOPIC, __info_publisher__)
  42. func = function.all_functions(mc)
  43. while (True):
  44. time.sleep(1)