nativescript-particle
Version:
Control your Particle.io devices from a NativeScript app!
234 lines (233 loc) • 9.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var particle_common_1 = require("./particle.common");
var toJsArray = function (nativeArray) {
var result = [];
if (nativeArray) {
for (var i = 0; i < nativeArray.count; i++) {
result.push(nativeArray.objectAtIndex(i));
}
}
return result;
};
var toJsonVariables = function (nativeDictionary) {
var result = [];
if (nativeDictionary) {
for (var i = 0; i < nativeDictionary.allKeys.count; i++) {
var name_1 = nativeDictionary.allKeys.objectAtIndex(i);
var val = nativeDictionary.valueForKey(name_1);
var type = void 0;
switch (val) {
case "int32":
type = "INT";
break;
case "double":
type = "DOUBLE";
break;
case "string":
type = "STRING";
break;
default:
console.log("Unsupported type (" + val + "), falling back to STRING.");
type = "STRING";
}
result.push({ name: name_1, type: type });
}
}
return result;
};
var MyTNSParticleDevice = (function () {
function MyTNSParticleDevice(nativeDevice) {
this.nativeDevice = nativeDevice;
this.id = nativeDevice.id;
this.name = nativeDevice.name;
this.status = nativeDevice.status;
this.connected = nativeDevice.connected;
this.type = particle_common_1.getDeviceType(nativeDevice.type);
this.functions = toJsArray(nativeDevice.functions);
this.variables = toJsonVariables(nativeDevice.variables);
this.eventIds = new Map();
}
MyTNSParticleDevice.prototype.rename = function (name) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.nativeDevice.renameCompletion(name, function (error) { return error ? reject(error.localizedDescription) : resolve(); });
});
};
MyTNSParticleDevice.prototype.getVariable = function (name) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.nativeDevice.getVariableCompletion(name, function (result, error) { return error ? reject(error.localizedDescription) : resolve(result); });
});
};
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 {
_this.nativeDevice.callFunctionWithArgumentsCompletion(name, args, function (resultCode, error) { return error ? reject(error.localizedDescription) : resolve(resultCode); });
}
catch (e) {
reject(e);
}
});
};
MyTNSParticleDevice.prototype.subscribe = function (prefix, eventHandler) {
if (this.eventIds.has(prefix)) {
console.log("Already subscribed for prefix '" + prefix + "' - not registering another event handler.");
return;
}
var id = this.nativeDevice.subscribeToEventsWithPrefixHandler(prefix, function (event, error) {
if (!error) {
event.data && eventHandler({
prefix: prefix,
event: event.event,
data: event.data,
date: event.time,
deviceID: event.deviceID
});
}
else {
console.log("Error subscribing to event: " + error);
}
});
this.eventIds.set(prefix, id);
};
MyTNSParticleDevice.prototype.unsubscribe = function (prefix) {
if (!this.eventIds.has(prefix)) {
console.log("No handler registered from prefix '" + prefix + "' - skipping unsubscribe");
return;
}
this.nativeDevice.unsubscribeFromEventWithID(this.eventIds.get(prefix));
this.eventIds.delete(prefix);
};
MyTNSParticleDevice.prototype.unclaim = function () {
var _this = this;
return new Promise(function (resolve, reject) {
_this.nativeDevice.unclaim(function (error) { return error ? reject(error.localizedDescription) : resolve(); });
});
};
return MyTNSParticleDevice;
}());
var Particle = (function () {
function Particle() {
this.eventIds = new Map();
}
Particle.prototype.login = function (options) {
return new Promise(function (resolve, reject) {
try {
ParticleCloud.sharedInstance().loginWithUserPasswordCompletion(options.username, options.password, function (error) { return error ? reject(error.localizedDescription) : resolve(); });
}
catch (e) {
reject(e);
}
});
};
Particle.prototype.loginWithToken = function (token) {
ParticleCloud.sharedInstance().injectSessionAccessToken(token);
};
Particle.prototype.setOAuthConfig = function (id, secret) {
ParticleCloud.sharedInstance().oAuthClientId = id;
ParticleCloud.sharedInstance().oAuthClientSecret = secret;
};
Particle.prototype.logout = function () {
ParticleCloud.sharedInstance().logout();
};
Particle.prototype.isAuthenticated = function () {
return ParticleCloud.sharedInstance().isAuthenticated;
};
Particle.prototype.accessToken = function () {
return ParticleCloud.sharedInstance().accessToken;
};
Particle.prototype.listDevices = function () {
return new Promise(function (resolve, reject) {
ParticleCloud.sharedInstance().getDevices(function (particleDevices, error) {
if (error) {
reject(error.localizedDescription);
return;
}
var devices = [];
if (particleDevices) {
for (var i = 0; i < particleDevices.count; i++) {
devices.push(new MyTNSParticleDevice(particleDevices.objectAtIndex(i)));
}
}
resolve(devices);
});
});
};
Particle.prototype.publish = function (name, data, isPrivate, ttl) {
if (ttl === void 0) { ttl = 60; }
return new Promise(function (resolve, reject) {
ParticleCloud.sharedInstance().publishEventWithNameDataIsPrivateTtlCompletion(name, data, isPrivate, ttl, (function (error) { return error ? reject(error.localizedDescription) : resolve(); }));
});
};
Particle.prototype.subscribe = function (prefix, eventHandler) {
if (this.eventIds.has(prefix)) {
console.log("There's already a handler registered for prefix '" + prefix + "' - skipping subscribe");
return;
}
var id = ParticleCloud.sharedInstance().subscribeToAllEventsWithPrefixHandler(prefix, function (event, error) {
if (!error) {
event.data && eventHandler({
prefix: prefix,
event: event.event,
data: event.data,
date: event.time,
deviceID: event.deviceID
});
}
else {
console.log("Error subscribing to event: " + error);
}
});
this.eventIds.set(prefix, id);
};
Particle.prototype.unsubscribe = function (prefix) {
if (!this.eventIds.has(prefix)) {
console.log("No handler registered from prefix '" + prefix + "' - skipping unsubscribe");
return;
}
ParticleCloud.sharedInstance().unsubscribeFromEventWithID(this.eventIds.get(prefix));
this.eventIds.delete(prefix);
};
Particle.prototype.startDeviceSetupWizard = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var setupController = ParticleSetupMainController.new();
_this.wizardDelegate = ParticleSetupControllerDelegateImpl.createWithOwnerAndCallback(new WeakRef(_this), function (result) { return resolve(result); });
setupController.delegate = _this.wizardDelegate;
UIApplication.sharedApplication.keyWindow.rootViewController.presentViewControllerAnimatedCompletion(setupController, true, null);
});
};
Particle.prototype.getDeviceSetupCustomizer = function () {
return ParticleSetupCustomization.sharedInstance();
};
return Particle;
}());
exports.Particle = Particle;
var ParticleSetupControllerDelegateImpl = (function (_super) {
__extends(ParticleSetupControllerDelegateImpl, _super);
function ParticleSetupControllerDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
ParticleSetupControllerDelegateImpl.new = function () {
return _super.new.call(this);
};
ParticleSetupControllerDelegateImpl.createWithOwnerAndCallback = function (owner, callback) {
var delegate = ParticleSetupControllerDelegateImpl.new();
delegate.owner = owner;
delegate.cb = callback;
return delegate;
};
ParticleSetupControllerDelegateImpl.prototype.particleSetupViewControllerDidFinishWithResultDevice = function (controller, result, device) {
this.cb && this.cb(result === 1);
};
ParticleSetupControllerDelegateImpl.prototype.particleSetupViewControllerDidNotSucceeedWithDeviceID = function (controller, deviceID) {
};
ParticleSetupControllerDelegateImpl.ObjCProtocols = [ParticleSetupMainControllerDelegate];
return ParticleSetupControllerDelegateImpl;
}(NSObject));