UNPKG

nativescript-particle

Version:

Control your Particle.io devices from a NativeScript app!

132 lines (131 loc) 5.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var particle_common_1 = require("./particle.common"); var MyTNSParticleDevice = (function () { function MyTNSParticleDevice(particleDevice) { this.particleDevice = particleDevice; this.id = particleDevice.getID(); this.name = particleDevice.getName(); this.status = particleDevice.isConnected() ? particleDevice.getStatus() : "offline"; this.connected = particleDevice.isConnected(); this.type = particle_common_1.getDeviceType(particleDevice.getProductID()); this.functions = toJsArray(particleDevice.getFunctions()); this.variables = toJsonVariables(particleDevice.getVariables()); this.eventIds = new Map(); } MyTNSParticleDevice.prototype.rename = function (name) { var _this = this; return new Promise(function (resolve, reject) { _this.particleDevice.setName(name); resolve(); }); }; MyTNSParticleDevice.prototype.getVariable = function (name) { var _this = this; return new Promise(function (resolve, reject) { try { var result = _this.particleDevice.getVariable(name); var className = result.getClass ? result.getClass().getName() : "default"; switch (className) { case "java.lang.Integer": case "java.lang.Long": case "java.lang.Double": resolve(Number(String(result))); break; default: resolve(String(result)); } } catch (e) { reject(e.nativeException.getBestMessage()); } }); }; MyTNSParticleDevice.prototype.callFunction = function (name) { var _this = this; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return new Promise(function (resolve, reject) { try { resolve(_this.particleDevice.callFunction(name, java.util.Arrays.asList(args))); } catch (e) { reject(e.nativeException.getBestMessage()); } }); }; MyTNSParticleDevice.prototype.subscribe = function (prefix, eventHandler, handlerId) { try { if (this.eventIds.has(handlerId)) { console.log("There's already a handler registered for prefix '" + handlerId + "' - skipping subscribe"); return; } var handler = new io.particle.android.sdk.cloud.ParticleEventHandler({ onEventError: function (exception) { global.postMessage({ success: false }); }, onEvent: function (eventName, event) { if (event) { global.postMessage({ success: true, handlerId: handlerId, data: { prefix: prefix, event: eventName, data: event.dataPayload, date: new Date(event.publishedAt.getTime()), deviceID: event.deviceId } }); } } }); var id = this.particleDevice.subscribeToEvents(prefix, handler); this.eventIds.set(handlerId, id); } catch (e) { console.log(e.nativeException.getBestMessage()); } }; MyTNSParticleDevice.prototype.unsubscribe = function (prefix, handlerId) { if (!this.eventIds.has(handlerId)) { console.log("No handler registered for prefix '" + handlerId + "' - skipping unsubscribe"); return; } this.particleDevice.unsubscribeFromEvents(this.eventIds.get(handlerId)); this.eventIds.delete(handlerId); }; MyTNSParticleDevice.prototype.unclaim = function () { var _this = this; return new Promise(function (resolve, reject) { _this.particleDevice.unclaim(); resolve(); }); }; return MyTNSParticleDevice; }()); exports.MyTNSParticleDevice = MyTNSParticleDevice; var toJsArray = function (nativeSet) { var result = []; if (nativeSet) { var it = nativeSet.iterator(); while (it.hasNext()) { result.push(it.next()); } } return result; }; var toJsonVariables = function (nativeMap) { var result = []; if (nativeMap) { var it = nativeMap.keySet().iterator(); while (it.hasNext()) { var name_1 = it.next(); var type = nativeMap.get(name_1).toString(); result.push({ name: name_1, type: type }); } } return result; };