Module: MQTT <--> Powerplug Enegenie
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

powerplug.py 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import config
  2. import logging
  3. import paho.mqtt.client as mqtt
  4. import report
  5. import subprocess
  6. import time
  7. try:
  8. from config import APP_NAME as ROOT_LOGGER_NAME
  9. except ImportError:
  10. ROOT_LOGGER_NAME = 'root'
  11. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
  12. class sispmctl(object):
  13. AMP_CHANNEL = 0
  14. def __init__(self):
  15. logger.info("Starting sispmctl module...")
  16. self.__state__ = [False, False, False, False]
  17. self.__state_requests__ = [{}, {}, {}, {}]
  18. try:
  19. out_txt = subprocess.check_output(["sudo", "sispmctl", "-f", "all"]).decode('UTF-8')
  20. except subprocess.CalledProcessError as grepexc:
  21. logger.error("sispm error code %d", grepexc.returncode)
  22. else:
  23. logger.info('sispmctl all channels switched off')
  24. def set_out_state(self, output, state):
  25. if output in range(1,5):
  26. if self.__state__[output - 1] != state:
  27. try:
  28. out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state else "-f", str(output)]).decode('UTF-8')
  29. except subprocess.CalledProcessError as grepexc:
  30. logger.error("sispm error code %d", grepexc.returncode)
  31. else:
  32. self.__state__[output - 1] = state
  33. logger.info('sispmctl channel %d changed to %s', output, state)
  34. def get_out_state(self, output):
  35. return self.__state__[output - 1]
  36. class mqtt_powerplug(object):
  37. SUBTOPICS = [
  38. "set/1",
  39. "set/2",
  40. "set/3",
  41. "set/4"
  42. ]
  43. def __init__(self):
  44. self.__client__ = mqtt.Client("mqtt_powerplug") # create client object
  45. self.__client__.on_message = self.__receive__ # attach function to callback
  46. self.__client__.connect("192.168.0.131", 1883) # establish connection
  47. self.__client__.loop_start() # start the loop
  48. self.__topics__ = []
  49. for subtopic in self.SUBTOPICS:
  50. self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
  51. self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic
  52. self.__sc__ = sispmctl()
  53. def __receive__(self, client, userdata, message):
  54. if message.topic in self.__topics__:
  55. output = int(message.topic.split("/")[-1])
  56. state = message.payload == b"true"
  57. logger.info("Received request to set output channel %d to state %s", output, str(state))
  58. self.__sc__.set_out_state(output, state)
  59. else:
  60. logger.warning("Ignoring unknown mqtt topic %s", message.topic)
  61. def __del__(self):
  62. self.__client__.loop_stop() # stop the loop
  63. if __name__ == '__main__':
  64. report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
  65. #
  66. mp = mqtt_powerplug()
  67. while True:
  68. time.sleep(2)