Browse Source

Logfile added

master
Dirk Alders 2 years ago
parent
commit
ca1e61c155
1 changed files with 13 additions and 2 deletions
  1. 13
    2
      exec_command.py

+ 13
- 2
exec_command.py View File

2
 import json
2
 import json
3
 import logging
3
 import logging
4
 import mqtt
4
 import mqtt
5
+import os
5
 import report
6
 import report
6
 import subprocess
7
 import subprocess
7
 import time
8
 import time
13
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
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
 class exec_command(mqtt.mqtt_client):
25
 class exec_command(mqtt.mqtt_client):
17
     def __init__(self):
26
     def __init__(self):
18
         self.__block_execution__ = False
27
         self.__block_execution__ = False
22
         # Start a pseudo process
31
         # Start a pseudo process
23
         self.process = subprocess.Popen(["sleep", "0"])
32
         self.process = subprocess.Popen(["sleep", "0"])
24
 
33
 
25
-    def exec_command(self, cmd):
34
+    def exec_command(self, cmd, message):
26
         self.process = subprocess.Popen(cmd.split(" "))
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
     def mqtt_rx(self, client, userdate, message):
39
     def mqtt_rx(self, client, userdate, message):
29
         payload = None
40
         payload = None
48
             if self.process.poll() is None:
59
             if self.process.poll() is None:
49
                 self.process.kill()
60
                 self.process.kill()
50
             logger.debug("Starting execution in background...")
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
 if __name__ == '__main__': 
65
 if __name__ == '__main__': 

Loading…
Cancel
Save