smartfacecloud-emulator-dispatcher
Version:
Handles Emulator Dispatcher Part of SmartfaceCloud
76 lines (62 loc) • 1.59 kB
JavaScript
const FileService = require('./FileService');
function Service() {
const FILE_SERVICE = "file-transfer";
const self = this;
this.eventEmitter = null;
this.fileService = new FileService();
this.init = function(emitter, opts) {
if (!emitter) {
throw new Error('Required parameters are missing');
}
if (self.eventEmitter) {
self.eventEmitter.removeListener('connection', onConnection);
self.eventEmitter.removeListener('message', onMessage);
}
self.eventEmitter = emitter;
self.eventEmitter.on('connection', onConnection);
self.eventEmitter.on('message', onMessage);
self.fileService.init(emit, opts);
};
this.replaceWebsocket = function(ws) {
self.fileService.replaceWebsocket(ws);
};
function onConnection(data) {
if (!data.meta || data.meta.service !== FILE_SERVICE) {
return;
}
}
function onMessage(data) {
if (skip(data.meta)) {
return;
}
data.data.__deviceId = data.meta.deviceId;
self.fileService.handleMessage(data.meta.from, removeMeta(data));
}
function addMeta(to, command, message) {
var messageWithMeta = {
"meta": {
"from": FILE_SERVICE,
"to": to,
"command": command
},
"data": message
};
return messageWithMeta;
}
function removeMeta(data) {
return data.data;
}
function skip(meta) {
if (meta.from === FILE_SERVICE) {
return true;
}
if (meta.to === FILE_SERVICE || meta.to === "*") {
return false;
}
return true;
}
function emit(event, to, command, message) {
self.eventEmitter.emit(event, addMeta(to, command, message));
}
}
module.exports = Service;