-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·275 lines (244 loc) · 9.05 KB
/
app.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
#!/usr/bin/env python3
"""Flask app scheduling/starting/stopping nightlight and radio"""
import argparse
from datetime import datetime, timedelta
import logging
import logging.handlers
import multiprocessing
from multiprocessing.sharedctypes import SynchronizedBase
import sys
import json
from apscheduler.schedulers.background import BackgroundScheduler
#from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from flask import Flask, Response, render_template, request
#from flask_sqlalchemy import SQLAlchemy
import musicpd
import serial
from classes.airly import Airly
from classes.c_ro_jazz import CRoJazz
from classes.open_weather_map import OpenWeatherMap
from classes import wizbulb
from classes import routines
from classes import eink
app = Flask(__name__)
def prepare_logger(args) -> None:
"""create global logger"""
level = logging.WARNING
level_str_to_int = {
'critical': logging.CRITICAL,
'fatal': logging.FATAL,
'error': logging.ERROR,
'warn': logging.WARNING,
'warning': logging.WARNING,
'info': logging.INFO,
'debug': logging.DEBUG,
}
if args.loglevel:
level=level_str_to_int[args.loglevel]
if args.trace:
level = logging.DEBUG
if args.debug:
level=logging.INFO
logging.basicConfig(format='[%(asctime)s] %(levelname)s - %(processName)s/%(threadName)s - '
'%(pathname)s:%(lineno)d - %(name)s - %(message)s', level=level)
def wrap_in_process(func, *args) -> None:
"""wrap passed function in separate process"""
proc = multiprocessing.Process(target=func,
args=args)
proc.start()
class PlainTextTcpHandler(logging.handlers.SocketHandler):
""" Sends plain text log message over TCP channel """
def makePickle(self, record):
message = self.formatter.format(record)
return message.encode()
def mpd(mpd_request: dict,) -> None:
"""handle mpd-related requests"""
app.producer_wakeup_int.send(True)
mpd_client = musicpd.MPDClient()
mpd_client.connect()
if mpd_request.args['mpd'] == 'off':
mpd_client.clear()
if mpd_request.args['mpd'] == 'on':
mpd_client.clear()
#mpd_client.setvol(100)
mpd_client.add('https://rozhlas.stream/jazz_aac_128.aac')
mpd_client.play()
if mpd_request.args['mpd'] == 'volume' and 'volume' in mpd_request.args:
app.logger.error("trying to set volume: %s",
mpd_request.args['volume'])
mpd_client.setvol(mpd_request.args['volume'])
@app.route('/', methods=['GET', 'POST'])
def index() -> str:
"""Webpage with advanced controls"""
mpd_client = musicpd.MPDClient()
mpd_client.connect()
app.logger.error("index form: %s", json.dumps(request.form))
if 'volume' in request.form:
mpd_client.setvol(request.form['volume'])
return render_template("index.html.j2",
audioVolume=mpd_client.status()['volume'])
@app.route('/api', methods=['GET', 'POST'])
async def api() -> Response:
"""Handle API calls"""
response = {}
if request.method == 'POST':
app.logger.error(json.dumps(request.args))
if 'bulb' in request.args:
wrap_in_process(wizbulb.set_bulb_sync, request, app.config)
if 'mpd' in request.args:
mpd(request)
else:
mpd_client = musicpd.MPDClient()
mpd_client.connect()
out = await wizbulb.get_bulb(app.config)
response = {
'volume': mpd_client.status()['volume'],
'commands': mpd_client.commands(),
'song': mpd_client.currentsong(),
'bulbs': out,
}
response = Response(json.dumps(response, indent=2),
200, mimetype='application/json')
response.headers["Content-Type"] = "application/json; charset=utf-8"
return response
#scheduled job
def add_alarms(sched: BackgroundScheduler,
consumer_wakeup_int: multiprocessing.connection.Connection,
flag_master_switch: SynchronizedBase) -> None:
"""Initialize scheduler with alarms from config"""
for alarm in app.config['ALARMS']:
minute=alarm['minute']
hour=alarm['hour']
day=alarm['day']
month=alarm['month']
day_of_week=alarm['day_of_week']
sched.add_job(
routines.wakeup,
trigger = 'cron',
args = [consumer_wakeup_int, app.config, flag_master_switch],
minute=minute,
hour=hour,
day=day,
month=month,
day_of_week=day_of_week)
app.logger.error("Alarm scheduled: %s", json.dumps(alarm))
#process
def tcplog(tcplog_consumer: multiprocessing.connection.Connection,
host: str, port: int) -> None:
"""Send text from pipe to TCP"""
log = logging.getLogger("tcplog")
log_handler = PlainTextTcpHandler(host, port)
log_handler.setFormatter(logging.Formatter('%(message)s'))
for handler in log.handlers:
log.removeHandler(handler)
log.addHandler(log_handler)
log.propagate = False
app.logger.warning("tcplog: starting loop")
while True:
app.logger.info("tcplog: consuming")
log.error(tcplog_consumer.recv())
#process
def serial_to_log(tcplog_producer:
multiprocessing.connection.Connection) -> None:
"""Read serial and send to logger"""
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=90)
ser.flushInput()
app.logger.warning("serial_to_log: starting loop")
while True:
try:
ser_out = ser.readline()
app.logger.info("serial_to_log: producing")
tcplog_producer.send(ser_out.decode("utf-8"))
except serial.serialutil.SerialException:
exc_type, value, _ = sys.exc_info()
app.logger.debug("Serial error: %s: %s", exc_type.__name__, value)
def main():
"""main wrapper"""
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--loglevel", type=str,
choices=['critical','error','warning','info','debug', 'trace'],
help="set loglevel")
parser.add_argument("-d", "--debug", "-v", "--verbose", action="store_true",
help="debug mode")
parser.add_argument("-dd", "--trace", "-vv", action="store_true",
help="debug mode")
args = parser.parse_args()
prepare_logger(args)
app.logger = logging.getLogger("home_page")
multiprocessing.log_to_stderr()
log_handler = logging.StreamHandler()
log_handler.setFormatter(
logging.Formatter('[%(asctime)s] %(levelname)s - %(processName)s/%(threadName)s - '
'%(pathname)s:%(lineno)d - %(name)s - %(message)s')
)
app.config.from_file("config.json", json.load)
#multiproc_logger = multiprocessing.get_logger()
#for handler in multiproc_logger.handlers:
# multiproc_logger.removeHandler(handler)
#multiproc_logger.addHandler(log_handler)
app.cro_jazz = CRoJazz()
app.open_weather = OpenWeatherMap(
app.config['FORECAST']['forecastLocation'],
app.config['FORECAST']['forecastToken']
)
app.smog_airly = Airly(
app.config['FORECAST']['smogLocations'],
app.config['FORECAST']['airlyToken']
)
app.flag_radio_playing = multiprocessing.Value('i', 1)
app.flag_master_switch = multiprocessing.Value('i', 1)
consumer_cro, producer_cro = multiprocessing.Pipe()
consumer_opw, producer_opw = multiprocessing.Pipe()
consumer_arl, producer_arl = multiprocessing.Pipe()
consumer_tcplog, producer_tcplog = multiprocessing.Pipe()
consumer_wakeup_int, app.producer_wakeup_int = multiprocessing.Pipe()
#APP.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
#DB = SQLAlchemy(APP)
scheduler = BackgroundScheduler()
scheduler_start = datetime.now()+timedelta(seconds=30)
scheduler.add_job(
app.cro_jazz.update,
trigger = 'interval',
args = [producer_cro],
minutes=1,
start_date=scheduler_start)
scheduler.add_job(
app.open_weather.update,
trigger = 'interval',
args = [producer_opw],
hours=1,
start_date=scheduler_start)
scheduler.add_job(
app.smog_airly.update,
trigger = 'interval',
args = [producer_arl],
hours=1,
start_date=scheduler_start)
scheduler.add_job(
app.open_weather.schedule_at_sunset,
trigger = 'cron',
args = [
scheduler,
routines.sunset,
app.config,
app.flag_master_switch,
timedelta(minutes=-30)
],
hour="15")
add_alarms(scheduler, consumer_wakeup_int, app.flag_master_switch)
wrap_in_process(tcplog, consumer_tcplog, '127.0.0.1', 5170)
wrap_in_process(serial_to_log, producer_tcplog)
#polling bulp with ping is unreliable :/
#wrap_in_process(routines.bulbs_state, app.config, app.flag_master_switch)
wrap_in_process(
eink.update_eink,
consumer_cro,
consumer_opw,
consumer_arl,
producer_tcplog,
app.flag_radio_playing
)
scheduler.start()
app.run()
if __name__ == "__main__":
main()