UNPKG

nativescript-particle

Version:

Control your Particle.io devices from a NativeScript app!

243 lines (242 loc) 9.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var utils = require("tns-core-modules/utils/utils"); var application_1 = require("tns-core-modules/application"); var worker, eventWorker; var Particle = (function () { function Particle() { this.eventIds = new Map(); io.particle.android.sdk.cloud.ParticleCloudSDK.init(utils.ad.getApplicationContext()); } Particle.prototype.initWorkerIfNeeded = function () { if (!worker) { if (global["TNS_WEBPACK"]) { var WorkerScript = require("nativescript-worker-loader!./particle-worker.js"); worker = new WorkerScript(); } else { worker = new Worker("./particle-worker.js"); } } }; Particle.prototype.initEventWorkerIfNeeded = function () { var _this = this; if (!eventWorker) { if (global["TNS_WEBPACK"]) { var EventWorkerScript = require("nativescript-worker-loader!./particle-event-worker.js"); eventWorker = new EventWorkerScript(); } else { eventWorker = new Worker("./particle-event-worker.js"); } eventWorker.onmessage = function (msg) { if (msg.data.success) { console.log(">> success.. " + msg.data.handlerId); var handlerId = msg.data.handlerId; var handler = _this.eventIds.get(handlerId); console.log(">> success.. handler: " + handler); handler && handler(msg.data.data); } else { console.log("----- no success"); } }; } }; Particle.prototype.login = function (options) { this.initWorkerIfNeeded(); return new Promise(function (resolve, reject) { worker.postMessage({ action: "login", options: options }); worker.onmessage = function (msg) { return msg.data.success ? resolve() : reject(msg.data.error); }; }); }; Particle.prototype.loginWithToken = function (token) { io.particle.android.sdk.cloud.ParticleCloudSDK.getCloud().setAccessToken(token); }; Particle.prototype.setOAuthConfig = function (id, secret) { console.log("'setOAuthConfig' is not currently implemented on Android. Feel like doing a PR? :)"); }; Particle.prototype.logout = function () { io.particle.android.sdk.cloud.ParticleCloudSDK.getCloud().logOut(); worker && worker.terminate(); eventWorker && eventWorker.terminate(); }; Particle.prototype.publish = function (name, data, isPrivate, ttl) { var _this = this; if (ttl === void 0) { ttl = 60; } return new Promise(function (resolve, reject) { _this.initWorkerIfNeeded(); worker.postMessage({ action: "publish", options: { name: name, data: data, isPrivate: isPrivate, ttl: ttl } }); resolve(); }); }; Particle.prototype.subscribe = function (prefix, eventHandler) { if (this.eventIds.has(prefix)) { console.log("Already subscribed for prefix '" + prefix + "' - not registering another event handler."); return; } this.eventIds.set(prefix, eventHandler); this.initEventWorkerIfNeeded(); eventWorker.postMessage({ action: "subscribe", options: { handlerId: prefix, prefix: prefix } }); }; Particle.prototype.unsubscribe = function (prefix) { if (!this.eventIds.has(prefix)) { console.log("No handler registered from prefix '" + prefix + "' - skipping unsubscribe"); return; } this.initEventWorkerIfNeeded(); eventWorker.postMessage({ action: "unsubscribe", options: { prefix: prefix } }); this.eventIds.delete(prefix); }; Particle.prototype.listDevices = function () { var _this = this; return new Promise(function (resolve, reject) { _this.initWorkerIfNeeded(); worker.postMessage({ action: "listDevices" }); worker.onmessage = function (msg) { if (msg.data.success) { var devices = msg.data.devices; devices.map(function (device) { device.rename = function (name) { return _this.renameDevice(device.id, name); }; device.callFunction = function (name, args) { return _this.callFunction(device.id, name, args); }; device.getVariable = function (name) { return _this.getVariable(device.id, name); }; device.subscribe = function (prefix, eventHandler) { return _this.subscribeDevice(device.id, prefix, eventHandler); }; device.unsubscribe = function (prefix) { return _this.unsubscribeDevice(device.id, prefix); }; }); _this.initEventWorkerIfNeeded(); eventWorker.postMessage({ action: "listDevices" }); resolve(devices); } else { reject(msg.data.error); } }; }); }; Particle.prototype.isAuthenticated = function () { return io.particle.android.sdk.cloud.ParticleCloudSDK.getCloud().isLoggedIn(); }; Particle.prototype.accessToken = function () { return io.particle.android.sdk.cloud.ParticleCloudSDK.getCloud().getAccessToken(); }; Particle.prototype.startDeviceSetupWizard = function () { return new Promise(function (resolve, reject) { var intent = application_1.android.foregroundActivity.getIntent(); io.particle.android.sdk.devicesetup.ParticleDeviceSetupLibrary.init(application_1.android.foregroundActivity); var builder = new io.particle.android.sdk.devicesetup.SetupCompleteIntentBuilder({ buildIntent: function (context, setupResult) { resolve(setupResult.wasSuccessful()); return intent; } }); io.particle.android.sdk.devicesetup.ParticleDeviceSetupLibrary.startDeviceSetup(application_1.android.foregroundActivity, builder); }); }; Particle.prototype.getDeviceSetupCustomizer = function () { }; Particle.prototype.renameDevice = function (deviceId, name) { var _this = this; return new Promise(function (resolve, reject) { _this.initWorkerIfNeeded(); worker.postMessage({ action: "rename", options: { deviceId: deviceId, name: name } }); worker.onmessage = function (msg) { return msg.data.success ? resolve() : reject(msg.data.error); }; }); }; Particle.prototype.callFunction = function (deviceId, name, args) { var _this = this; return new Promise(function (resolve, reject) { _this.initWorkerIfNeeded(); worker.postMessage({ action: "callFunction", options: { deviceId: deviceId, name: name, args: args } }); worker.onmessage = function (msg) { return msg.data.success ? resolve(msg.data.result) : reject(msg.data.error); }; }); }; Particle.prototype.getVariable = function (deviceId, name) { var _this = this; return new Promise(function (resolve, reject) { _this.initWorkerIfNeeded(); worker.postMessage({ action: "getVariable", options: { deviceId: deviceId, name: name } }); worker.onmessage = function (msg) { return msg.data.success ? resolve(msg.data.result) : reject(msg.data.error); }; }); }; Particle.prototype.subscribeDevice = function (deviceId, prefix, eventHandler) { var handlerId = deviceId + "_" + prefix; if (this.eventIds.has(handlerId)) { console.log("Already subscribed for prefix '" + prefix + "' - not registering another event handler."); return; } this.eventIds.set(handlerId, eventHandler); this.initEventWorkerIfNeeded(); eventWorker.postMessage({ action: "subscribeDevice", options: { handlerId: handlerId, deviceId: deviceId, prefix: prefix } }); }; Particle.prototype.unsubscribeDevice = function (deviceId, prefix) { var handlerId = deviceId + "_" + prefix; if (!this.eventIds.has(handlerId)) { console.log("No handler registered from prefix '" + handlerId + "' - skipping unsubscribe"); return; } this.initEventWorkerIfNeeded(); eventWorker.postMessage({ action: "unsubscribeDevice", options: { handlerId: handlerId, deviceId: deviceId, prefix: prefix, } }); this.eventIds.delete(handlerId); }; return Particle; }()); exports.Particle = Particle;