Skip to content

Commit a40be9b

Browse files
committed
Decide REST or state
1 parent 4a6a5e8 commit a40be9b

2 files changed

Lines changed: 134 additions & 84 deletions

File tree

‎dist/Predbat-Table-Card-Next.js‎

Lines changed: 63 additions & 43 deletions
Large diffs are not rendered by default.

‎src/services/predbat-service.ts‎

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,49 @@ import { RawData, PredbatRawDataSchema } from '../schemas/predbat';
22

33
export class PredbatService {
44
private _dataCallback: ((validatedHistoricPlanData: RawData, validatedPlanData: RawData) => void) | null;
5-
private _port: number;
6-
private _updateInterval: ReturnType<typeof setInterval> | null = null;
7-
private _retryTimeout: ReturnType<typeof setTimeout> | null = null;
8-
private _isAvailable = false;
9-
private _fetchInProgress = false;
5+
private _portRestApi: number;
6+
private _updateIntervalRestApi: ReturnType<typeof setInterval> | null = null;
7+
private _retryTimeoutRestApi: ReturnType<typeof setTimeout> | null = null;
8+
private _isAvailableRestApi = false;
9+
private _fetchInProgressRestApi = false;
10+
private _dataSource: 'REST_API' | 'HASS_STATES' | null = null;
1011

1112
public constructor(port: number, dataCallback: (validatedHistoricPlanData: RawData, validatedPlanData: RawData) => void) {
12-
this._port = port;
13+
this._portRestApi = port;
1314
this._dataCallback = dataCallback;
14-
this._startPolling();
15+
16+
this._getDataSource(port)
17+
.then((dataSource) => {
18+
this._dataSource = dataSource;
19+
20+
if (this._dataSource === 'REST_API') this._startPollingRestApi();
21+
})
22+
.catch((error) => {
23+
throw error;
24+
});
1525
}
1626

17-
private _startPolling(): void {
18-
this._fetchData();
27+
private _startPollingRestApi(): void {
28+
this._fetchDataRestApi();
1929

20-
this._retryTimeout = setInterval(() => {
21-
if (!this._isAvailable && !this._fetchInProgress) {
22-
this._fetchData();
30+
this._retryTimeoutRestApi = setInterval(() => {
31+
if (!this._isAvailableRestApi && !this._fetchInProgressRestApi) {
32+
this._fetchDataRestApi();
2333
}
24-
else if (this._isAvailable) {
25-
if (this._retryTimeout) {
26-
clearInterval(this._retryTimeout);
27-
this._retryTimeout = null;
34+
else if (this._isAvailableRestApi) {
35+
if (this._retryTimeoutRestApi) {
36+
clearInterval(this._retryTimeoutRestApi);
37+
this._retryTimeoutRestApi = null;
2838
}
2939
}
3040
}, 1000);
3141
}
3242

33-
private _fetchData(): void {
34-
if (this._fetchInProgress) return;
35-
this._fetchInProgress = true;
43+
private _fetchDataRestApi(): void {
44+
if (this._fetchInProgressRestApi) return;
45+
this._fetchInProgressRestApi = true;
3646

37-
this._getPlanData(this._port)
47+
this._getPlanDataRestApi(this._portRestApi)
3848
.then((planData) => {
3949
if (planData?.yesterday === undefined) {
4050
console.warn('No data received from Predbat API');
@@ -54,32 +64,32 @@ export class PredbatService {
5464
console.error(error);
5565
}
5666

57-
if (!this._isAvailable) {
67+
if (!this._isAvailableRestApi) {
5868
console.info('Successfully fetched Predbat API data');
5969

60-
this._isAvailable = true;
61-
this._scheduleHalfHourUpdate(5);
70+
this._isAvailableRestApi = true;
71+
this._scheduleHalfHourUpdateRestApi(5);
6272
}
6373
})
6474
.catch((error) => {
65-
if (this._isAvailable) {
75+
if (this._isAvailableRestApi) {
6676
console.warn('Predbat API request failed, will retry...', error.message);
6777

68-
this._isAvailable = false;
69-
this._startPolling();
78+
this._isAvailableRestApi = false;
79+
this._startPollingRestApi();
7080
}
7181
else {
7282
console.error('Predbat is unreachable');
7383
}
7484
})
7585
.finally(() => {
76-
this._fetchInProgress = false;
86+
this._fetchInProgressRestApi = false;
7787
});
7888
}
7989

80-
private _scheduleHalfHourUpdate(updateIntervalMinutes: number): void {
81-
if (this._updateInterval) {
82-
clearInterval(this._updateInterval);
90+
private _scheduleHalfHourUpdateRestApi(updateIntervalMinutes: number): void {
91+
if (this._updateIntervalRestApi) {
92+
clearInterval(this._updateIntervalRestApi);
8393
}
8494

8595
const now = new Date();
@@ -92,15 +102,15 @@ export class PredbatService {
92102
const delay = nextUpdate.getTime() - now.getTime() + 5000;
93103

94104
setTimeout(() => {
95-
this._fetchData();
96-
this._updateInterval = setInterval(() => {
97-
this._fetchData();
105+
this._fetchDataRestApi();
106+
this._updateIntervalRestApi = setInterval(() => {
107+
this._fetchDataRestApi();
98108
}, updateIntervalMinutes * 60 * 1000);
99109
}, delay);
100110
}
101111

102112
// eslint-disable-next-line @typescript-eslint/no-explicit-any
103-
private _getPlanData(port: number): Promise<any> {
113+
private _getPlanDataRestApi(port: number): Promise<any> {
104114
return fetch(`http://${window.location.hostname}:${port}/api/plan_data`)
105115
.then((response) => response.json())
106116
.then((data) => data)
@@ -109,15 +119,35 @@ export class PredbatService {
109119
});
110120
}
111121

122+
private _getDataSource(port: number): Promise<'REST_API' | 'HASS_STATES'> {
123+
return fetch(`http://${window.location.hostname}:${port}/api/plan_data`)
124+
.then((response): 'REST_API' | 'HASS_STATES' => {
125+
console.info('Using REST API for Predbat data');
126+
127+
if (response.ok) return 'REST_API';
128+
129+
console.info('Using HASS state for Predbat data (response error)');
130+
131+
return 'HASS_STATES';
132+
})
133+
.catch((): 'HASS_STATES' => {
134+
console.info('Using HASS state for Predbat data (fetch error)');
135+
136+
return 'HASS_STATES';
137+
});
138+
}
139+
112140
public disconnect(): void {
113-
if (this._retryTimeout) {
114-
clearInterval(this._retryTimeout);
115-
this._retryTimeout = null;
141+
if (this._retryTimeoutRestApi) {
142+
clearInterval(this._retryTimeoutRestApi);
143+
this._retryTimeoutRestApi = null;
116144
}
117-
if (this._updateInterval) {
118-
clearInterval(this._updateInterval);
119-
this._updateInterval = null;
145+
146+
if (this._updateIntervalRestApi) {
147+
clearInterval(this._updateIntervalRestApi);
148+
this._updateIntervalRestApi = null;
120149
}
121-
this._isAvailable = false;
150+
151+
this._isAvailableRestApi = false;
122152
}
123153
}

0 commit comments

Comments
 (0)