-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
server.js
executable file
·139 lines (126 loc) · 3.17 KB
/
server.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
#!/usr/bin/env node
const wifi = require("node-wifi");
const fs = require("fs");
var dir = "./whereamijs-data";
const predict = require("./predict.js");
const loading = require("loading-cli");
const util = require("util");
const readdir = util.promisify(fs.readdir);
const command = process.argv[2];
const room = process.argv[3];
let sample = [];
let load;
if (!command) {
console.log(`You forgot to pass a command argument.
Options:
learn, -l <room>, get and save wifi data for a specific room
predict, -p, predict current location
rooms, -r, list the rooms in the training data
`);
process.exit(9);
}
if (!room && command === "learn") {
console.error(
`You forgot to specify a room. For example: whereamijs learn kitchen`
);
process.exit(9);
}
wifi.init({
iface: null, // network interface, choose a random wifi interface if set to null
});
const getNetworks = () => {
if (!load) {
load = loading({
text: "✨ Getting Wifi data 📶 This can take up to 15s! ✨",
color: "yellow",
frames: ["◰", "◳", "◲", "◱"],
}).start();
}
return wifi.scan((error, networks) => {
if (error) {
throw new Error(error);
}
const networkObject = {};
networks.forEach((network) => {
const key = `${network.ssid} ${network.bssid}`;
const value = network.quality;
networkObject[key] = value;
return networkObject;
});
sample.push(networkObject);
if (sample.length < 5) {
getNetworks();
} else {
load.stop();
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
fs.writeFile(
`${dir}/${room}.json`,
JSON.stringify(sample),
function (err, data) {
if (err) {
return console.log(err);
}
}
);
}
return sample;
});
};
const predictLocation = () => {
if (!load) {
load = loading({
text: "✨ Predicting current location... ✨",
color: "yellow",
frames: ["◰", "◳", "◲", "◱"],
}).start();
}
return wifi.scan((error, networks) => {
if (error) {
throw new Error(error);
}
const networkObject = {};
networks.forEach((network) => {
const key = `${network.ssid} ${network.bssid}`;
const value = network.quality;
networkObject[key] = value;
return networkObject;
});
sample.push(networkObject);
load.stop();
return predict(sample);
});
};
const listRooms = async () => {
try {
let rooms = await readdir(`./whereamijs-data`);
rooms.map((room) => console.log(room.split(".")[0]));
return;
} catch (err) {
console.log(err);
}
};
if (command === "learn" || command === "-l") {
getNetworks();
}
if (command === "predict" || command === "-p") {
if (!fs.existsSync(dir)) {
console.error(
`You don't have any training data recorded. Start with the learn command!
For example: whereamijs learn bathroom
`
);
process.exit(9);
} else {
predictLocation();
}
}
if (command === "rooms" || command === "-r") {
if (!fs.existsSync(dir)) {
console.error(`You don't have any training data recorded.`);
process.exit(9);
} else {
listRooms();
}
}