-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreman.py
executable file
·148 lines (106 loc) · 3.01 KB
/
foreman.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/python3
import time
import subprocess
import requests
import sys
import os
import util
from threading import Thread, Event
import RPi.GPIO as gpio
SPACEAPI_URL = 'https://leinelab.net/ampel/spaceapi.php?open=%s'
class Ampel(object):
def __init__(self):
self.pins = {
"red": 12,
"green": 16
}
gpio.cleanup()
gpio.setmode( gpio.BOARD)
for number in self.pins.values():
gpio.setup( number, gpio.OUT )
def set(self, pin, enable=True):
if enable:
gpio.output(self.pins[pin], gpio.HIGH)
else:
gpio.output(self.pins[pin], gpio.LOW)
# Tasks
class BasicTask(Thread):
def __init__(self, ampel):
super().__init__()
self.stop = Event()
self.ampel = ampel
def shutdown(self):
self.stop.set()
# wait until the Task has finished his work
self.join()
class Opened(BasicTask):
def run(self):
util.log('Opened')
self.ampel.set('green', True)
self.ampel.set('red', False)
# open spaceapi
requests.get(SPACEAPI_URL % '1')
class Blink(BasicTask):
def run(self):
util.log('Started blinking')
while not self.stop.is_set():
self.ampel.set('red', True)
self.ampel.set('green', True)
time.sleep(1)
self.ampel.set('red', False)
self.ampel.set('green', False)
time.sleep(1)
class Closed(BasicTask):
def run(self):
util.log('Closed')
self.ampel.set('green', False)
self.ampel.set('red', True)
# close spaceapi
requests.get(SPACEAPI_URL % '0')
def PollForCommand():
global g_fifo
#util.log( "Poll for command..." )
try:
content = g_fifo.read() # .decode() ?
except KeyboardInterrupt:
exit(0)
except:
util.log( "Couldn't decode command." )
return ""
# security feature
if content != "" and content[0] not in './~':
return content.replace('\n', '')
else:
return ""
## MAIN ##
ampel = Ampel()
util.log( "Started. Wait for commands..." )
current_task = None
def change_task(new_task):
global current_task
if current_task is not None:
current_task.shutdown()
current_task = new_task(ampel)
current_task.start()
change_task(Closed)
g_fifo = util.open_fifo('r')
while True:
command = PollForCommand()
time.sleep(1)
if command == "":
continue
util.log(command)
if command == "OpenLab":
if type(current_task) is not Opened:
change_task(Opened)
elif command == "CloseLab":
if type(current_task) is not Closed:
change_task(Closed)
elif command == "BlinkLab":
if type(current_task) is not Blink:
change_task(Blink)
elif command == "ButtonPressed":
if type(current_task) is not Closed:
change_task(Closed)
else:
change_task(Opened)