-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalle.js
261 lines (241 loc) · 6.61 KB
/
walle.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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");
var net = require('net');
var smartCarSocket = undefined; //小车socket
var socketPort = 8080; //小车socket监听端口
var httpPort = 8081; //http port
var INF = 100000000;
//-------------------------------some callback function-------
function initIbeaconBuf(ibeaconDataBuf, json) {
ibeaconDataBuf = new Object();
ibeaconDataBuf.location = new Object();
ibeaconDataBuf.location.x = json.x;
ibeaconDataBuf.location.y = json.y;
ibeaconDataBuf.timeStamp = new Date();
ibeaconDataBuf.data = new Object();
ibeaconDataBuf.data[json.uuid] = json.rssi;
return ibeaconDataBuf;
}
function insertWifi(json) {
var wifiData = new Object();
wifiData.location = new Object();
wifiData.location.x = json.x;
wifiData.location.y = json.y;
wifiData.timeStamp = new Date();
wifiData.data = new Object();
var dataArray = json.data.split("+");
var itemArray;
for (var i in dataArray) {
itemArray = String(dataArray[i]).split(",");
if (itemArray.length != 7)
continue;
wifiData.data[itemArray[3]] = itemArray[2];
}
easymogo.insertMogo("wifiData", wifiData);
}
function getDistance(ob1, ob2) {
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);
// console.log(keySet);
var count = 0;
for (var i = 0; i < keySet.length; i++) {
if (keySet[i].length == 0)
continue;
if (!isNaN(Number(ob1[keySet[i]])) && !isNaN(Number(ob2[keySet[i]]))) {
dis += (ob1[keySet[i]] - ob2[keySet[i]]) * (ob1[keySet[i]] - ob2[keySet[i]]);
count++;
}
// console.log(dis);
}
// console.log(dis + "--" + count);
if (count == 0)
return INF;
else return dis / count;
}
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 = getDistance(cmp_obj1, cmp_obj2);
// console.log(dis);
if (dis == INF) {
continue; //无穷远
}
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: -1,
y: -1
}
}
response.writeHead(200, {
"Content-Type": "application/json;charset=utf-8"
});
response.write(JSON.stringify(output));
response.end();
}
//--------------------------------------------end function
var socketServer = net.createServer(function(socket) {
console.log('connect: ' + socket.remoteAddress + ':' + socket.remotePort + ' client has connected');
smartCarSocket = socket;
var ibeaconDataBuf = new Object();
//接收消息事件
socket.on('data', function(data) {
data = String(data);
var jsonArray = data.split("\r\n");
console.log(jsonArray);
for (var i = 0; i < jsonArray.length; i++) {
if (jsonArray[i].length == 0)
continue;
var json = [];
try{
json = JSON.parse(jsonArray[i]);
} catch (err){
console.log(err);
return;
}
switch (json.type) {
case "ibeacon":
//首次接收到蓝牙信息,初始化ibeaconBuf中的值(只会在程序一开始的时候运行一次)
if (!ibeaconDataBuf.location) {
ibeaconDataBuf = initIbeaconBuf(ibeaconDataBuf, json);
}
//如果检测到此次rssi的信号与buf中的值重叠,则把上个buf插入到数据库,并初始化buf
else if (ibeaconDataBuf.data[json.uuid]) {
easymogo.insertMogo("ibeaconData", ibeaconDataBuf);
ibeaconDataBuf = initIbeaconBuf(ibeaconDataBuf, json);
} else {
ibeaconDataBuf.data[json.uuid] = json.rssi;
}
break;
case "wifi":
insertWifi(json);
break;
default:
console.log("json 不合法");
return;
}
}
});
//数据错误事件
socket.on('error', function(exception) {
console.log('socket error:' + exception);
socket.end();
});
//客户端关闭事件
socket.on('close', function(data) {
console.log('close: ' + socket.remoteAddress + ' ' + socket.remotePort + ' client has disconnected');
smartCarSocket = undefined;
});
}).listen(socketPort);
var httpServer = http.createServer(function(req, response) {
req.setEncoding('utf-8');
var postData = "";
req.addListener("data", function(postDataChunk) {
postData += postDataChunk;
});
req.addListener("end", function() {
try{
postData = JSON.parse(postData);
} catch (err){
console.log(err);
return;
}
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":
console.log(postData);
easymogo.queryMogo("ibeaconDB", undefined, dealResult, [postData, response]);
break;
case "/wifi":
easymogo.queryMogo("wifiDB", undefined, dealResult, [postData, response]);
break;
case "/ctr":
if (postData.message != undefined) { //完全转发给小车
if (smartCarSocket != undefined) {
smartCarSocket.write(postData.message);
response.writeHead(200, {
"Content-Type": "application/json;charset=utf-8"
});
response.write('{"message":"ok"}');
response.end();
} else {
response.writeHead(200, {
"Content-Type": "application/json;charset=utf-8"
});
response.write('{"error":"smart car not online"}');
response.end();
}
} else {
response.writeHead(200, {
"Content-Type": "application/json;charset=utf-8"
});
response.write('{"error":"message can\'t be null"}');
response.end();
}
break;
default:
response.writeHead(200, {
"Content-Type": "application/json;charset=utf-8"
});
response.write('{"error":"path error"}');
response.end();
}
}
});
}).listen(httpPort);
console.log("server has started");