-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathterminal.js
205 lines (183 loc) · 8.8 KB
/
terminal.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
"use strict";
var Terminal = function () {
var TERM_SERVICE_UUID = "50270001-df25-45b0-8ad9-b27ceba6622f";
var WRITE_CHAR_UUID = "50270002-df25-45b0-8ad9-b27ceba6622f";
var NOTIFY_CHAR_UUID = "50270003-df25-45b0-8ad9-b27ceba6622f";
var CONFIG_CHAR_UUID = "50270004-df25-45b0-8ad9-b27ceba6622f";
var GATT_INFO_SERVICE = 0x1800;
var DEVICE_NAME_CHAR = 0x2a00;
var INFO_SERVICE = 0x180a;
var HW_CHAR = 0x2a27;
var SW_CHAR = 0x2a28;
function Terminal(bluetooth) {
this.connected = false;
this.options = undefined;
this.scanned_perips = [];
this.gateway = undefined;
this.writeCharacteristic = undefined;
this.configCharacteristic = undefined;
this.deviceNameCharacteristic = undefined;
this.hardwareCharacteristic = undefined;
this.softwareCharacteristic = undefined;
this.deviceName = undefined;
this.hardwareVersion = undefined;
this.softwareVersion = undefined;
this.configBuffer = undefined;
this.bluetooth = navigator.bluetooth;
this.bluetoothDevice = undefined;
}
Terminal.prototype.connect = function connect() {
var self = this;
var options = {filters: [{services: [TERM_SERVICE_UUID]}]};
return navigator.bluetooth.requestDevice(options)
.then(function (device) {
self.bluetoothDevice = device;
return device.gatt.connect();
})
.then(function (server) {
return Promise.all([
server.getPrimaryService(GATT_INFO_SERVICE)
.then(function (service) {
return service.getCharacteristic(DEVICE_NAME_CHAR)
.then(function (characteristic) {
self.deviceNameCharacteristic = characteristic;
characteristic.readValue()
.then(function (data) {
var value = '';
for (var i = 0; i < data.byteLength; i++) {
value = value + String.fromCharCode(data.getUint8(i));
}
value = value.trim();
self.deviceName = value;
});
});
}, function (error) {
console.warn('GATT Info Service not found');
Promise.resolve(true);
}),
server.getPrimaryService(INFO_SERVICE)
.then(function (service) {
service.getCharacteristic(HW_CHAR)
.then(function (characteristic) {
self.hardwareCharacteristic = characteristic;
characteristic.readValue()
.then(function (data) {
var value = '';
for (var i = 0; i < data.byteLength; i++) {
value = value + String.fromCharCode(data.getUint8(i));
}
value = value.trim();
self.hardwareVersion = value;
});
});
service.getCharacteristic(SW_CHAR)
.then(function (characteristic) {
self.softwareCharacteristic = characteristic;
characteristic.readValue()
.then(function (data) {
var value = '';
for (var i = 0; i < data.byteLength; i++) {
value = value + String.fromCharCode(data.getUint8(i));
}
value = value.trim();
self.softwareVersion = value;
});
});
}, function (error) {
console.warn('Info Service not found');
Promise.resolve(true);
}),
server.getPrimaryService(TERM_SERVICE_UUID)
.then(function (service) {
self.connected = true;
service.getCharacteristic(CONFIG_CHAR_UUID)
.then(function (characteristic) {
self.configCharacteristic = characteristic;
return self.configCharacteristic.readValue()
.then(function (value) {
self.configBuffer = value;
});
}),
service.getCharacteristic(WRITE_CHAR_UUID)
.then(function (characteristic) {
self.writeCharacteristic = characteristic;
}),
service.getCharacteristic(NOTIFY_CHAR_UUID)
.then(function (characteristic) {
return characteristic.startNotifications()
.then(function () {
characteristic.addEventListener('characteristicvaluechanged', function (event) {
//todo: generate event
if (self.updateUI) {
self.updateUI(event.target.value);
}
});
});
})
}, function (error) {
console.warn('TERM_SERVICE_UUID Service not found');
Promise.resolve(true);
})
])
})
.then(function () {
self.connected = true;
});
}
/* Firmware Version read service*/
Terminal.prototype.readDeviceName = function readDeviceName(char) {
return char.readValue()
.then(function (data) {
var value = '';
for (var i = 0; i < data.byteLength; i++) {
value = value + String.fromCharCode(data.getUint8(i));
}
value = value.trim();
Terminal.deviceName = value;
return Promise.resolve();
});
}
Terminal.prototype.writeData = function writeData(sendData) {
if (this.writeCharacteristic) {
return this.writeCharacteristic.writeValue(sendData);
}
return Promise.reject();
}
Terminal.prototype.setDeviceName = function setDeviceName(sendData) {
if (this.deviceNameCharacteristic) {
return this.deviceNameCharacteristic.writeValue(sendData);
}
return Promise.resolve();
}
Terminal.prototype.writeConfigData = function writeConfigData(cfgData) {
var self = this;
if (this.configCharacteristic) {
return this.configCharacteristic.writeValue(cfgData)
.then(function () {
return self.configCharacteristic.readValue()
.then(function (value) {
self.configBuffer = value;
return Promise.resolve();
});
});
}
}
Terminal.prototype.disconnect = function disconnect() {
var self = this;
if (!self.bluetoothDevice) {
return Promise.reject();
}
return self.bluetoothDevice.disconnect()
.then(function () {
self.connected = false;
self.writeCharacteristic = undefined;
self.configCharacteristic = undefined;
self.configBuffer = undefined;
return Promise.resolve();
});
}
return Terminal;
}();
if (window === undefined) {
module.exports.Terminal = Terminal;
}