Skip to content

Commit 61e14fc

Browse files
Merge pull request #442 from OpenDTU-App/423-handle-api-change-from-opendtu-project
2 parents 40ea89b + c65714b commit 61e14fc

File tree

5 files changed

+102
-9
lines changed

5 files changed

+102
-9
lines changed

src/api/opendtuapi.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,8 @@ class OpenDtuApi {
12341234
parsed,
12351235
});
12361236

1237+
console.log('setLimitConfig', 'response', res, parsed, config);
1238+
12371239
return res.status === 200 && parsed.type === 'success';
12381240
}
12391241

src/components/modals/LimitConfigModal.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@ import {
1414
import type { TFunction } from 'i18next';
1515

1616
import type { LimitConfig } from '@/types/opendtu/control';
17-
import { SetStatus } from '@/types/opendtu/control';
17+
import { LegacyLimitType, SetStatus } from '@/types/opendtu/control';
1818

1919
import type { ExtendableModalProps } from '@/components/BaseModal';
2020
import BaseModal from '@/components/BaseModal';
2121
import StyledTextInput from '@/components/styled/StyledTextInput';
2222

23+
import useDtuState from '@/hooks/useDtuState';
2324
import useInverterLimits from '@/hooks/useInverterLimits';
2425

26+
import { convertLimitTypeToCorrectEnum } from '@/utils/legacy';
27+
2528
import { useApi } from '@/api/ApiHandler';
29+
import type { OpenDtuFirmwareVersion } from '@/constants';
2630

2731
export interface LimitConfigModalProps extends ExtendableModalProps {
2832
inverterSerial: string;
2933
}
3034

31-
export type LimitType = 'absolute' | 'relative';
35+
export type LimitConfigType = 'absolute' | 'relative';
3236

3337
const buttonColors: Record<
3438
'permanent' | 'temporary',
@@ -72,7 +76,11 @@ const LimitConfigModal: FC<LimitConfigModalProps> = ({
7276

7377
const limitConfig = useInverterLimits(inverterSerial, state => state);
7478

75-
const [limitType, setLimitType] = useState<LimitType>('absolute');
79+
const currentFirmwareVersion = useDtuState(
80+
state => state?.systemStatus?.git_hash,
81+
);
82+
83+
const [limitType, setLimitType] = useState<LimitConfigType>('absolute');
7684
const [limitValue, setLimitValue] = useState<string>('0');
7785

7886
const api = useApi();
@@ -107,8 +115,21 @@ const LimitConfigModal: FC<LimitConfigModalProps> = ({
107115
return;
108116
}
109117

118+
if (typeof currentFirmwareVersion !== 'string') {
119+
setError(t('limitStatus.error'));
120+
return;
121+
}
122+
110123
const limitConfig: LimitConfig = {
111-
limit_type: (permanent ? 256 : 0) + (limitType === 'relative' ? 1 : 0),
124+
limit_type: convertLimitTypeToCorrectEnum(
125+
(permanent
126+
? LegacyLimitType.PermanentAbsolute
127+
: LegacyLimitType.TemporaryAbsolute) +
128+
(limitType === 'relative'
129+
? LegacyLimitType.TemporaryRelative
130+
: LegacyLimitType.TemporaryAbsolute),
131+
currentFirmwareVersion as OpenDtuFirmwareVersion,
132+
),
112133
limit_value: numberValue,
113134
serial: inverterSerial,
114135
};
@@ -127,6 +148,7 @@ const LimitConfigModal: FC<LimitConfigModalProps> = ({
127148
}
128149
},
129150
[
151+
currentFirmwareVersion,
130152
api,
131153
inverterLimitConfig,
132154
inverterSerial,
@@ -244,7 +266,7 @@ const LimitConfigModal: FC<LimitConfigModalProps> = ({
244266
}}
245267
>
246268
<RadioButton.Group
247-
onValueChange={value => setLimitType(value as LimitType)}
269+
onValueChange={value => setLimitType(value as LimitConfigType)}
248270
value={limitType}
249271
>
250272
<RadioButton.Item

src/constants.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,17 @@ export const spacing = 8;
2929

3030
export const defaultUser = 'admin';
3131

32-
export const minimumOpenDtuFirmwareVersion = 'v23.11.16';
32+
export type OpenDtuFirmwareVersion = `v${number}.${number}.${number}`;
3333

34-
export const maximumTestedOpenDtuFirmwareVersion = 'v24.4.12';
34+
export const minimumOpenDtuFirmwareVersion: OpenDtuFirmwareVersion =
35+
'v23.11.16';
36+
37+
export const maximumTestedOpenDtuFirmwareVersion: OpenDtuFirmwareVersion =
38+
'v24.4.12';
39+
40+
export const featureFlags: Record<string, OpenDtuFirmwareVersion> = {
41+
apiLimitConfigEnumChange: 'v25.9.11', // https://github.com/tbnobody/OpenDTU/commit/8cab3335f348d9ec5221ebf01634c593e3a4213b
42+
};
3543

3644
export const weblateUrl =
3745
'https://weblate.commanderred.xyz/engage/opendtu-react-native/';

src/types/opendtu/control.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,23 @@ export interface LimitStatusItem {
2828

2929
export type LimitStatusData = Record<InverterSerial, LimitStatusItem>;
3030

31-
export enum LimitType {
31+
export enum LegacyLimitType {
3232
TemporaryAbsolute = 0,
3333
TemporaryRelative = 1,
3434
PermanentAbsolute = 256,
3535
PermanentRelative = 257,
3636
}
3737

38+
export enum LimitType {
39+
AbsolutNonPersistent,
40+
RelativNonPersistent,
41+
AbsolutPersistent,
42+
RelativPersistent,
43+
PowerLimitControl_Max,
44+
}
45+
3846
export interface LimitConfig {
3947
serial: InverterSerial;
4048
limit_value: number;
41-
limit_type: LimitType;
49+
limit_type: LimitType | LegacyLimitType; // depends on featureFlags.apiLimitConfigEnumChange
4250
}

src/utils/legacy.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { compare } from 'compare-versions';
2+
3+
import { LegacyLimitType, LimitType } from '@/types/opendtu/control';
4+
5+
import type { OpenDtuFirmwareVersion } from '@/constants';
6+
7+
const legacyToNewlimitTypeMap = {
8+
[LegacyLimitType.TemporaryAbsolute]: LimitType.AbsolutNonPersistent,
9+
[LegacyLimitType.TemporaryRelative]: LimitType.RelativNonPersistent,
10+
[LegacyLimitType.PermanentAbsolute]: LimitType.AbsolutPersistent,
11+
[LegacyLimitType.PermanentRelative]: LimitType.RelativPersistent,
12+
} as const;
13+
14+
const newToLegacyLimitTypeMap = {
15+
[LimitType.AbsolutNonPersistent]: LegacyLimitType.TemporaryAbsolute,
16+
[LimitType.RelativNonPersistent]: LegacyLimitType.TemporaryRelative,
17+
[LimitType.AbsolutPersistent]: LegacyLimitType.PermanentAbsolute,
18+
[LimitType.RelativPersistent]: LegacyLimitType.PermanentRelative,
19+
[LimitType.PowerLimitControl_Max]: LegacyLimitType.TemporaryAbsolute,
20+
} as const;
21+
22+
export const getCorrectLimitType = (
23+
limitTypeFromApi: LimitType | LegacyLimitType,
24+
firmwareVersion: OpenDtuFirmwareVersion,
25+
): LimitType => {
26+
// if firmware version is >= v25.9.11, directly return the limit type from API. if not, map the legacy limit type to the new limit type
27+
if (
28+
compare(firmwareVersion, 'v25.9.11', '>=') // featureFlags.apiLimitConfigEnumChange
29+
) {
30+
return limitTypeFromApi as LimitType;
31+
}
32+
33+
return legacyToNewlimitTypeMap[
34+
limitTypeFromApi as LegacyLimitType
35+
] as LimitType;
36+
};
37+
38+
export const convertLimitTypeToCorrectEnum = (
39+
limitType: LegacyLimitType,
40+
firmwareVersion: OpenDtuFirmwareVersion,
41+
): LimitType | LegacyLimitType => {
42+
// if firmware version is >= v25.9.11, directly return the limit type. if not, map the new limit type to the legacy limit type
43+
44+
if (
45+
compare(firmwareVersion, 'v25.9.11', '>=') // featureFlags.apiLimitConfigEnumChange
46+
) {
47+
return newToLegacyLimitTypeMap[
48+
limitType as unknown as LimitType
49+
] as LegacyLimitType;
50+
}
51+
52+
return limitType;
53+
};

0 commit comments

Comments
 (0)