-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_web.py
More file actions
66 lines (57 loc) · 1.69 KB
/
demo_web.py
File metadata and controls
66 lines (57 loc) · 1.69 KB
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
try:
import uasyncio as asyncio
except ImportError:
import asyncio
from configuration import JSONStore
import logger
import service
import tmp
import lan
import web
import host
class PulseService(service.Service):
def __init__(self, pulseStatus, period=2.0):
self._pulseStatus = pulseStatus
self._period = period
async def onLoop(self, stopCallback):
async with self._pulseStatus.setter as setter:
isBeat = setter.get('is_beat')
setter.set('is_beat', not isBeat)
await asyncio.sleep(self._period / 2)
async def _amain():
with tmp.Path('lan.json') as lanPath:
api = web.API('/api/v0')
lanStatus = api.add(
'network', lan.JSONStore(lanPath)
)
pulseStatus = api.add(
'pulse', JSONStore(None, '{ "is_beat": false }')
)
def exampleCallback(request, data):
request.Response.ReturnOk("Hello, world! I'm a dynamic resource.")
routeList = (
web.Route('hello', web.HTTP.GET, '/hello', exampleCallback, None),
)
stopService, results = await service.Runner(
).add(
lan.Service(lanStatus)
).add(
web.Service(
log=log, root=host.ROOT, routeList=routeList, api=api, port=host.PORT
)
).add(
PulseService(pulseStatus)
).run()
log.info('%s %s', stopService.__class__.__name__, results or '')
def main(level=logger.INFO):
global log
logger.set(level=level)
log = logger.get('web')
try:
asyncio.run(_amain())
except KeyboardInterrupt:
pass
def debug():
main(level=logger.DEBUG)
main()
#debug()