-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay_control.py
61 lines (49 loc) · 1.36 KB
/
relay_control.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
import serial
from time import sleep
from sys import argv
import os
SAVEFILE = "relay_state_file.dat"
if (os.name == 'nt'):
SERIAL_PORT = 'COM5'
else:
SERIAL_PORT = '/dev/cu.usbmodem801141'
def update_relay(state):
ser = serial.Serial(SERIAL_PORT, 9600)
sleep(2)
ser.write(state)
ser.close()
def update_if_needed(new_state, old_state):
if (new_state != old_state):
update_relay(new_state)
def get_old_state_from_file(filename):
old_state = '0'
try:
with open(filename, "r") as text_file:
old_state = text_file.read()
except IOError:
# File doesn't exist, that's okay. Assume state is '0'
pass
return old_state
def write_state_to_file(filename, state):
with open(filename, "w+") as text_file:
text_file.write(state)
def update_relay_state(new_state):
old_state = get_old_state_from_file(SAVEFILE)
update_if_needed(new_state, old_state)
write_state_to_file(SAVEFILE, new_state)
def main():
toggle = False
if (len(argv) < 2):
# Didn't pass status, assuming toggle
toggle = True
else:
new_state = str(argv[1])
if (new_state != '0' and new_state != '1'):
toggle = True
old_state = get_old_state_from_file(SAVEFILE)
if (toggle):
new_state = '0' if old_state == '1' else '1'
update_if_needed(new_state, old_state)
write_state_to_file(SAVEFILE, new_state)
if __name__ == "__main__":
main()