-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviss_test.html
1006 lines (926 loc) · 34.6 KB
/
viss_test.html
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!-- test-ui.html -->
<!-- Copyright (c) 2017 ACCESS CO., LTD. All rights reserved. -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VISS test UI</title>
<style>
body{
width:1920px;
margin:10px;
margin-left:100px;
}
#header{
width:1920px;
margin:5px;
}
#wrapper{
float:left;
}
#main{
width:550px;
float:left;
}
#right{
width:1200px;
float:right;
}
</style>
<style>
#msg { height:600px; width:1200px; background-color:lightgrey; font-size:14px;}
#msg_vss { height:100px; width:500px; background-color:lightgrey; font-size:14px;}
#msg_getmetadata { height:20px; width:500px; background-color:lightgrey; font-size:14px;}
#msg_authorize { height:20px; width:500px; background-color:lightgrey; font-size:14px;}
.msg_vss_desc { height: 20px; width:500px; background-color:lightgrey; font-size:14px; display:inline-block;}
.val_area { height: 20px; width:150px; background-color:lightgrey; font-size:14px; display:inline-block;}
.val_area_middle { height: 20px; width:350px; background-color:lightgrey; font-size:14px; display:inline-block;}
.val_area_long { height: 20px; width:500px; background-color:lightgrey; font-size:14px; display:inline-block;}
#server_info {height:20px; width:500px; font-size:13px;}
#span_path {height:20px; width:20px; font-size:13px;}
.span_text {height:20px; width:20px; font-size:13px;}
</style>
<script src="./svr_config.js"></script>
</head>
<body>
<header id="header">
<h3> Vehicle Information Service Spec Test UI </h3>
<span id="server_info"></span>
<br>
</header>
<div id="wrapper">
<main id="main">
<button onclick="btnConnect();">ws connect</button>
<button onclick="btnDisconnect();">ws disconnect</button>
<span class="val_area" id="msg_connect"></span>
<br>
<button onclick="btnAuthorizeValidToken();" >authorize with valid token</button>
<button onclick="btnAuthorizeInvalidToken();">authorize with invalid token</button>
<span class="val_area" id="msg_authorize"></span>
<br><button onclick="btnGetMetadata();">getMetadata</button>
<span>path: </span><input type="text" class="val_area_middle" id="txt_getMetadata_path"></input>
<span class="val_area" id="msg_getmetadata"></span>
<br>
<br><button onclick="btnGetSpeed();">get speed</button>
<span class="val_area" id="msg_get_speed" ></span>
<br><button onclick="btnGetEngineRPM();">get engine RPM</button>
<span class="val_area" id="msg_get_rpm"></span>
<br><button onclick="btnGetSteeringAngle();">get steering angle</button>
<span class="val_area" id="msg_get_steer"></span>
<br>
<br><button onclick="btnSetDoorRow1RightLock();">set DoorLock(Row1-Right)</button>
<input type="text" class="val_area" id="txt_set_doorRow1RightLock" ></input>
<span class="span_text">(*authorize required)</span>
<br><button onclick="btnGetDoorRow1RightLock();">get DoorLock(Row1-Right)</button>
<span class="val_area" id="msg_get_doorRow1RightLock" ></span>
<br><button onclick="btnSetHVACRow1RightTemp();">set HVACTemp(Row1-Right)</button>
<input type="text" class="val_area" id="txt_set_hvacRow1RightTemp"></input>
<span class="span_text">(*authorize required)</span>
<br><button onclick="btnGetHVACRow1RightTemp();">get HVACTemp(Row1-Right)</button>
<span class="val_area" id="msg_get_hvacRow1RightTemp"></span>
<br>
<br><button onclick="btnSubscribeSpeed();">subscribe speed</button>
<button onclick="btnUnsubscribeSpeed();">unsubscribe</button>
<span class="val_area" id="msg_sub_speed"></span>
<br><button onclick="btnSubscribeEngineRpm();">subscribe engine RPM</button>
<button onclick="btnUnsubscribeEngineRpm();">unsubscribe</button>
<span class="val_area" id="msg_sub_rpm"></span>
<br><button onclick="btnSubscribeSteerAngle();">subscribe steer angle</button>
<button onclick="btnUnsubscribeSteerAngle();">unsubscribe</button>
<span class="val_area" id="msg_sub_steer"></span>
<br><button onclick="btnUnsubscribeAll();">unsubscribeAll</button>
<br>
<!-- use VSS data -->
<br><span id="span_path">data path: </span><select id="sel_path">
<option value="">----</option>
</select>
<br><input type="text" class="val_area_long" id="txt_vss_path"></input>
<br><span class="msg_vss_desc" id="msg_vss_desc1"></span>
<br><span class="msg_vss_desc" id="msg_vss_desc2"></span>
<br>
<button onclick="btnGetAnyItem();">get</button>
<button onclick="btnSubscribeAnyItem();">subscribe</button>
<button onclick="btnUnsubscribeAnyItem();">unsubscribe</button><br>
<button onclick="btnSetAnyItem();">set</button>
<input type="text" class="val_area" id="txt_vss_set" ></input>
<br>
<button onclick="clearVssMsgArea();">clearMsgArea</button>
<textarea id="msg_vss"></textarea>
</main>
<div id="right">
<button onclick="clearMsgArea();">clearMsgArea</button>
<textarea id="msg"></textarea>
</div>
</div>
<script>
// WebSocket readyState
var WS_CONNECTING = 0;
var WS_OPEN = 1;
var WS_CLOSING = 2;
var WS_CLOSED = 3;
var SUBPROTOCOL = "wvss1.0";
var VSS_FILENAME = "vss.json";
var g_vehicle = null; // only one WebSocket connection is allowed
var g_reqTable = null;
var g_vssObj = null; // VSS object loaded from vss.json
var g_ui = {
speedReqId : null,
engineRpmReqId : null,
steerAngleReqId : null,
vssSubscribeReqId : [], //vssアイテムsubscribe時に、path:reqId のhashを格納
// lock buttons to avoid subscribing a data twice
clearSubscribeBtnLock : function(reqId) {
if (reqId == this.speedReqId) {
this.speedReqId = null;
return true;
} else if (reqId == this.engineRpmReqId) {
this.engineRpmReqId = null;
return true;
} else if (reqId == this.steerAngleReqId) {
this.steerAngleReqId = null;
return true;
} else {
return false;
}
},
clearSubscribeBtnLockAll : function() {
this.speedReqId = null;
this.engineRpmReqId = null;
this.steerAngleReqId = null;
}
};
// =========================
// == define RequestTable ==
// =========================
// memo: all requests should be stored in this table
g_reqTable = {
subIdHash: {}, // store 'reqId:subId' for reqId : subId relation
requestHash: {}, // store 'reqId:reqObj' for reqId : reqObj relation
// add 'reqId:reqObj' into requestHash
addRequest: function(reqId, reqItem) {
showInMsgArea("addRequest: reqId="+reqId);
if (this.requestHash[reqId] != undefined) {
showInMsgArea("--:Error: requestId already used. reqId="+reqId);
return false;
}
this.requestHash[reqId] = reqItem;
showInMsgArea("--:EntryNum=" + Object.keys(this.requestHash).length);
return true;
},
// delete 'reqId:reqObj' from requestHash. delete from subIdHash too if exists
deleteRequest: function(reqId) {
showInMsgArea("deleteRequest: reqId="+reqId);
if (this.requestHash[reqId] == undefined)
return false;
var subId = this.requestHash[reqId].subscriptionId;
delete this.requestHash[reqId];
// requestはsubIdを持たない場合もある
// TODO: unsubscribeのエントリはsubIdHashには登録しない
if (subId != undefined)
delete this.subIdHash[subId];
showInMsgArea("--:entry deleted. reqId="+reqId+" ,subId="+subId);
return true;
},
// get reques object via requestId
getRequest: function(reqId) {
if (this.requestHash[reqId] == undefined) return false;
return this.requestHash[reqId];
},
// get request object via subscribe Id
getRequestBySubId: function(subId) {
var reqId = this.subIdHash[subId];
if (reqId == undefined) return false;
return this.getRequest(reqId);
},
//add subscriptionId to a 'reqId:reqObj' in requestHash
addSubId: function(reqId, subId) {
showInMsgArea("addSubId: reqId="+reqId+" subId="+subId);
// this func is only for 'subscribe' request.
// should not be used for 'unsubscribe' request, otherwise
// a subscriptionId may be used twice in hash array.
if (this.requestHash[reqId] == undefined) {
showInMsgArea("--:Error: this requestId entry not exits. reqId="+reqId);
return false;
}
if (this.subIdHash[subId] != undefined) {
showInMsgArea("--:Error: this subscriptionId already used. subId="+subId);
return false;
}
this.requestHash[reqId].subscriptionId = subId;
this.subIdHash[subId] = reqId; // for cross reference.
return true;
},
// get subscription Id via corresponding request Id
getSubIdByReqId: function(rId) {
var obj = this.requestHash[rId];
if (obj == undefined) return null;
return obj.subscriptionId;
},
// get request Id from corresponding subscription Id
getReqIdBySubId: function(sId) {
var reqId = this.subIdHash[sId]; //subIdHash はダブリなどないはず
if (reqId == undefined) return null;
return reqId;
},
// delete all subscribe entry
deleteAllSubscribe: function() {
for (var i in this.subIdHash) {
var reqId = this.subIdHash[i];
delete this.requestHash[reqId];
delete this.subIdHash[i];
}
this.dispSubIdHash();
this.dispRequestHash();
},
// clear requestHash
clearAllRequest: function() {
showInMsgArea("clearAllRequest:");
for (var i in this.requestHash) {
showInMsgArea("--:delete : " + i);
delete this.requestHash[i];
}
for (var i in this.subIdHash) {
showInMsgArea("--:delete : " + i);
delete this.subIdHash[i];
}
g_ui.clearSubscribeBtnLockAll();
},
// for debug
dispRequestHash: function() {
showInMsgArea("dispRequestHash:");
for (var i in this.requestHash) {
showInMsgArea("--:reqId="+i+", req="+JSON.stringify(this.requestHash[i]));
}
},
dispSubIdHash: function() {
showInMsgArea("dispSubIdHash:");
for (var i in this.subIdHash) {
showInMsgArea("--:subId="+i+", reqId="+this.subIdHash[i]);
}
}
};
// ===================
// == event handler ==
// ===================
function onWsOpen() {
showInMsgArea("onWsOpen: Connection opened");
}
function onWsClose() {
showInMsgArea("onWsClose: Connection closed");
}
function onWsMessage(event) {
//showInMsgArea("--: <== " + event.data.substr(0,3000));
showInMsgArea("--: <== " + event.data.substr(0,300));
var msg;
try {
msg = JSON.parse(event.data);
} catch(e) {
showInMsgArea("Irregular Json received. Ignore.");
return;
}
var reqTableItem=null, reqObj=null, action=null, reqId=null, subId=null;
if (msg.requestId != undefined) {
reqId = msg.requestId;
reqTableItem = g_reqTable.getRequest(reqId);
reqObj = reqTableItem.reqObj;
} else if (msg.subscriptionId != undefined) {
//reqIdなし、subIdあり=subscribe notification の場合
subId = msg.subscriptionId;
reqTableItem = g_reqTable.getRequestBySubId(subId);
reqObj = reqTableItem.reqObj;
}
if (reqObj)
action = reqObj.action;
// case of 'get'
if (action === "get") {
if (isGetSuccessResponse(msg)) {
if (reqTableItem.dispId === 'msg_vss')
showActionByPathResMsg(msg.value, reqObj.action, reqObj.path);
else
showActionResMsg(msg.value, reqTableItem.dispId)
// delete request from requestHash. delete even in error case
g_reqTable.deleteRequest(reqId);
} else if (isGetErrorResponse(msg)) {
showInMsgArea("--: <== 'get' request was rejected");
g_reqTable.deleteRequest(reqId);
}
// case of 'set'
} else if (action === "set") {
if (isSetSuccessResponse(msg)) {
// nothing to do
g_reqTable.deleteRequest(reqId);
} else if (isSetErrorResponse(msg)) {
g_reqTable.deleteRequest(reqId);
showInMsgArea("--: <== 'set' request was rejected");
}
// case of 'subscribe'
} else if (action === "subscribe") {
if (isSubscribeSuccessResponse(msg)) {
g_reqTable.addSubId(msg.requestId, msg.subscriptionId);
} else if (isSubscribeErrorResponse(msg)) {
g_reqTable.deleteRequest(msg.requestId);
showInMsgArea("--: <== 'subscribe' request was rejected");
} else if (isSubscriptionNotification(msg)) {
// case of subscribeNotification
if (reqTableItem.dispId === 'msg_vss')
showActionByPathResMsg(msg.value, reqObj.action, reqObj.path);
else
showActionResMsg(msg.value, reqTableItem.dispId)
} else if (isSubscriptionNotificationError(msg)) {
// noting to do special here. continu subscribe.
} else {
//no one come here
}
// case of 'unsubscribe'
} else if (action === "unsubscribe") {
if (msg.error != undefined) {
// unsubscribe failed
// - delete unsubscribe request from requestTable
g_reqTable.deleteRequest(reqId);
} else {
// unsubscribe success
// - delete subscribe request from requestTable
// - delete unsubscribe request from requestTable
var reqId = msg.requestId;
var targ_subId = msg.subscriptionId; //unsub対象のsubscribeのsubId
var targ_reqId = g_reqTable.getReqIdBySubId(targ_subId); //subscribeのreqId
g_reqTable.deleteRequest(targ_reqId); // delete subscribe's entry in reqTable
g_reqTable.deleteRequest(reqId); // delete unsub's entry in reqTable
// clear flag for button lock
g_ui.clearSubscribeBtnLock(targ_reqId);
}
// case of 'unsubscribeAll'
} else if (action === "unsubscribeAll") {
showInMsgArea("unsubscribeAll response received");
if (msg.error != undefined) {
console.log("unsubscribeAll: error");
// unsubscribeAll failed
// - delete unsubscribe request from requestTable
g_reqTable.deleteRequest(reqId);
} else {
console.log("unsubscribeAll: success");
// unsubscribeAll success
// - delete unsubscribe request from requestTable
// - delete ALL subscribe request from requestTable
var reqId = msg.requestId;
g_reqTable.deleteAllSubscribe(); // delete all entry of subscribe in reqTable
g_reqTable.deleteRequest(reqId); // delete unsub's entry in reqTable
// clear flag for ALL subscribe button lock
g_ui.clearSubscribeBtnLockAll();
}
// case of 'authorize'
} else if (action === "authorize") {
if (isAuthorizeSuccessResponse(msg)) {
showActionResMsg("Authorized! TTL="+msg.TTL+"sec", reqTableItem.dispId);
} else if (isAuthorizeErrorResponse(msg)) {
showActionResMsg("Rejected! err="+msg.error, reqTableItem.dispId);
}
g_reqTable.deleteRequest(reqId);
// case of 'getMetadata'
} else if (action === "getMetadata") {
console.log("getMetadata:");
// received VSS is shown in log msg textarea
if (isGetMetadataSuccessResponse(msg)) {
showActionResMsg("Successfully retrieved VSS json.", reqTableItem.dispId);
showInMsgArea("--:vss = " + JSON.stringify(msg.metadata).substr(0,600));
} else if (isGetMeatadataErrorResponse(msg)) {
console.log("getMetadata: error case");
showActionResMsg("Failed.", reqTableItem.dispId);
} else {
console.log("getMetadata: else case");
}
g_reqTable.deleteRequest(reqId);
} else {
// others
console.log("unknown action received : " + action);
}
}
// ================
// == UI handler ==
// ================
function btnConnect() {
showInMsgArea("btnConnect:");
if (g_vehicle != null) {
showInMsgArea("--:WebSocket already open");
return;
}
var url = "ws://" + VISS_IP + ":" + VISS_PORT;
showInMsgArea("--:URL = " + url);
showInMsgArea("--:subprotocol = " + SUBPROTOCOL);
g_vehicle = new WebSocket(url, SUBPROTOCOL);
g_vehicle.onopen = onWsOpen;
g_vehicle.onclose = onWsClose;
g_vehicle.onmessage = onWsMessage;
showConnectMsg("WS Connected");
}
function btnDisconnect() {
showInMsgArea("btnDisconnect:");
if (g_vehicle === null) {
showInMsgArea("--:WebSocket not connected");
return;
}
g_vehicle.close();
g_vehicle = null;
g_reqTable.clearAllRequest();
showConnectMsg("WS Disconnected");
}
// === authorize ===
function wsAuthorize(_userToken, _deviceToken) {
showInMsgArea("wsAuthorize:");
if (g_vehicle != null && g_vehicle.readyState === WS_OPEN) {
var reqId = getUniqueReqId();
var tokens = {"authorization" : _userToken,
"www-vehicle-device": _deviceToken};
var req = {"action": "authorize", "tokens": tokens, "requestId":reqId};
var item = {"reqObj": req, "dispId": 'msg_authorize'};
g_reqTable.addRequest(reqId, item);
var json_str = JSON.stringify(req);
g_vehicle.send(json_str);
showInMsgArea("--: ==> " + json_str);
}
}
function btnAuthorizeValidToken() {
wsAuthorize(TOKEN_VALID, TOKEN_VALID);
}
function btnAuthorizeInvalidToken() {
wsAuthorize(TOKEN_INVALID, TOKEN_INVALID);
}
// === getMetadata ===
function wsGetMetadata(_path) {
//TODO: get VSS under path: not suppoted yet
showInMsgArea("wsGetMetadata:");
if (g_vehicle != null && g_vehicle.readyState === WS_OPEN) {
var reqId = getUniqueReqId();
var req = {"action": "getMetadata", "path": _path, "requestId":reqId};
var item = {"reqObj": req, "dispId": 'msg_getmetadata'};
g_reqTable.addRequest(reqId, item);
var json_str = JSON.stringify(req);
g_vehicle.send(json_str);
showInMsgArea("--: ==> " + json_str);
}
}
function btnGetMetadata() {
var path = getMetadataPath();
wsGetMetadata(path);
}
function getMetadataPath() {
var val = document.getElementById("txt_getMetadata_path").value;
return val;
}
// === get ===
function wsGetItem(_path, _dispId) {
if (g_vehicle != null && g_vehicle.readyState === WS_OPEN) {
var reqId = getUniqueReqId();
var req = {"action": "get", "path": _path, "requestId":reqId};
var item = {"reqObj": req, "dispId": _dispId}; //戻り値の表示先IDを追加
g_reqTable.addRequest(reqId, item);
var json_str = JSON.stringify(req);
g_vehicle.send(json_str);
showInMsgArea("--: ==> " + json_str);
}
}
function btnGetSpeed() {
wsGetItem("Signal.Drivetrain.Transmission.Speed", "msg_get_speed" );
}
function btnGetEngineRPM() {
wsGetItem("Signal.Drivetrain.InternalCombustionEngine.RPM", "msg_get_rpm");
}
function btnGetSteeringAngle() {
wsGetItem("Signal.Chassis.SteeringWheel.Angle", "msg_get_steer");
}
// === set ===
// in case of 'set', dispId may not needed..
function wsSetItem(path,value, _dispId) {
showInMsgArea("wsSetItem:");
if (g_vehicle != null && g_vehicle.readyState === WS_OPEN) {
var reqId = getUniqueReqId();
var req = {"action": "set", "path": path, "value": value, "requestId":reqId};
var item = {"reqObj": req, "dispId": _dispId}; //戻り値の表示先IDを追加
g_reqTable.addRequest(reqId, item);
var json_str = JSON.stringify(req);
g_vehicle.send(json_str);
showInMsgArea("--: ==> " + json_str);
}
}
function btnSetDoorRow1RightLock() {
var value = document.getElementById("txt_set_doorRow1RightLock").value;
wsSetItem("Signal.Cabin.Door.Row1.Right.IsLocked", value, "txt_set_doorRow1RightLock");
}
function btnGetDoorRow1RightLock() {
wsGetItem("Signal.Cabin.Door.Row1.Right.IsLocked", "msg_get_doorRow1RightLock");
}
function btnSetHVACRow1RightTemp() {
var value = document.getElementById("txt_set_hvacRow1RightTemp").value;
wsSetItem("Signal.Cabin.HVAC.Row1.RightTemperature", value, "txt_set_hvacRow1RightTemp");
}
function btnGetHVACRow1RightTemp() {
wsGetItem("Signal.Cabin.HVAC.Row1.RightTemperature", "msg_get_hvacRow1RightTemp");
}
// === subscribe/unsubscribe ===
function wsSubscribeItem(path, reqId, _dispId) {
showInMsgArea("wsSubscribeItem: reqId = " + reqId);
if (g_vehicle != null && g_vehicle.readyState === WS_OPEN) {
var req ={"action": "subscribe", "path": path, "filters":"", "requestId":reqId};
var item = {"reqObj": req, "dispId": _dispId}; //戻り値の表示先IDを追加
g_reqTable.addRequest(reqId, item);
var json_str = JSON.stringify(req);
g_vehicle.send(json_str);
showInMsgArea("--: ==> " + json_str);
return true;
} else {
return false;
}
}
function wsUnsubscribeItem(path, subId, _dispId) {
showInMsgArea("wsUnsubscribeItem: subId=" + subId);
if (g_vehicle != null && g_vehicle.readyState === WS_OPEN) {
var reqId = getUniqueReqId(); //reqIdはunsub用に新しいものを使用
var req = {"action": "unsubscribe", "requestId":reqId, "subscriptionId":subId};
var item = {"reqObj": req, "dispId": _dispId}; //戻り値の表示先IDを追加
g_reqTable.addRequest(reqId, item);
var json_str = JSON.stringify(req);
g_vehicle.send(json_str);
showInMsgArea("--: ==> " + json_str);
}
}
function wsUnsubscribeAll( ) {
showInMsgArea("wsUnsubscribeAll: subId=");
if (g_vehicle != null && g_vehicle.readyState === WS_OPEN) {
var reqId = getUniqueReqId(); //reqIdはunsub用に新しいものを使用
var req = {"action": "unsubscribeAll", "requestId":reqId};
var item = {"reqObj": req, "dispId": ""}; //戻り値の表示先IDを追加
g_reqTable.addRequest(reqId, item);
var json_str = JSON.stringify(req);
g_vehicle.send(json_str);
showInMsgArea("--: ==> " + json_str);
}
}
function btnSubscribeSpeed() {
if (g_ui.speedReqId != null) {
showInMsgArea("Speed is already subscribed");
return;
}
g_ui.speedReqId = getUniqueReqId();
var path = "Signal.Drivetrain.Transmission.Speed";
var ret = wsSubscribeItem(path, g_ui.speedReqId, "msg_sub_speed");
if (!ret) g_ui.speedReqId = null;
}
function btnUnsubscribeSpeed() {
var path = "Signal.Drivetrain.Transmission.Speed";
var targ_subId = g_reqTable.getSubIdByReqId(g_ui.speedReqId); //unsub対象のsubIdを取得
wsUnsubscribeItem(path, targ_subId, "msg_sub_speed");
}
function btnSubscribeEngineRpm() {
if (g_ui.engineRpmReqId != null) {
showInMsgArea("EngineRpm is already subscribed");
return;
}
g_ui.engineRpmReqId = getUniqueReqId();
var path = "Signal.Drivetrain.InternalCombustionEngine.RPM";
var ret = wsSubscribeItem(path, g_ui.engineRpmReqId, "msg_sub_rpm");
if (!ret) g_ui.engineRpmReqId = null;
}
function btnUnsubscribeEngineRpm() {
var path = "Signal.Drivetrain.InternalCombustionEngine.RPM"
var targ_subId = g_reqTable.getSubIdByReqId(g_ui.engineRpmReqId);
wsUnsubscribeItem(path, targ_subId, "msg_sub_rpm");
}
function btnSubscribeSteerAngle() {
if (g_ui.steerAngleReqId != null) {
showInMsgArea("Steering Angle is already subscribed");
return;
}
g_ui.steerAngleReqId = getUniqueReqId();
var path = "Signal.Chassis.SteeringWheel.Angle";
var ret = wsSubscribeItem(path, g_ui.steerAngleReqId, "msg_sub_steer");
if (!ret) g_ui.steerAngleReqId = null;
}
function btnUnsubscribeSteerAngle() {
var path = "Signal.Chassis.SteeringWheel.Angle";
var targ_subId = g_reqTable.getSubIdByReqId(g_ui.steerAngleReqId);
wsUnsubscribeItem(path, targ_subId, "msg_sub_steer");
}
function btnUnsubscribeAll() {
wsUnsubscribeAll();
}
// === VSS item: get/set/sub/unsub ===
function btnGetAnyItem() {
var path = getSelectedVssItemPath();
wsGetItem(path, "msg_vss");
}
function btnSetAnyItem() {
var path = getSelectedVssItemPath();
var value = getItemValToSet();
wsSetItem(path,value, "msg_vss");
}
function btnSubscribeAnyItem() {
var path = getSelectedVssItemPath();
var reqId = getUniqueReqId();
// not allow subscribing same data path twice.
if (g_ui.vssSubscribeReqId[path] != undefined) {
showInMsgArea("Thie data item is already subscibed.")
return;
}
g_ui.vssSubscribeReqId[path] = reqId; //save path:reqId in global
wsSubscribeItem(path, reqId, "msg_vss");
}
function btnUnsubscribeAnyItem() {
var path = getSelectedVssItemPath();
var targ_reqId = g_ui.vssSubscribeReqId[path];
if (targ_reqId == undefined) {
showInMsgArea("This VSS item is not subscribed");
return;
} else {
delete g_ui.vssSubscribeReqId[path];
}
var targ_subId = g_reqTable.getSubIdByReqId(targ_reqId);
wsUnsubscribeItem(path, targ_subId, "msg_vss");
}
function getSelectedVssItemPath() {
var sel_path = document.getElementById("sel_path");
var idx = sel_path.selectedIndex;
var val = sel_path[idx].value;
return val;
}
function getItemValToSet() {
var val = document.getElementById("txt_vss_set").value;
return val;
}
function selVssPathOnChange() {
var path = getSelectedVssItemPath();
if (path=='' || path=='----') return;
if (g_vssObj == null) return;
var leaf = getLeaf(g_vssObj, path);
var _type = leaf.type!=undefined ? leaf.type:' - ';
var _unit = leaf.unit!=undefined ? leaf.unit:' - ';
var _max = leaf.max!=undefined ? leaf.max:' - ';
var _min = leaf.min!=undefined ? leaf.min:' - ';
var _desc = leaf.desc!=undefined ? leaf.desc:' - ';
var _enum = (function(){
if(leaf.enum!=undefined && leaf.enum.length>0)
return leaf.enum.join(' / ');
else
return ' - ';
})();
var txt1 = "type: "+_type+", unit: "+_unit+", max: "+_max+", min: "+_min;
var txt2 = "enum: "+_enum;
var vss_path = document.getElementById("txt_vss_path");
var vss_desc1 = document.getElementById("msg_vss_desc1");
var vss_desc2 = document.getElementById("msg_vss_desc2");
vss_path.value = path;
vss_desc1.innerHTML = txt1;
vss_desc2.innerHTML = txt2;
}
// == get helper funcs ==
function isGetSuccessResponse(msg) {
// This is getSuccessResponse if ...
// must exist : action, requestId, value, timestamp
// must not exist: error
if (msg.action === "get" && msg.requestId != undefined &&
msg.value != undefined && msg.timestamp != undefined &&
msg.error == undefined)
return true;
else
return false;
}
function isGetErrorResponse(msg) {
// This is getErrorResponse if ...
// must exist : action, requestId, error, timestamp
// must not exist: value
if (msg.action === 'get' && msg.requestId != undefined &&
msg.error != undefined && msg.timestamp != undefined &&
msg.value == undefined)
return true;
else
return false;
}
// == set helper funcs ==
function isSetSuccessResponse(msg) {
// This is setSuccessResponse if ...
// must exist : action, requestId, timestamp
// must not exist: error, value
if (msg.action === "set" && msg.requestId != undefined && msg.timestamp != undefined &&
msg.value == undefined && msg.error == undefined)
return true;
else
return false;
}
function isSetErrorResponse(msg) {
// This is setErrorResponse if ...
// must exist : action, requestId, error, timestamp
// must not exist: value
if (msg.action === 'set' && msg.requestId != undefined && msg.error != undefined && msg.timestamp != undefined &&
msg.value == undefined)
return true;
else
return false;
}
// == subscribe helper funcs ==
// Judge returned Json message's type
// TODO: better to verify with json shema?
function isSubscribeSuccessResponse(msg) {
// This is subscribeSuccessResponse if ...
// must exist : (action), requestId, subscriptionId, (timestamp)
// must not exist: error, value
if (msg.action === "subscribe" && msg.requestId != undefined && msg.subscriptionId != undefined &&
msg.error == undefined && msg.value == undefined)
return true;
else
return false;
}
function isSubscribeErrorResponse(msg) {
// This is subscribeErrorResponse if ...
// must exist : (path), requestId, error,(timestamp)
// must not exist: (action), subscriptionId, value
if (msg.path != undefined && msg.requestId != undefined && msg.error != undefined &&
msg.action == undefined && msg.subscriptionId == undefined && msg.value == undefined)
return true;
else
return false;
}
function isSubscriptionNotification(msg) {
// This is subscriptionNotification if ..
// must exist : subscriptionId, (path), value, (timestamp)
// must not exist: error, (requestId), (action)
if (msg.subscriptionId != undefined && msg.value != undefined &&
msg.error == undefined)
return true;
else
return false;
}
function isSubscriptionNotificationError(msg) {
// This is subscriptionNotificationError if ..
// following members exist : subscriptionId, (path), error, (filters), (timestamp)
// following members not exist: value, (requestId), (action)
if (msg.subscriptionId != undefined && msg.error != undefined &&
msg.value == undefined)
return true;
else
return false;
}
// == authorize helper funcs ==
// Judge returned Json message's type
function isAuthorizeSuccessResponse(msg) {
if (msg.action === "authorize" && msg.requestId != undefined && msg.TTL != undefined &&
msg.error == undefined)
return true;
else
return false;
}
function isAuthorizeErrorResponse(msg) {
if (msg.action === "authorize" && msg.requestId != undefined && msg.TTL == undefined &&
msg.error != undefined)
return true;
else
return false;
}
// == getMetadata helper funcs ==
// Judge returned Json message's type
function isGetMetadataSuccessResponse(msg) {
if (msg.action === "getMetadata" && msg.requestId != undefined && msg.metadata != undefined &&
msg.error == undefined)
return true;
else
return false;
}
function isGetMetadataErrorResponse(msg) {
if (msg.action === "getMetadata" && msg.requestId != undefined && msg.metadata == undefined &&
msg.error != undefined)
return true;
else
return false;
}
// ===================
// == Utility funcs ==
// ===================
// to get unique ID to use as requestID
function getUniqueReqId() {
// create semi-uniquID (for implementation easyness) as timestamp(milli sec)+random string
// uniqueness is not 100% guaranteed.
var strength = 1000;
var uniq = new Date().getTime().toString(16) + Math.floor(strength*Math.random()).toString(16);
return "reqid-"+uniq;
}
function showInMsgArea(msgText) {
showInMsgAreaById(msgText, 'msg', 6000);
}
function showInMsgAreaById(msgText, id, size_limit) {
var targ = document.getElementById(id);
var oldText = targ.innerHTML;
oldText = oldText.substring(0, size_limit);
var newText = msgText + "\n" + oldText;
targ.innerHTML = newText;
}
function showActionResMsg(value, targ_id) {
var targ = document.getElementById(targ_id);
targ.innerHTML = value;
}
function showActionByPathResMsg(value, action, path) {
var msgText = action + " : " + path + "\n==> value : "+value;
showInMsgAreaById(msgText, 'msg_vss', 200);
}
function showConnectMsg(msgText) {
var targ = document.getElementById('msg_connect');
targ.innerHTML = msgText;
}
function clearMsgArea() {
var targ = document.getElementById('msg');
targ.innerHTML = "";
}
function clearVssMsgArea() {
var targ = document.getElementById('msg_vss');
targ.innerHTML = "";
}
function createPathArry(_vssObj) {
var root = _vssObj;
var dbg_depth = 0;
var pathArry = [];
Object.keys(root).forEach(function(key) {
var node = root[key];
traverse(key, node, '', leafCallback, dbg_depth);
});
return pathArry;
// traverseしてleafに到達したら行う処理
function leafCallback(_key, _node, _path) {
var item = {'path':_path, 'node':_node};
pathArry.push(item);
}
}
// function for traversing VSS object tree
function traverse(_key, _node, _path, _leafCallback, _depth) {
var depth = _depth+1;
var path = _path=='' ? _key : _path+'.'+_key;
if (_node.type == undefined) {
return;
}
if (_node.type === 'branch') {
//console.log( Array(depth+1).join(' ')+"branch:" + _key);
//branch case
if (_node.children) {
// children以下の要素についてtraverseを行う
Object.keys(_node.children).forEach(function(key) {
var node = _node.children[key];
traverse(key, node, path, _leafCallback, depth);
});
} else {
//no children. do nothing.
}
} else {
//leaf case
//console.log( Array(depth+1).join(' ')+"leaf:" + _key);
_leafCallback(_key, _node, path);
}
}
// get leaf from VSS object tree
function getLeaf(_dataObj, _path) {
var pathArry = _path.split(".");
var obj = _dataObj;
for (var i=0, len=pathArry.length; i<len; i++) {
if (obj.children != undefined)
obj = obj.children;
if (obj[pathArry[i]] != undefined) {
obj = obj[pathArry[i]];
} else {
// this case should not exist
return undefined;
}
}
return obj;
}
// =============
// == UI init ==
// =============
(function init() {
var svr_area = document.getElementById("server_info");
svr_area.innerHTML = "Server IP:" + VISS_IP + " WebSocket Port:" + VISS_PORT;
var msg_areas = document.getElementsByClassName("val_area");
for (var i=0; i<msg_areas.length; i++) {
msg_areas.item(i).innerHTML = "---";
}
//load vss.json
{
var xhr = new XMLHttpRequest();
xhr.open("get", "/"+VSS_FILENAME, true);
xhr.onload = function() {
try {
g_vssObj = JSON.parse(this.responseText);
} catch(e) {
showInMsgArea("Irregural format of VSS Json. Need replace VSS Json and restart.");
return;
}
var pathArry = createPathArry(g_vssObj);
for (var i=0; i<pathArry.length; i++) {
var opt = document.createElement("option");
opt.innerText = pathArry[i].path;
opt.value = pathArry[i].path;
document.getElementById("sel_path").appendChild(opt);
}
var sel = document.getElementById("sel_path");
sel.onchange = function(e){ selVssPathOnChange();};
}
xhr.send(null);
}