forked from blueappio/terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.js
94 lines (79 loc) · 3.34 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
"use strict";
var Terminal = function () {
var TERM_SERVICE_UUID = "569a1101-b87f-490c-92cb-11ba5ea5167c";
var NOTIFY_CHAR_UUID = "569a2000-b87f-490c-92cb-11ba5ea5167c";
var WRITE_CHAR_UUID = "569a2001-b87f-490c-92cb-11ba5ea5167c";
var CONFIG_CHAR_UUID = "50270004-df25-45b0-8ad9-b27ceba6622f";
function Terminal(bluetooth) {
this.connected = false;
this.writeCharacteristic = undefined;
this.configCharacteristic = undefined;
this.configBuffer = undefined;
this.bluetooth = bluetooth;
}
Terminal.prototype.connect = function connect() {
var self = this;
var options = {filters: [{services: [TERM_SERVICE_UUID]}]};
return this.bluetooth.requestDevice(options)
.then(function (device) {
return device.gatt.connect();
})
.then(function (server) {
return server.getPrimaryService(TERM_SERVICE_UUID)
})
.then(function (service) {
return Promise.all([
/*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);
}
});
});
})
]);
})
.then(function () {
self.connected = true;
});
}
Terminal.prototype.writeData = function writeData(sendData) {
if (this.writeCharacteristic) {
return this.writeCharacteristic.writeValue(sendData);
}
return Promise.reject();
}
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();
});
});
}
}
return Terminal;
}();
if(window === undefined) {
module.exports.Terminal = Terminal;
}