-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.js
173 lines (145 loc) · 6.01 KB
/
runner.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
(function() {
"use strict";
String.prototype.format = function() {
let a = this;
for (let k in arguments) {
a = a.replace("{" + k + "}", arguments[k])
}
return a
}
document.addEventListener("DOMContentLoaded", event => {
let connectButton = document.querySelector("#connect");
let statusDisplay = document.querySelector("#status");
let bpaPatientId = document.querySelector("#bpa_patient_id");
let bpaDeviceDate = document.querySelector("#bpa_device_date");
let bpaPatientData = document.querySelector("#bpa_patient_data");
let port;
let unprocessedBytes = false;
// helper method... I should hide this? i dunno know.
// readLoop - concats all DataView from BPM
// responseData - Uint8Array
// return DataView
let readLoop = (responseData, expectedByteSize) => {
// set response if undefined
responseData = responseData || new Uint8Array();
return port.receive().then((result) => {
let newResponseData = new Uint8Array(result.data.buffer);
let newByteLength = responseData.byteLength + newResponseData.byteLength;
let mergedResponseData = new Uint8Array(newByteLength);
mergedResponseData.set(responseData);
mergedResponseData.set(newResponseData, responseData.byteLength);
responseData = mergedResponseData;
let decodedByteLength = bpaProtocol.countByteLength(new DataView(responseData.buffer));
// console.log("DecodedByteLength: " + decodedByteLength);
if(decodedByteLength < expectedByteSize) {
return readLoop(responseData, expectedByteSize);
} else {
// console.log("Raw: " + port.byteArrayToString(new DataView(responseData.buffer)));
return Promise.resolve(new DataView(responseData.buffer));
}
});
};
let readDataLoop = (responseData) => {
// set response if undefined
responseData = responseData || new Uint8Array();
return port.receive().then((result) => {
let newResponseData = new Uint8Array(result.data.buffer);
let newByteLength = responseData.byteLength + newResponseData.byteLength;
let mergedResponseData = new Uint8Array(newByteLength);
mergedResponseData.set(responseData);
mergedResponseData.set(newResponseData, responseData.byteLength);
responseData = mergedResponseData;
let decodedByteLength = bpaProtocol.countByteLength(new DataView(responseData.buffer));
if(decodedByteLength >= 5) {
// Decode current data set and get 5th byte
let decodedData = bpaProtocol.decodeData(new DataView(responseData.buffer));
let expectedByteLength = bpaProtocol.patientDataResponseSize(decodedData);
// console.log("Expected Byte Length: " + expectedByteLength);
return readLoop(responseData, expectedByteLength);
} else {
return readDataLoop(responseData);
}
});
};
function connect() {
port.connect().then(() => {
statusDisplay.textContent = "";
connectButton.textContent = "Disconnect";
port.onReceiveError = error => {
console.error(error);
};
// Begin requesting data from the BPM.
getDeviceData();
}, error => {
statusDisplay.textContent = error;
});
}
function formatPatientData(measurement) {
let formattedPatientData = "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>";
let dateTime = measurement["datetime"];
let sys = measurement["sys"];
let dia = measurement["dia"];
let pul = measurement["pul"];
return formattedPatientData.format(dateTime, sys, dia, pul);
}
function getDeviceData() {
console.log("Init bpaProtocol");
port.send(bpaProtocol.deviceDateTimeRequest()).then(() => {
return readLoop(null, bpaProtocol.deviceDataTimeResponseSize());
}).then((responseData) => {
// console.log(responseData.byteLength);
let decodedDateTimeResponse = bpaProtocol.decodeData(responseData);
let dateTime = bpaProtocol.decodeDeviceDateTime(decodedDateTimeResponse);
bpaDeviceDate.textContent = dateTime;
}).then(() => {
port.send(bpaProtocol.patientIdRequest());
}).then(() => {
return readLoop(null, bpaProtocol.patientIdResponseSize());
}).then((responseData) => {
// console.log("Patient Id: " + port.byteArrayToString(responseData));
let decodedPatientIdResponse = bpaProtocol.decodeData(responseData);
let patientId = bpaProtocol.decodePatientId(decodedPatientIdResponse);
// console.log(patientId);
bpaPatientId.textContent = patientId;
}).then(() => {
port.send(bpaProtocol.patientDataRequest());
}).then(() => {
return readDataLoop();
}).then((responseData) => {
// console.log("Measurements: " + port.byteArrayToString(responseData));
let decodedPatientData = bpaProtocol.decodeData(responseData);
let measurementData = bpaProtocol.decodePatientData(decodedPatientData);
for(let index = 0; index < measurementData.length; index++) {
bpaPatientData.innerHTML += formatPatientData(measurementData[index]);
}
}).then(() => {
// console.log("Finished gathering data");
});
}
// Event handlers
connectButton.addEventListener("click", function() {
if (port) {
port.disconnect();
connectButton.textContent = "Connect";
statusDisplay.textContent = "";
port = null;
} else {
serial.requestPort().then(selectedPort => {
port = selectedPort;
connect();
}).catch(error => {
statusDisplay.textContent = error;
});
}
});
serial.getPorts().then(ports => {
if (ports.length == 0) {
statusDisplay.textContent = "No device found.";
} else {
statusDisplay.textContent = "Connecting...";
port = ports[0];
connect();
}
});
});
})();