-
Notifications
You must be signed in to change notification settings - Fork 78
/
testthing.ts
307 lines (299 loc) · 12 KB
/
testthing.ts
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
function checkPropertyWrite(expected: string, actual: unknown) {
const output = "Property " + expected + " written with " + actual;
if (expected === actual) {
console.info("PASS: " + output);
} else {
throw new Error("FAIL: " + output);
}
}
function checkActionInvocation(name: string, expected: string, actual: unknown) {
const output = "Action " + name + " invoked with " + actual;
if (expected === actual) {
console.info("PASS: " + output);
} else {
throw new Error("FAIL: " + output);
}
}
// init property values
let bool = false;
let int = 42;
let num = 3.14;
let string = "unset";
let array: unknown[] = [2, "unset"];
let object: Record<string, unknown> = { id: 123, name: "abc" };
WoT.produce({
title: "TestThing",
properties: {
bool: {
title: "Boolean",
description: "Property that can be set to true or false",
type: "boolean",
},
int: {
title: "Integer",
description: "An integer value that can be read and written",
type: "integer",
},
num: {
title: "Number",
description: "A floating point value that can be read and written",
type: "number",
},
string: {
title: "String",
description: "A string value that can be read and written",
type: "string",
},
array: {
title: "Array",
description: "An Array (List) with no structure that can be read and written",
type: "array",
items: {},
},
object: {
title: "Object",
description: "An object with id and name that can be read and written",
type: "object",
properties: {
id: {
title: "ID",
description: "Integer identifier",
type: "integer",
},
name: {
title: "Name",
description: "Name associated to the identifier",
type: "string",
},
},
},
},
actions: {
"void-void": {
title: "void-void Action",
description: "Action without input nor output",
},
"void-int": {
title: "void-int Action",
description: "Action without input, but with integer output",
},
"int-void": {
title: "int-void Action",
description: "Action with integer input, but without output",
input: { type: "integer" },
},
"int-int": {
title: "int-int Action",
description: "Action with integer input and output",
input: { type: "integer" },
output: { type: "integer" },
},
"int-string": {
title: "int-string Action",
description: "Action with integer input and string output",
input: { type: "integer" },
output: { type: "string" },
},
"void-obj": {
title: "void-obj Action",
description: "Action without input, but with object output",
output: {
type: "object",
properties: {
prop1: {
type: "integer",
},
prop2: {
type: "string",
},
},
required: ["prop1", "prop2"],
},
},
"obj-void": {
title: "obj-void Action",
description: "Action with object input, but without output",
input: {
type: "object",
properties: {
prop1: { type: "integer" },
prop2: { type: "string" },
},
required: ["prop1", "prop2"],
},
},
},
events: {
"on-bool": {
title: "Bool Property Change",
description: "Event with boolean data that is emitted when the bool property is written to",
data: { type: "boolean" },
},
"on-int": {
title: "Int Property Change",
description: "Event with integer data that is emitted when the int property is written to ",
data: { type: "integer" },
},
"on-num": {
title: "Num Property Change",
description: "Event with number data that is emitted when the num property is written to",
data: { type: "number" },
},
"on-string": {
title: "String Property Change",
description: "Event with number data that is emitted when the string property is written to",
data: { type: "number" },
},
"on-array": {
title: "Array Property Change",
description: "Event with number data that is emitted when the array property is written to",
data: { type: "number" },
},
"on-object": {
title: "Object Property Change",
description: "Event with number data that is emitted when the object property is written to",
data: { type: "number" },
},
},
})
.then((thing) => {
console.log("Produced " + thing.getThingDescription().title);
// set property read/write handlers
thing
.setPropertyWriteHandler("bool", async (value) => {
const localBool = await value.value();
checkPropertyWrite("boolean", typeof localBool);
bool = localBool as boolean;
thing.emitEvent("on-bool", bool);
})
.setPropertyReadHandler("bool", async () => bool)
.setPropertyWriteHandler("int", async (value) => {
const localInt = await value.value();
if (localInt === Math.floor(localInt as number)) {
checkPropertyWrite("integer", "integer");
} else {
checkPropertyWrite("integer", typeof value);
}
int = localInt as number;
thing.emitEvent("on-int", int);
})
.setPropertyReadHandler("int", async () => int)
.setPropertyWriteHandler("num", async (value) => {
const localNum = await value.value();
checkPropertyWrite("number", typeof localNum);
num = localNum as number;
thing.emitEvent("on-num", num);
})
.setPropertyReadHandler("num", async () => num)
.setPropertyWriteHandler("string", async (value) => {
const localString = await value.value();
checkPropertyWrite("string", typeof localString);
string = localString as string;
thing.emitEvent("on-string", string);
})
.setPropertyReadHandler("string", async () => string)
.setPropertyWriteHandler("array", async (value) => {
const localArray = await value.value();
if (Array.isArray(localArray)) {
checkPropertyWrite("array", "array");
} else {
checkPropertyWrite("array", typeof localArray);
}
array = localArray as unknown[];
thing.emitEvent("on-array", array);
})
.setPropertyReadHandler("array", async () => array)
.setPropertyWriteHandler("object", async (value) => {
const localObject = await value.value();
if (Array.isArray(localObject)) {
checkPropertyWrite("object", "array");
} else {
checkPropertyWrite("object", typeof localObject);
}
object = localObject as Record<string, unknown>;
thing.emitEvent("on-object", object);
})
.setPropertyReadHandler("object", async () => object);
// set action handlers
thing
.setActionHandler("void-void", async (parameters) => {
checkActionInvocation("void-void", "undefined", typeof (await parameters.value()));
return undefined;
})
.setActionHandler("void-int", async (parameters) => {
checkActionInvocation("void-int", "undefined", typeof (await parameters.value()));
return 0;
})
.setActionHandler("int-void", async (parameters) => {
const localParameters = await parameters.value();
if (localParameters === Math.floor(localParameters as number)) {
checkActionInvocation("int-void", "integer", "integer");
} else {
checkActionInvocation("int-void", "integer", typeof parameters);
}
return undefined;
})
.setActionHandler("int-int", async (parameters) => {
const localParameters = await parameters.value();
if (localParameters === Math.floor(localParameters as number)) {
checkActionInvocation("int-int", "integer", "integer");
} else {
checkActionInvocation("int-int", "integer", typeof localParameters);
}
return (localParameters as number) + 1;
})
.setActionHandler("int-string", async (parameters) => {
const localParameters = await parameters.value();
const inputtype = typeof localParameters;
if (localParameters === Math.floor(localParameters as number)) {
checkActionInvocation("int-string", "integer", "integer");
} else {
checkActionInvocation("int-string", "integer", typeof localParameters);
}
if (inputtype === "number") {
// eslint-disable-next-line no-new-wrappers
return new String(localParameters)
.replace(/0/g, "zero-")
.replace(/1/g, "one-")
.replace(/2/g, "two-")
.replace(/3/g, "three-")
.replace(/4/g, "four-")
.replace(/5/g, "five-")
.replace(/6/g, "six-")
.replace(/7/g, "seven-")
.replace(/8/g, "eight-")
.replace(/9/g, "nine-");
} else {
throw new Error("ERROR");
}
})
.setActionHandler("void-obj", async (parameters) => {
checkActionInvocation("void-complex", "undefined", typeof (await parameters.value()));
return { prop1: 123, prop2: "abc" };
})
.setActionHandler("obj-void", async (parameters) => {
checkActionInvocation("complex-void", "object", typeof (await parameters.value()));
return undefined;
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
});
})
.catch((e) => {
console.log(e);
});