-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerControl.py
74 lines (59 loc) · 2.19 KB
/
PowerControl.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
from Communication import Comm
import configparser
import os
class PowerControl:
comm = None
config = None
outlets = {}
# Handle MQTT requests for power outlet switches
# power_request is expected to be a dict of the form
# {"id" : id, "val" : val},
# where id is a string representing the target
# and val is "on" or "off"
def handleRequest(self, power_request):
if "id" in power_request:
outlet_id = power_request["id"]
else:
print("Recieved power request but no \"id\" provided")
return
if "val" in power_request:
if power_request["val"] == "on" or power_request["val"] == "off":
outled_state = power_request["val"]
else:
print("\"val\" does not equal \"on\" or \"off\" : " + str(power_request["val"]))
return
else:
print("Missing \"val\" field in power request dict")
return
if outlet_id in self.outlets:
outlet_number = outlets[outlet_id]
else:
print("Unkown outlet ID: " + str(outlet_id))
return
if outlet_number in self.config.sections():
code = self.config[outlet_number][outled_state]
else:
print("Cant find outlet number " + str(outlet_number) + " in given radio_code config")
os.system("codesend " + str(code) + " 24")
def stopAll(self):
def __init__(self, configfile, outlet_assignemnt):
self.comm = Comm("PowerControl")
while not self.comm.connected()
pass
self.comm.subscribe("power_request", self.handleRequest)
self.config = configparser.ConfigParser()
self.config.read(configfile)
outlets = configparser.ConfigParser()
outlets.read(outlet_assignemnt)
for sec in outlets.sections():
for key in outlets[sec]:
self.outlets[key] = outlets[sec][key]
if __name__ == "__main__":
pc = PowerControl("cfg/radio_codes.cfg", "cfg/outlets.cfg")
print('Press Ctrl-C to quit.')
try:
while True:
time.sleep(100)
pass
except KeyboardInterrupt:
pc.stopAll()