|
@@ -2,6 +2,7 @@ import config
|
2
|
2
|
import json
|
3
|
3
|
import logging
|
4
|
4
|
import mqtt
|
|
5
|
+import os
|
5
|
6
|
import report
|
6
|
7
|
import subprocess
|
7
|
8
|
import time
|
|
@@ -13,6 +14,14 @@ except ImportError:
|
13
|
14
|
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
14
|
15
|
|
15
|
16
|
|
|
17
|
+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)
|
|
18
|
+my_handler.setLevel(logging.INFO)
|
|
19
|
+my_handler.setFormatter(logging.Formatter("%(asctime)s: %(message)s"))
|
|
20
|
+bell_log = logging.getLogger('root')
|
|
21
|
+bell_log.setLevel(logging.INFO)
|
|
22
|
+bell_log.addHandler(my_handler)
|
|
23
|
+
|
|
24
|
+
|
16
|
25
|
class exec_command(mqtt.mqtt_client):
|
17
|
26
|
def __init__(self):
|
18
|
27
|
self.__block_execution__ = False
|
|
@@ -22,8 +31,10 @@ class exec_command(mqtt.mqtt_client):
|
22
|
31
|
# Start a pseudo process
|
23
|
32
|
self.process = subprocess.Popen(["sleep", "0"])
|
24
|
33
|
|
25
|
|
- def exec_command(self, cmd):
|
|
34
|
+ def exec_command(self, cmd, message):
|
26
|
35
|
self.process = subprocess.Popen(cmd.split(" "))
|
|
36
|
+ if 'bell' in message.topic:
|
|
37
|
+ bell_log.info('Door Bell activated (%s)', message.topic)
|
27
|
38
|
|
28
|
39
|
def mqtt_rx(self, client, userdate, message):
|
29
|
40
|
payload = None
|
|
@@ -48,7 +59,7 @@ class exec_command(mqtt.mqtt_client):
|
48
|
59
|
if self.process.poll() is None:
|
49
|
60
|
self.process.kill()
|
50
|
61
|
logger.debug("Starting execution in background...")
|
51
|
|
- self.exec_command(config.EXEC_LIST[message.topic]['command'])
|
|
62
|
+ self.exec_command(config.EXEC_LIST[message.topic]['command'], message)
|
52
|
63
|
|
53
|
64
|
|
54
|
65
|
if __name__ == '__main__':
|