-
Notifications
You must be signed in to change notification settings - Fork 2
/
ip_characteristic.js
70 lines (55 loc) · 1.84 KB
/
ip_characteristic.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
var util = require('util');
var bleno = require('bleno');
exec = require('child_process').exec;
var BlenoCharacteristic = bleno.Characteristic;
var BlenoDescriptor = bleno.Descriptor;
var ip = require('ip');
var current_ip = ip.address();
function IpCharacteristic(ip) {
IpCharacteristic.super_.call(this, {
uuid: '01010101010101010166616465524742',
properties: ['read', 'notify', 'write', 'indicate'],
descriptors: [
new BlenoDescriptor({
uuid: '2901',
value: 'Public IP broadcast service'
})
]
});
this.ip = ip;
};
util.inherits(IpCharacteristic, BlenoCharacteristic);
IpCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
console.log('on -> onWriteRequest: value = ' + data.toString());
callback(this.RESULT_SUCCESS);
exec('/home/root/hellonode/set_network ' + data.toString(),
function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
});
};
IpCharacteristic.prototype.onReadRequest = function(offset, callback) {
console.log('on -> onReadRequest: value = ' + data.toString());
callback(this.RESULT_SUCCESS, ip.address());
};
IpCharacteristic.prototype.onSubscribe = function(maxValueSize,
updateValueCallback) {
console.log('on -> onSubscribe');
this._updateValueCallback = updateValueCallback;
setInterval(function() {
updateValueCallback(new Buffer(ip.address().toString('hex')));
}, 100 * 60);
};
IpCharacteristic.prototype.onNotify = function() {
console.log('on -> onNotify - ' + ip.address());
exec('node server.js',
function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
});
};
IpCharacteristic.prototype.onUnsubscribe = function() {
console.log('on -> onUnsubscribe');
this._updateValueCallback = null;
};
module.exports = IpCharacteristic;