biodome
Version:
Home automation you can live with
138 lines (113 loc) • 4 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _http = require('http');
var _http2 = _interopRequireDefault(_http);
var _https = require('https');
var _https2 = _interopRequireDefault(_https);
var _ws = require('ws');
var _ws2 = _interopRequireDefault(_ws);
var _validateCommand = require('./validateCommand');
var _validateCommand2 = _interopRequireDefault(_validateCommand);
var Server = (function () {
function Server(_ref) {
var _this = this;
var ssl = _ref.ssl;
var events = _ref.events;
var port = _ref.port;
var keys = _ref.keys;
_classCallCheck(this, Server);
this.http = ssl ? _https2['default'].Server(keys) : _http2['default'].Server();
this.events = events;
this.http.on('request', function (req, res) {
return _this.requestHandler(req, res);
});
this.http.listen(port, function () {
_this.ws = new _ws2['default'].Server({ server: _this.http });
_this.ws.on('connection', function (client) {
return _this.registerSocketClient(client);
});
_this.events.emit('server.open');
});
}
_createClass(Server, [{
key: 'registerSocketClient',
value: function registerSocketClient(client) {
var _this2 = this;
client.on('message', function (msg) {
try {
msg = JSON.parse(msg);
} catch (e) {
// TODO send error to client
console.error('Invalid message: Could not parse JSON');
return;
}
if (msg.type === 'command') {
_this2.emitCommand(msg.data, client);
} else if (msg.type === 'data') {
_this2.events.emit('data', msg.data);
} else {
// TODO send error to client
console.error('Unknown socket message received: ' + msg);
}
});
}
}, {
key: 'emitCommand',
value: function emitCommand(command, client) {
var validation = (0, _validateCommand2['default'])(command);
if (validation.valid) {
this.events.emit('command', command);
} else {
// TODO send validation error to client
console.error(validation.error);
}
}
}, {
key: 'requestHandler',
value: function requestHandler(req, res) {
res.end(404);
}
}, {
key: 'broadcastData',
value: function broadcastData(dataStream) {
var _this3 = this;
dataStream.observe(function (x) {
_this3.broadcastMessage({
type: 'data',
data: x
});
});
}
}, {
key: 'broadcastCommands',
value: function broadcastCommands(commandStream) {
var _this4 = this;
commandStream.observe(function (x) {
_this4.broadcastMessage({
type: 'command',
data: x
});
});
}
}, {
key: 'broadcastMessage',
value: function broadcastMessage(message) {
this.ws.clients.forEach(function (client) {
return client.send(message);
});
}
}, {
key: 'close',
value: function close() {
this.http.close();
}
}]);
return Server;
})();
exports['default'] = Server;
module.exports = exports['default'];