-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenu.py
305 lines (263 loc) · 9.94 KB
/
menu.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
'''
Copyright © 2016 Daniel Müllner <http://danifold.net>
All changes from 2017-12-27 on: Copyright © Google Inc. <http://google.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import sys
if sys.hexversion < 0x03000000:
from ConfigParser import RawConfigParser
else:
from configparser import RawConfigParser
import datetime
import logging
import psutil
import RPi.GPIO as GPIO
from threading import Lock
import time
from component import Component
from ip import get_ip_address
from uptime import Uptime
logger = logging.getLogger('fancontrol')
config = RawConfigParser()
config.read('fancontrol.cfg')
button_left = config.getint('pins', 'button_left')
button_right = config.getint('pins', 'button_right')
button_front = config.getint('pins', 'button_front')
button_back = config.getint('pins', 'button_back')
GPIO.setmode(GPIO.BOARD)
class Buttoncontroller:
def __init__(self, messageboard):
self.messageboard = messageboard
self.buttons = []
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
for button in self.buttons:
GPIO.remove_event_detect(button)
GPIO.cleanup(self.buttons)
del self.buttons
def addbuttoncallback(self, button, callback):
self.buttons.append(button)
print('Set up button {}.'.format(button))
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def wrapped_callback(*args, **kwargs):
try:
callback(*args, **kwargs)
except Exception as e:
self.messageboard.post('Exception', e)
GPIO.add_event_detect(button, GPIO.BOTH, callback=wrapped_callback, bouncetime=100)
class Menu(Component):
def __init__(self):
Component.__init__(self, 'menu')
self.menus = [MainScreen(self.messageboard)]
self.menus[-1].display()
def __enter__(self):
with self.lock:
self.buttoncontroller = Buttoncontroller(self.messageboard)
self.buttoncontroller.__enter__()
self.buttoncontroller.addbuttoncallback(button_left, self.cancel)
self.buttoncontroller.addbuttoncallback(button_back, self.back)
self.buttoncontroller.addbuttoncallback(button_front, self.forward)
self.buttoncontroller.addbuttoncallback(button_right, self.select)
return Component.__enter__(self)
def __exit__(self, exc_type, exc_value, traceback):
with self.lock:
self.buttoncontroller.__exit__(exc_type, exc_value, traceback)
Component.__exit__(self, exc_type, exc_value, traceback)
def cancel(self, pin):
with self.lock:
pressed = not GPIO.input(pin)
if pressed:
if len(self.menus) > 1:
self.menus.pop().leave()
self.menus[-1].display()
self.wait_until_released(pin)
def back(self, pin):
with self.lock:
pressed = not GPIO.input(pin)
if pressed:
newmenu = self.menus[-1].back()
if newmenu:
self.menus.append(newmenu)
newmenu.display()
self.wait_until_released(pin)
def forward(self, pin):
with self.lock:
pressed = not GPIO.input(pin)
if pressed:
newmenu = self.menus[-1].forward()
if newmenu:
self.menus.append(newmenu)
newmenu.display()
self.wait_until_released(pin)
def select(self, pin):
with self.lock:
pressed = not GPIO.input(pin)
if pressed:
newmenu = self.menus[-1].select(pin)
if newmenu:
self.menus.append(newmenu)
newmenu.display()
self.wait_until_released(pin)
@staticmethod
def wait_until_released(pin):
while not GPIO.input(pin):
time.sleep(.1)
class MainScreen:
def __init__(self, messageboard):
self.messageboard = messageboard
def display(self):
self.messageboard.post('MainScreen', True)
def forward(self):
return MainMenu(self.messageboard)
def back(self):
return MainMenu(self.messageboard)
def select(self, pin):
return MainMenu(self.messageboard)
def leave(self):
pass
class MainMenu:
displayLines = 6
def __init__(self, messageboard):
self.messageboard = messageboard
self.items = [u'Info',
u'XXX',
u'Schliesse Fenster',
u'Öffne Fenster',
u'Ventilator aus',
u'Ventilator an',
u'Restart WLAN',
u'Herunterfahren']
self.currentitem = 0
self.firstline = 0
self.status = ''
def display(self):
mode = self.messageboard.query('Mode')
if mode == 'manual':
self.items[1] = u'Modus: manuell'
else:
self.items[1] = u'Modus: Automatik'
self.messageboard.post('Menu', (self.items[self.firstline: self.firstline + self.displayLines],
self.currentitem - self.firstline,
self.status))
def forward(self):
if self.currentitem < len(self.items) - 1:
self.currentitem += 1
if self.currentitem + self.firstline >= self.displayLines:
self.firstline += 1
self.display()
def back(self):
if self.currentitem > 0:
self.currentitem -= 1
if self.currentitem < self.firstline:
self.firstline -= 1
self.display()
def select(self, pin):
if self.currentitem == 0:
self.status = ''
return InfoScreen(self.messageboard)
elif self.currentitem == 1:
self.status = ''
mode = self.messageboard.query('Mode')
if mode == 'manual':
mode = 'auto'
else:
mode = 'manual'
self.messageboard.post('Mode', mode)
self.display()
logger.info('user,Mode:{}'.format(mode))
elif self.currentitem == 2:
self.status = u'Schliesse Fenster…'
self.messageboard.post('Mode', 'manual')
self.display()
logger.info('user,CloseWindow')
self.messageboard.post('Devices', 'StartCloseWindow')
while not GPIO.input(pin):
time.sleep(.1)
self.messageboard.post('Devices', 'StopWindowMotor')
self.status = u'Fensteröffner ist aus.'
self.display()
elif self.currentitem == 3:
self.status = u'Öffne Fenster…'
self.messageboard.post('Mode', 'manual')
self.display()
logger.info('user,OpenWindow')
self.messageboard.post('Devices', 'StartOpenWindow')
while not GPIO.input(pin):
time.sleep(.1)
self.messageboard.post('Devices', 'StopWindowMotor')
self.status = u'Fensteröffner ist aus.'
self.display()
elif self.currentitem == 4:
self.messageboard.post('Mode', 'manual')
self.messageboard.post('Devices', 'FanOff')
self.status = u'Ventilator ist aus.'
self.display()
logger.info('user,FanOff')
elif self.currentitem == 5:
self.messageboard.post('Mode', 'manual')
self.messageboard.post('Devices', 'FanOn')
self.status = u'Ventilator ist an.'
self.display()
logger.info('user,FanOn')
elif self.currentitem == 6:
self.status = u'Restart WLAN…'
self.display()
self.messageboard.post('RestartWLAN', True)
self.display()
elif self.currentitem == 7:
self.status = u'Herunterfahren…'
self.display()
self.messageboard.post('Shutdown', True)
def leave(self):
pass
prefix = [(float(1 << e), p) for e, p in ((30, 'G'), (20, 'M'), (10, 'K'))]
def humanBytes(n):
for m, p in prefix:
if n >= m:
return '{:.1f}{}'.format(n / m, p)
return "{}B".format(n)
def getAvailableRAM():
return humanBytes(psutil.virtual_memory().available)
class InfoScreen:
progstart = Uptime()
displayLines = 7
def __init__(self, messageboard):
self.messageboard = messageboard
self.index = 0
def display(self):
infolines = (
['IP Ethernet/WLAN:'],
['', get_ip_address('eth0')],
['', get_ip_address('wlan0')],
['Bootvorgang vor:'],
['', str(datetime.timedelta(seconds=int(Uptime())))],
['Programmstart vor:'],
['', str(datetime.timedelta(seconds=int(Uptime() - self.progstart)))],
['Freies RAM:', getAvailableRAM()],
)
self.index = max(min(self.index, len(infolines) - self.displayLines), 0)
self.messageboard.post(
'Info',
infolines[self.index:self.index + self.displayLines])
def forward(self):
self.index += 1
self.display()
def back(self):
self.index -= 1
self.display()
def select(self, pin):
pass
def leave(self):
pass