UNPKG

@iotile/iotile-cloud

Version:

A typescript library for interfacing with the IOTile Cloud API

588 lines (587 loc) 21.2 kB
import { Project, Org, Stream, Streamer, StreamerReport, Device, Variable, SensorGraph, VarType, User, ProjectTemplate, PropertyTemplate, Property, Membership, ServerInformation, DataPoint, Note, ApiFilter } from "../models"; import { LoggingBase } from "@iotile/iotile-common"; import { LogLevel } from "typescript-logging"; /** * @ngdoc overview * @name iotile.cloud * @description * * # Introduction * The `iotile.cloud` module contains all services and classes needed for interacting * with the IOTile Cloud. It is designed to be dropped into an otherwise IOTile * unaware application and provide a small API that encapsulates * all necessary interactions with IOTile Devices. * * The main point of entry in the `iotile.cloud` module is the `IOTileCloud` service, which * is the only public service provided by `iotile.cloud`. * */ export interface StreamerAck { deviceSlug: string; index: number; highestAck: number; } export interface ProjectMetaData { name: string; id: string; } export interface OrgMetaData { name: string; slug: string; projects: ProjectMetaData[]; } export interface CloudEnvConfig { LOG_LEVEL: LogLevel; HTTP_TIMEOUT: number; SERVER_URLS: [ServerInformation]; } export interface CloudConfig { ENV: Partial<CloudEnvConfig>; } export declare class IOTileCloud extends LoggingBase { private Config; private _server?; private event; initialized: Promise<void>; inProgressConnections: number; constructor(Config: CloudConfig); private initConfig; serverList(): ServerInformation[]; defaultServer(): ServerInformation; isDefault(): boolean; server: ServerInformation; Project(item: any): Project; Org(item: any): Org; /** * @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. */ fetchProject(projectID: string): Promise<Project>; /** * @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. */ fetchProjects(filter?: ApiFilter): Promise<Project[]>; createOrg(name: string, about: string): Promise<any>; createProject(orgSlug: string, name: string): Promise<string>; checkClaimable(data: any): Promise<{} | {}[]>; claimDevice(deviceSlug: string, projectID: string): Promise<void>; /** * @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. */ fetchOrgs(filter?: ApiFilter): Promise<Org[]>; /** * @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 */ fetchOrgMembership(org: Org, filter?: ApiFilter): Promise<Membership>; /** * @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. */ fetchOrgMetaData(projectFilter?: ApiFilter): Promise<OrgMetaData[]>; /** * @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. */ fetchAllDevices(filter?: ApiFilter): Promise<Device[]>; /** * @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. */ fetchProjectDevices(projectId: string, filter?: ApiFilter): Promise<Device[]>; /** * @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. */ fetchDevice(deviceSlug: string): Promise<Device>; /** * @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. */ fetchAllStreams(filter?: ApiFilter): Promise<Stream[]>; /** * @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. */ fetchProjectStreams(projectId: string, filter?: ApiFilter): Promise<Stream[]>; /** * @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. */ fetchStream(streamSlug: string): Promise<Stream>; delete(targetType: string, targetId: number | string): Promise<any>; /** * @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. */ fetchStreamData(streamSlug: string, filter?: ApiFilter): Promise<Array<DataPoint>>; fetchNotes(targetSlug: string, filter?: ApiFilter): Promise<Array<Note>>; createStream(variable: string, dataLabel: string): Promise<{} | {}[]>; createVariable(name: string, project: string, lid: number): Promise<any>; postStreamData(streamSlug: string, type: string, value: any): Promise<{} | {}[]>; /** * @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. */ fetchAllVariables(filter?: ApiFilter): Promise<Variable[]>; /** * @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. */ fetchProjectVariables(projectId: string, filter?: ApiFilter): Promise<Variable[]>; /** * @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. */ fetchVariable(variableSlug: string): Promise<Variable>; /** * @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. */ fetchAllVarTypes(filter?: ApiFilter): Promise<VarType[]>; /** * @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. */ fetchAllProjectTemplates(filter?: ApiFilter): Promise<ProjectTemplate[]>; /** * @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. */ fetchAllPropertyTemplates(filter?: ApiFilter): Promise<PropertyTemplate[]>; /** * @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. */ fetchProperties(deviceSlug: string, filter?: ApiFilter): Promise<Property[]>; /** * @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. */ fetchVarType(variableSlug: string): Promise<VarType>; fetchSensorGraphs(filter?: ApiFilter): Promise<SensorGraph[]>; refreshToken(currentToken: string): Promise<string>; logout(): Promise<void>; login(email: string, password: string): Promise<User>; register(username: string, name: string, email: string, password1: string, password2: string): Promise<{} | {}[]>; fetchUserData(token: string): Promise<User>; setToken(token: string): void; private deleteFromServer; private fetchFromServer; private fetchPagesFromServer; private postToServer; postFirmwareUpgrade(slug: string, version: string): Promise<void>; postEvent(stream: string, extra_data: object): Promise<void>; postFileToNote(note_id: string, file: File): Promise<void>; postNote(target: string, timestamp: Date, note: string): Promise<{} | {}[]>; postLocation(target: string, timestamp: Date, lat: number, lon: number): Promise<void>; /** * @ngdoc method * @name iotile.cloud.service:IOTileCloud#fetchStreamer * @methodOf iotile.cloud.service:IOTileCloud * * @description * Fetches a specific variable type from the IOTile Cloud. * * **This is an async method!** * * Returns a Streamer object with the slug requested. * * @example * <pre> * // Get a Streamer object for variable with slug: streamerSlug * var streamer = await IOTileCloud.fetchStreamer(streamerSlug); * console.log("Found streamer with slug: " + streamer.slug); * </pre> * * @param {string} streamerSlug The slug property of the streamer we're requesting. * * @returns {Streamer} An IOTile Streamer. */ fetchStreamer(streamerSlug: string, filter?: ApiFilter): Promise<Streamer>; fetchStreamerReport(filter: ApiFilter): Promise<StreamerReport[]>; fetchAcknowledgements(deviceSlug?: string): Promise<StreamerAck[]>; /** * @ngdoc method * @name iotile.cloud.service:IOTileCloud#patchStream * @methodOf iotile.cloud.service:IOTileCloud * * @description * Updates the IOTile Cloud with any changes made to a Stream object. * * **This is an async method!** * * Returns a promise that will resolve with the status of the http call. * * @example * <pre> * // Patch our changed Stream object (streamObj) up to the cloud * var status = await IOTileCloud.patchStream(streamObj); * console.log("Patched stream with status: " + status.status); * </pre> * * @param {Stream} streamObj The stream object that has been updated. * * @returns {Promise} A promise that will resolve with the http status. */ patchModel(slug: string, patchPayload: {}): Promise<any>; }