forked from LW-Ho/CoAP_CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestCoAP.py
executable file
·58 lines (51 loc) · 1.81 KB
/
RestCoAP.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
import logging
log = logging.getLogger("REST_CoAP")
import sys
import time
from coapthon.client.helperclient import HelperClient
# coap get "coap://[fd00::212:4b00:615:a736]:5683/g/sht21?pp=2&thd=20"
port = 5683
def postPayloadToNode(node, resource, payload_data):
resource = "res/"+resource
log.info("{0} : {1}, payload -> {2}".format(node, resource, payload_data))
try:
coap_client = HelperClient(server=(node, port))
start = time.time()
coap_client.post(path=resource, payload=payload_data ,timeout=30)
coap_client.close()
elapsed = time.time() - start
log.info("{} successful delivery, {:.2f} seconds. ".format(node, (elapsed-1)))
return True
except:
coap_client.close()
log.info("{0} did not successfully send out, retry again.".format(node))
return False
def postQueryToNode(node,resource,query):
query = "?"+query
resource = "res/"+resource+query
try:
coap_client = HelperClient(server=(node, port))
start = time.time()
coap_client.post(path=resource, payload='' ,timeout=30)
coap_client.close()
elapsed = time.time() - start
log.info("{} successful delivery, {:.2f} seconds. ".format(node, elapsed-1))
except:
coap_client.close()
log.info("{0} did not successfully send out, retry again.".format(node))
def postToAllNode(List,resource,query):
query = "?"+query
resource = "res/"+resource+query
for node in List:
try:
coap_client = HelperClient(server=(node, port))
start = time.time()
coap_client.post(path=resource, payload='' ,timeout=30)
coap_client.close()
elapsed = time.time() - start
log.info("{} successful delivery, {:.2f} seconds. ".format(node, elapsed-1))
except:
coap_client.close()
log.info("{0} did not successfully send out, retry again.".format(node))
pass
return