-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoap.py
More file actions
52 lines (33 loc) · 1.3 KB
/
coap.py
File metadata and controls
52 lines (33 loc) · 1.3 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
from aiocoap import *
import asyncio
import json
async def request(ip, code, payload):
context = await Context.create_client_context()
request = Message(
code=code, uri=f"coap://{ip}/", payload=json.dumps(payload).encode("utf-8")
)
try:
response = await context.request(request).response
if not response.payload:
return None
return json.loads(response.payload.decode("utf-8"))
except Exception as e:
return None
def request_info(ip):
return asyncio.run(request(ip, GET, ["device"]))
def request_locate(ip):
asyncio.run(request(ip, PUT, {"locate": 1}))
def request_get_brightness(ip):
return asyncio.run(request(ip, GET, ["brightness"]))
def request_set_brightness(ip, max, min, magrin):
asyncio.run(
request(ip, PUT, {"brightness": {"max": max, "min": min, "margin": magrin}})
)
def request_reset(ip):
asyncio.run(request(ip, PUT, {"reset": 1, "restart": 1}))
def request_restart(ip):
asyncio.run(request(ip, PUT, {"restart": 1}))
def request_update(ip, url, signature):
asyncio.run(request(ip, PUT, {"update": {"url": url, "signature": signature}}))
def request_setup(ip, ssid, password, username):
asyncio.run(request(ip, PUT, {"wifi": {"ssid": ssid, "password": password, "username": username}}))