@iotile/iotile-cloud
Version:
A typescript library for interfacing with the IOTile Cloud API
1,256 lines • 71.9 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var models_1 = require("../models");
var iotile_common_1 = require("@iotile/iotile-common");
var config_1 = require("../config");
var axios = require("axios");
var lodash = require('lodash');
var HTTP_TIMEOUT_DEFAULT = 30000;
var IOTileCloud = /** @class */ (function (_super) {
__extends(IOTileCloud, _super);
function IOTileCloud(Config) {
var _this = _super.call(this, config_1.catCloud) || this;
_this.Config = _this.initConfig(Config);
_this.inProgressConnections = 0;
_this.event = new iotile_common_1.BlockingEvent();
_this.initialized = _this.event.wait();
_this.server = _this.defaultServer();
return _this;
}
IOTileCloud.prototype.initConfig = function (Config) {
var initializedConfig = Object.assign({ ENV: {} }, Config);
if (initializedConfig.ENV.LOG_LEVEL) {
this.setLogLevel(initializedConfig.ENV.LOG_LEVEL, true);
}
if (!initializedConfig.ENV.HTTP_TIMEOUT) {
this.logWarning("No config HTTP_TIMEOUT specified, using default value of " + HTTP_TIMEOUT_DEFAULT);
initializedConfig.ENV.HTTP_TIMEOUT = HTTP_TIMEOUT_DEFAULT;
}
return initializedConfig;
};
IOTileCloud.prototype.serverList = function () {
return lodash.get(this.Config, 'ENV.SERVER_URLS', []);
};
IOTileCloud.prototype.defaultServer = function () {
for (var _i = 0, _a = this.serverList(); _i < _a.length; _i++) {
var server = _a[_i];
if (lodash.get(server, 'default')) {
return server;
}
}
this.logWarning('No Default Server specified; targeting iotile.cloud');
return {
"shortName": "PRODUCTION",
"longName": "Production Server",
"url": "https://iotile.cloud/api/v1",
"default": true
};
};
IOTileCloud.prototype.isDefault = function () {
var defServer = this.defaultServer();
if (defServer) {
return (this.server.url == defServer.url);
}
else {
return false;
}
};
Object.defineProperty(IOTileCloud.prototype, "server", {
get: function () {
if (this._server) {
return this._server;
}
else {
throw new iotile_common_1.ArgumentError("No Server has been set.");
}
},
set: function (server) {
this._server = server;
this.event.set();
},
enumerable: true,
configurable: true
});
IOTileCloud.prototype.Project = function (item) {
return new models_1.Project(item);
};
;
IOTileCloud.prototype.Org = function (item) {
return new models_1.Org(item);
};
;
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchProject
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches a single project from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns a Project object
*
* @example
* <pre>
* // Get a project specified by id
* var project = await IOTileCloud.fetchProject();
* console.log("Found " + project.name + "!");
* </pre>
*
* @returns {Project} An IOTile project with the given id.
*/
IOTileCloud.prototype.fetchProject = function (projectID) {
return __awaiter(this, void 0, void 0, function () {
var that, projData;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
that = this;
return [4 /*yield*/, that.fetchFromServer('/project/' + projectID + '/')];
case 1:
projData = _a.sent();
return [2 /*return*/, new models_1.Project(projData)];
}
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchProjects
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches all available projects from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns an array of Project objects with all projects retrieved.
*
* @example
* <pre>
* // Get an array of all projects available
* var projects = await IOTileCloud.fetchProjects();
* console.log("Found " + projects.length + " IOTile Projects!");
* </pre>
*
* @returns {Project[]} A list of the IOTile projects.
*/
IOTileCloud.prototype.fetchProjects = function (filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/project/', filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newProject = new models_1.Project(item);
list.push(newProject);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
//Create a new org
IOTileCloud.prototype.createOrg = function (name, about) {
return __awaiter(this, void 0, void 0, function () {
var data, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
data = {
'name': name,
'about': about
};
return [4 /*yield*/, this.postToServer('/org/', data)];
case 1:
response = _a.sent();
return [2 /*return*/, response];
}
});
});
};
//Create a new project in an org and return the project id
IOTileCloud.prototype.createProject = function (orgSlug, name) {
return __awaiter(this, void 0, void 0, function () {
var data, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
data = {
name: name,
org: orgSlug
};
return [4 /*yield*/, this.postToServer('/project/new/', data)];
case 1:
response = _a.sent();
return [2 /*return*/, response['id']];
}
});
});
};
IOTileCloud.prototype.checkClaimable = function (data) {
return __awaiter(this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.postToServer('/device/claimable/', data)];
case 1:
response = _a.sent();
return [2 /*return*/, response];
}
});
});
};
//FIXME: Verify that we use the project id here and not the slug
IOTileCloud.prototype.claimDevice = function (deviceSlug, projectID) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
data = {
device: deviceSlug,
project: projectID
};
return [4 /*yield*/, this.postToServer('/device/claim/', data)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchOrgs
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches all available organizations from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns an array of Org objects with all organizations retrieved.
*
* @example
* <pre>
* // Get an array of all orgs available
* var orgs = await IOTileCloud.fetchOrgs();
* console.log("Found " + orgs.length + " IOTile Orgs!");
* </pre>
*
* @returns {Org[]} A list of the IOTile organizations.
*/
IOTileCloud.prototype.fetchOrgs = function (filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/org/', filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newOrg = new models_1.Org(item);
list.push(newOrg);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchOrgMembership
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches user membership information from a given Org
*
* **This is an async method!**
*
* Returns a Membership object: the user's membership information for the given Org
*
* @example
* <pre>
* // Get membership information for org
* var membership = await IOTileCloud.fetchOrgMembership(org.slug);
* </pre>
*
* @returns {Membership} The membership information of the User in the given Org
*/
IOTileCloud.prototype.fetchOrgMembership = function (org, filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/org/' + org.slug + '/membership/', filter)
.then(function (result) {
var membership = new models_1.Membership(result);
resolve(membership);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchOrgList
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches a list of OrgMetaData objects with the names and ids
* of all projects in all orgs.
*
* **This is an async method!**
*
* Returns an array of OrgMetaData objects with all organizations retrieved.
*/
IOTileCloud.prototype.fetchOrgMetaData = function (projectFilter) {
return __awaiter(this, void 0, void 0, function () {
var orgs, projects, projectMap, orgMap, _i, projects_1, project, _a, orgs_1, org, projectID, project, orgSlug, orgList, orgSlug;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.fetchOrgs()];
case 1:
orgs = _b.sent();
return [4 /*yield*/, this.fetchProjects(projectFilter)];
case 2:
projects = _b.sent();
projectMap = {};
orgMap = {};
for (_i = 0, projects_1 = projects; _i < projects_1.length; _i++) {
project = projects_1[_i];
projectMap[project.id] = project;
}
for (_a = 0, orgs_1 = orgs; _a < orgs_1.length; _a++) {
org = orgs_1[_a];
orgMap[org.slug] = {
name: org.name,
slug: org.slug,
projects: []
};
}
for (projectID in projectMap) {
project = projectMap[projectID];
orgSlug = project.orgSlug;
if (!(orgSlug in orgMap)) {
this.logError('Received a project from IOTile.cloud that did not have an org', {
project: project,
orgSlug: orgSlug
});
continue;
}
orgMap[orgSlug].projects.push({
name: project.name,
id: project.id
});
}
orgList = [];
for (orgSlug in orgMap) {
orgList.push(orgMap[orgSlug]);
}
return [2 /*return*/, orgList];
}
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchAllDevices
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches all available devices from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns an array of Device objects with all devices retrieved.
*
* @example
* <pre>
* // Get an array of all devices available
* var devices = await IOTileCloud.fetchAllDevices();
* console.log("Found " + devices.length + " IOTile Devices!");
* </pre>
*
* @returns {Device[]} A list of the IOTile devices.
*/
IOTileCloud.prototype.fetchAllDevices = function (filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/device/', filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newDevice = new models_1.Device(item);
list.push(newDevice);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchProjectDevices
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches all devices for a given project from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns an array of Device objects with all devices retrieved.
*
* @example
* <pre>
* // Get an array of all devices available for project with ID: projId
* var projDevices = await IOTileCloud.fetchProjectDevices(projId);
* console.log("Found " + projDevices.length + " IOTile Devices!");
* </pre>
*
* @param {string} projectId The id property of the project object that
* devices will be fetched from.
*
* @returns {Device[]} A list of the IOTile devices.
*/
IOTileCloud.prototype.fetchProjectDevices = function (projectId, filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
if (!filter) {
filter = new models_1.ApiFilter();
}
else {
filter = filter.copy();
}
console.log('projectId:', projectId);
filter.addFilter('project', projectId, true);
that.fetchFromServer('/device/', filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
console.log("ITEM:", item);
var newDevice = new models_1.Device(item);
console.log('newDevice:', newDevice);
list.push(newDevice);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchDevice
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches a specific device from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns a Device object with the device requested.
*
* @example
* <pre>
* // Get a device object for device with slug: devSlug
* var device = await IOTileCloud.fetchDevice(devSlug);
* console.log("Found device with label: " + device.label);
* </pre>
*
* @param {string} deviceSlug The slug property of the device that will
* be fetched.
*
* @returns {Device} An IOTile Device.
*/
IOTileCloud.prototype.fetchDevice = function (deviceSlug) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/device/' + deviceSlug + '/')
.then(function (item) {
var newDevice = new models_1.Device(item);
resolve(newDevice);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchAllStreams
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches all available streams from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns an array of Stream objects with all streams retrieved.
*
* @example
* <pre>
* // Get an array of all streams available
* var streams = await IOTileCloud.fetchAllStreams();
* console.log("Found " + streams.length + " IOTile Streams!");
* </pre>
*
* @returns {Stream[]} A list of the IOTile streams.
*/
IOTileCloud.prototype.fetchAllStreams = function (filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/stream/', filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newStream = new models_1.Stream(item);
list.push(newStream);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchProjectStreams
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches all streams for a given project from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns an array of Stream objects with all streams retrieved.
*
* @example
* <pre>
* // Get an array of all streams available for project with ID: projId
* var projStreams = await IOTileCloud.fetchProjectStreams(projId);
* console.log("Found " + projStreams.length + " IOTile Streams!");
* </pre>
*
* @param {string} projectId The id property of the project object that
* streams will be fetched from.
*
* @returns {Stream[]} A list of the IOTile streams.
*/
IOTileCloud.prototype.fetchProjectStreams = function (projectId, filter) {
return __awaiter(this, void 0, void 0, function () {
var that, uri;
return __generator(this, function (_a) {
that = this;
uri = '/stream/';
return [2 /*return*/, new Promise(function (resolve, reject) {
if (!filter) {
filter = new models_1.ApiFilter();
}
else {
filter = filter.copy();
}
filter.addFilter('project', projectId, true);
that.fetchFromServer(uri, filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newStream = new models_1.Stream(item);
list.push(newStream);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchStream
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches a specific stream from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns a Stream object with the stream requested.
*
* @example
* <pre>
* // Get a stream object for stream with slug: streamSlug
* var stream = await IOTileCloud.fetchStream(streamSlug);
* console.log("Found stream with slug: " + stream.slug);
* </pre>
*
* @param {string} streamSlug The slug property of the stream that will
* be fetched.
*
* @returns {Stream} An IOTile Stream.
*/
IOTileCloud.prototype.fetchStream = function (streamSlug) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/stream/' + streamSlug + '/')
.then(function (item) {
var newStream = new models_1.Stream(item);
resolve(newStream);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
IOTileCloud.prototype.delete = function (targetType, targetId) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.deleteFromServer("/" + targetType + "/" + targetId + '/')
.then(function (resp) {
resolve(resp);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchStreamData
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches a specific stream's datapoints from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns a list of DataPoints from the stream requested.
*
* @example
* <pre>
* // Get an array of DataPoints for stream with slug: streamSlug
* var datapoints = await IOTileCloud.fetchStreamData(streamSlug);
* console.log(`Found ${datapoints.length} datapoints from stream with slug: ${stream.slug}`);
* </pre>
*
* @param {string} streamSlug The slug property of the stream that will
* be fetched.
*
* @returns {Array<DataPoint>} An array of DataPoints.
*/
IOTileCloud.prototype.fetchStreamData = function (streamSlug, filter) {
return __awaiter(this, void 0, void 0, function () {
var that, datapoints;
return __generator(this, function (_a) {
that = this;
datapoints = [];
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/data/?filter=' + streamSlug, filter)
.then(function (result) {
lodash.forEach(result, function (item) {
var newData = new models_1.DataPoint(item);
datapoints.push(newData);
});
resolve(datapoints);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
IOTileCloud.prototype.fetchNotes = function (targetSlug, filter) {
return __awaiter(this, void 0, void 0, function () {
var that, notes;
return __generator(this, function (_a) {
that = this;
notes = [];
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/note/?target=' + targetSlug, filter)
.then(function (result) {
lodash.forEach(result, function (item) {
var newData = new models_1.Note(item);
notes.push(newData);
});
resolve(notes);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
IOTileCloud.prototype.createStream = function (variable, dataLabel) {
return __awaiter(this, void 0, void 0, function () {
var data, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
data = {
'variable': variable,
'data_label': dataLabel
};
return [4 /*yield*/, this.postToServer('/stream/', data)];
case 1:
response = _a.sent();
return [2 /*return*/, response];
}
});
});
};
IOTileCloud.prototype.createVariable = function (name, project, lid) {
return __awaiter(this, void 0, void 0, function () {
var data, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
data = {
'name': name,
'project': project,
'lid': lid
};
return [4 /*yield*/, this.postToServer('/variable/', data)];
case 1:
response = _a.sent();
return [2 /*return*/, response];
}
});
});
};
IOTileCloud.prototype.postStreamData = function (streamSlug, type, value) {
return __awaiter(this, void 0, void 0, function () {
var data, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
data = {
'stream': streamSlug,
'type': type,
'timestamp': new Date(),
'int_value': value
};
return [4 /*yield*/, this.postToServer('/data/', data)];
case 1:
response = _a.sent();
return [2 /*return*/, response];
}
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchAllVariables
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches all available variables from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns an array of Variable objects with all variables retrieved.
*
* @example
* <pre>
* // Get an array of all variables available
* var variables = await IOTileCloud.fetchAllVariables();
* console.log("Found " + variables.length + " IOTile Variables!");
* </pre>
*
* @returns {Variable[]} A list of the IOTile variables.
*/
IOTileCloud.prototype.fetchAllVariables = function (filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/variable/', filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newVariable = new models_1.Variable(item);
list.push(newVariable);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchProjectVariables
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches all variables for a given project from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns an array of Variable objects with all variables retrieved.
*
* @example
* <pre>
* // Get an array of all variables available for project with ID: projId
* var projVariables = await IOTileCloud.fetchProjectVariables(projId);
* console.log("Found " + projVariables.length + " IOTile Variables!");
* </pre>
*
* @param {string} projectId The id property of the project object that
* variables will be fetched from.
*
* @returns {Variable[]} A list of the IOTile variables.
*/
IOTileCloud.prototype.fetchProjectVariables = function (projectId, filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/variable/?project=' + projectId, filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newVariable = new models_1.Variable(item);
list.push(newVariable);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchVariable
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches a specific variable from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns a Variable object with the variable requested.
*
* @example
* <pre>
* // Get a Variable object for variable with slug: variableSlug
* var variable = await IOTileCloud.fetchVariable(variableSlug);
* console.log("Found variable with slug: " + variable.slug);
* </pre>
*
* @param {string} variableSlug The slug property of the variable that will
* be fetched.
*
* @returns {Variable} An IOTile Variable.
*/
IOTileCloud.prototype.fetchVariable = function (variableSlug) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/variable/' + variableSlug + '/')
.then(function (item) {
var newVariable = new models_1.Variable(item);
resolve(newVariable);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchAllVarTypes
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches all available vartypes from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns an array of VarType objects with all vartypes retrieved.
*
* @example
* <pre>
* // Get an array of all vartypes available
* var vartypes = await IOTileCloud.fetchAllVarTypes();
* console.log("Found " + vartypes.length + " IOTile VarTypes!");
* </pre>
*
* @returns {VarType[]} A list of the IOTile vartypes.
*/
IOTileCloud.prototype.fetchAllVarTypes = function (filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/vartype/', filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newVarType = new models_1.VarType(item);
list.push(newVarType);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchAllProjectTemplates
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches all available project templates from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns an array of ProjectTemplate objects with all project templates retrieved.
*
* @example
* <pre>
* // Get an array of all project templates available
* var project_templates = await IOTileCloud.fetchAllProjectTemplates();
* console.log("Found " + project_templates.length + " IOTile ProjectTemplates!");
* </pre>
*
* @returns {ProjectTemplate[]} A list of the IOTile project templates.
*/
IOTileCloud.prototype.fetchAllProjectTemplates = function (filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/pt/', filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newProjectTemplate = new models_1.ProjectTemplate(item);
list.push(newProjectTemplate);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchAllPropertyTemplates
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches all available property templates from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns an array of PropertyTemplate objects with all property templates retrieved.
*
* @example
* <pre>
* // Get an array of all property templates available
* var property_templates = await IOTileCloud.fetchAllPropertyTemplates();
* console.log("Found " + property_templates.length + " IOTile PropertyTemplates!");
* </pre>
*
* @returns {PropertyTemplate[]} A list of the IOTile property templates.
*/
IOTileCloud.prototype.fetchAllPropertyTemplates = function (filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/propertytemplate/', filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newPropertyTemplate = new models_1.PropertyTemplate(item);
list.push(newPropertyTemplate);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchProperties
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches properties for a given target from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns a Property object for the target requested.
*
* @example
* <pre>
* // Get the Properties for a target with slug: slug
* var properties = await IOTileCloud.fetchProperties(slug);
* </pre>
*
* @param {string} deviceSlug The slug property of the device for which
* the property is associated.
*
* @returns {Property[]} An IOTile Property.
*/
IOTileCloud.prototype.fetchProperties = function (deviceSlug, filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/property/?target=' + deviceSlug, filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newProperty = new models_1.Property(item);
list.push(newProperty);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
/**
* @ngdoc method
* @name iotile.cloud.service:IOTileCloud#fetchVarType
* @methodOf iotile.cloud.service:IOTileCloud
*
* @description
* Fetches a specific variable type from the IOTile Cloud.
*
* **This is an async method!**
*
* Returns a VarType object with the variable type requested.
*
* @example
* <pre>
* // Get a VarType object for variable with slug: variableSlug
* var vartype = await IOTileCloud.fetchVarType(variableSlug);
* console.log("Found vartype with slug: " + vartype.slug);
* </pre>
*
* @param {string} variableSlug The slug property of the variable for which
* the vartype is associated.
*
* @returns {Variable} An IOTile Variable.
*/
IOTileCloud.prototype.fetchVarType = function (variableSlug) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/variable/' + variableSlug + '/type/')
.then(function (item) {
var newVarType = new models_1.VarType(item);
resolve(newVarType);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
IOTileCloud.prototype.fetchSensorGraphs = function (filter) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.fetchFromServer('/sg/', filter)
.then(function (result) {
var list = [];
lodash.forEach(result, function (item) {
var newSG = new models_1.SensorGraph(item);
list.push(newSG);
});
resolve(list);
}).catch(function (err) {
reject(err);
});
})];
});
});
};
IOTileCloud.prototype.refreshToken = function (currentToken) {
return __awaiter(this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.postToServer('/auth/api-jwt-refresh/', { token: currentToken })];
case 1:
response = _a.sent();
return [2 /*return*/, response['token']];
}
});
});
};
IOTileCloud.prototype.logout = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.postToServer('/auth/logout/', undefined)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
IOTileCloud.prototype.login = function (email, password) {
return __awaiter(this, void 0, void 0, function () {
var response, user, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
return [4 /*yield*/, this.postToServer('/auth/api-jwt-auth/', { username: email, password: password })];
case 1:
response = _a.sent();
return [4 /*yield*/, this.fetchUserData(response['token'])];
case 2:
user = _a.sent();
return [2 /*return*/, user];
case 3:
err_1 = _a.sent();
if (!(err_1 instanceof models_1.HttpError)) {
err_1 = new models_1.HttpError(err_1);
}
throw err_1;
case 4: return [2 /*return*/];
}
});
});
};
IOTileCloud.prototype.register = function (username, name, email, password1, password2) {
try {
return this.postToServer('/account/', {
username: username,
name: name,
email: email,
password: password1,
confirm_password: password2
});
}
catch (err) {
if (!(err instanceof models_1.HttpError)) {
err = new models_1.HttpError(err);
}
throw err;
}
};
IOTileCloud.prototype.fetchUserD