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

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