UNPKG

@remote.it/core

Version:

Core remote.it JavasScript/TypeScript library

175 lines (145 loc) 4.71 kB
import debug from 'debug' import { Service } from './Service' import { ServiceTypes } from './ServiceTypes' import { TargetConfig } from './ConfigFile' import { Environment } from './Environment' import { Logger } from './Logger' import { ServiceModel } from './ServiceModel' const d = debug('remoteit:Device') // TODO: Maybe just extend Service interface since a device is a service? export class Device extends Service { services: Service[] type = ServiceTypes.bulk //--------------------------------------------------------- // In-stance methods //-------------------------------------------------------- constructor(conf: TargetConfig) { super(conf) d('creating device instance:', conf) this.services = [] } async addService(config: { name?: string; port?: number; type?: number }) { Logger.info('adding service to device:', config) d('adding service to device:', config) const type = config.type ? ServiceTypes.findByID(config.type) : ServiceTypes.tcp const name = this.name + ' - ' + (config.name || type.name) const port = Number(config.port || type.defaultPort) if (!port) throw new Error('Must provide a valid port for this service!') d('Registering service', name, 'at port', port) const service = await ServiceModel.register({ hardwareID: this.hardwareID, name, type, port, }) this.services.push(new Service(service)) d('Registered service:', service) return service } /** * Sends a command to the device's bulk service which the * device can respond to. Must Base64 encode the command * to work with the API. */ // async send(command: string) { // return API.post('/device/send/smart', { // deviceaddress: this.uid, // command: Buffer.from(command).toString('base64'), // }) // // { // // "deviceaddress": "string", // // "command": "string", // // "hostip": "string", // // "sc": "[true, false]" // // } // } //--------------------------------------------------------- // Class methods //--------------------------------------------------------- static async create( name: string = Environment.hostname, port: number = 65535 ) { d('creating device:', { name, port }) if (!port) throw new Error('port must be provided!') const service = await ServiceModel.register({ name, overload: 40, port, type: ServiceTypes.bulk, }) if (!service.uid) throw new Error('No device UID created by API!') Logger.info('registered device:', { service }) d('registered device', service) return new Device(service) } // static async find(id: string) { // // Get the bulk service // const resp = await API.get<DeviceResponse>(`/device/${id}`) // const raw = resp.device // const device = new Device({ name: raw.name }) // const uid = raw.deviceaddress // device.uid = uid // device.hardwareID = uid // // Get all nested services // const metadata = await API.get<MetadataResponse>('/device/metadata/list/me') // const serviceIDs = Object.keys(metadata.devices).reduce( // (ids: string[], id) => { // if ( // metadata.devices[id].hardwareid.toLowerCase() === uid.toLowerCase() && // id.toLowerCase() !== uid.toLowerCase() // ) { // ids.push(id) // } // return ids // }, // [] // ) // // Construct the services for this device // device.services = await Promise.all( // serviceIDs.map(id => // API.get<DeviceResponse>(`/device/${id}`).then(resp => { // const s = resp.device // const type = // ServiceTypes.findByTypeString(s.devicetype) || ServiceTypes.tcp // return new Service({ // hardwareID: uid, // name: s.name, // type, // uid: s.deviceaddress.toUpperCase(), // API is inconsistent with case... :( // }) // }) // ) // ) // d('SERVICES:', device.services) // d(device) // return device // } } // interface DeviceResponse { // status: 'true' | 'false' // device: RawDevice // } // interface RawDevice { // deviceaddress: string // name: string // created: string // enabled: string // alerted: string // title: string // devicetype: string // region: string // manufacturer: string // devicestate: string // secret: string // } // interface MetadataResponse { // status: 'true' | 'false' // devices: { [address: string]: RawMetadata } // } // interface RawMetadata { // hardwareid: string // [key: string]: any // }