-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
395 lines (301 loc) · 10.9 KB
/
index.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
const EventEmmiter = require("events")
const ws = require("ws");
const SimpleDDP = require("simpleddp");
const crypto = require("crypto");
const log4js = require("log4js");
const logger = log4js.getLogger();
logger.level = "debug";
const simpleDDPLogin = require("simpleddp-plugin-login").simpleDDPLogin;
/**
* Establishes a connection with IoT Catalogue, allows a real time subscription with IoT Catalogue and to the dataset authenticated for the user
* @extends EventEmmiter
*/
class Connection extends EventEmmiter{
/**
* Props related with data subscription
* @typedef {Object} connectionProps
* @property {object} dataFields - Fields that must be returned from the data subscription
*/
/**
*
* @param {String} socketAddress Web socket address to IoT Catalogue instance
* @param {String} token Token used to authenticate the user
* @param {Object} serviceDescription Object describing the features of the external service
* @param {connectionProps} connectionProps Props related with data subscription
*/
constructor(socketAddress, token, serviceDescription, connectionProps) {
super();
this.socketAddress = socketAddress
this.socketAddressURL = new URL(this.socketAddress)
this.token = token
this.serviceDescription = serviceDescription
this.connectionProps = connectionProps
this.collectionHandlers = []
this.collectionSubscription = []
this.pingInterval = 60
this.timeout = 60
this.waitForServerInterval = 10
if(! this.socketAddress){
throw {"err":"Missing socketAddress"}
}
if(!this.token){
throw {err:"Missing token"}
}
const opts = {
endpoint: this.socketAddress + "/websocket",
SocketConstructor: ws,
reconnectInterval: 5000
};
this.ddpConnection = new SimpleDDP(opts,[simpleDDPLogin])
this.ddpConnection.on("connected",async (info)=>{
logger.info("Connected to " + this.socketAddress);
try{
await this.waitForServer()
const user = await this.ddpConnection.login({userToken:this.hashUserToken()})
this.emit("loggedIn",user);
await this.registerExternalServiceConnection()
await this.subscribeToData()
this.schedulePing()
}catch(error){
logger.error(error)
this.ddpConnection.disconnect();
}
})
this.ddpConnection.on('error', (error) => {
logger.error(error)
});
this.ddpConnection.on('disconnected', () => {
clearInterval(this.setIntervalId)
for(const collectionHandler of this.collectionHandlers){
collectionHandler.stop()
}
this.collectionHandlers = []
if(this.actionSub){
this.actionSub.remove()
this.actionSub = null;
}
if(this.activeConnectionSub ){
this.activeConnectionSub.remove();
this.activeConnectionSub = null;
}
for(const collectionSubscription of this.collectionSubscription){
collectionSubscription.remove()
}
this.collectionSubscription = []
logger.info("Disconnected from " + this.socketAddress)
});
}
async waitForServer(){
const {isServerReady} = await this.ddpConnection.call("getServerState")
if(!isServerReady){
logger.info("Server not ready, waiting " + this.waitForServerInterval + "s")
await new Promise(resolve=>setTimeout(resolve,this.waitForServerInterval*1000))
return this.waitForServer()
}else{
logger.info("Server ready")
}
}
tryToReconnect(){
logger.info("Reconnecting")
this.ddpConnection.disconnect();
this.ddpConnection.connect()
}
async schedulePing(){
this.setIntervalId = setInterval(async ()=>{
try{
const error = new Promise((resolve, reject) => {
setTimeout(reject, this.timeout*1000, {reason:'ping timeout', timeout:true});
});
const res = await Promise.race([this.ddpConnection.call("externalServicePing"), error])
if(!res?.connectionEstablished){
logger.error("Ping failed: connection not established with IoT Catalogue")
this.tryToReconnect()
}
}catch(err){
logger.error("Ping failed",err)
this.tryToReconnect()
}
},this.pingInterval*1000)
}
observeCollection(collectionName, onChange){
const collection = this.ddpConnection.collection(collectionName)
const collectionHandler = collection.onChange((obj)=>{
if(typeof onChange == "function"){
onChange( collectionName, obj)
}
})
this.collectionHandlers.push(collectionHandler)
}
observeActions(){
const onChange = async (collectionName, obj)=>{
const actions = []
if (obj.added) {
actions.push("added")
if(obj.added.state === "added"){
this.emit("actionAdded",
obj.added,
(result, error)=>{
return this.actionCallback(obj.added, result, error)
}
)
}
}
if(obj.changed) {
actions.push("changed")
}
if(obj.removed) {
actions.push("removed")
}
}
this.observeCollection("externalServiceCommunication",onChange)
}
async subscribeToData(){
const collectionNames = await this.ddpConnection.call("getUserDataCollectionNames")
for(const collectionName of collectionNames){
this.observeCollection(collectionName,(collectionName, obj)=>{
this.emit("dataChange",collectionName, {
changed:this.fixIdFromObject(obj.changed),
added:this.fixIdFromObject(obj.added),
removed:this.fixIdFromObject(obj.removed)
})
})
await this.subscribeToCollection(collectionName)
}
}
async subscribeToCollection(collectionName){
const fields = this.connectionProps?.dataFields || this.connectionProps?.fields
const sub = this.ddpConnection.subscribe("subscribeToServiceData",collectionName, {fields})
await sub.ready()
this.collectionSubscription.push(sub)
}
async subscribeToExternalServiceCommunication(){
this.observeActions()
this.actionSub = this.ddpConnection.subscribe("subscribeToExternalServiceCommunication")
await this.actionSub.ready()
}
async registerExternalServiceConnection(){
this.observeCollection("externalServiceActiveConnections",(collectionName, obj)=>{
const res = this.checkNewConnectionState(obj,"serviceAssigned")
if(res){
this.emit("subscribedToService",res.externalService)
this.subscribeToExternalServiceCommunication()
}
})
this.activeConnectionSub = this.ddpConnection.subscribe("registerExternalServiceConnection",this.serviceDescription)
await this.activeConnectionSub.ready()
}
checkNewConnectionState(obj, state){
if(obj.changed?.prev?.state !== obj.changed?.next?.state && obj.changed?.next?.state === state){
return obj.changed.next
}
if(obj.added?.state === state){
return obj.added
}
}
actionCallback(obj, result, error){
return this.ddpConnection.call("actionCallback",obj.id, result, error)
}
hashUserToken(){
const hash = crypto.createHash('sha256');
hash.update(this.token);
return {
digest: hash.digest('base64'),
algorithm: "sha-256"
}
}
/**
* Attach an event handler function for the connection
*
* possible events are:<br>
* <ul>
* <li>connected</li>
* <li>disconnected</li>
* <li>actionAdded</li>
* <li>dataChange</li>
* <li>subscribedToService</li>
* </ul>
*@param {string} event - Name of the event
* @param {function} callback - Callback for the event
**/
on(event, callback){
if(event === "disconnected" || event ==="connected"){
return this.ddpConnection.on(event,callback)
}
return super.on(event, callback)
}
/**
*
* Notify the external app when actions are added, the reply is given through a callback
*
*
* @param {actionAddedCallback} callback Callback that handles actionAdded
*/
onActionAdded(callback){
return this.on("actionAdded",callback)
}
/**
* @callback actionAddedCallback
* @param {object} action object describing the action
* @param {function} callback used to reply to the action
*/
/**
* @param {dataChangeCallback} callback Callback to process data change
*/
onDataChange(callback){
return this.on("dataChange",callback)
}
/**
* @callback dataChangeCallback
* @param {string} collectionName Name of the collection
* @param {function} obj object representing the data added
*/
/**
*
* @param {subscribedToServiceCallback}
*/
onSubscribedToService(callback){
return this.on("subscribedToService",callback)
}
/**
*
* @param {onLoggedInCallback}
*/
onLoggedIn(callback){
return this.on("loggedIn",callback)
}
/**
* @callback subscribedToServiceCallback
* @param {object} serviceInfo Description of the service and its properties
*/
/**
* Attach an event handler function when a connection is established
* @param {function} callback - Callback which receives new data from the subscription
*
*/
onConnected(callback){
return this.on("connected",callback)
}
/**
* Attach an event handler function when a connection is finished
* @param {function} callback - Callback which receives new data from the subscription
*
*/
onDisconnected(callback){
return this.on("disconnected",callback)
}
/**
* Call a function from meteor.
*
*/
call(){
return this.ddpConnection.call(...arguments)
}
fixIdFromObject(obj){
if(typeof obj == "object" && obj.id.length === 25 && obj.id.startsWith("-") && /[0-9A-Fa-f]{24}/g.test(obj.id)){
const id = obj.id.substring(1,obj.id.length)
return {...obj,id}
}
return obj
}
}
exports.Connection = Connection