-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainProtoBuffer.js
145 lines (113 loc) · 3.11 KB
/
mainProtoBuffer.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
var moment = require('moment');
var fs = require("fs");
var NobleDevice = require('noble-device');
var protobuf = require('protocol-buffers')
var socket = require('net').Socket();
var messages = protobuf(fs.readFileSync('polarH7.proto'))
var tempName = [];
tempName[0] = moment().format('X');
tempName[1] = ".txt";
var tempHeader = [];
tempHeader[0] = "timestamp";
tempHeader[1] = "HR";
tempHeader[2] = "RR";
tempHeader[3] = "\n";
var tempFsBuffer = [];
tempFsBuffer[0] = "0";
tempFsBuffer[1] = "0";
tempFsBuffer[2] = "0";
tempFsBuffer[3] = "\n";
var headerName = tempHeader.toString();
var fileName = tempName.toString();
var fsContent = tempFsBuffer.toString();
socket.on('connect', function() {
console.log('Socket connected!');
// Start logging file
fs.appendFile(fileName, headerName, (err) => {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
});
socket.on('close', function() {
console.log('Connection closed');
connectionTry();
});
socket.on('disconnect', function() {
console.log('Connection disconnect');
});
socket.on('error', function() {
console.log('Connection error, reconnect');
});
var connectionTry = function() {
setTimeout(function() {
socket.connect(9000, '127.0.0.1');
}, 1000);
};
// Try connection
connectionTry();
var polarH7 = function(device) {
NobleDevice.call(this, device);
};
var idPolar = '0022d0b98acb';
polarH7.is = function(device) {
var localName = device.advertisement.localName;
return (device.id === idPolar);
};
NobleDevice.Util.inherits(polarH7, NobleDevice);
NobleDevice.Util.mixin(polarH7, NobleDevice.DeviceInformationService);
NobleDevice.Util.mixin(polarH7, NobleDevice.HeartRateMeasumentService);
// override HeartRateMeasumentService.prototype.convertMeasument
polarH7.prototype.convertMeasument = function(data, callback) {
var flags = data.readUInt8(0);
// Proto-object to store parsed data
var obj = { HR: -1, RR: -1 };
// If RR is enabled
if(flags & 0x10) {
obj.RR = data.readUInt16LE(2);
}
if (flags & 0x01) {
// uint16
obj.HR = data.readUInt16LE(1);
callback(obj);
} else {
// uint8
obj.HR = data.readUInt8(1);
callback(obj);
}
};
polarH7.discoverAll(function(device) {
console.log('discovered: ' + device);
device.on('disconnect', function() {
console.log('disconnected!');
console.log(polarH7.state);
//polarH7.startScanning();
//process.exit(0);
});
device.on('measumentChange', function(data) {
console.log("update measument: " + data.HR + " - " + data.RR);
var timeBuff = Date.now();
var buf = messages.Test.encode({
HR: data.HR,
RR: data.RR,
timestamp: timeBuff,
})
socket.write(buf);
// Log to file
tempHeader[0] = timeBuff;
tempHeader[1] = data.HR;
tempHeader[2] = data.RR;
tempFsBuffer[3] = "\n";
fsContent = tempHeader.toString();
// Start logging file
fs.appendFile(fileName, fsContent, (err) => {
if (err) throw err;
});
});
device.connectAndSetUp(function(callback) {
console.log('connectAndSetUp');
polarH7.stopScanning();
device.notifyMeasument(function(counter) {
console.log('notifyMeasument');
});
});
});