UNPKG

thing-it-device-kisi

Version:

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

318 lines (272 loc) 7.96 kB
module.exports = { metadata: { family: "kisiAdmin", plugin: "kisiAdmin", label: "Kisi Door Admin", tangible: true, discoverable: true, state: [{ id: "switch", label: "Switch", type: { id: "boolean" } }, { id: "level", label: "Level", type: { id: "number" } }, { id: "place", label: "Place", type: { id: "string" } }, { id: "unlock", label: "Unlock", type: { id: "string" } }, { id: "availablePlaces", label: "Available Places", type: { id: "Object" } }], actorTypes: [], sensorTypes: [], services: [{ id: "signin", label: "Sign In" }, { id: "signout", label: "Sign Out" }, { id: "toggleSignin", label: "Log In/Out" }, { id: "listPlaces", label: "List Places" }, { id: "getPlace", label: "Get Place" }, { 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" } }] }, 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 = { switch: false, level: 0, place: 0, unlock: 0, availablePlaces: [] }; 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; }; /** * Return list of available places for a given user */ Kisi.prototype.getAvailablePlaces = function () { this.state.availablePlaces = availablePlaces; return this.state.availablePlaces; }; /** * Login/logout */ Kisi.prototype.toggleSignin = function () { this.logDebug("Signed in? ", this.state.switch); this.logDebug("User: ", this.configuration.user + " pwd: " + this.configuration.pwd); if (this.state.switch) { return this.signout(); } else { return this.signin(); } }; /** * Log into remote Kisi system */ 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.switch = 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 */ Kisi.prototype.signout = function () { var deferred = q.defer(); this.client.signOut().then(function () { this.logDebug("KISI LOGOUT SUCCESS!"); this.state.switch = false; this.publishStateChange(); deferred.resolve(); }.bind(this)).catch(function (error) { this.logError("Kisi Signout Error: " + error); deferred.reject(error); }.bind(this)); return deferred.promise; }; /** * Retrieve a list of all available locations from Kisi * */ Kisi.prototype.listPlaces = function () { var deferred = q.defer(); this.client.get("places").then(function (places) { this.state.availablePlaces = []; for (var obj in places['data']) { this.logDebug("key: " + obj + " id: " + places['data'][obj].id + " name: " + places['data'][obj].name + " address: " + places['data'][obj].address); var place = { id: places['data'][obj].id, name: places['data'][obj].name, address: places['data'][obj].address }; this.state.availablePlaces.push(place); } for (var p in this.state.availablePlaces) { this.logDebug("KISI AVAILABLE PLACE: " + this.state.availablePlaces[p].address); } this.publishStateChange(); deferred.resolve(); }.bind(this)).catch(function (error) { this.logError("KISI PLACES ERROR: " + error); deferred.reject(error); }.bind(this)); return deferred.promise; }; /** * Retrieve a specific location from Kisi * * @param parameters */ Kisi.prototype.getPlace = function (parameters) { var deferred = q.defer(); this.logDebug("Calling " + "places/" + parameters.place); this.client.get("places/" + parameters.place).then(function (place) { this.logDebug("KISI SINGLE PLACE: " + place.name); deferred.resolve(); }.bind(this)).catch(function (error) { this.logError("SINGLE PLACE ERROR: " + error); deferred.reject(); }.bind(this)); return deferred.promise; }; /** * Location setter - not really used? */ Kisi.prototype.setPlace = function (place) { this.state.place = place; this.publishStateChange(); }; /** * Retrieve a specific location from Kisi * * @param parameters */ Kisi.prototype.unlockDoor = function (parameters) { var deferred = q.defer(); this.logDebug("Calling " + "locks/" + parameters.unlock + "/unlock"); 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; }; /** * */ Kisi.prototype.getLock = function () { return this.state.lock; }; /** * Location setter - not really used? */ Kisi.prototype.setLock = function (lock) { this.state.lock = lock; this.publishStateChange(); }; }