-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-utils.js
86 lines (78 loc) · 2.32 KB
/
app-utils.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
const EventEmitter = require('events');
class Emitter extends EventEmitter {}
const events = new Emitter();
function getRandomBytes(numberOfBytes = 1) {
/**
* can be done with crypto lib
*
var crypto = require("crypto");
var id = crypto.randomBytes(20).toString('hex'); // "bb5dc8842ca31d4603d6aa11448d1654"
*/
let numberOfBytesx = numberOfBytes * 2;
var byteString = '0x';
var possible = 'ABCDEF0123456789';
for (var i = 0; i < numberOfBytesx; i++) {
byteString += possible.charAt(Math.floor(Math.random() * possible.length));
}
return byteString;
}
function getRandomRawValue(JSONValues) {
if (JSONValues) {
return JSONValues[Math.floor(Math.random() * JSONValues.length)] || null;
} else {
return null;
}
}
function getJSONFile() {
const JSONdataFileName = process.argv[2];
if (!JSONdataFileName) {
console.log(`
Missing argument!
example: 'npm start my-json-file.json'
`);
process.exit(-1);
} else {
return require('./' + JSONdataFileName);
}
}
function getRandomInterval(min, max, charName) {
if (min < max) {
return Math.floor(Math.random() * max) + min;
} else {
return null;
}
}
function setControlledinterval(interval, characteristic) {
let int = setInterval(() => {
let rawValue = getRandomRawValue(characteristic.notifyValues);
events.emit(characteristic.characteristicName, {
rawValue: Buffer.from(rawValue),
characteristicName: characteristic.characteristicName,
});
let interval = getRandomInterval(
characteristic.notifyIntervalMin,
characteristic.notifyIntervalMax
);
clearInterval(int);
setControlledinterval(interval * 1000, characteristic);
}, interval);
}
function getNotifyValuesForChar(charUuid, JSONData) {
// console.log('getNotifyValueForChar', charUuid, typeof JSONData)
for (let service of JSONData.services) {
for (let characteristic of service.characteristics) {
if (characteristic.uuid.split('-').join('') === charUuid) {
return characteristic.notifyValues || null;
}
}
return null;
}
}
module.exports = {
getRandomBytes: getRandomBytes,
getRandomRawValue: getRandomRawValue,
// getRandomInterval: getRandomInterval,
// setControlledinterval: setControlledinterval,
getJSONFile: getJSONFile,
getNotifyValuesForChar: getNotifyValuesForChar,
};