-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.js
146 lines (137 loc) · 3.46 KB
/
http.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
var http = require("http");
var easymogo = require('./easymogo');
var utils = require('./utils');
var FastPriorityQueue = require("fastpriorityqueue");
var generateDB = require("./calculate.js");
var url = require("url");
/*
request json:
{
"00 00 00 00 11 11 22 22 33 33 CC CC CC CC CC 16":-10,
"00 00 00 00 11 11 22 22 33 33 CC CC CC CC CC 11":-80,
"00 00 00 00 11 11 22 22 33 33 CC CC CC CC CC 12":-9,
"00 00 00 00 11 11 22 22 33 33 CC CC CC CC CC 15":-91,
"00 00 00 00 11 11 22 22 33 33 CC CC CC CC CC 23":-20,
"00 00 00 00 11 11 22 22 33 33 CC CC CC CC CC 22":-33
}
response json:
{
x:100,
y:100
}
*/
function getDistence(ob1, ob2) {
var MIN_RSSI = -127;
var dis = 0;
var array1 = new Array();
var array2 = new Array();
for (var i in ob1) {
array1.push(i);
}
for (var i in ob2) {
array2.push(i);
}
var keySet = utils.array_union(array1, array2);
var count = 0;
for (var i = 0; i < keySet.length; i++) {
count++;
if (ob1[keySet[i]] == undefined && ob2[keySet[i]] == undefined) {
count--;
continue;
} else if (ob1[keySet[i]] == undefined) {
dis += (ob2[keySet[i]] - MIN_RSSI) * (ob2[keySet[i]] - MIN_RSSI);
} else if (ob2[keySet[i]] == undefined) {
dis += (ob1[keySet[i]] - MIN_RSSI) * (ob1[keySet[i]] - MIN_RSSI);
} else {
dis += (ob1[keySet[i]] - ob2[keySet[i]]) * (ob1[keySet[i]] - ob2[keySet[i]]);
}
}
return dis / count; //no sqrt
}
function dealResult(rows, params) {
var request = params[0];
var response = params[1];
var p_que = new FastPriorityQueue(function(a, b) {
return a.dis < b.dis
});
var K = 1;
var cmp_obj1 = request;
//generate priority_queue
for (var i = 0; i < rows.length; i++) {
var cmp_obj2 = new Object();
var data = rows[i].data;
for (var j in data) {
cmp_obj2[j] = data[j].mean;
}
var dis = getDistence(cmp_obj1, cmp_obj2);
p_que.add({
"location": rows[i].location,
"dis": dis
});
}
//get the ans
var xx = 0,
yy = 0;
var count = 0;
while (!p_que.isEmpty() && (K--) > 0) {
var item = p_que.poll().location;
xx += item.x;
yy += item.y;
count++;
}
// while (!p_que.isEmpty()) {
// console.log(p_que.poll());
// }
if (count > 0) {
var output = {
x: xx / count,
y: yy / count
}
} else {
var output = {
x: 0,
y: 0
}
}
response.writeHead(200, {
"Content-Type": "application/json;charset=utf-8"
});
response.write(JSON.stringify(output));
response.end();
}
var server = http.createServer(function(req, response) {
req.setEncoding('utf-8');
var postData = "";
req.addListener("data", function(postDataChunk) {
postData += postDataChunk;
});
req.addListener("end", function() {
postData = JSON.parse(postData);
if (postData.task != undefined) { //所有指定task的请求,都认为是生成指纹库的请求
generateDB.start();
response.writeHead(200, {
"Content-Type": "application/json;charset=utf-8"
});
response.write('{"message":"ok"}');
response.end();
} else {
var pathname = url.parse(req.url).pathname;
console.log(pathname);
switch (pathname) {
case "/ibeacon":
easymogo.queryMogo("ibeaconDB", undefined, dealResult, [postData, response]);
break;
case "/wifi":
easymogo.queryMogo("wifiDB", undefined, dealResult, [postData, response]);
break;
default:
response.writeHead(200, {
"Content-Type": "application/json;charset=utf-8"
});
response.write('{"error":"path error"}');
response.end();
}
}
});
}).listen(8081);
console.log("server is listening");