-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiotdm_api.py
110 lines (77 loc) · 2.94 KB
/
iotdm_api.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import sys
import ciotdm
from urlparse import urlparse
import json
from txThings.examples.client import Agent
from twisted.internet import reactor
from twisted.python import log
import txThings.txthings.coap as coap
import txThings.txthings.resource as resource
def restConf(URI, Cse_name, username, password):
uri = urlparse(URI)
response = ciotdm.reconf(uri.netloc, Cse_name, (username, password))
print str(response[0]) + '\n' + response[1]
def cleanup(URI, username, password):
uri = urlparse(URI)
response = ciotdm.kill(uri.netloc, (username, password))
print str(response[0]) + '\n' + response[1]
def create(URI, resource_type, payload, origin=None, requestID=None):
uri = urlparse(URI)
if uri.scheme == "http":
response = ciotdm.create(URI, resource_type, payload, origin, requestID)
print str(response[0]) + '\n' + response[1]
elif uri.scheme == "coap":
log.startLogging(sys.stdout)
endpoint = resource.Endpoint(None)
protocol = coap.Coap(endpoint)
Agent(protocol, "post", URI, payload=payload, ty=resource_type, origin=origin, requestID=requestID)
reactor.listenUDP(0, protocol)
reactor.run()
else:
print "Invalid protocol."
sys.exit(2)
def retrieve(URI, origin=None, requestID=None):
uri = urlparse(URI)
if uri.scheme == "http":
response = ciotdm.retrieve(URI, origin, requestID)
print str(response[0]) + '\n' + response[1]
elif uri.scheme == "coap":
log.startLogging(sys.stdout)
endpoint = resource.Endpoint(None)
protocol = coap.Coap(endpoint)
Agent(protocol, "get", URI, origin=origin, requestID=requestID)
reactor.listenUDP(0, protocol)
reactor.run()
else:
print "Invalid protocol."
sys.exit(2)
def update(URI, payload, origin=None, requestID=None):
uri = urlparse(URI)
if uri.scheme == "http":
response = ciotdm.update(URI, payload, origin, requestID)
print str(response[0]) + '\n' + response[1]
elif uri.scheme == "coap":
log.startLogging(sys.stdout)
endpoint = resource.Endpoint(None)
protocol = coap.Coap(endpoint)
Agent(protocol, "put", URI, payload=payload, origin=origin, requestID=requestID)
reactor.listenUDP(0, protocol)
reactor.run()
else:
print "Invalid protocol."
sys.exit(2)
def delete(URI, origin=None, requestID=None):
uri = urlparse(URI)
if uri.scheme == "http":
response = ciotdm.delete(URI, origin, requestID)
print str(response[0]) + '\n' + response[1]
elif uri.scheme == "coap":
log.startLogging(sys.stdout)
endpoint = resource.Endpoint(None)
protocol = coap.Coap(endpoint)
Agent(protocol, "delete", URI, origin=origin, requestID=requestID)
reactor.listenUDP(0, protocol)
reactor.run()
else:
print "Invalid protocol."
sys.exit(2)