nativescript-particle
Version:
Control your Particle.io devices from a NativeScript app!
79 lines (78 loc) • 3.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("globals");
var particle_worker_base_1 = require("./particle-worker-base");
var cachedDevices;
var login = function (options) {
try {
io.particle.android.sdk.cloud.ParticleCloudSDK.getCloud().logIn(options.username, options.password);
global.postMessage({ success: true });
}
catch (e) {
global.postMessage({ success: false, error: e.nativeException.getBestMessage() });
}
};
var listDevices = function () {
try {
var particleDevices = io.particle.android.sdk.cloud.ParticleCloudSDK.getCloud().getDevices();
cachedDevices = [];
for (var i = 0; i < particleDevices.size(); i++) {
cachedDevices.push(new particle_worker_base_1.MyTNSParticleDevice(particleDevices.get(i)));
}
global.postMessage({ success: true, devices: cachedDevices });
}
catch (e) {
global.postMessage({ success: false, error: e.nativeException.getBestMessage() });
}
};
var getVariable = function (device, name) {
device.getVariable(name)
.then(function (result) { return (global.postMessage({ success: true, result: result })); })
.catch(function (error) { return global.postMessage({ success: false, error: error }); });
};
var callFunction = function (device, name, args) {
device.callFunction(name, args)
.then(function (result) { return global.postMessage({ success: true, result: result }); })
.catch(function (error) { return global.postMessage({ success: false, error: error }); });
};
var renameDevice = function (device, name) {
device.rename(name)
.then(function (result) { return global.postMessage({ success: true }); })
.catch(function (error) { return global.postMessage({ success: false, error: error }); });
};
var publish = function (name, data, isPrivate, ttl) {
io.particle.android.sdk.cloud.ParticleCloudSDK.getCloud().publishEvent(name, data, isPrivate ? io.particle.android.sdk.cloud.ParticleEventVisibility.PRIVATE : io.particle.android.sdk.cloud.ParticleEventVisibility.PUBLIC, ttl);
};
var getDevice = function (id) {
return cachedDevices.filter(function (cachedDevice) { return cachedDevice.id === id; })[0];
};
global.onmessage = function (msg) {
var request = msg.data;
if (request.action === "login") {
login(request.options);
return;
}
else if (request.action === "listDevices") {
listDevices();
return;
}
else if (request.action === "rename") {
renameDevice(getDevice(request.options.deviceId), request.options.name);
return;
}
else if (request.action === "callFunction") {
callFunction(getDevice(request.options.deviceId), request.options.name, request.options.args);
return;
}
else if (request.action === "getVariable") {
getVariable(getDevice(request.options.deviceId), request.options.name);
return;
}
else if (request.action === "publish") {
publish(request.options.name, request.options.data, request.options.isPrivate, request.options.ttl);
return;
}
else {
global.postMessage({ success: false, error: "Unsupported action sent to worker: '" + request.action + "'" });
}
};