-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (60 loc) · 2.79 KB
/
main.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
import socketio # pip install "python-socketio[asyncio_client]"
import asyncio # pip install asyncio
import random
'''
KANO_BASE_URL : The base URL of the Kano instance
WEBSOCKET_PATH : The path of the websocket
JWT_TOKEN : The JWT token to authenticate the user
'''
KANO_BASE_URL = ""
WEBSOCKET_PATH = "/apiws"
JWT_TOKEN=""
sio = socketio.AsyncClient(engineio_logger=True, logger=True)
def generateRandomGeometry():
'''Generate a random geometry inside a bounding box (France)'''
return {
"type": "Point",
"coordinates": [
random.uniform(-5, 10),
random.uniform(40, 52)
]
}
async def main():
headers = {"Authorization": f"Bearer {JWT_TOKEN}"}
# By default socketio uses the path /socket.io but Kano uses /apiws
await sio.connect(KANO_BASE_URL, socketio_path=WEBSOCKET_PATH, headers=headers)
# There are two ways to authenticate the user :
# 1. By sending the JWT token in the connection headers (as shown above)
# 2. By sending a create message to the authentication service (as shown below) (all the messages sent after this one will be authenticated)
# more info (https://feathersjs.com/api/client/socketio#authentication)
#
# await sio.emit("create",
# ("api/authentication",
# {
# "strategy": 'jwt',
# "accessToken": JWT_TOKEN
# },
# ),
# callback=print
# )
# Example of sending a patch message to the server
# Update the geometry of the features with properties.deviceId = 3 every 100ms with a random geometry
for i in range(0,1000):
newGeometry = generateRandomGeometry()
await sio.emit(
'patch', # The event of the operation
( # The rest of the arguments are sent as a tuple
'api/features', # [0] The path/name of the service
None, # [1] The document id (null for a all documents)
{"geometry": newGeometry}, # [2] The patch to apply
{ # [3] The query to select the documents to patch
"upsert": True, # Create the document if it does not exist
"properties.deviceId":3 # Select the document with the deviceId
},
),
callback=print # The callback to call when the operation is finished
)
await asyncio.sleep(0.1)
await sio.wait() # block the main thread until the connection is closed
if __name__ == '__main__':
asyncio.run(main())