Smarthome Functionen
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import json
  2. import logging
  3. import task
  4. try:
  5. from config import APP_NAME as ROOT_LOGGER_NAME
  6. except ImportError:
  7. ROOT_LOGGER_NAME = 'root'
  8. class common_base(dict):
  9. DEFAULT_VALUES = {}
  10. def __init__(self, default_values=None):
  11. super().__init__(default_values or self.DEFAULT_VALUES)
  12. #
  13. self.__callback_list__ = []
  14. self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild("devices")
  15. def add_callback(self, key, data, callback, on_change_only=False, init_now=False):
  16. """
  17. key: key or None for all keys
  18. data: data or None for all data
  19. """
  20. cb_tup = (key, data, callback, on_change_only)
  21. if cb_tup not in self.__callback_list__:
  22. self.__callback_list__.append(cb_tup)
  23. if init_now and self.get(key) is not None:
  24. callback(self, key, self[key])
  25. def set(self, key, data, block_callback=[]):
  26. if key in self.keys():
  27. value_changed = self[key] != data
  28. self[key] = data
  29. for cb_key, cb_data, callback, on_change_only in self.__callback_list__:
  30. if cb_key is None or key == cb_key: # key fits to callback definition
  31. if cb_data is None or cb_data == self[key]: # data fits to callback definition
  32. if value_changed or not on_change_only: # change status fits to callback definition
  33. if not callback in block_callback: # block given callbacks
  34. callback(self, key, self[key])
  35. else:
  36. self.logger.warning("Unexpected key %s", key)
  37. class mqtt_base(common_base):
  38. def __init__(self, mqtt_client, topic, default_values=None):
  39. super().__init__(default_values)
  40. #
  41. self.mqtt_client = mqtt_client
  42. self.topic = topic
  43. for entry in self.topic.split('/'):
  44. self.logger = self.logger.getChild(entry)
  45. class videv_base(mqtt_base):
  46. KEY_INFO = '__info__'
  47. #
  48. SET_TOPIC = "set"
  49. def __init__(self, mqtt_client, topic, default_values=None):
  50. super().__init__(mqtt_client, topic, default_values=default_values)
  51. self.__display_dict__ = {}
  52. self.__control_dict__ = {}
  53. self.__periodic__ = task.periodic(300, self.send_all)
  54. self.__periodic__.run()
  55. def send_all(self, rt):
  56. for key in self:
  57. if self[key] is not None:
  58. self.__tx__(key, self[key])
  59. def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
  60. """
  61. listen to data changes of ext_device and update videv information
  62. """
  63. if my_key not in self.keys():
  64. self[my_key] = None
  65. if ext_device.__class__.__name__ == "group":
  66. # store information to identify callback from ext_device
  67. self.__display_dict__[(id(ext_device[0]), ext_key)] = my_key
  68. # register a callback to listen for data from external device
  69. ext_device[0].add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only, init_now=True)
  70. else:
  71. # store information to identify callback from ext_device
  72. self.__display_dict__[(id(ext_device), ext_key)] = my_key
  73. # register a callback to listen for data from external device
  74. ext_device.add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only, init_now=True)
  75. # send initial display data to videv interface
  76. data = ext_device.get(ext_key)
  77. if data is not None:
  78. self.__tx__(my_key, data)
  79. def __rx_ext_device_data__(self, ext_device, ext_key, data):
  80. my_key = self.__display_dict__[(id(ext_device), ext_key)]
  81. self.set(my_key, data)
  82. self.__tx__(my_key, data)
  83. def __tx__(self, key, data):
  84. if type(data) not in (str, ):
  85. data = json.dumps(data)
  86. self.mqtt_client.send('/'.join([self.topic, key]), data)
  87. def add_control(self, my_key, ext_device, ext_key, on_change_only=True):
  88. """
  89. listen to videv information and pass data to ext_device
  90. """
  91. if my_key not in self.keys():
  92. self[my_key] = None
  93. # store information to identify callback from videv
  94. self.__control_dict__[my_key] = (ext_device, ext_key, on_change_only)
  95. # add callback for videv changes
  96. self.mqtt_client.add_callback('/'.join([self.topic, my_key, self.SET_TOPIC]), self.__rx_videv_data__)
  97. def __rx_videv_data__(self, client, userdata, message):
  98. my_key = message.topic.split('/')[-2]
  99. try:
  100. data = json.loads(message.payload)
  101. except json.decoder.JSONDecodeError:
  102. data = message.payload
  103. ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
  104. if my_key in self.keys():
  105. if data != self[my_key] or not on_change_only:
  106. ext_device.send_command(ext_key, data)
  107. else:
  108. self.logger.info("Ignoring rx message with topic %s", message.topic)
  109. def add_routing(self, my_key, ext_device, ext_key, on_change_only_disp=True, on_change_only_videv=True):
  110. """
  111. listen to data changes of ext_device and update videv information
  112. and
  113. listen to videv information and pass data to ext_device
  114. """
  115. # add display
  116. self.add_display(my_key, ext_device, ext_key, on_change_only_disp)
  117. self.add_control(my_key, ext_device, ext_key, on_change_only_videv)