-
Notifications
You must be signed in to change notification settings - Fork 10
/
collisionStream.js
34 lines (31 loc) · 1011 Bytes
/
collisionStream.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
import characteristicsStream from './characteristicsStream';
import xsFromCallback from 'xstream-from-callback';
import {decodeToUint8Array} from './base64';
import delay from 'xstream/extra/delay';
export default characteristicsStream
.filter(({uuid}) => uuid === '22bb746f-2ba6-7554-2d6f-726568705327')
.compose(delay(100))
.map(characteristic =>
xsFromCallback(characteristic.monitor.bind(characteristic))(),
)
.flattenConcurrently()
.map(characteristic => characteristic.value)
.map(decodeToUint8Array)
.map(binary => {
if (binary.length < 5) {
return {};
}
const dataView = new DataView(binary.buffer);
const dataLength = dataView.getUint8(4);
const data = binary.slice(5, 5 + dataLength);
return {
async: dataView.getUint8(1) !== 0xff,
idCode: dataView.getUint8(2),
dataLength,
data,
};
})
.filter(
({async, dataLength, idCode}) => async && dataLength > 0 && idCode === 7,
)
.filter(({idCode}) => idCode === 7);