Compare commits
2 Commits
5736a42f6e
...
5974ec1c8b
Author | SHA1 | Date | |
---|---|---|---|
5974ec1c8b | |||
ca1e61c155 |
@ -9,6 +9,8 @@ MQTT_USER = "user"
|
|||||||
MQTT_PASS = "pass"
|
MQTT_PASS = "pass"
|
||||||
MQTT_SERVER = "host"
|
MQTT_SERVER = "host"
|
||||||
|
|
||||||
|
ENABLED_TOPIC = 'topic'
|
||||||
|
|
||||||
EXEC_LIST = {
|
EXEC_LIST = {
|
||||||
'topic_1': {
|
'topic_1': {
|
||||||
'key': 'key', # Give a key or None to compare plain DATA
|
'key': 'key', # Give a key or None to compare plain DATA
|
||||||
|
@ -2,6 +2,7 @@ import config
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import mqtt
|
import mqtt
|
||||||
|
import os
|
||||||
import report
|
import report
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
@ -13,17 +14,39 @@ except ImportError:
|
|||||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
||||||
|
|
||||||
|
|
||||||
|
my_handler = logging.handlers.RotatingFileHandler(os.path.join(config.__BASEPATH__, 'door_bell.log'), mode='a', maxBytes=5*1024*1024, backupCount=3, encoding=None, delay=0)
|
||||||
|
my_handler.setLevel(logging.INFO)
|
||||||
|
my_handler.setFormatter(logging.Formatter("%(asctime)s: %(message)s"))
|
||||||
|
bell_log = logging.getLogger('root')
|
||||||
|
bell_log.setLevel(logging.INFO)
|
||||||
|
bell_log.addHandler(my_handler)
|
||||||
|
|
||||||
|
|
||||||
class exec_command(mqtt.mqtt_client):
|
class exec_command(mqtt.mqtt_client):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__block_execution__ = False
|
self.__enabled__ = True
|
||||||
mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
||||||
|
self.add_callback(config.ENABLED_TOPIC + '/set', self.set_enabled)
|
||||||
for topic in config.EXEC_LIST:
|
for topic in config.EXEC_LIST:
|
||||||
self.add_callback(topic, self.mqtt_rx)
|
self.add_callback(topic, self.mqtt_rx)
|
||||||
# Start a pseudo process
|
# Start a pseudo process
|
||||||
self.process = subprocess.Popen(["sleep", "0"])
|
self.process = subprocess.Popen(["sleep", "0"])
|
||||||
|
self.publish_states()
|
||||||
|
|
||||||
def exec_command(self, cmd):
|
def set_enabled(self, client, userdata, message):
|
||||||
|
try:
|
||||||
|
payload = json.loads(message.payload)
|
||||||
|
except:
|
||||||
|
logger.exception("Error decoding json mqtt message")
|
||||||
|
else:
|
||||||
|
if self.__enabled__ != payload:
|
||||||
|
self.__enabled__ = payload
|
||||||
|
self.publish_states()
|
||||||
|
|
||||||
|
def exec_command(self, cmd, message):
|
||||||
self.process = subprocess.Popen(cmd.split(" "))
|
self.process = subprocess.Popen(cmd.split(" "))
|
||||||
|
if 'bell' in message.topic:
|
||||||
|
bell_log.info('Door Bell activated (%s)', message.topic)
|
||||||
|
|
||||||
def mqtt_rx(self, client, userdate, message):
|
def mqtt_rx(self, client, userdate, message):
|
||||||
payload = None
|
payload = None
|
||||||
@ -45,10 +68,16 @@ class exec_command(mqtt.mqtt_client):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
logger.exception("payload seems to be no dictionary")
|
logger.exception("payload seems to be no dictionary")
|
||||||
if data is None or payload == data:
|
if data is None or payload == data:
|
||||||
if self.process.poll() is None:
|
if self.__enabled__:
|
||||||
self.process.kill()
|
if self.process.poll() is None:
|
||||||
logger.debug("Starting execution in background...")
|
self.process.kill()
|
||||||
self.exec_command(config.EXEC_LIST[message.topic]['command'])
|
logger.debug("Starting execution in background...")
|
||||||
|
self.exec_command(config.EXEC_LIST[message.topic]['command'], message)
|
||||||
|
else:
|
||||||
|
logger.info("Execution is disabled")
|
||||||
|
|
||||||
|
def publish_states(self):
|
||||||
|
self.send(config.ENABLED_TOPIC, json.dumps(self.__enabled__))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@ -58,6 +87,7 @@ if __name__ == '__main__':
|
|||||||
#
|
#
|
||||||
while True:
|
while True:
|
||||||
time.sleep(30)
|
time.sleep(30)
|
||||||
|
ec.publish_states()
|
||||||
try:
|
try:
|
||||||
ec.join()
|
ec.join()
|
||||||
finally:
|
finally:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user