-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNGSI.gs
231 lines (218 loc) · 6.23 KB
/
NGSI.gs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Create NGSI Client instance.
*
* Example
* let credential = {
* Authorization: "Bearer xxxxxxxxxx"
* }
* var client = NGSIClient("https://<orion-host>:1026", credential)
*
* @param {string} baseUrl Specify Orion URL.
* @param {string} credential Specify your credential.
*/
function Client(baseUrl, credential) {
this.credential = credential;
return new Orion(baseUrl);
}
/**
* Orion Class
*
* @param {string} baseUrl Orion url
*/
function Orion(baseUrl) {
this.orionBaseUrl = baseUrl
}
/**
* List entities.
*
* Example
* var entities = client.listEntities("MyService", "/MyServicePath")
*
* @param {string} fiwareService Specify your Fiware Service.
* @param {string} fiwareServicePath Specify your Fiware Service Path.
* @return {map} HTTP response body.
*/
Orion.prototype.listEntities = function(fiwareService, fiwareServicePath) {
var headers = {
"Fiware-Service": fiwareService,
"Fiware-ServicePath": fiwareServicePath
}
return getRequest_(this.orionBaseUrl + "/v2/entities", headers);
}
/**
* Get entity.
*
* Example
* var entities = client.getEntity("room1", "MyService", "/MyServicePath")
*
* @param {string} id Specify the ID of the entity to be got.
* @param {string} fiwareService Specify your Fiware Service.
* @param {string} fiwareServicePath Specify your Fiware Service Path.
* @return {map} HTTP response body.
*/
Orion.prototype.getEntity = function(id, fiwareService, fiwareServicePath) {
var headers = {
"Fiware-Service": fiwareService,
"Fiware-ServicePath": fiwareServicePath
}
return getRequest_(this.orionBaseUrl + "/v2/entities/" + id, headers);
}
/**
* Create entity.
*
* Example
* var entity = {
* id: "room1",
* type: "Room",
* temperature: {
* type: "Number",
* value: 28
* }
* }
* client.createEntity(entity, "MyService", "/MyServicePath")
*
* @param {map} entity Normalized entity.
* @param {string} fiwareService Specify your Fiware Service.
* @param {string} fiwareServicePath Specify your Fiware Service Path.
*/
Orion.prototype.createEntity = function(entity, fiwareService, fiwareServicePath) {
var headers = {
"Fiware-Service": fiwareService,
"Fiware-ServicePath": fiwareServicePath
}
postRequest_(this.orionBaseUrl + "/v2/entities", headers, entity);
}
/**
* Update entity.
*
* Example
* var attributes = temperature: {
* type: "Number",
* value: 25
* }
* client.updateEntity(entityId, attributes, "MyService", "/MyServicePath")
*
* @param {string} entityId Specify the ID of the entity to be updated.
* @param {map} attributes Normalized attributes.
* @param {string} fiwareService Specify your Fiware Service.
* @param {string} fiwareServicePath Specify your Fiware Service Path.
*/
Orion.prototype.updateEntity = function(entityId, attributes, fiwareService, fiwareServicePath) {
var headers = {
"Fiware-Service": fiwareService,
"Fiware-ServicePath": fiwareServicePath
}
patchRequest_(this.orionBaseUrl + "/v2/entities/" + entityId + "/attrs", headers, attributes);
}
/**
* Update attribute value.
*
* Example
* client.updateAttributeValue("room1", "temperature", 25, "/MyServicePath")
*
* @param {string} entityId Specify the ID of the entity to be updated.
* @param {string} attrName Specify the attribute name of the entity to be updated.
* @param {any} attrValue Specify the attribute value of the entity to be updated.
* @param {string} fiwareService Specify your Fiware Service.
* @param {string} fiwareServicePath Specify your Fiware Service Path.
*/
Orion.prototype.updateAttributeValue = function(entityId, attrName, attrValue, fiwareService, fiwareServicePath) {
var headers = {
"Fiware-Service": fiwareService,
"Fiware-ServicePath": fiwareServicePath
}
putRequest_(this.orionBaseUrl + "/v2/entities/" + entityId + "/attrs/" + attrName + "/value", headers, attrValue);
}
/**
* Delete entity.
*
* Example
* client.deleteEntity("room1", "MyService", "/MyServicePath")
*
* @param {string} id Specify the ID of the entity to be deleted.
* @param {string} fiwareService Specify your Fiware Service.
* @param {string} fiwareServicePath Specify your Fiware Service Path.
*/
Orion.prototype.deleteEntity = function(id, fiwareService, fiwareServicePath) {
var headers = {
"Fiware-Service": fiwareService,
"Fiware-ServicePath": fiwareServicePath
}
deleteRequest_(this.orionBaseUrl + "/v2/entities/" + id, headers);
}
function getRequest_(url, headers) {
var headers = Object.assign_(headers, this.credential);
var options = {
method: "get",
headers: headers
}
try {
var response = UrlFetchApp.fetch(url, options);
return JSON.parse(response.getContentText());
} catch (err) {
console.log(err);
return undefined;
}
}
function postRequest_(url, headers, payload) {
var headers = Object.assign_(headers, this.credential);
var options = {
method: "post",
headers: headers,
contentType: "application/json",
payload: JSON.stringify(payload)
};
try {
UrlFetchApp.fetch(url, options);
} catch (err) {
console.log(err);
}
}
function patchRequest_(url, headers, payload) {
headers = Object.assign_(headers, this.credential);
var options = {
method: "patch",
headers: headers,
contentType: "application/json",
payload: JSON.stringify(payload)
}
try {
UrlFetchApp.fetch(url, options);
} catch (err) {
console.log(err);
}
}
function putRequest_(url, headers, payload) {
headers = Object.assign_(headers, this.credential);
var options = {
method: "put",
headers: headers,
contentType: "text/plain",
payload: JSON.stringify(payload)
}
try {
UrlFetchApp.fetch(url, options);
} catch (err) {
console.log(err);
}
}
function deleteRequest_(url, headers) {
var headers = Object.assign_(headers, this.credential);
var options = {
method: "delete",
headers: headers
};
try {
UrlFetchApp.fetch(url, options);
} catch (err) {
console.log(err);
}
}
Object.assign_ = function (target, source){
if (!target || !source)
throw new Error("Invalid arguments.");
for (var property in source)
if (source.hasOwnProperty(property))
target[property] = source[property];
return target;
};