Smarthome Functionen
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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. VERS_MAJOR = 1
  12. VERS_MINOR = 2
  13. VERS_PATCH = 6
  14. INFO_TOPIC = "__info__"
  15. INFO_DATA = {
  16. "app_name": os.path.splitext(os.path.basename(__file__))[0],
  17. "version": {
  18. "readable": "%d.%d.%d" % (VERS_MAJOR, VERS_MINOR, VERS_PATCH),
  19. "major": VERS_MAJOR,
  20. "minor": VERS_MINOR,
  21. "patch": VERS_PATCH
  22. },
  23. "git": {
  24. "url": subprocess.check_output(["git", "config", "--get", "remote.origin.url"])[:-1].decode("utf-8"),
  25. "ref": subprocess.check_output(["git", "log", "-1", '--format="%H"'])[1:-2].decode("utf-8")
  26. }
  27. }
  28. def __info_publisher__(client, userdata, message):
  29. data = json.loads(message.payload)
  30. if data != INFO_DATA:
  31. client.publish(INFO_TOPIC, json.dumps(INFO_DATA))
  32. if __name__ == "__main__":
  33. if config.DEBUG:
  34. report.appLoggingConfigure(None, 'stdout', ((config.APP_NAME, logging.DEBUG), ),
  35. target_level=logging.WARNING, 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)