Module: MQTT <--> Powerplug Enegenie
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

powerplug.py 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
  47. self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
  48. self.__client__.loop_start() # start the loop
  49. self.__topics__ = []
  50. for subtopic in self.SUBTOPICS:
  51. self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
  52. self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic
  53. self.__sc__ = sispmctl()
  54. def __receive__(self, client, userdata, message):
  55. if message.topic in self.__topics__:
  56. output = int(message.topic.split("/")[-1])
  57. state = message.payload == b"true"
  58. logger.info("Received request to set output channel %d to state %s", output, str(state))
  59. self.__sc__.set_out_state(output, state)
  60. else:
  61. logger.warning("Ignoring unknown mqtt topic %s", message.topic)
  62. def __del__(self):
  63. self.__client__.loop_stop() # stop the loop
  64. if __name__ == '__main__':
  65. report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
  66. #
  67. mp = mqtt_powerplug()
  68. while True:
  69. time.sleep(2)