Skip to content

Commit

Permalink
feat: expose getParsedTelegram to polling
Browse files Browse the repository at this point in the history
  • Loading branch information
jvandenaardweg committed Dec 21, 2022
1 parent 41fddde commit 3ae639f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/energy-socket-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export class EnergySocketApi extends BaseApi {
getData<T extends EnergySocketDataResponse>(): Promise<T> {
return super.getData();
}
startPolling(
protected startPolling(
method: 'getData',
apiMethod: <T extends EnergySocketDataResponse>() => Promise<T>,
): Promise<void>;
startPolling(
protected startPolling(
method: 'getState',
apiMethod: <T extends StateResponse>() => Promise<T>,
): Promise<void>;
startPolling(method: string, apiMethod: () => Promise<unknown>): Promise<void> {
protected startPolling(method: string, apiMethod: () => Promise<unknown>): Promise<void> {
return super.startPolling(method, apiMethod);
}

Expand Down
2 changes: 1 addition & 1 deletion src/kwh-meter-1-phase-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class KwhMeter1PhaseApi extends BaseApi {
getData<T extends KwhMeter1PhaseDataResponse>(): Promise<T> {
return super.getData();
}
startPolling(
protected startPolling(
method: 'getData',
apiMethod: <T extends KwhMeter1PhaseDataResponse>() => Promise<T>,
): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/kwh-meter-3-phase-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class KwhMeter3PhaseApi extends BaseApi {
getData<T extends KwhMeter3PhaseDataResponse>(): Promise<T> {
return super.getData();
}
startPolling(
protected startPolling(
method: 'getData',
apiMethod: <T extends KwhMeter3PhaseDataResponse>() => Promise<T>,
): Promise<void> {
Expand Down
29 changes: 20 additions & 9 deletions src/p1-meter-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { BaseApi, BaseApiOptions, BasePolling, PollMethod } from '@/base-api';
import { BasicInformationResponse, P1MeterDataResponse, TelegramResponse } from '@/types';
import { ParsedTelegram, parseTelegram } from './utils/telegram';

export interface P1MeterPolling<TTelegramResponse extends TelegramResponse>
extends BasePolling<P1MeterDataResponse> {
export interface P1MeterPolling<
TTelegramResponse extends TelegramResponse,
TParsedTelegram extends ParsedTelegram,
> extends BasePolling<P1MeterDataResponse> {
getTelegram: PollMethod<TTelegramResponse>;
getParsedTelegram: PollMethod<TParsedTelegram>;
}

export class P1MeterApi extends BaseApi {
Expand All @@ -14,15 +17,19 @@ export class P1MeterApi extends BaseApi {
getData<T extends P1MeterDataResponse>(): Promise<T> {
return super.getData();
}
startPolling(
protected startPolling(
method: 'getData',
apiMethod: <T extends P1MeterDataResponse>() => Promise<T>,
): Promise<void>;
startPolling(
protected startPolling(
method: 'getTelegram',
apiMethod: <T extends TelegramResponse>() => Promise<T>,
): Promise<void>;
startPolling(method: string, apiMethod: () => Promise<unknown>): Promise<void> {
protected startPolling(
method: 'getParsedTelegram',
apiMethod: <T extends TelegramResponse>() => Promise<T>,
): Promise<void>;
protected startPolling(method: string, apiMethod: () => Promise<unknown>): Promise<void> {
return super.startPolling(method, apiMethod);
}

Expand All @@ -45,12 +52,11 @@ export class P1MeterApi extends BaseApi {
* To start polling, call the `start()` method on the returned object.
* The polling can be stopped by calling the `stop()` method.
*
* Supported methods: `getData`, `getTelegram`
* Supported methods: `getData`, `getTelegram` and `getParsedTelegram`
*/
get polling(): P1MeterPolling<TelegramResponse> {
get polling(): P1MeterPolling<TelegramResponse, ParsedTelegram> {
const getTelegram = 'getTelegram';

// TODO: add getParsedTelegram
const getParsedTelegram = 'getParsedTelegram';

return {
...super.polling,
Expand All @@ -59,6 +65,11 @@ export class P1MeterApi extends BaseApi {
stop: () => super.stopPolling(getTelegram),
on: super.on.bind(this),
},
[getParsedTelegram]: {
start: () => super.startPolling(getParsedTelegram, this.getParsedTelegram.bind(this)),
stop: () => super.stopPolling(getParsedTelegram),
on: super.on.bind(this),
},
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/water-meter-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class WaterMeterApi extends BaseApi {
getData<T extends WaterMeterDataResponse>(): Promise<T> {
return super.getData();
}
startPolling(
protected startPolling(
method: 'getData',
apiMethod: <T extends WaterMeterDataResponse>() => Promise<T>,
): Promise<void> {
Expand Down

0 comments on commit 3ae639f

Please sign in to comment.