import axios , { AxiosError , AxiosInstance } from 'axios' ; import { ParsedUrlQueryInput , stringify } from 'querystring' ; import * as NatureRemo from './interfaces' ; export class Cloud { private readonly token : string ; private readonly instance : AxiosInstance ; constructor ( token ) { this . token = token ; this . instance = axios . create ( { baseURL : 'https://api.nature.global' , headers : { Authorization : `Bearer ${ this . token }` , 'Content-Type' : 'application/x-www-form-urlencoded; charset=utf-8' , } , timeout : 5000 , } ) ; } /**
     * Fetch the authenticated user’s information.
     */ public async getUser ( ) { const response = await this . _get < NatureRemo . IUser > ( '/1/users/me' ) ; return response ; } returns public async updateUser ( nickname ) { const response = await this . _post < NatureRemo . IUser > ( '/1/users/me' , { nickname , } ) ; return response ; } /**
     * Fetch the list of Remo devices the user has access to.
     */ public async getDevices ( ) { const response = await this . _get < NatureRemo . IDeviceWithEvents [ ] > ( '/1/devices' ) ; return response ; } param message public async detectAppliance ( message ) { const response = await this . _post < NatureRemo . IDetectedAirconModel > ( '/1/users/me' , { message : JSON . stringify ( message ) , } ) ; return response ; } /**
     * get sensor value of arbitrary device. "humidity" and "illumination" will be undefined if Nature Remo mini
     */ public async getSensorValue ( ) { const device = await this . getDevices ( ) ; return { humidity : device [ 0 ] . newest_events . hu ?. val , illumination : device [ 0 ] . newest_events . il ?. val , temperature : device [ 0 ] . newest_events . te . val , } as NatureRemo . ISensorValue ; } /**
     * Fetch the list of appliances.
     */ public async getAppliances ( type ? ) { const response = await this . _get < NatureRemo . IAppliance [ ] > ( '/1/appliances' ) ; if ( type ) { return response . filter ( ( appliance ) => appliance . type === type ) ; } else { return response ; } } /**
     * Create a new appliance.
     */ public async createAppliance ( nickname , device , image , model ? ) { const response = await this . _post < NatureRemo . IAppliance [ ] > ( '/1/appliances' , { device , image , model , nickname , } ) ; return response ; } param appliances public async updateAppliancesOrder ( appliances ) { const response = await this . _post ( '/1/appliance_orders' , { appliances : appliances . join ( ',' ) , } ) ; return response ; } /**
     * Delete appliance.
     */ public async deleteAppliance ( applianceId ) { const response = await this . _post ( `/1/appliances/${ applianceId }/delete` ) ; return response ; } /**
     * Update appliance.
     */ public async updateAppliance ( applianceId , nickname , imageId ) { const response = await this . _post < NatureRemo . IAppliance > ( `/1/appliances/${ applianceId }` , { image : imageId , nickname , } ) ; return response ; } /**
     * get all appliances which has AC characteristics
     */ public async listAircon ( ) { return await this . getAppliances ( 'AC' ) ; } /**
     * Update air conditioner settings.
     */ public async updateAirconSettings ( applianceId , settings ) { const response = await this . _post < NatureRemo . IUpdateAirconSettingsResponse > ( `/1/appliances/${ applianceId }/aircon_settings` , settings ) ; return response ; } /**
     * get all appliances which has TV characteristics
     */ public async listTV ( ) { return await this . getAppliances ( 'TV' ) ; } /**
     * Update TV button.
     */ public async updateTV ( applianceId , button ) { const response = await this . _post < NatureRemo . ITVState > ( `/1/appliances/${ applianceId }/tv` , { button } ) ; return response ; } /**
     * get all appliances which has LIGHT characteristics
     */ public async listLight ( ) { return await this . getAppliances ( 'LIGHT' ) ; } /**
     * Update Light button.
     */ public async updateLight ( applianceId , button ) { const response = this . _post < NatureRemo . ILightState > ( `/1/appliances/${ applianceId }/light` , { button } ) ; return response ; } /**
     * Fetch signals registered under this appliance.
     */ public async getApplianceSignals ( applianceId ) { const response = await this . _get < NatureRemo . ISignal [ ] > ( `/1/appliances/${ applianceId }/signals` ) ; return response ; } /**
     * Create a signal under this appliance.
     */ public async createApplianceSignal ( applianceId , name , message , imageId ) { const response = await this . _post < NatureRemo . ISignal > ( `/1/appliances/${ applianceId }/signals` , { appliance : applianceId , image : imageId , message : JSON . stringify ( message ) , name , } ) ; return response ; } /**
     * Reorder signals under this appliance.
     */ public async updateSignalOrders ( applianceId , signalIds ) { const response = await this . _post ( `/1/appliances/${ applianceId }/signal_orders` , { signals : signalIds . join ( ',' ) , } ) ; return response ; } /**
     * Update infrared signal.
     */ public async updateSignal ( signalId , name , imageId ) { const response = await this . _post ( `/1/signals/${ signalId }` , { image : imageId , name , } ) ; return response ; } /**
     * Delete an infrared signal.
     */ public async deleteSignal ( signalId ) { const response = await this . _post ( `/1/signals/${ signalId }/delete` ) ; return response ; } /**
     * Send infrared signal.
     */ public async sendSignal ( signalId ) { const response = await this . _post ( `/1/signals/${ signalId }/send` ) ; return response ; } /**
     * Update Remo
     */ public async updateDevice ( deviceId , name ) { const response = await this . _post ( `/1/devices/${ deviceId }` , { name , } ) ; return response ; } /**
     * Delete Remo
     */ public async deleteDevice ( deviceId ) { const response = await this . _post ( `/1/devices/${ deviceId }/delete` ) ; return response ; } /**
     * Update temperature offset.
     */ public async updateTemperatureOffset ( deviceId , offset ) { const response = await this . _post ( `/1/devices/${ deviceId }/temperature_offset` , { offset , } ) ; return response ; } /**
     * Update humidity offset.
     */ public async updateHumidityOffset ( deviceId , offset ) { const response = await this . _post ( `/1/devices/${ deviceId }/humidity_offset` , { offset , } ) ; return response ; } private async _post < T > ( path , body ? ) { try { const response = await this . instance . post ( path , stringify ( body ) ) ; return response . data ; } catch ( error ) { const errResponse = ( error as AxiosError ) . response ; if ( errResponse ) { throw new Error ( `${ errResponse . statusText } (${ errResponse . status })` ) ; } throw error ; } } private async _get < T > ( path ) { try { const response = await this . instance . get ( path ) ; return response . data ; } catch ( error ) { const errResponse = ( error as AxiosError ) . response ; if ( errResponse ) { throw new Error ( `${ errResponse . statusText } (${ errResponse . status })` ) ; } throw error ; } } }