Skip to content
This repository was archived by the owner on Oct 4, 2022. It is now read-only.

Commit 4eac459

Browse files
author
Takeshi Kishi
authored
improve log and axios (#24)
1 parent 3482bdf commit 4eac459

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/natureRemoApi.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { URLSearchParams } from 'url';
2-
import axios, { Axios, AxiosError } from 'axios';
32
import { Mutex } from 'async-mutex';
3+
import axios, { AxiosError } from 'axios';
44
import { API, HapStatusError, Logger } from 'homebridge';
55
import { ACButton, AirConParams, Appliance, Device, LIGHTState, OperationMode } from './types';
66
import { API_URL, CACHE_THRESHOLD } from './settings';
@@ -22,7 +22,6 @@ type AirConSettings = { button?: ACButton; operation_mode?: OperationMode; tempe
2222
export class NatureRemoApi {
2323

2424
private readonly mutex = new Mutex();
25-
private readonly client: Axios;
2625

2726
private applianceCache: ApplianceCache = { updated: 0, appliances: null };
2827
private deviceCache: DeviceCache = { updated: 0, devices: null };
@@ -31,10 +30,9 @@ export class NatureRemoApi {
3130
private readonly logger: Logger,
3231
private readonly api: API,
3332
accessToken: string) {
34-
this.client = axios.create({
35-
baseURL: API_URL,
36-
headers: { 'Authorization': `Bearer ${accessToken}` },
37-
});
33+
axios.defaults.baseURL = API_URL;
34+
axios.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
35+
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
3836
}
3937

4038
async getAllAppliances(): Promise<Appliance[]> {
@@ -115,7 +113,7 @@ export class NatureRemoApi {
115113

116114
private async getMessage(url: string): Promise<Appliance[] | Device[]> {
117115
try {
118-
const res = await this.client.get(url);
116+
const res = await axios.get(url);
119117
return res.data;
120118
} catch (err) {
121119
throw this.convertToHapStatusError(err as AxiosError);
@@ -125,7 +123,7 @@ export class NatureRemoApi {
125123
private async postMessage(url: string, params: Record<string, string>): Promise<void> {
126124
try {
127125
const data = new URLSearchParams(params);
128-
await this.client.post(url, data.toString());
126+
await axios.post(url, data.toString());
129127
} catch (err) {
130128
throw this.convertToHapStatusError(err as AxiosError);
131129
}

src/tvAccessory.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class NatureNemoTvAccessory {
5959

6060
async getActive(): Promise<CharacteristicValue> {
6161
this.platform.logger.debug('getActive called');
62+
this.platform.logger.info('[%s] Active -> %s', this.name, this.state.active === this.platform.Characteristic.Active.ACTIVE);
6263
return this.state.active;
6364
}
6465

@@ -72,12 +73,13 @@ export class NatureNemoTvAccessory {
7273
return;
7374
}
7475
await this.platform.natureRemoApi.setTvButton(this.id, 'power');
75-
this.platform.logger.info('[%s] Active <- %s', this.name, value);
76+
this.platform.logger.info('[%s] Active <- %s', this.name, value === this.platform.Characteristic.Active.ACTIVE);
7677
this.state.active = value;
7778
}
7879

7980
async getActiveIdentifier(): Promise<CharacteristicValue> {
80-
this.platform.logger.debug('getAgetActiveIdentifierctive called');
81+
this.platform.logger.debug('getActiveIdentifier called');
82+
this.platform.logger.info('[%s] ActiveIdentifierctive -> %s', this.name, this.state.activeIdentifier);
8183
return this.state.activeIdentifier;
8284
}
8385

@@ -90,17 +92,20 @@ export class NatureNemoTvAccessory {
9092
this.platform.logger.debug('[%s] Same state. skip sending', this.name);
9193
return;
9294
}
95+
this.platform.logger.info('[%s] ActiveIdentifierctive <- %s', this.name, value);
9396
this.state.activeIdentifier = value;
9497
}
9598

9699
async setRemoteKey(value: CharacteristicValue): Promise<void> {
97100
this.platform.logger.debug('setRemoteKey called ->', value);
98-
await this.platform.natureRemoApi.setTvButton(this.id, this.convertRemoteKey(value));
99-
this.platform.logger.info('[%s] Remote Key <- %s', this.name, value);
101+
const key = this.convertRemoteKey(value);
102+
await this.platform.natureRemoApi.setTvButton(this.id, key);
103+
this.platform.logger.info('[%s] Remote Key <- %s', this.name, key);
100104
}
101105

102106
async getMute(): Promise<CharacteristicValue> {
103107
this.platform.logger.debug('getMute called');
108+
this.platform.logger.info('[%s] Mute -> %s', this.name, this.state.mute);
104109
return this.state.mute;
105110
}
106111

@@ -120,8 +125,9 @@ export class NatureNemoTvAccessory {
120125

121126
async setVolumeSelector(value: CharacteristicValue): Promise<void> {
122127
this.platform.logger.debug('setVolumeSelector called ->', value);
123-
await this.platform.natureRemoApi.setTvButton(this.id, this.convertVolumeSelector(value));
124-
this.platform.logger.info('[%s] VolumeSelector <- %s', this.name, value);
128+
const key = this.convertVolumeSelector(value);
129+
await this.platform.natureRemoApi.setTvButton(this.id, key);
130+
this.platform.logger.info('[%s] VolumeSelector <- %s', this.name, key);
125131
}
126132

127133
private convertRemoteKey(value: CharacteristicValue): string {

0 commit comments

Comments
 (0)