-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
144 lines (113 loc) · 3.87 KB
/
web.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
import logging
import asyncio
from datetime import datetime, timedelta
from aiohttp import web
async def index(request):
return web.FileResponse(path='public/index.html')
async def database(request):
return web.FileResponse(path='database.db')
def floor_date(date, resolution):
date = date.replace(microsecond=0, second=0)
if resolution == "hour":
date = date.replace(minute=0)
elif resolution == "day":
date = date.replace(minute=0, hour=0)
return date
def format_start(start, resolution):
return floor_date(datetime.fromisoformat(str(start).replace('Z', '')), resolution).isoformat()
def format_end(end, resolution):
date = datetime.fromisoformat(str(end).replace('Z', ''))
if resolution == "minute":
date += timedelta(minutes=1)
elif resolution == "hour":
date += timedelta(hours=1)
elif resolution == "day":
date += timedelta(days=1)
return floor_date(date, resolution).isoformat()
async def history(request):
resolution = request.query['resolution'] if 'resolution' in request.query else 'day'
start = format_start(request.query['start'] if 'start' in request.query else datetime.now(
) - timedelta(days=7), resolution)
end = format_end(
request.query['end'] if 'end' in request.query else datetime.now(), resolution)
return web.json_response({
'start': start,
'end': end,
'resolution': resolution,
'data': request.app['db'].get_moisture_stats(start, end, resolution)
})
def status(request):
return web.json_response({
'moisture': request.app['sensor'].moisture,
'state': request.app['state_manager'].state,
'base_state': request.app['state_manager'].base_state,
})
def config(request):
return web.json_response({
'thresholds': request.app['state_manager'].thresholds,
})
class PlantWebServer():
def __init__(self, port=8080):
self.port = port
self._app = web.Application()
self._app.add_routes([
web.get('/', index),
web.get('/history', history),
web.get('/status', status),
web.get('/config', config),
web.static('/images', 'images'),
web.static('/', 'public')])
@property
def db(self):
return self._app['db']
@db.setter
def db(self, db):
self._app['db'] = db
@property
def sensor(self):
return self._app['sensor']
@sensor.setter
def sensor(self, sensor):
self._app['sensor'] = sensor
@property
def state_manager(self):
return self._app['state_manager']
@state_manager.setter
def state_manager(self, state_manager):
self._app['state_manager'] = state_manager
async def start(self):
runner = web.AppRunner(self._app)
await runner.setup()
site = web.TCPSite(runner, '0.0.0.0', self.port)
await site.start()
logging.info(f"web server started on port {self.port}")
while 1:
await asyncio.sleep(1)
if __name__ == "__main__":
from database import Database
db = Database()
db.setup()
class FauxSensor():
def __init__(self):
self.moisture = 1700
class FauxStateManager():
def __init__(self):
self.state = 'cap'
self.base_state = 'wet'
self.thresholds = {
'very-dry': 1550,
'dry': 1500,
'moist': 1410,
}
faux_sensor = FauxSensor()
faux_state_manager = FauxStateManager()
loop = asyncio.get_event_loop()
try:
plant_web_server = PlantWebServer()
plant_web_server.db = db
plant_web_server.sensor = faux_sensor
plant_web_server.state_manager = faux_state_manager
loop.run_until_complete(plant_web_server.start())
except KeyboardInterrupt:
pass
loop.close()