UNPKG

thing-it-device-kisi

Version:

[thing-it-node] Device Plugin for Kisi products.

243 lines (208 loc) 6.69 kB
module.exports = { metadata: { family: "kisiDoor", plugin: "kisiDoor", label: "Kisi Door Control", tangible: true, discoverable: true, state: [{ id: "unlock", label: "Unlock", type: { id: "boolean" } }, { id: "signedIn", label: "Signed In", type: { id: "boolean" } }], actorTypes: [], sensorTypes: [], services: [{ id: "unlockDoor", label: "Unlock Door" }], configuration: [{ id: "simulated", label: "Simulated", type: { id: "boolean" } }, { id: "location", label: "Location", type: { id: "string" } }, { id: "user", label: "User", type: { id: "string" } }, { id: "pwd", label: "Password", type: { id: "string" } }, { id: "doorId", label: "Door ID", type: { id: "integer" } }] }, create: function (device) { return new Kisi(); } }; var q = require('q'); var KisiClient = require('kisi-client'); var client; /** * */ function Kisi() { /** * Initialize on start up */ Kisi.prototype.start = function () { var deferred = q.defer(); // Initialize state values this.state = { signedIn: false, doorId: 0, unlock: 0 }; this.logDebug("Kisi state: ", this.state); this.logDebug("Kisi configuration: ", this.configuration); // Initialize KisiClient this.client = new KisiClient.default(); this.logLevel = "debug"; // this.publishStateChange(); deferred.resolve(); return deferred.promise; }; /** * */ Kisi.prototype.setState = function (state) { this.state = state; this.publishStateChange(); }; /** * */ Kisi.prototype.getState = function () { return this.state; }; /** * Log into remote Kisi system * TODO: Probably no longer needed but left temporarily */ Kisi.prototype.signin = function () { var deferred = q.defer(); if (this.configuration.user && this.configuration.pwd) { this.client.signIn(this.configuration.user, this.configuration.pwd).then(function (user) { this.logDebug("KISI LOGIN SUCCESS! " + user.email); this.state.unlock = true; this.publishStateChange(); deferred.resolve(); }.bind(this)).catch(function (error) { this.logError("Kisi Signin Error: " + error); deferred.reject(error); }.bind(this)); } else { this.logError("Kisi Signin - no user and/or password!"); } return deferred.promise; }; /** * Log out of remote Kisi system * TODO: Probably no longer needed but left temporarily */ Kisi.prototype.signout = function () { var deferred = q.defer(); this.client.signOut().then(function () { this.logDebug("KISI LOGOUT SUCCESS!"); this.state.unlock = false; this.publishStateChange(); deferred.resolve(); }.bind(this)).catch(function (error) { this.logError("Kisi Signout Error: " + error); deferred.reject(error); }.bind(this)); return deferred.promise; }; /** * Timer callback to reset lock button */ Kisi.prototype.reset = function() { this.state.unlock = false; this.publishStateChange(); }; /** * Unlock the specificed door * TODO What happens if the user/pwd config changes when already logged in? * * @param parameters */ Kisi.prototype.unlockDoor = function (parameters) { var deferred = q.defer(); if (!this.state.signedIn) { if (this.configuration.user && this.configuration.pwd) { // If not logged in already, do so this.client.signIn(this.configuration.user, this.configuration.pwd).then(function (user) { this.logDebug("KISI LOGIN SUCCESS! " + user.email); this.state.signedIn = true; // Animate lock button this.state.unlock = true; this.publishStateChange(); setTimeout(function () { this.reset(); }.bind(this), 2500); // Post unlock call to Kisi.com this.logDebug("Calling " + "locks/" + parameters.place + "/unlock"); this.client.post("locks/" + parameters.place + "/unlock", {}).then(function (place) { this.logDebug("KISI UNLOCK: " + place.name); this.publishStateChange(); deferred.resolve(); }.bind(this)).catch(function (error) { this.logError("UNLOCK ERROR: " + error); deferred.reject(); }.bind(this)); }.bind(this)).catch(function (error) { this.logError("Kisi Signin Error: " + error); deferred.reject(error); }.bind(this)); } else { this.logError("Kisi Signin - no user and/or password!"); } } else { // Already logged in, just call unlock via POST this.logDebug("ALREADY SIGNED IN: Calling " + "locks/" + parameters.unlock + "/unlock"); // Animate lock button this.state.unlock = true; this.publishStateChange(); setTimeout(function () { this.reset(); }.bind(this), 2500); // Post unlock call to Kisi.com this.client.post("locks/" + parameters.place + "/unlock", {}).then(function (place) { this.logDebug("KISI UNLOCK: " + place.name); deferred.resolve(); }.bind(this)).catch(function (error) { this.logError("UNLOCK ERROR: " + error); deferred.reject(); }.bind(this)); } return deferred.promise; }; }