-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
287 lines (256 loc) · 9.97 KB
/
application.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
$(document).ready(function() {
var UPDATETIME = 10;
// var maxHeight = 0;
//ID to name objects
var roomNamesById = {};
var tagNamesById = {};
var colorsById = {};
var avtrImgByName = { "Peter" : 'css/img/avtr_peter.png',
"William" : 'css/img/avtr_william.jpg',
"Hugo" : 'css/img/avtr_hugo.jpg',
"Bryan" : 'css/img/avtr_bryan.jpg',
"Paul" : 'css/img/avtr_paul.jpg',
"Eric" : 'css/img/avtr_eric.jpg',
"Brandon" : 'css/img/avtr_brandon.jpg',
"Brionna" : 'css/img/avtr_f_wt_shirt.png',
"Gino" : 'css/img/avtr_m_bl_suit.png',
"Tom" : 'css/img/avtr_tom.jpg',
"Betsy" : 'css/img/avtr_betsy.jpg',
"Shopping Cart 01" : 'css/img/avtr_cart.png',
"Demo" : 'css/img/avtr_bcomTag.png',
"Oliver" : 'css/img/avtr_oliver.png'
};
//Firebase Refercences
var fbGateways = new Firebase("https://bletracker.firebaseio.com/gateways/");
var fbTags = new Firebase("https://bletracker.firebaseio.com/tags/");
var fbMoveTracker = new Firebase("https://bletracker.firebaseio.com/moveTracker/");
var fbRoomTracker = new Firebase("https://bletracker.firebaseio.com/roomTracker/");
//Firebase Connections
var fbMoveTrackerAll, fbMoveTrackerOne
//// Runtime ////
getTagNames();
getGatewayNames(getCurrentRoomData);
getHistoricalData();
updateTimeStamps();
//// Listeners ////
$(".tag-sorter").on("click", "a", submitTagSelector);
//// Loops ////
var tsLoop;
function updateTimeStamps() {
tsLoop = window.setInterval(function() {
for (var i = 0; i < $(".time-stamp").length; i++) {
$(".time-stamp")[i].innerHTML = "updated: " + moment(parseInt($(".time-stamp")[i].dataset.ts)).fromNow();
}
}, 30000);
}
function clearTimeStampsLoop() {
window.clearInterval(tsLoop);
tsLoop = undefined;
}
//// Firebase Functions ////
function getGatewayNames(callback) {
//retrieve stored gateway names
fbGateways.once("value", function(s) {
buildGWNames(s);
buildRoomDivs(callback);
});
//open listener for changes in gateway names
fbGateways.on("child_changed", function(s) {
buildGWNames(s, callback);
})
}
function getTagNames() {
fbTags.once("value", buildTagNames);
fbTags.on("child_changed", function(s) {
buildTagNames(s, getCurrentRoomData);
});
}
function getCurrentRoomData() {
var data = {};
//get initial data
fbRoomTracker.once("value", function(dataSnapshot) {
dataSnapshot.forEach(function(childSnapshot) {
buildRoomData(childSnapshot, data);
});
});
//listen for changes
fbRoomTracker.on("child_changed", function(childSnapshot, prevChildName) {
buildRoomData(childSnapshot, data);
});
}
function getHistoricalData() {
var data = {};
clearHistory();
fbMoveTracker.off('child_added', fbMoveTrackerOne);
fbMoveTrackerAll = fbMoveTracker.limitToLast(200).on("child_added", function(snapshot) {
buildHistoricalData(snapshot, data, 100);
});
}
function getHistoricalDataByTag(tagId) {
var data = {};
clearHistory();
fbMoveTracker.off('child_added', fbMoveTrackerAll);
fbMoveTrackerOne = fbMoveTracker.orderByChild("tag").equalTo(tagId).limitToLast(50).on("child_added", function(snapshot) {
buildHistoricalData(snapshot, data, 50);
});
}
function buildGWNames(snapshot, callback) {
if (callback) {
roomNamesById[snapshot.key()] = snapshot.val().location;
colorsById = {};
buildRoomDivs(callback);
getHistoricalData();
} else {
snapshot.forEach(function(cs) {
roomNamesById[cs.key()] = cs.val().location;
});
}
}
function buildTagNames(snapshot, callback) {
clearSortDropdown();
if (callback) {
if(snapshot.val().name) {
tagNamesById[snapshot.key()] = snapshot.val().name;
addTagToSortDropdown(snapshot.key());
callback();
getHistoricalData();
}
} else {
snapshot.forEach(function(cs) {
if(cs.val().name) {
tagNamesById[cs.key()] = cs.val().name;
addTagToSortDropdown(cs.key());
}
});
}
}
function buildRoomDivs(callback) {
assignColorsToRooms();
clearRoomDivs();
createRoomDivs();
if (callback) { callback() };
}
function buildRoomData(snapshot, data) {
data.roomID = snapshot.key();
data.roomName = roomNamesById[snapshot.key()];
snapshot.forEach(function(cs) {
if (cs.key() in tagNamesById) {
data.tagId = cs.key();
data.tagName = tagNamesById[cs.key()];
if (cs.val().rssi) {
data.rssi = cs.val().rssi;
} else {
data.rssi = "N/A";
}
data.ts = moment(cs.val().time * 1000);
data.rssiStatus = getRSSIStatus(data.rssi);
createTagWidget(data);
}
});
}
function buildHistoricalData(snapshot, data, limit) {
if(snapshot.val().tag in tagNamesById) {
data.preLoc = roomNamesById[snapshot.val().locBefore];
data.currLoc = roomNamesById[snapshot.val().locNow];
data.tag = tagNamesById[snapshot.val().tag];
data.ts = (new Date(snapshot.val().time * 1000)).toLocaleTimeString();
updateHistory(data, limit);
}
}
function getRSSIStatus(rssi) {
if (rssi === "N/A") { return "status-empty" };
if (rssi < -79) { return "status-one" };
if (rssi < -69) { return "status-two" };
if (rssi < -59) { return "status-three" };
if (rssi < -49) { return "status-four" };
if (rssi < -39) { return "status-full" };
}
//// View Functions ////
function submitTagSelector(e) {
e.preventDefault
var tagId = e.currentTarget.id;
if(tagId === "all") {
getHistoricalData();
} else {
getHistoricalDataByTag(tagId);
}
}
function updateHistory(data, limit) {
if (data.preLoc) {
$(".history tbody").prepend("<tr class = 'data'><td class = 'time'>"+data.ts+"</td><td class = 'name'>"+data.tag+"</td><td class = 'event'>Moved from "+data.preLoc+" into "+data.currLoc+"</td></tr>")
// } else {
// $(".history tbody").prepend("<tr class = 'data'><td class = 'time'>"+data.ts+"</td><td class = 'name'>"+data.tag+"</td><td class = 'event'>Entered "+data.currLoc+"</td></tr>")
}
if ($(".history .data").length > limit) {
$(".history .data").last().remove();
}
}
function clearHistory() {
$(".history tbody").html("");
}
function createRoomDivs() {
for (gwId in roomNamesById) {
var roomNameClass = ""
if (roomNamesById[gwId]) {
roomNameClass = roomNamesById[gwId].replace(/\s+/g, "").toLowerCase();
if ($("." + roomNameClass).length === 1) {
$("." + roomNameClass).addClass("rm-" + gwId);
}
if ($("." + roomNameClass).length === 0) {
for (var i = 0; i < $(".room h2").length; i++) {
if ( $(".room h2")[i].innerHTML === "" ) {
$(".room h2")[i].parentNode.classList.add(roomNameClass);
$(".room h2")[i].parentNode.classList.add("rm-" + gwId);
$("." + roomNameClass + " h2").text(roomNamesById[gwId]);
break;
}
}
}
}
}
}
function clearRoomDivs() {
// 2 row format
$(".rt-tracker").html('<div class="row top"><div class="col-md-12 room electricimpsuite"><h2>Electric Imp Suite</h2></div></div><div class="row bottom"><div class="col-md-12 room outofrange rm-OutOfRange"><h2>Out Of Range</h2></div></div>');
// side by side format
// $(".rt-tracker").html('<div class="row top"><div class="col-md-6 room electricImpSuite"><h2>Electric Imp Suite</h2></div><div class="col-md-6 room outofrange rm-OutOfRange"><h2>Out Of Range</h2></div></div>');
}
function createTagWidget(data) {
var avtrImg = 'css/img/avtr_blank.png'
if (data.tagName in avtrImgByName) { avtrImg = avtrImgByName[data.tagName]}
if ($("."+data.tagId).length > 0 ) {
if ($("."+data.tagId).parent().hasClass("rm-"+data.roomID)) {
$("."+data.tagId).html("<img class='avtr' src='"+avtrImg+"' height='50' width='40'><ul><li>"+data.tagName+"</li><li>RSSI: "+data.rssi+"</li><li class='time-stamp' data-ts='"+data.ts+"'>updated: "+data.ts.fromNow()+"</li></ul><div class='status "+data.rssiStatus+"'></div>")
} else {
$("."+data.tagId).remove();
$(".rm-"+data.roomID).append("<div class='"+data.tagId+" "+colorsById[data.roomID]+" tag'><img class='avtr' src='"+avtrImg+"' height='50' width='40'><ul><li>"+data.tagName+"</li><li>RSSI: "+data.rssi+"</li><li class='time-stamp' data-ts='"+data.ts+"'>updated: "+data.ts.fromNow()+"</li></ul><div class='status "+data.rssiStatus+"'></div></div>");
}
} else {
$(".rm-"+data.roomID).append("<div class='"+data.tagId+" "+colorsById[data.roomID]+" tag'><img class='avtr' src='"+avtrImg+"' height='50' width='40'><ul><li>"+data.tagName+"</li><li>RSSI: "+data.rssi+"</li><li class='time-stamp' data-ts='"+data.ts+"'>updated: "+data.ts.fromNow()+"</li></ul><div class='status "+data.rssiStatus+"'></div></div>");
}
}
function addTagToSortDropdown(tagId) {
$(".tag-sorter ul").append("<li><a href='#tracking-history' id='"+tagId+"'>"+tagNamesById[tagId]+"</a></li>");
}
function clearSortDropdown() {
$(".tag-sorter ul").html("<li><a href='#tracking-history' id='all'>All Tags</a></li>");
}
function assignColorsToRooms() {
var colors = ["blue", "yellow", "green", "purple", "orange"];
var roomColors = {"Out Of Range" : "red"}
for(var id in roomNamesById) {
var name = roomNamesById[id];
if(name === "Out Of Range") {
colorsById[id] = "red";
} else if (name) {
if (roomColors[name]) {
colorsById[id] = roomColors[name];
} else {
var color = colors.shift();
colorsById[id] = color;
roomColors[name] = color;
}
}
}
}
});