-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (44 loc) · 1.73 KB
/
main.py
File metadata and controls
61 lines (44 loc) · 1.73 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
from aiohttp import web
from zigbee_controller import ZigbeeController
import asyncio
import os
import logging
LOGGER = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
web_app = web.Application()
routes = web.RouteTableDef()
zigbee_controller = ZigbeeController()
@routes.post('/permitjoin')
async def permit_join(request):
LOGGER.info("permitting devices to joing for the next 60s ...")
permit_join_future = asyncio.create_task(zigbee_controller.permit_join())
permit_join_future.add_done_callback(lambda future: LOGGER.info("devices can no longer join the network"))
return web.Response()
@routes.get('/devices')
async def devices(request):
return web.json_response(zigbee_controller.get_devices())
#@routes.get('/states')
#async def states(request):
# return web.json_response(zigbee_controller.get_states())
@routes.get('/state/{device_ieee}')
async def states_by_ieee(request):
device_ieee = int(request.match_info['device_ieee'])
v = await zigbee_controller.get_state_by_ieee(device_ieee)
return web.json_response(v)
@routes.post('/{device_ieee}')
async def control_device(request):
device_ieee = int(request.match_info['device_ieee'])
body = await request.json()
if "params" not in body:
body["params"] = ""
try:
await zigbee_controller.send_command(device_ieee, body["command"], body["params"])
except Exception as e:
LOGGER.exception("Failed to control device!")
return web.Response(status=500, text=str(e))
return web.Response()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(zigbee_controller.setup_network())
web_app.add_routes(routes)
web.run_app(web_app, port=os.getenv('PORT', 8080))