UNPKG

thing-it-device-blueid

Version:

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

317 lines (271 loc) 8.04 kB
module.exports = { metadata: { family: "blueIdAdmin", plugin: "blueIdAdmin", label: "Blue ID 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 BlueId(); } }; var q = require('q'); var client; /** * */ function BlueId() { /** * Initialize on start up */ BlueId.prototype.start = function () { var deferred = q.defer(); // Initialize state values this.state = { switch: false, level: 0, place: 0, unlock: 0, availablePlaces: [] }; this.logDebug("BlueId state: ", this.state); this.logDebug("BlueId configuration: ", this.configuration); // Initialize BlueIdClient // this.client = new BlueIdClient.default(); this.logLevel = "debug"; // this.publishStateChange(); deferred.resolve(); return deferred.promise; }; /** * */ BlueId.prototype.setState = function (state) { this.state = state; this.publishStateChange(); }; /** * */ BlueId.prototype.getState = function () { return this.state; }; /** * Return list of available places for a given user */ BlueId.prototype.getAvailablePlaces = function () { this.state.availablePlaces = availablePlaces; return this.state.availablePlaces; }; /** * Login/logout */ BlueId.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 BlueId system */ BlueId.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("BlueId LOGIN SUCCESS! " + user.email); // this.state.switch = true; // this.publishStateChange(); // deferred.resolve(); // }.bind(this)).catch(function (error) { // this.logError("BlueId Signin Error: " + error); // deferred.reject(error); // }.bind(this)); } else { this.logError("BlueId Signin - no user and/or password!"); } return deferred.promise; }; /** * Log out of remote BlueId system */ BlueId.prototype.signout = function () { var deferred = q.defer(); this.client.signOut().then(function () { this.logDebug("BlueId LOGOUT SUCCESS!"); this.state.switch = false; this.publishStateChange(); deferred.resolve(); }.bind(this)).catch(function (error) { this.logError("BlueId Signout Error: " + error); deferred.reject(error); }.bind(this)); return deferred.promise; }; /** * Retrieve a list of all available locations from BlueId * */ BlueId.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("BlueId AVAILABLE PLACE: " + this.state.availablePlaces[p].address); } this.publishStateChange(); deferred.resolve(); }.bind(this)).catch(function (error) { this.logError("BlueId PLACES ERROR: " + error); deferred.reject(error); }.bind(this)); return deferred.promise; }; /** * Retrieve a specific location from BlueId * * @param parameters */ BlueId.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("BlueId 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? */ BlueId.prototype.setPlace = function (place) { this.state.place = place; this.publishStateChange(); }; /** * Retrieve a specific location from BlueId * * @param parameters */ BlueId.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("BlueId UNLOCK: " + place.name); // deferred.resolve(); // }.bind(this)).catch(function (error) { // this.logError("UNLOCK ERROR: " + error); // deferred.reject(); // }.bind(this)); return deferred.promise; }; /** * */ BlueId.prototype.getLock = function () { return this.state.lock; }; /** * Location setter - not really used? */ BlueId.prototype.setLock = function (lock) { this.state.lock = lock; this.publishStateChange(); }; }