-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarling.js
240 lines (210 loc) · 8.04 KB
/
starling.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
/**
* Copyright reelyActive 2022
* We believe in an open Internet of Things
*/
let starling = (function() {
// Internal constants
let DEFAULT_TRANSMITTERS = [
{ id: "fee150bada55", idType: 3, dynambProperties: [],
statid: { uri: "https://sniffypedia.org/Product/Any_BLE-Device/",
name: "Harald" } },
{ id: "d070b11d070b", idType: 3, dynambProperties: [],
statid: { deviceIds: [ "496f4944434f4445b73e5554462d3332/0001/f989" ] },
url: "https://www.reelyactive.com/team/barnowl/" },
{ id: "0be118ad0660", idType: 3, dynambProperties: [],
statid: { deviceIds: [ "496f49445554462d3332/00000001f415"] },
url: "https://www.reelyactive.com/team/obelix/" },
{ id: "c07105de81ce", idType: 3, dynambProperties: [],
statid: { uri: "https://sniffypedia.org/Product/Any_Curious-Device/" } },
{ id: "a991ede81ce1", idType: 3, dynambProperties: [],
statid: { uri: "https://sniffypedia.org/Organization/Apple_Inc/" } },
{ id: "a991ede81ce2", idType: 3, dynambProperties: [],
statid: { uri: "https://sniffypedia.org/Organization/Apple_Inc/AirPlay/" } },
{ id: "a991ede81ce3", idType: 3, dynambProperties: [],
statid: { deviceIds: [ "1beac04beac04beac04beac04beac045/1234/5678" ],
uri: "https://sniffypedia.org/Organization/Apple_Inc/iBeacon/" } },
{ id: "a991ede81ce4", idType: 3, dynambProperties: [],
statid: { uri: "https://sniffypedia.org/Product/Apple_AirPods/" } },
{ id: "ac233fa00001", idType: 2, dynambProperties: [ 'acceleration' ],
statid: { uri: "https://sniffypedia.org/Product/Minew_E8/" } },
{ id: "ac233fa00002", idType: 2,
dynambProperties: [ 'temperature', 'relativeHumidity' ],
statid: { uri: "https://sniffypedia.org/Organization/Shenzhen_Minew_Technologies_Co_Ltd/" } },
{ id: "ac233fa00003", idType: 2,
dynambProperties: [ 'isButtonPressed' ],
statid: { uri: "https://sniffypedia.org/Organization/Shenzhen_Minew_Technologies_Co_Ltd/" } },
{ id: "e50000000001", idType: 3,
dynambProperties: [ 'illuminance', 'isMotionDetected' ],
statid: { uri: "https://sniffypedia.org/Organization/EnOcean_GmbH/" },
tags: [ "room" ] },
{ id: "e50010000002", idType: 3 ,
dynambProperties: [ 'acceleration', 'illuminance', 'isContactDetected',
'isMotionDetected', 'temperature',
'relativeHumidity' ],
statid: { uri: "https://sniffypedia.org/Organization/EnOcean_GmbH/" },
tags: [ "chair" ] },
{ id: "0c4708eca570", idType: 3, dynambProperties: [],
statid: { uri: "https://sniffypedia.org/Product/Google_Chromecast/",
uuids: [ "fea0" ], name: "Ambient Display" } }
];
let DEFAULT_RECEIVERS = [
{ id: "001bc50940810000", idType: 1,
url: "https://www.reelyactive.com/parc/stories/entrance/",
directory: "parc:entrance", position: [ -73.57127, 45.50889 ] },
{ id: "0b1e6a7e8a40", idType: 2,
url: "https://www.reelyactive.com/parc/stories/reelyactive/",
directory: "parc:reelyactive", position: [ -73.57120, 45.50886 ] }
];
let DEFAULT_UPDATE_CYCLE_MILLISECONDS = 4000;
// Internal variables
let eventCallbacks = { raddec: [], dynamb: [], connect: [] };
let transmitters = DEFAULT_TRANSMITTERS;
let receivers = DEFAULT_RECEIVERS;
let transmitterIndex = 0;
let isEmulating = false;
let isConnect = false;
// Emulate a raddec
function createRaddec(transmitter, receivers) {
let raddec = {
transmitterId: transmitter.id,
transmitterIdType: transmitter.idType,
rssiSignature: [],
events: [],
timestamp: Date.now()
};
receivers.forEach((receiver) => {
raddec.rssiSignature.push({
receiverId: receiver.id,
receiverIdType: receiver.idType,
rssi: -90 + Math.round(Math.random() * 40)
});
});
raddec.rssiSignature.sort((a, b) => (b.rssi - a.rssi));
if(Math.random() > 0.5) {
raddec.events.push(1);
}
if(transmitter.dynambProperties.length > 0) {
raddec.events.push(2);
}
if(raddec.events.length === 0) {
raddec.events.push(3);
}
return raddec;
}
// Emulate a dynamb
function createDynamb(device) {
if(device.dynambProperties.length === 0) {
return null;
}
let dynamb = {
deviceId: device.id,
deviceIdType: device.idType,
timestamp: Date.now()
};
device.dynambProperties.forEach((property) => {
dynamb[property] = createDynambProperty(property);
});
return dynamb;
}
// Emulate a dynamb property
function createDynambProperty(property) {
let randomBoolean = (Math.random() > 0.5) ? true : false;
switch(property) {
case 'acceleration':
return [ (Math.random() * 2) - 1, (Math.random() * 2) - 1,
(Math.random() * 2) - 1 ];
case 'illuminance':
return Math.round(Math.random() * 1000);
case 'isButtonPressed':
return [ randomBoolean ];
case 'isContactDetected':
return [ randomBoolean ];
case 'isMotionDetected':
return [ randomBoolean ];
case 'temperature':
return Math.round(Math.random() * 20) + 15;
case 'relativeHumidity':
return Math.round(Math.random() * 100);
}
}
// Emulate a context of devices
function createDevices(route) {
let devices = {};
transmitters.forEach((transmitter) => {
let signature = transmitter.id + SIGNATURE_SEPARATOR + transmitter.idType;
let device = { nearest: [] };
let dynamb = createDynamb(transmitter);
receivers.forEach((receiver) => {
if((Math.random() * device.nearest.length) < 0.7) {
device.nearest.push({
device: receiver.id + SIGNATURE_SEPARATOR + receiver.idType,
rssi: -90 + Math.round(Math.random() * 40)
});
}
});
device.nearest.sort((a, b) => (b.rssi - a.rssi));
if(dynamb) { device.dynamb = dynamb; }
if(transmitter.statid) { device.statid = transmitter.statid; }
if(transmitter.tags) { device.tags = transmitter.tags; }
if(transmitter.url) { device.url = transmitter.url; }
devices[signature] = device;
});
receivers.forEach((receiver) => {
let signature = receiver.id + SIGNATURE_SEPARATOR + receiver.idType;
let device = {};
if(receiver.url) { device.url = receiver.url; }
if(receiver.directory) { device.directory = receiver.directory; }
if(receiver.position) { device.position = receiver.position; }
devices[signature] = device;
});
return devices;
}
// Iterate a single emulated event and set timeout for the next
function iterate() {
let raddec = createRaddec(transmitters[transmitterIndex], receivers);
let dynamb = createDynamb(transmitters[transmitterIndex]);
let interval = DEFAULT_UPDATE_CYCLE_MILLISECONDS / transmitters.length;
eventCallbacks.raddec.forEach((callback) => {
callback(raddec);
});
if(dynamb) {
eventCallbacks.dynamb.forEach((callback) => {
callback(dynamb);
});
}
if(isConnect) {
eventCallbacks.connect.forEach((callback) => callback());
isConnect = false;
}
transmitterIndex = (transmitterIndex + 1) % transmitters.length;
setTimeout(iterate, interval);
}
// Emulate a connection
let connect = function(url) {
isConnect = true;
return this;
}
// Get the (emulated) context for a specific route
let getContext = function(route) {
return { devices: createDevices(route) };
};
// Register a callback for the given event type
let setEventCallback = function(event, callback) {
if(!(callback && (typeof callback === 'function'))) {
return;
}
if(eventCallbacks.hasOwnProperty(event)) {
eventCallbacks[event].push(callback);
if(!isEmulating) {
iterate();
isEmulating = true;
}
}
};
// Expose the following functions and variables
return {
on: setEventCallback,
getContext: getContext,
connect: connect
}
}());