-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.js
137 lines (122 loc) · 4.03 KB
/
websocket.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
var WebSocketServer = require("ws").Server;
var server = new WebSocketServer({ port: 4201 });
var serializer = require("./serializer.js");
var utils = require("./utils.js");
var storage, particle;
setTimeout(function() {
// delay loading some scripts which could have cyclic dependencies
storage = require("./storage.js");
particle = require("./particle-calls.js");
}, 50);
var socketMap = []; // primary key by deviceName, contains an array of sockets
server.on("connection", function (socket) {
var deviceName;
socket.on("message", function(message) {
var obj = JSON.parse(message);
if ("deviceName" in obj) {
if (deviceName != null) removeFromSocketMap(deviceName, socket);
deviceName = handleDeviceName(obj, socket);
} else { // this must be a message from a computer vision system
// we don't need to store the socket as we won't reply
handleLocationData(obj);
}
});
socket.on('close', function(code, message) {
removeFromSocketMap(deviceName, socket);
});
});
function removeFromSocketMap(deviceName, socket) {
if (socketMap[deviceName] == null) return;
var index = socketMap[deviceName].indexOf(socket);
if (index >= 0) {
socketMap[deviceName].splice(index, 1);
}
}
function handleDeviceName(data, socket) {
var deviceName = data.deviceName;
if (socketMap[deviceName] == null) {
socketMap[deviceName] = [];
}
socketMap[deviceName][socketMap[deviceName].length] = socket;
updateClient(deviceName, socket);
return deviceName;
}
function handleLocationData(data) {
if (storage == null) return;
var input = {body : data};
utils.parseLocationData(input);
if ("hazards" in data) {
storage.storeHazardData(data.hazards);
}
}
function updateClient(deviceName, socket) {
for (var property in module.exports.WebSocketUpdateEnum) {
var enumeration = module.exports.WebSocketUpdateEnum[property];
// don't find the latest event, client will pull down all events
if (enumeration != module.exports.WebSocketUpdateEnum.EVENT) {
module.exports.needsUpdated(deviceName, enumeration, socket);
}
}
}
function getDataFromStorage(deviceName, key) {
if (storage == null) return null;
switch (key) {
case module.exports.WebSocketUpdateEnum.DISTANCE:
return storage.getLatestDistance(deviceName);
case module.exports.WebSocketUpdateEnum.GYRO_READING:
return storage.getLatestGyroReading(deviceName);
case module.exports.WebSocketUpdateEnum.LOCATION:
return storage.getLatestLocation(deviceName);
case module.exports.WebSocketUpdateEnum.ROTATION:
return storage.getLatestRotation(deviceName);
case module.exports.WebSocketUpdateEnum.EVENT:
return storage.getLatestEvent(deviceName);
case module.exports.WebSocketUpdateEnum.HAZARD:
return storage.getHazardData();
}
return null;
}
function sendOnSockets(deviceName, data) {
if (socketMap[deviceName] == null) return;
for (var i = 0; i < socketMap[deviceName].length; i++) {
if (socketMap[deviceName][i].readyState == 1) {
socketMap[deviceName][i].send(data);
}
}
}
module.exports = {
WebSocketUpdateEnum: {
DISTANCE : "distance",
GYRO_READING : "gyroReading",
EVENT : "event",
LOCATION : "location",
ROTATION : "rotation",
HAZARD : "hazards"
},
needsUpdated: function(deviceName, key, socket) {
if (deviceName != "all" && socketMap[deviceName] == null) {
socketMap[deviceName] = [];
return;
}
var value = getDataFromStorage(deviceName, key);
if (value == null) {
return;
}
serializer.startJson();
serializer.addJsonBlock(serializer.genKeyPairs(null, value), key);
var string = serializer.endJson();
if (string == JSON.stringify({})) return;
if (socket == null) {
if (deviceName == "all") {
for (var nameKey in particle.deviceArray) {
var name = particle.deviceArray[nameKey].name;
sendOnSockets(name, string);
}
} else {
sendOnSockets(deviceName, string);
}
} else {
socket.send(string);
}
}
};