-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
157 lines (136 loc) · 5.21 KB
/
main.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
(function () {
var http = require("http"),
url = require("url"),
events = require("events"),
utils = require("./utils.js"),
engine = require("./engine.js"),
db = require("./db.js"),
app = {};
app.handleFetch = function (req, res) {
console.log("Handling request to /fetch");
var emitter = new events.EventEmitter(), togo = {}, data = "";
req.on("data", function (chunk) {
data += chunk;
});
req.on("end", function () {
if (!data) {
return app.errorHandler(res, "Request payload is empty");
}
data = JSON.parse(data);
togo.data = data;
togo.radioId = engine.getRadioId(data);
emitter.on("error", function (errorMessage) {
app.errorHandler(res, errorMessage);
});
emitter.on("radio", function (radios) {
if (!radios) {
return app.errorHandler(res, "Response is empty");
}
togo.radios = radios;
app.successHandler(res, engine.calculateDistances(togo));
});
emitter.on("tagToFingerprint", function (fingerprints) {
if (!fingerprints) {
return app.errorHandler(res, "Response is empty");
}
togo.fingerprints = fingerprints;
var radios = engine.buildKeys(engine.parseRadioIds(fingerprints));
if (!utils.find(radios.keys, function (key) {
if (key === togo.radioId) {
return key;
}
})) {
radios.keys.push(togo.radioId);
}
db.post({
path: "/tagin/_design/tagin/_view/radio?group=true",
data: radios
}, emitter, "radio");
});
emitter.on("fingerprintToTag", function (fingerprints) {
if (!fingerprints) {
return app.errorHandler(res, "Response is empty");
}
db.post({
path: "/tagin/_design/tagin/_view/tagToFingerprint",
data: engine.getKeys(fingerprints)
}, emitter, "tagToFingerprint");
});
emitter.on("macToFingerprint", function (fingerprints) {
if (!fingerprints) {
return app.errorHandler(res, "Response is empty");
}
if (fingerprints.rows.length < 1) {
return app.successHandler(res, {});
}
db.post({
path: "/tagin/_design/tagin/_view/fingerprintToTag?group=true",
data: engine.getKeys(fingerprints)
}, emitter, "fingerprintToTag");
});
db.post({
path: "/tagin/_design/tagin/_view/macToFingerprint?group=true",
data: engine.getMacKeys(data)
}, emitter, "macToFingerprint");
});
};
app.handleSave = function (req, res) {
console.log("Handling request to /save");
var emitter = new events.EventEmitter(), data = "";
emitter.on("saveWifi", function (response) {
if (!response) {
return app.errorHandler(res, "Response is empty");
}
app.successHandler(res, response);
});
emitter.on("error", function (errorMessage) {
app.errorHandler(res, errorMessage);
});
req.on("data", function (chunk) {
data += chunk;
});
req.on("end", function () {
if (!data) {
return app.errorHandler(res, "Request payload is empty");
}
db.post({
path: "/tagin/_bulk_docs",
data: {
docs: JSON.parse(data)
}
}, emitter, "saveWifi");
});
};
app.successHandler = function (res, response) {
console.log(JSON.stringify(response));
res.writeHead(200, {"Content-Type": "application/json"});
res.end(typeof response === "string" ? response : JSON.stringify(response));
};
app.errorHandler = function (res, error) {
console.log(error);
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify({
isError: true,
message: error
}));
};
app.applyHandler = function (handler, req, res) {
if (!handler) {
return app.errorHandler(res, "Invalid Path");
}
handler(req, res);
};
app.start = function (config) {
http.createServer(function (req, res) {
req.setEncoding('utf8');
var requestHandler = config[url.parse(req.url).pathname.slice(1)];
app.applyHandler(requestHandler, req, res);
}).listen(config.port, config.url);
};
app.start({
url: "0.0.0.0",
port: 8080,
fetch: app.handleFetch,
save: app.handleSave
});
})();