-
Notifications
You must be signed in to change notification settings - Fork 0
/
visiblethings.js
155 lines (134 loc) · 6.22 KB
/
visiblethings.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
(function() {
var gattip = null;
var count = 0;
function VisibleThings() {
var perId = 0;
this.peripherals = {};
gattip = navigator.bluetooth.gattip;
gattip.once('ready', function(gateway) {
console.log('ready');
function onScan(peripheral) {
var mfrData;
mfrData = peripheral.getMfrData('1019');
if (!mfrData && peripheral.advdata) {
mfrData = peripheral.advdata.manufacturerData['1019'];
}
// console.log(mfrData);
if (mfrData) {
mfrData = mfrData.toUpperCase();
var d = new Date();
var currDate = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
var data_array = mfrData.match(/.{1,2}/g);
visibleThings.peripherals[peripheral.uuid] = peripheral;
var n = 0;
var n2 = data_array.length;
while (n < n2) {
var n5 = '0x' + data_array[n];
var rawData = n5 >> 4 & 15;
var multiplier = Math.pow(2.0, (n5 & 15) - 8);
switch (rawData) {
case 1:
{
var extractedAcc = extractTripletData(n,multiplier,data_array);
extractedAcc = extractedAcc.map(function(value) {
return value > 127 ? value - 255 : value;
});
extractedAcc = extractedAcc.map(function(value) {
return (value/100).toFixed(2);
});
storeTripleData(visibleThings.peripherals[peripheral.uuid], 'accData', extractedAcc, count, currDate);
break;
}
case 2:
{
var extractedGyro = extractTripletData(n,multiplier,data_array);
extractedGyro = extractedGyro.map(function(value) {
return value > 127 ? value - 255 : value;
});
storeTripleData(visibleThings.peripherals[peripheral.uuid], 'gyroData', extractedGyro, count, currDate);
break;
}
case 3:
{
var extractedMag = extractTripletData(n,multiplier,data_array);
extractedMag = extractedMag.map(function(value) {
return value > 127 ? value - 255 : value;
});
storeTripleData(visibleThings.peripherals[peripheral.uuid], 'magData', extractedMag, count, currDate);
break;
}
case 4:
{
var extractedLight = extractTripletData(n,multiplier,data_array);
storeTripleData(visibleThings.peripherals[peripheral.uuid], 'lightData', extractedLight, count, currDate);
break;
}
case 5:
{
if (data_array[n]=='58') {
var extractedTemp = extractOneData(n,multiplier,data_array);
storeTempData(visibleThings.peripherals[peripheral.uuid], 'tempData', extractedTemp, count, currDate);
}
break;
}
case 10:
{
var extractedHum = extractOneData(n,multiplier,data_array);
storeHumData(visibleThings.peripherals[peripheral.uuid], 'humidityData', extractedHum, count, currDate);
break;
}
}
n = n + 2;
}
}
}
gateway.scan();
gateway.on('scan', onScan);
});
gattip.on('error', function(err) {
console.log(err);
});
}
window.visibleThings = new VisibleThings();
})();
function extractOneData(n,multiplier,data_array) {
var hex_data = data_array[n + 1];
return data = parseInt(hex_data, 16) * multiplier;
}
function extractTripletData(n,multiplier,data_array) {
var counter= n + 1;
var counter1=counter+1;
var value=[];
value[0]= parseInt(data_array[counter], 16) * multiplier;
value[1]= parseInt(data_array[counter1], 16) * multiplier;
value[2]= parseInt(data_array[++counter1], 16) * multiplier;
return value;
}
function storeTripleData(peripheral, data, extracted, count, currDate) {
if (!isNaN(Number(extracted[0])) && !isNaN(Number(extracted[1])) && !isNaN(Number(extracted[2]))) {
peripheral[data] = {
timeNum: count,
date: currDate,
value: extracted
};
}
}
function storeTempData(peripheral, data, extracted, count, currDate) {
if (!isNaN(Number(extracted))){
peripheral[data] = {
timeNum: count,
date: currDate,
value: extracted,
fahrenheit: extracted * 9 / 5 + 32
};
}
}
function storeHumData(peripheral, data, extracted, count, currDate) {
if (!isNaN(Number(extracted))){
peripheral[data] = {
timeNum: count,
date: currDate,
value: extracted
};
}
}