-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual_devices_server.ts
820 lines (661 loc) · 30 KB
/
virtual_devices_server.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
import {Servient} from '@node-wot/core';
import {HttpServer, HttpClientFactory} from '@node-wot/binding-http'
import {Helpers} from '@node-wot/core';
// for reading local TD file
import * as fs from 'fs';
// load remote api for coppeliasim
const { RemoteAPIClient } = require("coppelia-ws-api");
// load virtual sensor class
import {virtualSensor,virtualConveyor, virtualUarm, virtualDobot,virtualLight,virtualPanTilt} from "./virtualDevices";
import { makeWoTinteraction } from './clientClass';
// add delay function
function delay(ms: number) {
return new Promise( resolve => setTimeout(resolve, ms) );
}
// read the virtual devices TD file
let sensorTD1 = JSON.parse(fs.readFileSync("../../TDs/Virtual/virtual_infrared_sensor1.td.json", "utf8"));
let sensorTD2 = JSON.parse(fs.readFileSync("../../TDs/Virtual/virtual_infrared_sensor2.td.json", "utf8"));
// read the conveyor TD file
let conveyorTD1 = JSON.parse(fs.readFileSync("../../TDs/Virtual/virtual_conveyor_left.td.json", "utf8"));
let conveyorTD2 = JSON.parse(fs.readFileSync("../../TDs/Virtual/virtual_conveyor_right.td.json", "utf8"));
// read the uarm TD file
let uarmTD =JSON.parse(fs.readFileSync("../../TDs/Virtual/virtual_uarm.td.json", "utf8"));
// read the dobot TD file
let dobotTD =JSON.parse(fs.readFileSync("../../TDs/Virtual/virtual_dobot.td.json", "utf8"));
// read the color sensor TD file
let colorTD =JSON.parse(fs.readFileSync("../../TDs/Virtual/virtual_color_sensor.td.json", "utf8"));
// read the spotlight TD file
let spotlightTD1 = JSON.parse(fs.readFileSync("../../TDs/Virtual/virtual_light_left.td.json", "utf8"));
let spotlightTD2 = JSON.parse(fs.readFileSync("../../TDs/Virtual/virtual_light_right.td.json", "utf8"));
// read the pantilt TD file
let pantiltTD1 = JSON.parse(fs.readFileSync("../../TDs/Virtual/virtual_PanTilt1.json", "utf8"));
let pantiltTD2 = JSON.parse(fs.readFileSync("../../TDs/Virtual/virtual_PanTilt2.json", "utf8"));
// set virtual scene address
//console.log(__dirname); // get current file absolute path
let virtualSceneadress = __dirname + "/IoT_remote_lab.ttt";
async function init(address:String){
console.log('Hello...');
const client = new RemoteAPIClient('localhost', 23050,'json');
console.log('Connecting...');
await client.websocket.open();
console.log('Getting proxy object "sim"...');
let sim = await client.getObject('sim');
// load the CoppeliaSim scene
await sim.loadScene(address);
await sim.startSimulation();
await delay(1000);
return sim;
}
async function main() {
let sceneAddress:String = virtualSceneadress;
var sim = await init(sceneAddress);
// the virtual iot lab total server, now we consider to move every device into one server and the same port
let virtualIoTlabserver = new Servient();
virtualIoTlabserver.addServer(
new HttpServer({
port: 9001, // set port 9000 as request
})
);
Helpers.setStaticAddress("localhost");
virtualIoTlabserver.start().then((WoT) => {
//sensor1
WoT.produce(sensorTD1).then(async(thing) => {
console.log("Produced " + thing.getThingDescription().title);
// init the coppeliasim
let sceneAddress:String = virtualSceneadress; // you need to modify to your own path
//var sim = await init(sceneAddress); // initialize scene and sim
//await sim.startSimulation();
//await delay(500);
let sensor1 = new virtualSensor(sim,"/InfraredSensor1");
// set property handlers (using async-await)
// set objectPresence propety handlers
thing.setPropertyReadHandler("objectPresence", async() => await (sensor1.objectDetect()))
// set detectedObject event handles
let flag = false;
thing.setEventSubscribeHandler("detectedObject", async() => {
setInterval(async() => {
let state = await (sensor1.objectDetect());
//console.log(state,flag);
// when detect object, only publish event once
if (state == true){
if (flag==false){
thing.emitEvent("detectedObject", true);
flag = true;
}
}
else{
flag = false;
}
}, 1000);
})
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
});
});
//sensor2
WoT.produce(sensorTD2).then(async(thing) => {
console.log("Produced " + thing.getThingDescription().title);
// init the coppeliasim
//let sceneAddress:String = virtualSceneadress; // you need to modify to your own path
//var sim = await init(sceneAddress); // initialize scene and sim
//await sim.startSimulation();
//await delay(500);
let sensor2 = new virtualSensor(sim,"/InfraredSensor2");
// set property handlers (using async-await)
// set objectPresence propety handlers
thing.setPropertyReadHandler("objectPresence", async() => await (sensor2.objectDetect()))
// set detectedObject event handles
let flag = false;
thing.setEventSubscribeHandler("detectedObject", async() => {
setInterval(async() => {
let state = await (sensor2.objectDetect());
//console.log(state,flag);
// when detect object, only publish event once
if (state == true){
if (flag==false){
thing.emitEvent("detectedObject", true);
flag = true;
}
}
else{
flag = false;
}
}, 1000);
})
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
});
});
// conveyor1
WoT.produce(conveyorTD1).then(async(thing) => {
console.log("Produced " + thing.getThingDescription().title);
// init the coppeliasim
//let sceneAddress:String = virtualSceneadress; // you need to modify to your own path
//var sim = await init(sceneAddress); // initialize scene and sim
//await sim.startSimulation();
//await delay(500);
let conveyor1 = new virtualConveyor(sim,"/ConveyorBelt1");
await conveyor1.setConveyorSpeed(0);
// set action handlers (using async-await)
// set startBeltBackward action handlers
thing.setActionHandler("startBeltBackward", async() =>{
try {
await conveyor1.setConveyorSpeed(0.02);
return "";
}
catch{
console.log("failed");
return "";
}
});
// set startBeltForward action handlers
thing.setActionHandler("startBeltForward", async() =>{
try {
await conveyor1.setConveyorSpeed(-0.02);
return "";
}
catch{
console.log("failed");
return "";
}
});
thing.setActionHandler("stopBelt", async() =>{
try {
await conveyor1.setConveyorSpeed(0);
return "";
}
catch{
console.log("failed");
return "";
}
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
});
});
//conveyor2
WoT.produce(conveyorTD2).then(async(thing) => {
console.log("Produced " + thing.getThingDescription().title);
// init the coppeliasim
//let sceneAddress:String = virtualSceneadress; // you need to modify to your own path
//var sim = await init(sceneAddress); // initialize scene and sim
//await sim.startSimulation();
//await delay(500);
let conveyor2 = new virtualConveyor(sim,"/ConveyorBelt2");
await conveyor2.setConveyorSpeed(0);
// set action handlers (using async-await)
// set startBeltBackward action handlers
thing.setActionHandler("startBeltBackward", async() =>{
try {
await conveyor2.setConveyorSpeed(0.03);
return "";
}
catch{
console.log("failed");
return "";
}
});
// set startBeltForward action handlers
thing.setActionHandler("startBeltForward", async() =>{
try {
await conveyor2.setConveyorSpeed(-0.03);
return "";
}
catch{
console.log("failed");
return "";
}
});
thing.setActionHandler("stopBelt", async() =>{
try {
await conveyor2.setConveyorSpeed(0);
return "";
}
catch{
console.log("failed");
return "";
}
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
});
});
//uarm
WoT.produce(uarmTD).then(async(thing) => {
console.log("Produced " + thing.getThingDescription().title);
// init the coppeliasim
//let sceneAddress:String = virtualSceneadress; // you need to modify to your own path
//var sim = await init(sceneAddress); // initialize scene and sim
//await sim.startSimulation();
//await delay(200);
let uarm = new virtualUarm(sim, "/uarm");
uarm.setGripperstate(false);
// set action handlers (using async-await)
// set startBeltBackward action handlers
thing.setActionHandler("goTo", async(data) =>{
try {
let pos:any = await data.value();
let finalPos:number[] = [pos["x"]/1000, pos["y"]/1000, pos["z"]/1000]; // convert to meter
//console.log(finalPos);
await uarm.goWithspeed(finalPos,1400);
return "";
}
catch{
console.log("failed");
return "";
}
});
// set startBeltForward action handlers
thing.setActionHandler("gripClose", async() =>{
try {
await uarm.setGripperstate(true);
return "";
}
catch{
console.log("failed");
return "";
}
});
thing.setActionHandler("gripOpen", async() =>{
try {
await uarm.setGripperstate(false);
return "";
}
catch{
console.log("failed");
return "";
}
});
thing.setActionHandler("goWithSpeed", async(data) =>{
try {
let pos:any = await data.value();
let finalPos:number[] = [pos["x"]/1000, pos["y"]/1000, pos["z"]/1000]; // convert to meter
let speed = pos["speed"];
//console.log(finalPos);
await uarm.goWithspeed(finalPos,speed);
return "";
}
catch{
console.log("failed");
return "";
}
})
thing.setActionHandler("goHome", async() =>{
try {
let pos = [0.20,0,0.08];
await uarm.goWithspeed(pos,1400);
return "";
}
catch{
console.log("failed");
return "";
}
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
});
});
//dobot
WoT.produce(dobotTD).then(async(thing) => {
console.log("Produced " + thing.getThingDescription().title);
let dobot = new virtualDobot(sim, "/dobot");
let cubeDetect = new virtualSensor(sim,"/cubesensor");
dobot.setGripperstate(false);
// set action handlers (using async-await)
// set startBeltBackward action handlers
// set startBeltForward action handlers
thing.setActionHandler("getCube", async() =>{
try {
let distance = 0;
if ((await cubeDetect.objectDetect())==false){
await dobot.setGripperstate(true);
await dobot.dobotMove(-0.62);
await dobot.setJointangle([0,20,60,40,55]);
await dobot.moveTopos([0.8345, -0.8, 1.10, 2.1218905033038e-07, -1.6752026965605e-07, -0.70711588859558, 0.70709764957428]);
await dobot.moveTopos([0.8345, -0.8, 1.058, 2.1218905033038e-07, -1.6752026965605e-07, -0.70711588859558, 0.70709764957428]);
while (true){
distance = distance + 0.0055;
if (await cubeDetect.objectDetect()){
await dobot.dobotMove(-0.62+distance);
break;
}
await dobot.dobotMove(-0.62+distance);
await delay(100);
}
}
await dobot.setJointangle([0,20,60,40,0]);
await dobot.dobotMove(-0.38);
await dobot.setJointangle([0,20,60,40,59]);
await dobot.setGripperstate(false);
await dobot.moveTopos([0.8345, -0.58, 1.085, 2.1218905033038e-07, -1.6752026965605e-07, 0.707, 0.707]);
await dobot.moveTopos([0.8345, -0.58, 1.068, 2.1218905033038e-07, -1.6752026965605e-07, 0.707, 0.707]);
await dobot.moveTopos([0.8345, -0.58, 1.056, 2.1218905033038e-07, -1.6752026965605e-07, 0.707, 0.707]);
await dobot.setGripperstate(true);
await dobot.moveTopos([0.8345, -0.58, 1.11, 2.1218905033038e-07, -1.6752026965605e-07, 0.707, 0.707]);
await dobot.setJointangle([0,20,60,40,-90]);
await dobot.dobotMove(0.113); //0.112 - 0.115
await dobot.moveTopos([0.98, 0.113, 1.12, 2.1218905033038e-07, -1.6752026965605e-07, 0.707, 0.707]);
await dobot.moveTopos([0.98, 0.113, 1.11, 2.1218905033038e-07, -1.6752026965605e-07, 0.707, 0.707]);
await dobot.setGripperstate(false);
await dobot.setJointangle([0,20,60,40,0]);
await dobot.dobotMove(-0.43);
await dobot.setJointangle([0,40,100,60,0]); // go to the position like real iot lab
return "";
}
catch{
console.log("failed");
return "";
}
});
thing.setActionHandler("returnCube", async() =>{
try {
await dobot.setJointangle([0,20,60,40,-55]);
await dobot.setGripperstate(false);
await dobot.dobotMove(-0.833);
await dobot.moveTopos([0.993, -0.833, 1.2, 2.1218826873337e-07, -1.6752122178332e-07, -0.70711296796799, 0.70710062980652]);
//await dobot.moveTopos([0.993, -0.833, 1.15, 2.1218826873337e-07, -1.6752122178332e-07, -0.70711296796799, 0.70710062980652]);
await dobot.moveTopos([0.993, -0.833, 1.12, 2.1218826873337e-07, -1.6752122178332e-07, -0.70711296796799, 0.70710062980652]);
await dobot.moveTopos([0.993, -0.833, 1.094, 2.1218826873337e-07, -1.6752122178332e-07, -0.70711296796799, 0.70710062980652]);
await dobot.setGripperstate(true);
await dobot.moveTopos([0.993, -0.833, 1.25, 2.1218826873337e-07, -1.6752122178332e-07, -0.70711296796799, 0.70710062980652]);
await dobot.setJointangle([0,20,60,40,0]);
await dobot.dobotMove(-0.62);
await dobot.setJointangle([0,20,60,40,55]);
await dobot.moveTopos([0.83214, -0.8, 1.10, 2.1218905033038e-07, -1.6752026965605e-07, -0.70711588859558, 0.70709764957428]);
await dobot.moveTopos([0.83214, -0.8, 1.06, 2.1218905033038e-07, -1.6752026965605e-07, -0.70711588859558, 0.70709764957428]);
// detect cube then slowly move
let distance = 0;
while (true){
distance = distance + 0.005;
await dobot.dobotMove(-0.62+distance);
if (await cubeDetect.objectDetect()){
await dobot.dobotMove(-0.62+distance+0.005);
break;
}
await delay(100);
}
//await dobot.dobotMove(0.47);
await dobot.moveTopos([0.83214, -0.805+distance, 1.06, 2.1218905033038e-07, -1.6752026965605e-07, -0.70711588859558, 0.70709764957428]);
await dobot.setGripperstate(false);
await dobot.moveTopos([0.83214, -0.805+distance, 1.1, 2.1218905033038e-07, -1.6752026965605e-07, -0.70711588859558, 0.70709764957428]);
await dobot.setJointangle([0,20,60,40,0]);
await dobot.dobotMove(-0.43);
await dobot.setJointangle([0,40,100,60,0]); // go to the position like real iot lab
await dobot.setGripperstate(true);
return "";
}
catch{
console.log("failed");
return "";
}
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
});
});
//color sensor
WoT.produce(colorTD).then(async(thing) => {
console.log("Produced " + thing.getThingDescription().title);
let colorSensor = new virtualSensor(sim,"/Color_sensor");
thing.setPropertyReadHandler("color", async() => await (colorSensor.getObjectcolor()))
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
});
});
//Spotlight1
WoT.produce(spotlightTD1).then(async(thing) => {
console.log("Produced " + thing.getThingDescription().title);
let light1 = new virtualLight(sim,"/Spotlight1");
//// property lightState
thing.setPropertyReadHandler("lightState", async() => await light1.getLightstate());
thing.setPropertyWriteHandler("lightState", async(intOutput) =>{
try{
let state = await intOutput.value();
await light1.setLightstate(Boolean(state));
//return ;
}
catch{
console.log("failed");
//return ;
}
});
// property lightColor
thing.setPropertyReadHandler("lightColor", async() => (await (light1.getLightinfo()))[2]);
thing.setPropertyWriteHandler("lightColor", async(intOutput) =>{
try{
let color:any = await intOutput.value();
await light1.setLightcolor(color);
//return ;
}
catch{
console.log("failed");
//return ;
}
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
});
});
//Spotlight2
WoT.produce(spotlightTD2).then(async(thing) => {
console.log("Produced " + thing.getThingDescription().title);
let light2 = new virtualLight(sim,"/Spotlight2");
//// property lightState
thing.setPropertyReadHandler("lightState", async() => await light2.getLightstate());
thing.setPropertyWriteHandler("lightState", async(intOutput) =>{
try{
let state = await intOutput.value();
await light2.setLightstate(Boolean(state));
//return ;
}
catch{
console.log("failed");
//return ;
}
});
// property lightColor
thing.setPropertyReadHandler("lightColor", async() => (await (light2.getLightinfo()))[2]);
thing.setPropertyWriteHandler("lightColor", async(intOutput) =>{
try{
let color:any = await intOutput.value();
await light2.setLightcolor(color);
//return ;
}
catch{
console.log("failed");
//return ;
}
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
});
});
// pantilt 1
WoT.produce(pantiltTD1).then(async(thing) => {
console.log("Produced " + thing.getThingDescription().title);
let pantilt1 = new virtualPanTilt(sim,"/pantilt_base1");
//// property panPosition
thing.setPropertyReadHandler("panPosition", async() => (await pantilt1.panPosition()));
// property tiltPosition
thing.setPropertyReadHandler("tiltPosition", async() => (await pantilt1.tiltPosition()));
// action goHome
thing.setActionHandler("goHome", async() =>{
try {
await pantilt1.goHome();
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// action moveTo
thing.setActionHandler("moveTo", async(data) =>{
try {
let pos:any = await data.value();
let finalPos = [pos["panAngle"], pos["tiltAngle"]];
await pantilt1.moveTo(finalPos);
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// action panContinuously
thing.setActionHandler("panContinuously", async() =>{
try {
await pantilt1.panContinuously();
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// action panTo
thing.setActionHandler("panTo", async(data) =>{
try {
let pos:any = await data.value();
await pantilt1.panTo(pos);
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// action stopMovement
thing.setActionHandler("stopMovement", async(data) =>{
try {
await pantilt1.stopMovement();
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// action tiltTo
thing.setActionHandler("tiltTo", async(data) =>{
try {
let pos:any = await data.value();
await pantilt1.tiltTo(pos);
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
});
});
// pantilt 2
WoT.produce(pantiltTD2).then(async(thing) => {
console.log("Produced " + thing.getThingDescription().title);
let pantilt2 = new virtualPanTilt(sim,"/pantilt_base2");
//// property panPosition
thing.setPropertyReadHandler("panPosition", async() => (await pantilt2.panPosition()));
// property tiltPosition
thing.setPropertyReadHandler("tiltPosition", async() => (await pantilt2.tiltPosition()));
// action goHome
thing.setActionHandler("goHome", async() =>{
try {
await pantilt2.goHome();
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// action moveTo
thing.setActionHandler("moveTo", async(data) =>{
try {
let pos:any = await data.value();
let finalPos = [pos["panAngle"], pos["tiltAngle"]];
await pantilt2.moveTo(finalPos);
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// action panContinuously
thing.setActionHandler("panContinuously", async() =>{
try {
await pantilt2.panContinuously();
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// action panTo
thing.setActionHandler("panTo", async(data) =>{
try {
let pos:any = await data.value();
await pantilt2.panTo(pos);
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// action stopMovement
thing.setActionHandler("stopMovement", async(data) =>{
try {
await pantilt2.stopMovement();
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// action tiltTo
thing.setActionHandler("tiltTo", async(data) =>{
try {
let pos:any = await data.value();
await pantilt2.tiltTo(pos);
return undefined;
}
catch{
console.log("failed");
return undefined;
}
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
});
});
});
}
main()