-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
189 lines (154 loc) · 5.23 KB
/
server.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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var moment = require('moment');
require('console.table');
app.use('/', express.static('public'));
app.use('/remote', express.static('remote'));
http.listen(3434, function() {
console.log('listening on *:3434');
});
var clients = {
screen: 0,
remote: 0
};
function isRemote(str) {
return /\/remote/.test(str);
}
io.on('connection', function(socket) {
console.log('connection', socket.handshake.headers.referer);
if (isRemote) {
clients.remote++;
var kioskDirective = {
directive : 'updateKiosks',
kiosks : randomData.kiosks
};
//socket.broadcast.emit('remoteEvent', kioskDirective);
io.emit('remoteEvent', kioskDirective);
}
else {
clients.screen++;
}
console.log('Connected clients:', clients);
socket.on('remoteEvent', function(data) {
console.log('REMOTE EVENT', data.directive);
socket.broadcast.emit('remoteEvent', data);
if (data.directive === 'toggleRandomData') {
//toggle it
randomData.active = !randomData.active;
console.log(randomData.active);
if (randomData.active === true) {
randomEventLoop();
}
}
if (data.directive === 'updateKiosks') {
randomData.kiosks = data.kiosks;
console.log(randomData.kiosks);
}
});
socket.on('disconnect', function(socket) {
if (isRemote) {
clients.remote--;
}
else {
clients.screen--;
}
console.log('Connected clients:', clients);
});
});
//randomEventLoop();
function randomEvent() {
var eventType = Math.floor((Math.random() * 3) + 1);
var randomId = Math.floor((Math.random() * 200));
var event;
//puffTaken
if (eventType <= 2) {
event = randomData.eventTemplates.puffTaken;
event.eventId = 'P-100' + randomId;
var medicationType = Math.floor((Math.random() * 2) + 1);
if (medicationType === 1) {
event.eventDetails.medicationType = "Control";
}
if (medicationType === 2) {
event.eventDetails.medicationType = "Rescue";
}
}
//lungFunctionTest
if (eventType === 3) {
event = randomData.eventTemplates.lungFunctionTest;
var randomPef = Math.floor((Math.random() * 200) + 400) / 60;
event.eventDetails.pef = randomPef;
event.eventId = 'LF-100' + randomId;
}
var now = moment().format('HH:mm:ss');
event.localTimestamp = now;
event.eventDetails.eventTimeLocal = now;
event.eventDetails.syncToDeviceTimeLocal = now;
var randomKiosk = Math.floor(Math.random() * randomData.kiosks.length);
var kioskName = randomData.kiosks[randomKiosk];
event.title = kioskName;
console.log('\nEvent Emitted\n');
console.table(event);
io.emit('kioskEvent', event);
}
function randomEventLoop() {
var rand = Math.round(Math.random() * 4000) + 2000;
if (randomData.active === true) {
setTimeout(function() {
randomEvent();
randomEventLoop();
}, rand);
}
}
var randomData = {
eventTemplates: {},
kiosks: ['Glenlivet', 'Dewars', 'Jameson', 'Laphroaig'],
active: false
};
randomData.eventTemplates.puffTaken = {
"eventType": "puffTaken",
"eventId": "P-12345678",
/*Dmitry this will be "P" for Puff, and the Id from the Puff table for puffs, for PFTs it will "LF" for lung function, and the Id from the UserMeasurements table like "LF-678363" */
"userId": "18043",
"title": "Jameson",
/*Will be first name of "account"*/
"localTimestamp": "20:11:30",
"utcTimestamp": "16:11:30",
"eventDetails": { /*This object will be different for puffs vs. lung function tests*/
"medication": "Atrovent HFA",
"medicationType": "Control",
/*This would be either "Control", "Rescue - Emergency", "Rescue - Sport"*/
"eventTimeLocal": "20:11:30",
"eventTimeUTC": "16:11:30",
"syncToDeviceTimeLocal": "20:11:31",
"syncToDeviceTimeUTC": "16:11:31",
"syncToServerTimeLocal": "20:11:33",
"syncToServerTimeUTC": "16:11:33",
"inhalerBattery": 98,
/*0-100*/
"remainingPuffs": 67
}
};
randomData.eventTemplates.lungFunctionTest = {
"eventType": "lungFunctionTest",
"eventId": "LF-456797",
/*Dmitry this will be "P" for Puff, and the Id from the Puff table for puffs, for PFTs it will "LF" for lung function, and the Id from the UserMeasurements table like "LF-678363" */
"userId": "18041",
"title": "Glenlivet",
/*Will be first name of "account"*/
"localTimestamp": "20:12:30",
"utcTimestamp": "16:12:30",
"eventDetails": { /*This object will be different for puffs vs. lung function tests*/
"pef": 8.85,
"fev1": 4.32,
"fvc": 326.98,
"eventTimeLocal": "20:12:30",
"evenTimeUTC": "16:12:30",
"syncToDeviceTimeLocal": "20:12:31",
"syncToDeviceTimeUTC": "16:12:31",
"syncToServerTimeLocal": "20:12:33",
"syncToServerTimeUTC": "16:12:33",
"battery": 77 /*0-100*/
}
};