-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebTuner.py
240 lines (199 loc) · 8.4 KB
/
WebTuner.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
import _thread
import os
import cherrypy as http
import jinja2
from NadSerial import Device
class Storage:
def __init__(self):
self.standby = None
self.mute = None
self.blend = None
self.rdsps = ''
self.frequency = 0
self.rdsrt = {}
class WebTuner:
def __init__(self):
if 'TUNER_PORT' in os.environ:
self.Tuner = Device(os.environ['TUNER_PORT'])
else:
self.Tuner = Device()
self.Storage = Storage()
self.Tuner.get_device_id()
_thread.start_new_thread(self.serial_poller, ())
self.Tuner.serial_send(self.Tuner.getter.POWER)
self.Tuner.serial_send(self.Tuner.getter.BLEND)
self.Tuner.serial_send(self.Tuner.getter.FM_MUTE)
self.Tuner.serial_send(self.Tuner.getter.FM_FREQUENCY)
self.jinja2 = jinja2.Environment(
loader=jinja2.FileSystemLoader('templates'))
def __rds_text__(self):
result = []
for i in sorted(self.Storage.rdsrt):
result += self.Storage.rdsrt[i]
return ''.join(result).replace('^M', '').replace('^@', '')
# return ''.join(result) # .replace('^M', '').replace('^@', '')
def serial_poller(self):
debug = True
if debug:
http.log('Serial Poller: Started')
while 1:
response = self.Tuner.__read_bytes__()
if response[2] == 21:
if response[4] == 64:
self.Storage.standby = False
elif response[4] == 65:
self.Storage.standby = True
if debug:
http.log('Serial Poller: Power Update: {}'.format(
self.Storage.standby))
if response[1] == 27:
if response[2] == 2:
ps = ''
else:
ps = response[2:10].decode('ascii', errors='ignore')
if debug:
http.log('Serial Poller: RDS PS Update: {}'.format(ps))
self.Storage.rdsps = ps
if response[1] == 28:
if len(response) == 4:
self.Storage.rdsrt = {0: ' ', 4: ' ', 8: ' ', 12: ' ', 16: ' ', 20: ' ',
24: ' ', 28: ' ', 32: ' ', 36: ' ', 40: ' ', 44: ' ',
48: ' ', 52: ' ', 56: ' ', 60: ' '}
if debug:
http.log('Serial Poller: RDS Text Update Reset')
http.log('Serial Poller: RDS Text Update Reset')
else:
if response[2] == 94:
pos = response[3] - 64
content = response[4:][:-2]
else:
pos = response[2]
content = response[3:][:-2]
if debug:
http.log(
'Serial Poller: RDS Text Pos {} Test {}'.format(pos, content))
# if '^M' in content.decode('ascii', errors='ignore'):
# http.log(str(pos))
# for k in self.Storage.rdsrt:
# if k >= pos:
# del self.Storage.rdsrt[k]
# self.Storage.rdsrt = {0: ' ', 4: ' ', 8: ' ', 12: ' ', 16: ' ', 20: ' ',
# 24: ' ', 28: ' ', 32: ' ', 36: ' ', 40: ' ', 44: ' ',
# 48: ' ', 52: ' ', 56: ' ', 60: ' '}
http.log(self.__rds_text__())
# http.log(
# 'Serial Poller: RDS Text Update Position {} Value {}'.format(pos, content))
#
# if '^M' in str(response):
# TODO: What do we need to do when we get a ^M,
# for now strip it out in the self.__rds_text__()
self.Storage.rdsrt[pos] = content.decode(
'ascii', errors='ignore')
if response[2] == 45:
if response[5] == 2:
freq_bytes = bytes([response[3], response[4]])
elif response[5] in [35, 38, 39, 42, 94] or (response[3] == 94 and response[4] != 94):
freq_bytes = bytes([response[4] - 64, response[5]])
else:
freq_bytes = bytes([response[4], response[5]])
frequency = int.from_bytes(freq_bytes, "little") / 100
self.Storage.frequency = frequency
if debug:
http.log('Serial Poller: Frequency Update: {}'.format(frequency))
if response[2] == 47:
if response[4] == 64:
self.Storage.mute = False
elif response[4] == 65:
self.Storage.mute = True
if debug:
http.log('Serial Poller: Mute Update: {}'.format(
self.Storage.mute))
if response[2] == 49:
if response[4] == 64:
self.Storage.blend = False
elif response[4] == 65:
self.Storage.blend = True
if debug:
http.log('Serial Poller: Blend Update: {}'.format(
self.Storage.blend))
@http.expose
@http.tools.json_out()
def status(self):
if self.Storage.blend:
blend = 'Enabled'
else:
blend = 'Disabled'
if self.Storage.mute:
mute = 'Enabled'
else:
mute = 'Disabled'
if self.Storage.standby:
return {'frequency': '{0:.2f} Mhz'.format(self.Storage.frequency),
'rdsps': self.Storage.rdsps,
'rdsrt': self.__rds_text__().rstrip(' ').replace(' ', ' '),
'blend': blend, 'mute': mute}
@http.expose
def index(self):
if self.Storage.standby:
powerstyle = "button-error pure-button"
powertext = "Turn Off"
else:
powerstyle = "button-success pure-button"
powertext = "Turn On"
template = self.jinja2.get_template('index.html')
return template.render(tuner=self.Tuner.id, powerstyle=powerstyle, powertext=powertext)
@http.expose
def mute(self, mute='0'):
if mute == 'on':
http.log('Mute on selected')
if not self.Storage.mute:
self.Tuner.set_mute_on()
http.log('Enable mute')
else:
self.Tuner.set_mute_off()
http.log('Mute is off')
@http.expose
def blend(self, blend=''):
if blend == 'on':
http.log('Blend on selected')
if not self.Storage.blend:
self.Tuner.set_blend_on()
http.log('Enable blend')
else:
http.log('Blend is off')
if self.Storage.blend:
self.Tuner.set_blend_off()
http.log('Disable blend')
@http.expose
def tuner(self, frequency=''):
# Trigger query actual power status to work around bug in C 425
# self.Tuner.serial_send(self.Tuner.getter.POWER)
# time.sleep(.5)
if frequency:
frequency = frequency.replace(',', '.')
if self.Tuner.frequency != float(frequency):
self.Tuner.set_frequency_fm(frequency=float(frequency))
http.log('Frequency change to: {}'.format(float(frequency)))
raise http.HTTPRedirect("/")
@http.expose
def tune(self, step=''):
http.log('freq {}'.format(self.Storage.frequency))
newfreq = round(self.Storage.frequency + float(step), 2)
print(self.Tuner.frequency)
if 108 >= newfreq >= 87.50:
self.Tuner.set_frequency_fm(frequency=newfreq)
http.log('Frequency step change to: {}'.format(newfreq))
if __name__ == "__main__":
conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd())
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './public'
}
}
http.config.update({'server.socket_host': "0.0.0.0",
'server.socket_port': 8181})
http.quickstart(WebTuner(), '/', conf)