forked from q89123003/BLE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
characteristic_v2.js
121 lines (91 loc) · 3.09 KB
/
characteristic_v2.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
var util = require('util');
var Type = require('type-of-is');
var bleno = require('../bleno');
var BlenoCharacteristic = bleno.Characteristic;
var fs = require('fs');
var array = fs.readFileSync('/home/pi/Documents/info.txt').toString().split("\n");
for(i in array) {
console.log(array[i]);
}
var arr = array[0].split(' ');
typeID = arr[1];
arr = array[3].split(' ');
serviceID = arr[1];
var sendFlag = 0;
var sendBuffer = "";
var net = require('net');
var client = net.createConnection("/tmp/scan_node.socket");
client.on("connect", function() {
console.log('Connected to sensor.socket');
client.write('Hello, Server.')
});
client.on('data', function(data) {
console.log('Received: ' + data);
});
client.on('close', function() {
console.log('Connection closed');
});
var sensorCon = net.createConnection("/tmp/sensor_node.socket");
sensorCon.on("connect", function() {
console.log('Connected to sensor.socket');
sensorCon.write('Hello, Server.')
});
sensorCon.on('close', function() {
console.log('Connection closed');
});
var EchoCharacteristic = function() {
EchoCharacteristic.super_.call(this, {
uuid: 'ec0e',
properties: ['read', 'write', 'notify'],
value: null
});
this._value = new Buffer(0);
this._updateValueCallback = null;
this._subscribeFlag = 0;
EchoCharacteristic.prototype.ActiveSend = function(data, callback){
console.log("Active Sending: " + data);
callback(data);
}
};
util.inherits(EchoCharacteristic, BlenoCharacteristic);
EchoCharacteristic.prototype.onReadRequest = function(offset, callback) {
console.log('EchoCharacteristic - onReadRequest: value = ' + this._value.toString('hex'));
callback(this.RESULT_SUCCESS, this._value);
};
EchoCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
this._value = data;
console.log('EchoCharacteristic - onWriteRequest: value = ' + this._value.toString('hex'));
sensorCon.write(data.toString())
if (this._updateValueCallback) {
console.log('EchoCharacteristic - onWriteRequest: notifying');
//console.log(Type(data));
//this._updateValueCallback(this._value);
this._updateValueCallback(Buffer.from("Got it.", 'utf8'));
}
callback(this.RESULT_SUCCESS);
};
//When subscribed, it means Slave Agent is connected.
//Set _updateValueCallback to proper callback function and tell Scan Center.
EchoCharacteristic.prototype.onSubscribe = function(maxValueSize, updateValueCallback) {
console.log('EchoCharacteristic - onSubscribe');
this._subscribeFlag = 1;
this._updateValueCallback = updateValueCallback;
client.write('CON_SIG');
};
EchoCharacteristic.prototype.onUnsubscribe = function() {
console.log('EchoCharacteristic - onUnsubscribe');
this._updateValueCallback = null;
};
module.exports.EchoCharacteristic = EchoCharacteristic;
module.exports.sensorCon = sensorCon;
//var main = require('./main_v2');
//echochar = main.echochar;
/*
function intervalFunc () {
if(sendFlag == 1){
console.log("Calling ActiveSend From Socket Client");
sendFlag = 0;
echochar.ActiveSend(data, echochar._updateValueCallback);
}
}
*/