1 """This library should work with ciotdm, both work for iotdm project."""
5 def connect_to_iotdm(host, user, password, prot):
6 """According to protocol, connect to iotdm."""
7 return ciotdm.connect(host, base="InCSE1", auth=(
8 user, password), protocol=prot)
11 def create_resource(connection, parent, restype, attribute=None, name=None):
12 """Create resource without command."""
13 restype = int(restype)
14 response = connection.create(parent, restype, attribute, name=name)
15 Check_Response(response, "create")
19 def create_resource_with_command(connection, parent, restype,
20 command, attribute=None, name=None):
21 """According to command in the header, create the resource."""
22 restype = int(restype)
23 response = connection.createWithCommand(parent, restype,
24 command, attribute, name=name)
25 Check_Response(response, "create")
29 def create_subscription(connection, parent, ip, port):
30 """Create subscription."""
31 uri = "http://%s:%d" % (ip, int(port))
32 response = connection.create(parent, "subscription", {
33 "notificationURI": uri,
34 "notificationContentType": "wholeResource"})
35 Check_Response(response, "create")
39 def retrieve_resource(connection, resid):
40 """Retrieve resource according to resourceID."""
41 response = connection.retrieve(resid)
42 Check_Response(response, "retrieve")
46 def retrieve_resource_with_command(connection, resid, command):
47 """According to command, retrieve source with the resourceID."""
48 response = connection.retrieveWithCommand(resid, command)
49 Check_Response(response, "retrieve")
53 def update_resource(connection, resid, restype, attr, nm=None):
54 """According to resourceID, update resource."""
55 response = connection.update(resid, restype, attr, nm)
56 Check_Response(response, "update")
60 def update_resource_with_command(connection, resid,
61 restype, command, attr, nm=None):
62 """According to command, update resource with resourceID."""
63 response = connection.updateWithCommand(resid, restype, command, attr, nm)
64 Check_Response(response, "update")
68 def delete_resource(connection, resid):
69 """According to resourceID, delete the resource."""
70 response = connection.delete(resid)
71 Check_Response(response, "delete")
75 def delete_resource_with_command(connection, resid, command):
76 """According to command, delete the resource with resourceID."""
77 response = connection.deleteWithCommand(resid, command)
78 Check_Response(response, "delete")
83 """Return resource ID."""
84 return ciotdm.resid(response)
88 """Return resourceName."""
89 resourceName = ciotdm.name(response)
90 if resourceName is None:
91 raise AssertionError('Cannot find this resource')
96 """Return whole resource in text."""
100 def lastModifiedTime(response):
101 """Return resource lastModifiedTime."""
102 return ciotdm.lastModifiedTime(response)
105 def status_code(response):
106 """Return resource status_code."""
107 return response.status_code
111 """Return resource in json format."""
112 return response.json()
115 def elapsed(response):
116 """Return resource elapsed."""
117 return response.elapsed.total_seconds()
120 def kill_the_tree(host, CSEID, username, password):
121 """Delete the whole tree."""
122 connection = ciotdm.connect(host, base=CSEID,
123 auth=(username, password), protocol="http")
127 def Check_Response(response, operation):
128 """Check whether the connection is none."""
130 raise AssertionError('Cannot %s this resource') % (operation)
131 elif hasattr(response, 'status_code'):
132 if response.status_code < 200 or response.status_code > 299:
133 raise AssertionError(
134 'Cannot %s this resource [%d] : %s' %
135 (operation, response.status_code, response.text))