forked from sandeepmistry/node-tethercell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (69 loc) · 2.61 KB
/
index.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
var async = require('async');
var NobleDevice = require('noble-device');
var SERVICE_UUID = '5ec0fff03cf2a682e2112af96efdf667';
var AUTHORIZATION_UUID = '5ec0fffc3cf2a682e2112af96efdf667';
var FET_STATE_UUID = '5ec0fff23cf2a682e2112af96efdf667';
var VOLTAGE_UUID = '5ec0fff33cf2a682e2112af96efdf667';
var DEVICE_NAME_UUID = '5ec0fff93cf2a682e2112af96efdf667';
var Tethercell = function(peripheral) {
NobleDevice.call(this, peripheral);
};
Tethercell.SCAN_UUIDS = [SERVICE_UUID];
Tethercell.is = function(peripheral) {
return (peripheral.advertisement.localName === undefined);
};
NobleDevice.Util.inherits(Tethercell, NobleDevice);
NobleDevice.Util.mixin(Tethercell, NobleDevice.DeviceInformationService);
Tethercell.prototype.writeServiceDataCharacteristic = function(uuid, data, callback) {
this.writeDataCharacteristic(SERVICE_UUID, uuid, data, callback);
};
Tethercell.prototype.readServiceDataCharacteristic = function(uuid, callback) {
this.readDataCharacteristic(SERVICE_UUID, uuid, callback);
};
Tethercell.prototype.authorize = function(pin, callback) {
this.writeServiceDataCharacteristic(AUTHORIZATION_UUID, new Buffer(pin, 'hex'), callback);
};
Tethercell.prototype.readFetState = function(callback) {
this.readServiceDataCharacteristic(FET_STATE_UUID, function(error, data) {
callback(error, data);
}.bind(this));
};
Tethercell.prototype.writeFetState = function(on, callback) {
var data = new Buffer([on ? 0x01 : 0x00]);
this.writeServiceDataCharacteristic(FET_STATE_UUID, data, callback);
};
Tethercell.prototype.turnOn = function(callback) {
this.writeFetState(true, callback);
};
Tethercell.prototype.turnOff = function(callback) {
this.writeFetState(false, callback);
};
Tethercell.prototype.isOn = function(callback) {
this.readFetState(function(error, data) {
if (error) {
callback(error);
} else {
callback(null, data[0] ? true : false);
}
}.bind(this));
};
Tethercell.prototype.readVoltage = function(callback) {
this.readUInt16LECharacteristic(SERVICE_UUID, VOLTAGE_UUID, function(error, value) {
if (error) {
callback(error);
} else {
var voltage = 1.36 * (value / 1662.0);
callback(error, voltage);
}
}.bind(this));
};
Tethercell.prototype.readDeviceName = function(callback) {
this.readStringCharacteristic(SERVICE_UUID, DEVICE_NAME_UUID, callback);
};
Tethercell.prototype.writeDeviceName = function(deviceName, callback) {
for (var i = deviceName.length; i < 16; i++) {
deviceName += '\0';
}
this.writeStringCharacteristic(SERVICE_UUID, DEVICE_NAME_UUID, deviceName, callback);
};
module.exports = Tethercell;