-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathble_to_rpc.py
48 lines (36 loc) · 1.51 KB
/
ble_to_rpc.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
import asyncio
import bleak
from jsonrpc import JSONRPCResponseManager, dispatcher
from werkzeug.serving import run_simple
from werkzeug.wrappers import Request, Response
# The event loop is needed to run the coroutine
loop = asyncio.get_event_loop()
# The scanner object is used to discover BLE devices
scanner = bleak.BleakScanner()
@Request.application
def application(request):
"""
The Werkzeug HTTP server will call this function whenever a request is
received. The request is a JSON-RPC request, which is parsed and
dispatched to the appropriate method.
:param request: The HTTP request object
:return: A JSON response
"""
# Functions that can be called by JSON RPC requests
def get_executed_coroutine_discover():
"""
BleakScanner.discover() is a coroutine, so we need to run it in an event
loop.
:return: The only result of the coroutine, a list of BLEDevice
objects address as strings.
"""
result = loop.run_until_complete(asyncio.gather(scanner.discover()))[0]
# turn all BLEDevice objects into dictionaries
return [device.address for device in result]
# Dispatcher is dictionary {<method_name>: callable}
dispatcher["scan"] = get_executed_coroutine_discover
# JSON RPC 2.0 handler
response = JSONRPCResponseManager.handle(request.data, dispatcher)
return Response(response.json, mimetype="application/json")
if __name__ == "__main__":
run_simple("localhost", 4000, application)