UNPKG

holly-sdk

Version:
122 lines (96 loc) 4.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.App = undefined; 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; }; }(); var _express = require("express"); var _express2 = _interopRequireDefault(_express); var _bodyParser = require("body-parser"); var _bodyParser2 = _interopRequireDefault(_bodyParser); var _fs = require("fs"); var _fs2 = _interopRequireDefault(_fs); var _api = require("./api"); var _keystore = require("./keystore"); var _deviceManager = require("./device-manager"); var _nedb = require("nedb"); var _nedb2 = _interopRequireDefault(_nedb); 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 App = exports.App = function () { function App(manifestPath, publicPath) { _classCallCheck(this, App); this.manifest = JSON.parse(_fs2.default.readFileSync(manifestPath, "utf8")); this.routes = (0, _express2.default)().use(_express2.default.static(publicPath)).use(_bodyParser2.default.json()); this.api = new _api.API(this); // this.devices is an array of objects // eg. [{reference_device_name: door, device_id: 8}] this.devices = null; this.deviceManager = null; this.keystore = null; this.appInstallation = null; } _createClass(App, [{ key: "inform", value: function inform(address) { return this.api.appInstallations.updateAppInstallation({ port: address.port }); } }, { key: "db", value: function db(name) { if (!this.appInstallation) { return null; //Not yet initialized! } return new _nedb2.default({ filename: this.dbPath() + "/" + name + ".db", autoload: true }); } }, { key: "run", value: function run() { var _this = this; var port = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.getPort(); var address = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "127.0.0.1"; return new Promise(function (resolve) { var server = _this.routes.listen(port, address, function () { // Inform the Hub API of the server's port number. _this.inform(server.address()).then(function (appInstallation) { _this.appInstallation = appInstallation; _this.devices = appInstallation.installation_devices; var self = _this; _this.keystore = new _keystore.Keystore(function (name) { return self.db(name); }); _deviceManager.DeviceManager.create(_this).then(function (deviceManager) { _this.deviceManager = deviceManager; }).then(function () { resolve(appInstallation); console.log("Application Started"); }); }).catch(function (err) { console.error(err); process.exit(1); }); }); }); } }, { key: "getPort", value: function getPort() { if (process.env.HALLEY_PORT) { return process.env.HALLEY_PORT; } return 0; } }, { key: "dbPath", value: function dbPath() { // HALLEY_LOCAL_DB takes any argument. Local DB has a fixed location. if (process.env.HALLEY_LOCAL_DB) { return __dirname + "/../"; } return this.appInstallation.database_dir; } }]); return App; }();