-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (63 loc) · 2.24 KB
/
index.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
const noble = require("@stoprocent/noble");
const osc = require("osc");
let deviceRSSIHistory = {};
// Set up an OSC UDP port listening on port 9000
const udpPort = new osc.UDPPort({
localAddress: "0.0.0.0",
localPort: 9000,
});
udpPort.on("message", (oscMessage) => {
console.log("Received OSC message:", oscMessage);
});
udpPort.open();
function calculateSlope(rssiValues) {
if (rssiValues.length < 2) return 0;
const [rssi1, rssi2, rssi3] = rssiValues.slice(-3);
return (rssi3 - rssi1) / 2; // Simple slope calculation
}
function printDeviceInfo() {
const sortedDevices = Object.entries(deviceRSSIHistory).sort(
([_, a], [__, b]) =>
b.rssiValues[b.rssiValues.length - 1] -
a.rssiValues[a.rssiValues.length - 1]
);
console.clear(); // Clear the console for a fresh display
sortedDevices.forEach(([localName, { rssiValues }]) => {
const currentRSSI = rssiValues[rssiValues.length - 1];
const slope = calculateSlope(rssiValues);
const stars = "*".repeat(
Math.max(0, Math.floor((currentRSSI + 100) / 2.5))
); // Convert RSSI to stars
console.log(`Device: ${localName}`);
console.log(`Current RSSI: ${currentRSSI} ${stars}`);
console.log(`Last 3 RSSI: ${rssiValues.slice(-3).join(", ")}`);
console.log(`Slope: ${slope > 0 ? "Positive" : "Negative"} (${slope})`);
});
}
noble.on("stateChange", async (state) => {
if (state === "poweredOn") {
setInterval(async () => {
await noble.startScanningAsync([], false);
console.log("Scanning for devices...");
setTimeout(async () => {
await noble.stopScanningAsync();
}, 5000); // Scan for 5 seconds
}, 10000); // Repeat every 10 seconds
setInterval(printDeviceInfo, 1000); // Print device info every second
} else {
await noble.stopScanningAsync();
}
});
noble.on("discover", (peripheral) => {
const localName = peripheral.advertisement.localName;
if (localName && localName.startsWith("Crown-")) {
if (!deviceRSSIHistory[localName]) {
deviceRSSIHistory[localName] = { rssiValues: [] };
}
const rssiValues = deviceRSSIHistory[localName].rssiValues;
rssiValues.push(peripheral.rssi);
if (rssiValues.length > 3) {
rssiValues.shift(); // Keep only the last 3 RSSI values
}
}
});