-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
979 lines (793 loc) · 30.8 KB
/
script.js
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
//--------------------------------------------------------------------------------------
// Written by SparkFun Electronics, Spring 2021
//
// An Client Property Sheet App to connect to an embedded device (like SparkFun OpenLog),
// determine available propertys using BLE and dynamically render the appropriate user
// interface for each property.
//
// The app leverages the WEB BLE API, available on Chrome-based browsers.
//
// For this app to operate correctly, the underlying device must encode and advertise properties
// as shown by the examples included in the apps respository.
//
//==================================================================================
// Copyright (c) 2021 SparkFun Electronics
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//==================================================================================
// SETUP - Define the Target BLE Service
//--------------------------------------------------------------------------------------
const kTargetServiceUUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
const kTargetServiceName = "SparkFun Board";
//--------------------------------------------------------------------------------------
//
console.clear();
//--------------------------------------------------------------------------------------
// Property Creation and Management
//
// Dynamically create and manage properties
//--------------------------------------------------------------------------------------
// BLE Codes for our service
const kBLEDescSFEPropCoreUUID = 0xA101;
var bIsConnected = false;
// Text conversion helpers
const textDecoder = new TextDecoder();
function dataToText(data){ return textDecoder.decode(data);}
const textEncoder = new TextEncoder();
function textToData(text){ return textEncoder.encode(text);}
const targetID = 'settings-container';
// Property list - keeps track of the devices properties.
const currentProperties=[];
let deviceName=kTargetServiceName;
function setDeviceName(name){
deviceName = name;
document.getElementById("settings-title").innerHTML= name+ " Settings";
}
// DEBUG?
let debugLoadTime=0;
// Setup our progress bar object -- used when loading
const progressBar = {
increment: 1,
set_value: function(value){
document.getElementById("myBar").style.width=value + "%";
},
add_value: function(value){
let element = document.getElementById("myBar");
element.style.width = (parseInt(element.style.width)+value) + "%";
},
start: function(){
this.set_value(0);
document.getElementById("myProgress").style.display="flex";
},
end: function(){
document.getElementById("myProgress").style.display="none";
}
};
// Simple error/warning/info message box object
const messageBox = {
msg_error : document.querySelector("#message-error"),
msg_warning : document.querySelector("#message-warning"),
_show_msg : function(msg, message, temporary){
msg.innerHTML = message;
msg.classList.replace("hide", "show");
if(temporary)
window.setTimeout(() =>this._hide_msg(msg), 4000);
},
_hide_msg : function(msg){
msg.classList.replace("show", "hide");
},
showError: function(message, temporary=true){
this._show_msg(this.msg_error, message, temporary);
},
showWarning: function(message, temporary=true){
this._show_msg(this.msg_warning, message, temporary);
},
hideMessages: function(){
this._hide_msg(this.msg_error);
this._hide_msg(this.msg_warning);
}
}
//---------------------------------------
// property things
//---------------------------------------
// Used to main unique ids for property objects
let namecnt = 0;
const kBlkRange = 0x02;
const kBlkTitle = 0x01;
const kBlkSelectOps = 0x03;
const kBlkIncrement = 0x04;
// Each property "type" is subclassed from a base class the defines the
// property interface.
//
// Each specialization class implements the UX of the property element and how
// to manage the property value. Also it manages information decoding from
// the BLE data received.
//
// Base property class
class Property{
constructor(bleChar, name, order){
this.characteristic = bleChar;
this.name = name;
this.order = order;
this.ID = "property-" + namecnt++; // unique id for the DOM
this.div = null;
this.timer=-1;
this.group=null;
this.title=null;
}
// Called to process data from the properties descriptor
processBlk(blkType, value, iPos){
if(blkType != kBlkTitle){ // does this property have a group title?
return iPos;
}
let len = value.getUint8(iPos++);
this.title = dataToText(new DataView(value.buffer, iPos, len));
return iPos + len;
}
init(){
// For now - enable notifications on all values ...
this.enableNotifications();
// check for group title - process using a Promise...
return new Promise( (resolve) => {
// title?
if(this.title != null && this.title.length > 2){
this.group= document.createElement("div");
this.group.innerHTML = ` <h2 class="title">`+ this.title + `</h2>`;
document.getElementById(targetID).appendChild(this.group);
}
this.generateElement();
resolve(0);
}).catch(error => {
console.log("Error setting up property: ", this.name);
});
}
updateValue(value){}
update(){
this.characteristic.readValue().then( value =>{
this.updateValue(value);
});
}
enableNotifications(){
if(this.characteristic){
this.characteristic.startNotifications().then( _ =>{
let target = this;
this.characteristic.addEventListener('characteristicvaluechanged', function(event){
target.updateValue(event.target.value);
});
}).catch(error => {
//console.log("Notifications not available.");
// just eat this
});
}
}
deleteElement(){
if(this.div){
this.div.remove();
this.div=null;
}
if(this.group){
this.group.remove();
this.group=null;
}
}
}
// -------------------------------------------
// Boolean property
class boolProperty extends Property{
// create the ux/html / element
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="setting">
<input type="checkbox" id="`+ this.ID + `" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
// event handler wireup - on change event, save the new value to BLE device
this.inputField.addEventListener("change", () => {
this.saveValue();
});
this.update();
}
updateValue(value){
this.inputField.checked = value.getUint8(0, true);
}
saveValue(){
// Get the value from the input field and save it to the BLEcharacteristic
let buff = new ArrayBuffer(1);
let newValue = new Uint8Array(buff);
newValue[0] = this.inputField.checked;
this.characteristic.writeValue(buff);
}
}
//-------------------------------------------------------------
// Range Property
//
// Define some colors/UX
const range_fill = "#0B1EDF";
const range_background = "rgba(255, 255, 255, 0.214)";
class rangeProperty extends Property{
// Grab min and max values from the data block for this property
processBlk(blkType, value, iPos){
if(blkType != kBlkRange){
return super.processBlk(blkType, value, iPos);
}
this.min = value.getInt32(iPos,true);
this.max = value.getInt32(iPos+4, true);
return iPos + 8;
}
// Used to update the UX when the value changes...
_updateUX(value){
//set text
this.txtValue.setAttribute("data-length", ' ' + value);
// set gutter color -
const percentage = (100 * (value - this.input.min)) / (this.input.max - this.input.min);
const bg = `linear-gradient(90deg, ${range_fill} ${percentage}%, ${range_background} ${percentage + 0.1}%)`;
this.input.style.background = bg;
}
//------------------------
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="length range__slider" data-min="`+ this.min +`" data-max="` + this.max +`">
<div class="length__title field-title" data-length='0'>`+ this.name +`:</div>
<input id="slider" type="range" min="`+ this.min +`" max="`+ this.max +`" value="16" />
</div>
`;
this.input = this.div.querySelector('input');
this.txtValue = this.div.querySelector('.length__title');
document.getElementById(targetID).appendChild(this.div);
// Update slider values - event handler
this.input.addEventListener("input", event => {
this._updateUX(event.target.value);
});
// On change, set the underlying value
this.input.addEventListener("change", () => {
this.saveValue();
});
// and now update to the current value
this.update();
}
updateValue(value){
this.input.value = value.getInt32(0, true);
this._updateUX(this.input.value);
}
saveValue(){
// Get the value from the input field and save it to the BLE characteristic
let buff = new ArrayBuffer(4);
let newValue = new Int32Array(buff);
newValue[0] = this.input.value;
this.characteristic.writeValue(buff);
}
}
//-------------------------------------------------------------
// textProperty Object
class textProperty extends Property{
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="text-prop">
<input type="text" id="`+ this.ID + `" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
this.inputField.addEventListener("change", () => {
this.saveValue();
});
this.update();
}
updateValue(value){
this.inputField.value = dataToText(value);
}
saveValue(){
// Get the value from the input field and save it to the characteristic
this.characteristic.writeValue(textToData(this.inputField.value));
}
}
//-------------------------------------------------------------
// intProperty Object
class intProperty extends Property{
constructor(bleChar, name, order){
super(bleChar, name, order);
this.increment = 1;
}
processBlk(blkType, value, iPos){
if(blkType != kBlkIncrement){
return super.processBlk(blkType, value, iPos);
}
this.increment = value.getUint32(iPos,true);
return iPos + 4;
}
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="number-prop">
<input type="number" id="`+ this.ID + `" step="` + this.increment + `" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
this.inputField.addEventListener("change", () => {
this.saveValue();
});
this.update();
}
updateValue(value){
// get the value from the BLE char and place it in the field
this.inputField.value = value.getInt32(0, true);
}
saveValue(){
// The arrows in the number field can fire off rapid events, which
// can hammer the update, so buffer using a timer
window.clearTimeout(this.timer);
this.timer = window.setTimeout(()=>{
// Get the value from the input field and save it to the characteristic
let buff = new ArrayBuffer(4);
let newValue = new Int32Array(buff);
newValue[0] = this.inputField.value;
this.characteristic.writeValue(buff);
}, 1500);
}
}
//-------------------------------------------------------------
// dateProperty Object
class dateProperty extends Property{
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="date-prop">
<input type="date" id="`+ this.ID + `" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
this.inputField.addEventListener("change", () => {
this.saveValue();
});
this.update();
}
updateValue(value){
// get the value from the BLE char and place it in the field
// Check for a valid date format...
let digits = dataToText(value).split("-", 3).concat(['','']);
// if any of the digits are not a digit, default to current date
if(isNaN(digits[0]) || isNaN(digits[1]) || isNaN(digits[2])){
let currTime = new Date();
digits = [currTime.getFullYear(), currTime.getMonth(), currTime.getDay()];
}
if(digits[0].length == 2){ // short year value?
digits[0] = '20'+digits[0];
}
// set value - pad month and day numbers with zeros
this.inputField.value = [digits[0], ('0'+digits[1]).slice(-2), ('0'+digits[2]).slice(-2)].join('-');
}
saveValue(){
// Typing in the timefield is slow, so you can hammer the BLE
// char with multiple sets as the date changes. Buffer this using a timer
window.clearTimeout(this.timer);
this.timer = window.setTimeout(()=>{
// Get the value from the input field and save it to the characteristic
this.characteristic.writeValue(textToData(this.inputField.value));
}, 1500);
}
}
//-------------------------------------------------------------
// timeProperty Object
class timeProperty extends Property{
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="time-prop">
<input type="time" id="`+ this.ID + `" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
this.inputField.addEventListener("change", () => {
this.saveValue();
});
this.update();
}
updateValue(value){
// Check for a valid time - split value, concat empty value to ensure 2 elements
let digits = dataToText(value).split(":", 2).concat('');
// if any of the time value are not a digit, default to current time
if(isNaN(digits[0]) || isNaN(digits[1])){
let currTime = new Date();
digits = [currTime.getHours(), currTime.getMinutes()];
}
// set value - pad numbers with zeros
this.inputField.value = [('0'+digits[0]).slice(-2), ('0'+digits[1]).slice(-2)].join(':');
}
saveValue(){
// Typing in the timefield is slow, so you can hammer the BLE
// char with multiple sets as the date changes. Buffer this using a timer
window.clearTimeout(this.timer);
this.timer = window.setTimeout(()=>{
// Get the value from the input field and save it to the characteristic
this.characteristic.writeValue(textToData(this.inputField.value));
}, 1500);
}
}
//-------------------------------------------------------------
// floatProperty Object
class floatProperty extends Property{
processBlk(blkType, value, iPos){
if(blkType != kBlkIncrement){
return super.processBlk(blkType, value, iPos);
}
this.increment = value.getFloat32(iPos,true).toFixed(5); //round it
return iPos + 4;
}
constructor(bleChar, name, order){
super(bleChar, name, order);
this.increment = .01;
}
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="number-prop">
<input type="number" id="`+ this.ID + `" step="`+this.increment+`" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
this.inputField.addEventListener("change", () => {
this.saveValue();
});
this.update();
}
updateValue(value){
// We are limiting floats to three decimal places.
// Round out the floating number noise
this.inputField.value = Math.round((value.getFloat32(0,true) + Number.EPSILON)*1000)/1000;
}
saveValue(){
// The arrows in the number field can fire off rapid events, which
// can hammer the update, so buffer using a timer
window.clearTimeout(this.timer);
this.timer = window.setTimeout(()=>{
// Get the value from the input field and save it to the characteristic
let buff = new ArrayBuffer(4);
let newValue = new Float32Array(buff);
newValue[0] = this.inputField.value;
this.characteristic.writeValue(buff);
}, 1500);
}
}
//-------------------------------------------------------------
// selectProperty Object
//
// Select a value from a list of possible values
class selectProperty extends Property{
processBlk(blkType, value, iPos){
if(blkType != kBlkSelectOps){
return super.processBlk(blkType, value, iPos);
}
let len = value.getUint8(iPos++);
let buffer = dataToText(new DataView(value.buffer, iPos, len));
this.options = buffer.split('|');
return iPos + len;
}
//------------------------
generateElement(){
this.div = document.createElement("div");
let buffer =`<div class="select-prop">
<select id="`+ this.ID + `">`;
for(const option of this.options ){
buffer = buffer + ` <option value="` + option + `">` + option + `</option>`;
}
this.div.innerHTML = buffer + ` </select>
<label for="` + this.ID + `">` + this.name + `</label>
</div>`;
this.select = this.div.querySelector('select');
document.getElementById(targetID).appendChild(this.div);
// On change, set the underlying value
this.select.addEventListener("change", () => {
this.saveValue();
});
// and now update to the current value
this.update();
}
updateValue(value){
this.select.value = dataToText(value);
}
saveValue(){
// Get the value from the select and save it.
this.characteristic.writeValue(textToData(this.select.value));
}
}
// --------------------
// Delete all properties
function deleteProperties(){
while(currentProperties.length > 0){
currentProperties[0].deleteElement(); // delete UI
currentProperties.splice(0,1); // pop out of array
}
//document.getElementById(targetID).style.display="none"; // hide settings area
}
function showProperties(){
/*
if(currentProperties.length > 0){
document.getElementById(targetID).style.display="flex";
}
*/
}
// Used for property sorting --
function compairPropOrder(a, b){
if ( a.order < b.order ){
return -1;
}
if ( a.order > b.order ){
return 1;
}
return 0;
}
async function renderProperties(){
// first sort our props so they display as desired
currentProperties.sort(compairPropOrder);
// build the UX for each property - want this is order -- so wait
for(const aProp of currentProperties){
let result = await aProp.init();
}
progressBar.set_value(100);
showProperties();
endConnecting(true);
}
//--------------------------------------------------------------------------------------
// Add a property to the system based on a BLE Characteristic
//
// Our property object defs = KEY: The order is same as type code index above.
const propFactory = [boolProperty, intProperty, rangeProperty, textProperty, dateProperty,
timeProperty, floatProperty, selectProperty];
// use a promise to encapsulate the async BLE calls
function addPropertyToSystem(bleCharacteristic){
// use a promise to encapsualte this add - might be overkill ... but
// with a lot of props ...
return new Promise( (resolve) => {
// Get the core descriptor
bleCharacteristic.getDescriptor(kBLEDescSFEPropCoreUUID).then(descType =>{
if(descType == null){
console.log("No type descriptor found.")
resolve(-1);
}
// Get the value of the type descriptor
descType.readValue().then(value =>{
let iPos = 0;
let attributes = value.getUint32(0,true);
let order = attributes >> 8 & 0xFF;
let type = attributes & 0xFF;
if(type < 0x1 || type > propFactory.length ){
console.log("Invalid Type value: " + type);
resolve(1);
}
iPos += 4;
// get name
let lenName = value.getUint8(iPos++);
let name = dataToText(new DataView(value.buffer, iPos, lenName));
iPos += lenName;
// build prop object - notice index into array of prop class defs
let property = new propFactory[type-1](bleCharacteristic, name, order);
// Does this property have other data blocks in the descriptor value
while(iPos < value.byteLength){
let blkType = value.getUint8(iPos++);
iPos = property.processBlk(blkType, value , iPos);
}
currentProperties.push(property);
progressBar.add_value(progressInc);
resolve(0);
}).catch(error => {
console.log("readValue error: ", error);
resolve(1);
});
}).catch(error => {
console.log("getDescriptor - property type failed", error);
resolve(1);
});
});
}
//--------------------------------------------------------------------------------------
// BLE Logic
//--------------------------------------------------------------------------------------
let theGattServer = null;
let isConnecting = false;
function disconnectBLEService(){
if( theGattServer != null && theGattServer.connected){
theGattServer.disconnect();
theGattServer = null;
}
}
function bleConnected(gattServer){
if(!gattServer || !gattServer.connected){
return;
}
theGattServer = gattServer;
}
// disconnect event handler.
function onDisconnected(){
if(isConnecting){
return;
}
document.getElementById("connect").innerHTML ="Connect To " + deviceName;
// Delete our properties....
deleteProperties();
theGattServer=null;
}
function startConnecting(){
document.body.style.cursor = "wait";
// update button label
let button= document.getElementById("connect");
button.innerHTML ="Connecting to " + deviceName + '...';
button.style.fontStyle='italic';
button.disabled=true;
button.style.cursor = "not-allowed";
progressBar.start();
isConnecting=true;
}
function endConnecting(success){
isConnecting=false;
document.body.style.cursor = "default";
let button= document.getElementById("connect");
button.style.fontStyle ='';
button.disabled=false;
button.style.cursor = "auto";
progressBar.end();
if(success){
// update button label
document.getElementById("connect").innerHTML ="Disconnect From " + deviceName;
}else{
onDisconnected();
}
console.log("Properties Load Time:", Date.now()-debugLoadTime);
}
//------------------------------------------------------
// For connectToGatt() - cascading timer obj
const gattWatchdog = {
timer_id: 0,
start: (timeout) => {
// Let's cascade warning and reset timeouts
timer_id = setTimeout(()=>{
// first warning
messageBox.showWarning("Still trying to connect...");
// set the reset timer
timer_id = setTimeout(() =>{
disconnectBLEService();
messageBox.showError("The bluetooth service is not responding. Resetting the application connection...", false);
// system is hung, reload page
window.setTimeout(()=>window.location.reload(false), 4000);
},timeout/2);
}, timeout/2);
},
cancel: () => clearTimeout(this.timer_id)
};
//------------------------------------------------------
// connectToGATT()
//
// Will retry if the connection seqeunce fails - retries nTries.
//
// On failure, this routine will recurse on itself.
//
function connectToGATT(device, nTries){
if(nTries<1){
console.log("Error connecting GATT server on device");
messageBox.showError("Unable to connect with the BLE GATT server. Disconnecting.");
endConnecting(false);
return;
}
nTries--;
progressBar.set_value(5);
return device.gatt.connect().then(gattServer => {
bleConnected(gattServer);
progressBar.add_value(15);
// On Windows - the get service call hangs sometimes. To catch
// this create a watchdog obj that cascades timer - issues warnings and if
// timeout exceeded, reloads page
gattWatchdog.start(15000);
// Connect to our target Service
return gattServer.getPrimaryService(kTargetServiceUUID).then(primaryService => {
// kill watchdog
gattWatchdog.cancel();
progressBar.add_value(20);
// Now get all the characteristics for this service
primaryService.getCharacteristics().then(theCharacteristics => {
// Add the characteristics to the property sheet
// The adds are async - so use promises, chaining everything
// together
progressBar.add_value(5);
progressInc = 70./theCharacteristics.length; // for updates in promises
// Chain together
let chain = Promise.resolve();
for(const aChar of theCharacteristics){
chain = chain.then(()=>addPropertyToSystem(aChar));
}
chain.then(() =>renderProperties());
}).catch(error => {
messageBox.showWarning("Having trouble accessing the device properties. Retrying ...");
disconnectBLEService();
setTimeout(()=>connectToGATT(device, nTries), 200);
});
}).catch(error => {
gattWatchdog.cancel();
disconnectBLEService();
messageBox.showWarning("Having trouble accessing bluetooth service. Retrying ...");
setTimeout(()=>connectToGATT(device, nTries), 200);
});
}).catch(error => {
messageBox.showWarning("Having trouble accessing bluetooth device. Retrying ...");
setTimeout(()=>connectToGATT(device, nTries), 200);
});
}
function connectToBLEService() {
// is ble supported?
if(typeof navigator.bluetooth == "undefined" ){
messageBox.showWarning("Bluetooth is not available in this browser. Only Chrome-based browers support Bluetooth connections.");
return;
}
let filters = [];
// KDB - for esp32 ...
// Filtering on the name on it's own will find the device, but not the device with the
// desired service. So adding the service to the search will find a device with the service
// BUT not the desired name (at least in the returned objects ...)
filters.push({name: kTargetServiceName});
filters.push({services:[kTargetServiceUUID]});
let options = {};
options.filters = filters;
startConnecting();
return navigator.bluetooth.requestDevice(options).then(device => {
debugLoadTime=Date.now(); // time the loading of the settings parameters ...
if(device.name)
setDeviceName(device.name);
progressBar.set_value(5);
device.addEventListener('gattserverdisconnected', onDisconnected);
///////////////////////////////////
// Connect to the GATT -
//
// GATT service connection is touchy on windoze. This routine inplements a retry
// strategy, trying 3 times to connect.
////////////////////////////////////
connectToGATT(device, 3);
}).catch(error => {
console.log(error);
messageBox.showWarning("Connection to bluetooth device cancelled.");
endConnecting(false);
});
}
//--------------------------------------------------------------------------------------
// Misc Items
//--------------------------------------------------------------------------------------
// The connect button
const connectBtn = document.getElementById("connect");
connectBtn.addEventListener("click", () => {
if(theGattServer && theGattServer.connected){
disconnectBLEService();
}else{
connectToBLEService();
}
});
window.onload = function(){
setDeviceName(kTargetServiceName);
}