-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupervisor.py
77 lines (56 loc) · 2.17 KB
/
supervisor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# This is the main python file
# upon boot, this will attempt to connect to the mqtt server
# will control other scripts via environment variables
# ie. armed=1 for on. armed=0 for off
# will send status update to mqtt server
# Will announce if triggered
# ie. triggered=1 for yes. triggered=0 for no
import atexit
import subprocess
from time import sleep
import os
import logzero
from logzero import logger
import paho.mqtt.client as mqtt
MOTION_PIR = "motion-pir"
MOTION_MICROWAVE = "motion-microwave"
ACCELEROMETER = "accelerometer"
pwd = "/home/pi/the-palantir"
LOG_FILENAME = pwd + '/log/supervisor.log'
topic = "mqttHQ-client-test-3242342352341"
broker = "public.mqtthq.com"
if not os.path.isfile(LOG_FILENAME):
os.mkdir(pwd + "/log")
logzero.logfile(LOG_FILENAME)
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("$SYS/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
#logger.info(msg.topic+" "+str(msg.payload))
message = (str(str(msg.payload).replace("b", "")).replace("'", ""))
def stop_service(service):
bash_command = "systemctl start " + service
process = subprocess.Popen(bash_command, stdout=subprocess.PIPE)
output, error = process.communicate()
def stop_service(service):
bash_command = "systemctl stop " + service
process = subprocess.Popen(bash_command, stdout=subprocess.PIPE)
output, error = process.communicate()
def on_exit():
logger.info("Exit command triggered")
client.publish(topic, payload="Exit command triggered", qos=0, retain=False)
def main():
logger.info("Starting Supervisor")
client.publish(topic, payload="Starting Supervisor", qos=0, retain=False)
atexit.register(on_exit)
client.loop_forever()
if __name__ == "__main__":
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker, 1883, 60)
main()