Smarthome Functionen
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

smart_brain.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import config
  2. import devdi.devices
  3. import function
  4. import json
  5. import logging
  6. import mqtt
  7. import os
  8. import report
  9. import subprocess
  10. import time
  11. logger = logging.getLogger(config.APP_NAME)
  12. VERS_MAJOR = 1
  13. VERS_MINOR = 3
  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. #
  35. # Logging
  36. #
  37. if config.DEBUG:
  38. report.appLoggingConfigure(None, 'stdout', ((config.APP_NAME, logging.DEBUG), ),
  39. target_level=logging.WARNING, fmt=report.SHORT_FMT, host='localhost', port=19996)
  40. else:
  41. report.stdoutLoggingConfigure(((config.APP_NAME, config.LOGLEVEL), ), report.SHORT_FMT)
  42. #
  43. # MQTT Client
  44. #
  45. mc = mqtt.mqtt_client(host=config.MQTT_SERVER, port=config.MQTT_PORT, username=config.MQTT_USER,
  46. password=config.MQTT_PASSWORD, name=config.APP_NAME)
  47. mc.add_callback(INFO_TOPIC, __info_publisher__)
  48. #
  49. # Smarthome physical Devices
  50. #
  51. pd = devdi.devices.physical_devices(mc)
  52. #
  53. # Smarthome physical Devices
  54. #
  55. vd = devdi.devices.videv_devices(mc)
  56. #
  57. # Smart Home Functionality
  58. #
  59. func = function.all_functions(mc, pd, vd)
  60. while (True):
  61. time.sleep(1)