UNPKG

biodome

Version:

Home automation you can live with

171 lines (142 loc) 5.15 kB
'use strict'; 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 _commandMatcherFactory = require('./commandMatcherFactory'); var _commandMatcherFactory2 = _interopRequireDefault(_commandMatcherFactory); var _log = require('./log'); var _log2 = _interopRequireDefault(_log); var Endpoint = (function () { function Endpoint(_ref) { var _this = this; var id = _ref.id; var type = _ref.type; var driver = _ref.driver; var events = _ref.events; var commandMatcher = _ref.commandMatcher; var refreshRate = _ref.refreshRate; var writeTimeout = _ref.writeTimeout; var readTimeout = _ref.readTimeout; _classCallCheck(this, Endpoint); this.id = id; // Free text this.type = type; // data is emitted on this event instance this.events = events; // must implement biodome.driver interface this.driver = driver; this.shouldExecuteCommand = commandMatcher || (0, _commandMatcherFactory2['default'])(this); // refresh rate in ms (optional) this.refreshRate = refreshRate; // maximum ms allowed for write attempt this.writeTimeout = writeTimeout || 2000; // maximum ms allowed for read attempt this.readTimeout = readTimeout || 1000; if (this.refreshRate) { this.refreshTimer = global.setInterval(function () { _this.read(); }, this.refreshRate); } } _createClass(Endpoint, [{ key: 'broadcastData', value: function broadcastData(value) { this.events.emit('data', { id: this.id, type: this.type, timestamp: Date.now(), value: value }); } }, { key: 'write', value: function write(value) { var _this2 = this; return new Promise(function (resolve, reject) { setTimeout(function () { reject(new Error('Exceeded maximum write execution time')); }, _this2.writeTimeout); _this2.driver.write(value).then(function (newValue) { _this2.broadcastData(newValue); resolve(newValue); })['catch'](function (e) { _log2['default'].error({ source: _this2.toString(), data: value }, 'Write failed - ' + e.message); reject(new Error('Hardware failure')); }); }); } }, { key: 'read', value: function read() { var _this3 = this; return new Promise(function (resolve, reject) { setTimeout(function () { reject(new Error('Exceeded maximum read execution time')); }, _this3.readTimeout); _this3.driver.read().then(function (newValue) { _this3.broadcastData(newValue); resolve(newValue); })['catch'](function (e) { _log2['default'].error({ source: _this3.toString(), data: null }, 'Read failed - ' + e.message); reject(new Error('Hardware failure')); }); }); } }, { key: 'subscribeToCommands', value: function subscribeToCommands(commands) { var _this4 = this; commands.filter(function (cmd) { return _this4.shouldExecuteCommand(cmd); }).observe(function (cmd) { return _this4.executeCommand(cmd); }); } }, { key: 'executeCommand', value: function executeCommand(command) { var _this5 = this; if (command.instruction.type === 'write') { return this.write(command.instruction.value)['catch'](function (e) { _log2['default'].error({ source: 'command:' + _this5, message: 'Command failed: ' + e.message, data: command }); }); } else if (command.instruction.type === 'read') { return this.read()['catch'](function (e) { _log2['default'].error({ source: 'command:' + _this5, message: 'Command failed: ' + e.message, data: command }); }); } } }, { key: 'toString', value: function toString() { return 'endpoint:' + this.id; } }, { key: 'destroy', value: function destroy() { if (this.refreshTimer) { global.clearInterval(this.refreshTimer); } } }]); return Endpoint; })(); exports['default'] = Endpoint; module.exports = exports['default'];