-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitcher-server
executable file
·121 lines (77 loc) · 2.88 KB
/
switcher-server
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python3
# Program to run continually and accept socket connections from 'client'
# scripts, either to change relay state, or to return relay status.
from threading import Thread
import socket
import subprocess
import gpiozero
BUTTON_PIN = 24
BUTTON_LED = 26 # Currently unused, because it's hard to see on top of the case
GREEN_LED_PIN = 27
GREEN_LED_DUTY_CYCLE = 0.02
RED_LED_PIN = 22
RED_LED_DUTY_CYCLE = 0.02
RELAY_PIN = 17
SERVER_LISTEN_PORT = 5555
def setup():
relay = gpiozero.DigitalOutputDevice(RELAY_PIN)
red_led = gpiozero.PWMLED(RED_LED_PIN, initial_value=RED_LED_DUTY_CYCLE)
green_led = gpiozero.PWMLED(GREEN_LED_PIN, initial_value=0)
button = gpiozero.Button(BUTTON_PIN, pull_up=False, hold_time=5)
# Toggle relay state when pressed
button.when_pressed = lambda: set_relay('toggle', relay, red_led, green_led)
# Shut down when held down for 5 seconds
button.when_held = lambda: shutdown(red_led, green_led)
return (relay, red_led, green_led, button)
def shutdown(red_led, green_led):
green_led.off()
red_led.off()
red_led.pulse(fade_in_time=0.2, fade_out_time=0.2, n=5, background=False)
red_led.pulse(fade_in_time=0.2, fade_out_time=0.2, background=True)
subprocess.check_call(['sudo', 'poweroff'])
def set_relay(state, relay, red_led, green_led):
if state == 'on':
if not relay.is_active:
relay.on()
red_led.off()
green_led.value = GREEN_LED_DUTY_CYCLE
elif state == 'off':
if relay.is_active:
relay.off()
green_led.off()
red_led.value = RED_LED_DUTY_CYCLE
elif state == 'toggle':
if relay.is_active:
set_relay('off', relay, red_led, green_led)
else:
set_relay('on', relay, red_led, green_led)
else:
raise ValueError("Expected 'on', 'off', or 'toggle'.")
def run_server(relay, red_led, green_led, listen_port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', listen_port))
sock.listen(3)
while True:
conn, addr = sock.accept()
print("{} connected".format(addr))
conn.settimeout(60)
Thread(target=handle_client, args=(conn, relay, red_led, green_led)).start()
def handle_client(conn, relay, red_led, green_led):
while True:
data = conn.recv(1)
if not data:
conn.close()
return
elif data == b'0':
set_relay('off', relay, red_led, green_led)
elif data == b'1':
set_relay('on', relay, red_led, green_led)
elif data == b'S':
conn.sendall(b'1' if relay.is_active else b'0')
else:
raise ValueError("Unexpected message: '{}'".format(data))
def main():
(relay, red_led, green_led, button) = setup()
run_server(relay, red_led, green_led, SERVER_LISTEN_PORT)
if __name__ == '__main__':
main()