diff --git a/src/dingzAccessory.ts b/src/dingzAccessory.ts index 9fe1353..9f73e4a 100644 --- a/src/dingzAccessory.ts +++ b/src/dingzAccessory.ts @@ -21,9 +21,11 @@ import { DimmerState, DimmerTimer, DingzDeviceInfo, - DingzDimmerConfig, + DingzDevices, + DingzDeviceSystemConfig, + DingzDeviceDimmerConfig, + DingzDeviceInputConfig, DingzDimmerConfigValue, - DingzInputConfig, DingzInputInfoItem, DingzLEDState, DingzMotionData, @@ -152,8 +154,11 @@ export class DingzDaAccessory extends EventEmitter { ); // FIXME: Is there a better way to handle errors? - this.getConfigs() - .then(([inputConfig, dimmerConfig]) => { + DingzDaAccessory.getConfigs({ + address: this.device.address, + token: this.device.token, + }) + .then(({ dingzDevices, systemConfig, inputConfig, dimmerConfig }) => { if ( inputConfig?.inputs && dimmerConfig?.dimmers && @@ -402,7 +407,7 @@ export class DingzDaAccessory extends EventEmitter { const inputConfig: DingzInputInfoItem[] | undefined = this.device .dingzInputInfo; - const dimmerConfig: DingzDimmerConfig | undefined = this.device + const dimmerConfig: DingzDeviceDimmerConfig | undefined = this.device .dimmerConfig; /** DIP Switch @@ -1151,7 +1156,11 @@ export class DingzDaAccessory extends EventEmitter { '-> Check for changed config.', ); - this.getConfigs().then(([inputConfig, dimmerConfig]) => { + // FIXME: [FIX] refactor dingz.updateAccessory #103 + DingzDaAccessory.getConfigs({ + address: this.device.address, + token: this.device.token, + }).then(({ inputConfig, dimmerConfig }) => { if (inputConfig?.inputs[0]) { this._updatedDeviceInputConfig = inputConfig.inputs[0]; } @@ -1211,7 +1220,7 @@ export class DingzDaAccessory extends EventEmitter { updatedDingzDeviceInfo.dip_config === 3) ) { // Only add Dimmer 0 if we're not in "WindowCover" mode - const dimmerConfig: DingzDimmerConfig | undefined = this.device + const dimmerConfig: DingzDeviceDimmerConfig | undefined = this.device .dimmerConfig; this.platform.log.warn( @@ -1436,12 +1445,13 @@ export class DingzDaAccessory extends EventEmitter { * TODO: Refactor duplicate code into proper API caller */ private async getDingzDeviceInfo(): Promise { - const [dingzDevices] = await this.platform.getDingzDeviceInfo({ + const dingzDevices = await DingzDaAccessory.getConfigs({ address: this.device.address, token: this.device.token, }); try { - const dingzDeviceInfo: DingzDeviceInfo = dingzDevices[this.device.mac]; + const dingzDeviceInfo: DingzDeviceInfo = + dingzDevices.dingzDevices[this.device.mac]; if (dingzDeviceInfo) { return dingzDeviceInfo; } @@ -1452,29 +1462,68 @@ export class DingzDaAccessory extends EventEmitter { } // Get Input & Dimmer Config - private async getConfigs(): Promise<[DingzInputConfig, DingzDimmerConfig]> { - const getInputConfigUrl = `${this.baseUrl}/api/v1/input_config`; - const getDimmerConfigUrl = `${this.baseUrl}/api/v1/dimmer_config`; - - return Promise.all([ - this.platform.fetch({ - url: getInputConfigUrl, + public static async getConfigs({ + address, + token, + }: { + address: string; + token?: string; + }): Promise<{ + dingzDevices: DingzDevices; + systemConfig: DingzDeviceSystemConfig; + inputConfig: DingzDeviceInputConfig; + dimmerConfig: DingzDeviceDimmerConfig; + }> { + const deviceInfoUrl = `http://${address}/api/v1/device`; + const deviceConfigUrl = `http://${address}/api/v1/system_config`; + const inputConfigUrl = `http://${address}/api/v1/input_config`; + const dimmerConfigUrl = `http://${address}/api/v1/dimmer_config`; + + const [ + dingzDevices, + systemConfig, + inputConfig, + dimmerConfig, + ] = await Promise.all< + DingzDevices, + DingzDeviceSystemConfig, + DingzDeviceInputConfig, + DingzDeviceDimmerConfig + >([ + DingzDaHomebridgePlatform.fetch({ + url: deviceInfoUrl, returnBody: true, - token: this.device.token, + token: token, }), - this.platform.fetch({ - url: getDimmerConfigUrl, + DingzDaHomebridgePlatform.fetch({ + url: deviceConfigUrl, returnBody: true, - token: this.device.token, + token: token, + }), + DingzDaHomebridgePlatform.fetch({ + url: inputConfigUrl, + returnBody: true, + token: token, + }), + DingzDaHomebridgePlatform.fetch({ + url: dimmerConfigUrl, + returnBody: true, + token: token, }), ]); + return { + dingzDevices: dingzDevices, + systemConfig: systemConfig, + inputConfig: inputConfig, + dimmerConfig: dimmerConfig, + }; } private async getDeviceMotion(): Promise { const getMotionUrl = `${this.baseUrl}/api/v1/motion`; const release = await this.mutex.acquire(); try { - return await this.platform.fetch({ + return await DingzDaHomebridgePlatform.fetch({ url: getMotionUrl, returnBody: true, token: this.device.token, @@ -1494,7 +1543,7 @@ export class DingzDaAccessory extends EventEmitter { const setDimmerUrl = `${this.baseUrl}/api/v1/dimmer/${index}/${ isOn ? 'on' : 'off' }/${level ? '?value=' + level : ''}`; - await this.platform.fetch({ + await DingzDaHomebridgePlatform.fetch({ url: setDimmerUrl, method: 'POST', token: this.device.token, @@ -1514,7 +1563,7 @@ export class DingzDaAccessory extends EventEmitter { // The API says the parameters can be omitted. This is not true // {{ip}}/api/v1/shade/0?blind=&lamella= const setWindowCoveringUrl = `${this.baseUrl}/api/v1/shade/${id}`; - await this.platform.fetch({ + await DingzDaHomebridgePlatform.fetch({ url: setWindowCoveringUrl, method: 'POST', token: this.device.token, @@ -1535,7 +1584,7 @@ export class DingzDaAccessory extends EventEmitter { const getWindowCoveringUrl = `${this.baseUrl}/api/v1/shade/${id}`; const release = await this.mutex.acquire(); try { - return await this.platform.fetch({ + return await DingzDaHomebridgePlatform.fetch({ url: getWindowCoveringUrl, returnBody: true, token: this.device.token, @@ -1554,7 +1603,7 @@ export class DingzDaAccessory extends EventEmitter { color: string; }): Promise { const setLEDUrl = `${this.baseUrl}/api/v1/led/set`; - await this.platform.fetch({ + await DingzDaHomebridgePlatform.fetch({ url: setLEDUrl, method: 'POST', token: this.device.token, @@ -1577,7 +1626,7 @@ export class DingzDaAccessory extends EventEmitter { const getDeviceStateUrl = `${this.baseUrl}/api/v1/state`; const release = await this.mutex.acquire(); try { - return await this.platform.fetch({ + return await DingzDaHomebridgePlatform.fetch({ url: getDeviceStateUrl, returnBody: true, token: this.device.token, @@ -1591,7 +1640,7 @@ export class DingzDaAccessory extends EventEmitter { public async getButtonCallbackUrl(): Promise { const getCallbackUrl = `${this.baseUrl}/api/v1/action/generic/generic`; this.platform.log.debug('Getting the callback URL -> ', getCallbackUrl); - return await this.platform.fetch({ + return await DingzDaHomebridgePlatform.fetch({ url: getCallbackUrl, method: 'GET', token: this.device.token, @@ -1602,7 +1651,7 @@ export class DingzDaAccessory extends EventEmitter { async enablePIRCallback() { const setActionUrl = `${this.baseUrl}/api/v1/action/pir/press_release/enable`; this.platform.log.debug('Enabling the PIR callback -> '); - await this.platform.fetch({ + await DingzDaHomebridgePlatform.fetch({ url: setActionUrl, method: 'POST', token: this.device.token, diff --git a/src/myStromLightbulbAccessory.ts b/src/myStromLightbulbAccessory.ts index 3e399cc..fd014c1 100644 --- a/src/myStromLightbulbAccessory.ts +++ b/src/myStromLightbulbAccessory.ts @@ -224,7 +224,7 @@ export class MyStromLightbulbAccessory { }): Promise { // Weird URL :-) const setDimmerUrl = `${this.baseUrl}/api/v1/device/${this.device.mac}`; - await this.platform.fetch({ + await DingzDaHomebridgePlatform.fetch({ url: setDimmerUrl, method: 'POST', token: this.device.token, @@ -241,14 +241,12 @@ export class MyStromLightbulbAccessory { private async getDeviceReport(mac: string): Promise { const reportUrl = `${this.baseUrl}/api/v1/device/`; - return await this.platform - .fetch({ - url: reportUrl, - returnBody: true, - token: this.device.token, - }) - .then((response) => { - return response[mac]; - }); + return await DingzDaHomebridgePlatform.fetch({ + url: reportUrl, + returnBody: true, + token: this.device.token, + }).then((response) => { + return response[mac]; + }); } } diff --git a/src/myStromPIRAccessory.ts b/src/myStromPIRAccessory.ts index b760ecf..642dbfa 100644 --- a/src/myStromPIRAccessory.ts +++ b/src/myStromPIRAccessory.ts @@ -265,7 +265,7 @@ export class MyStromPIRAccessory { const getSensorsUrl = `${this.baseUrl}/api/v1/sensors`; const release = await this.mutex.acquire(); try { - return await this.platform.fetch({ + return await DingzDaHomebridgePlatform.fetch({ url: getSensorsUrl, returnBody: true, token: this.device.token, diff --git a/src/myStromSwitchAccessory.ts b/src/myStromSwitchAccessory.ts index d9186fa..858e90c 100644 --- a/src/myStromSwitchAccessory.ts +++ b/src/myStromSwitchAccessory.ts @@ -188,7 +188,7 @@ export class MyStromSwitchAccessory { private setDeviceState(isOn: boolean) { const relayUrl = `${this.baseUrl}/relay?state=${isOn ? '1' : '0'}`; - this.platform.fetch({ + DingzDaHomebridgePlatform.fetch({ url: relayUrl, token: this.device.token, }); @@ -196,7 +196,7 @@ export class MyStromSwitchAccessory { private async getDeviceReport(): Promise { const reportUrl = `${this.baseUrl}/report`; - return await this.platform.fetch({ + return await DingzDaHomebridgePlatform.fetch({ url: reportUrl, returnBody: true, token: this.device.token, diff --git a/src/platform.ts b/src/platform.ts index 796d57a..6a81933 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -15,12 +15,7 @@ import e = require('express'); import * as os from 'os'; // Internal Types -import { - ButtonId, - DingzDevices, - DingzDeviceInfo, - DingzDeviceSystemConfig, -} from './util/dingzTypes'; +import { ButtonId, DingzDeviceInfo } from './util/dingzTypes'; import { MyStromDeviceInfo, MyStromSwitchTypes } from './util/myStromTypes'; import { @@ -247,8 +242,8 @@ export class DingzDaHomebridgePlatform implements DynamicPlatformPlugin { // Run a diacovery of changed things every 10 seconds this.log.debug(`Add configured device -> ${name} (${address})`); - const success = this.getDingzDeviceInfo({ address, token }).then( - ([dingzDevices, dingzConfig]) => { + const success = DingzDaAccessory.getConfigs({ address, token }).then( + ({ dingzDevices, systemConfig: dingzConfig }) => { this.log.debug('Got Device ->', JSON.stringify(dingzDevices)); if (typeof dingzDevices !== 'undefined') { const keys = Object.keys(dingzDevices); @@ -869,7 +864,7 @@ export class DingzDaHomebridgePlatform implements DynamicPlatformPlugin { this.log.debug('Setting the callback URL -> ', callbackUrl); endpoints.forEach((endpoint) => { this.log.debug(setActionUrl, 'Endpoint -> ', endpoint); - this.fetch({ + DingzDaHomebridgePlatform.fetch({ url: `${setActionUrl}${endpoint}`, method: 'POST', token: token, @@ -943,34 +938,6 @@ export class DingzDaHomebridgePlatform implements DynamicPlatformPlugin { return `post://${hostname}:${port}/button`; } - /** - * Device Methods -- these are used to retrieve the data from the dingz - * TODO: Refactor duplicate code into proper API caller - */ - async getDingzDeviceInfo({ - address, - token, - }: { - address: string; - token?: string; - }): Promise<[DingzDevices, DingzDeviceSystemConfig]> { - const deviceInfoUrl = `http://${address}/api/v1/device`; - const deviceConfigUrl = `http://${address}/api/v1/system_config`; - - return Promise.all([ - this.fetch({ - url: deviceInfoUrl, - returnBody: true, - token, - }), - this.fetch({ - url: deviceConfigUrl, - returnBody: true, - token, - }), - ]); - } - /** * Device Methods -- these are used to retrieve the data from the dingz * FIXME: API Endpoint @@ -987,14 +954,14 @@ export class DingzDaHomebridgePlatform implements DynamicPlatformPlugin { endpoint?: 'api/v1/info' | 'info'; }): Promise { const deviceInfoUrl = `http://${address}/${endpoint}`; - return await this.fetch({ + return await DingzDaHomebridgePlatform.fetch({ url: deviceInfoUrl, returnBody: true, token, }); } - async fetch({ + static async fetch({ url, method = 'get', returnBody = false, @@ -1016,15 +983,15 @@ export class DingzDaHomebridgePlatform implements DynamicPlatformPlugin { Token: token ?? '', }, data: body, - } as AxiosRequestConfig) - .then((response) => { - if (returnBody) { - return response.data; - } else { - return response.status; - } - }) - .catch(this.handleError.bind(this)); + } as AxiosRequestConfig).then((response) => { + if (returnBody) { + return response.data; + } else { + return response.status; + } + }); + // FIXME: #103 Error handler at the wrong place + // .catch(this.handleError.bind(this)); return data; } diff --git a/src/util/commonTypes.ts b/src/util/commonTypes.ts index a79fc8f..364d05c 100644 --- a/src/util/commonTypes.ts +++ b/src/util/commonTypes.ts @@ -11,7 +11,7 @@ import { MyStromPIRAccessory } from '../myStromPIRAccessory'; import { DingzDeviceInfo, DingzInputInfoItem, - DingzDimmerConfig, + DingzDeviceDimmerConfig, } from './dingzTypes'; import { MyStromDeviceInfo } from './myStromTypes'; @@ -42,7 +42,7 @@ export interface DeviceInfo { model?: string; token?: string; hwInfo?: DingzDeviceInfo | MyStromDeviceInfo; - dimmerConfig?: DingzDimmerConfig; + dimmerConfig?: DingzDeviceDimmerConfig; dingzInputInfo?: DingzInputInfoItem[]; lastUpdate?: Date; accessoryClass?: diff --git a/src/util/dingzTypes.ts b/src/util/dingzTypes.ts index 9af41e8..689f181 100644 --- a/src/util/dingzTypes.ts +++ b/src/util/dingzTypes.ts @@ -14,7 +14,7 @@ export interface DingzLightData { state: 'night' | 'day'; } -export interface DingzInputConfig { +export interface DingzDeviceInputConfig { inputs: DingzInputInfoItem[]; } export interface DingzInputInfoItem { @@ -99,7 +99,7 @@ export type DingzDimmerConfigValue = | 'ohmic' | 'not_connected'; -export interface DingzDimmerConfig { +export interface DingzDeviceDimmerConfig { dimmers: [ { output: DingzDimmerConfigValue;