Skip to content

Commit 8e39264

Browse files
committed
All helper function to refactor scheduled charging computed properties
1 parent 056ed68 commit 8e39264

1 file changed

Lines changed: 86 additions & 114 deletions

File tree

packages/modules/web_themes/koala/source/src/stores/mqtt-store.ts

Lines changed: 86 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,10 +1499,12 @@ export const useMqttStore = defineStore('mqtt', () => {
14991499
const planIndex = plans.findIndex((plan) => plan.id === planId);
15001500
if (planIndex === -1) return;
15011501
const objectPath = `time_charging.plans.${planIndex}.active`;
1502-
updateTopic(`openWB/chargepoint/${chargePointId}/set/charge_template`,
1502+
updateTopic(
1503+
`openWB/chargepoint/${chargePointId}/set/charge_template`,
15031504
newValue,
15041505
objectPath,
1505-
true,);
1506+
true,
1507+
);
15061508
},
15071509
});
15081510
};
@@ -1965,20 +1967,42 @@ export const useMqttStore = defineStore('mqtt', () => {
19651967
return plan?.active;
19661968
},
19671969
set(newValue: boolean) {
1968-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
1969-
const planIndex = plans.findIndex((plan) => plan.id === planId);
1970-
if (planIndex === -1) return;
1971-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.active`;
1972-
updateTopic(
1973-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
1970+
updateScheduledChargingPlanSubtopic(
1971+
chargePointId,
1972+
planId,
1973+
'active',
19741974
newValue,
1975-
objectPath,
1976-
true,
19771975
);
19781976
},
19791977
});
19801978
};
19811979

1980+
/**
1981+
* Helper function to update a subtopic of a scheduled charging plan
1982+
* @param chargePointId charge point id
1983+
* @param planId scheduled plan id
1984+
* @param propertyPath path to the property to update
1985+
* @param newValue new value to set
1986+
* @returns void
1987+
*/
1988+
const updateScheduledChargingPlanSubtopic = <T>(
1989+
chargePointId: number,
1990+
planId: number,
1991+
propertyPath: string,
1992+
newValue: T,
1993+
): void => {
1994+
const plans = vehicleScheduledChargingPlans.value(chargePointId);
1995+
const planIndex = plans.findIndex((plan) => plan.id === planId);
1996+
if (planIndex === -1) return;
1997+
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.${propertyPath}`;
1998+
updateTopic(
1999+
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2000+
newValue,
2001+
objectPath,
2002+
true,
2003+
);
2004+
};
2005+
19822006
/**
19832007
* Get or set the active state {energy tariff} of the scheduled charging plan identified by the scheduled charge plan id
19842008
* @param chargePointId charge point id
@@ -1996,15 +2020,11 @@ export const useMqttStore = defineStore('mqtt', () => {
19962020
return plan?.et_active;
19972021
},
19982022
set(newValue: boolean) {
1999-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2000-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2001-
if (planIndex === -1) return;
2002-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.et_active`;
2003-
updateTopic(
2004-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2023+
updateScheduledChargingPlanSubtopic(
2024+
chargePointId,
2025+
planId,
2026+
'et_active',
20052027
newValue,
2006-
objectPath,
2007-
true,
20082028
);
20092029
},
20102030
});
@@ -2103,15 +2123,11 @@ export const useMqttStore = defineStore('mqtt', () => {
21032123
return plan?.current;
21042124
},
21052125
set(newValue: number) {
2106-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2107-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2108-
if (planIndex === -1) return;
2109-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.current`;
2110-
updateTopic(
2111-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2126+
updateScheduledChargingPlanSubtopic(
2127+
chargePointId,
2128+
planId,
2129+
'current',
21122130
newValue,
2113-
objectPath,
2114-
true,
21152131
);
21162132
},
21172133
});
@@ -2134,15 +2150,11 @@ export const useMqttStore = defineStore('mqtt', () => {
21342150
return plan?.limit.selected;
21352151
},
21362152
set(newValue: string) {
2137-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2138-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2139-
if (planIndex === -1) return;
2140-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.limit.selected`;
2141-
updateTopic(
2142-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2153+
updateScheduledChargingPlanSubtopic(
2154+
chargePointId,
2155+
planId,
2156+
'limit.selected',
21432157
newValue,
2144-
objectPath,
2145-
true,
21462158
);
21472159
},
21482160
});
@@ -2170,15 +2182,11 @@ export const useMqttStore = defineStore('mqtt', () => {
21702182
return valueObject.scaledValue;
21712183
},
21722184
set(newValue: number) {
2173-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2174-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2175-
if (planIndex === -1) return;
2176-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.limit.amount`;
2177-
updateTopic(
2178-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2185+
updateScheduledChargingPlanSubtopic(
2186+
chargePointId,
2187+
planId,
2188+
'limit.amount',
21792189
newValue,
2180-
objectPath,
2181-
true,
21822190
);
21832191
},
21842192
});
@@ -2201,15 +2209,11 @@ export const useMqttStore = defineStore('mqtt', () => {
22012209
return plan?.name;
22022210
},
22032211
set(newValue: string) {
2204-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2205-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2206-
if (planIndex === -1) return;
2207-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.name`;
2208-
updateTopic(
2209-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2212+
updateScheduledChargingPlanSubtopic(
2213+
chargePointId,
2214+
planId,
2215+
'name',
22102216
newValue,
2211-
objectPath,
2212-
true,
22132217
);
22142218
},
22152219
});
@@ -2232,15 +2236,11 @@ export const useMqttStore = defineStore('mqtt', () => {
22322236
return plan?.time;
22332237
},
22342238
set(newValue: string) {
2235-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2236-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2237-
if (planIndex === -1) return;
2238-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.time`;
2239-
updateTopic(
2240-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2239+
updateScheduledChargingPlanSubtopic(
2240+
chargePointId,
2241+
planId,
2242+
'time',
22412243
newValue,
2242-
objectPath,
2243-
true,
22442244
);
22452245
},
22462246
});
@@ -2263,15 +2263,11 @@ export const useMqttStore = defineStore('mqtt', () => {
22632263
return plan?.frequency.selected;
22642264
},
22652265
set(newValue: 'once' | 'daily' | 'weekly') {
2266-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2267-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2268-
if (planIndex === -1) return;
2269-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.frequency.selected`;
2270-
updateTopic(
2271-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2266+
updateScheduledChargingPlanSubtopic(
2267+
chargePointId,
2268+
planId,
2269+
'frequency.selected',
22722270
newValue,
2273-
objectPath,
2274-
true,
22752271
);
22762272
},
22772273
});
@@ -2294,15 +2290,11 @@ export const useMqttStore = defineStore('mqtt', () => {
22942290
return plan?.frequency.once;
22952291
},
22962292
set(newValue: string) {
2297-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2298-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2299-
if (planIndex === -1) return;
2300-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.frequency.once`;
2301-
updateTopic(
2302-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2293+
updateScheduledChargingPlanSubtopic(
2294+
chargePointId,
2295+
planId,
2296+
'frequency.once',
23032297
newValue,
2304-
objectPath,
2305-
true,
23062298
);
23072299
},
23082300
});
@@ -2325,15 +2317,11 @@ export const useMqttStore = defineStore('mqtt', () => {
23252317
return plan?.frequency.weekly;
23262318
},
23272319
set(newValue: boolean[]) {
2328-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2329-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2330-
if (planIndex === -1) return;
2331-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.frequency.weekly`;
2332-
updateTopic(
2333-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2320+
updateScheduledChargingPlanSubtopic(
2321+
chargePointId,
2322+
planId,
2323+
'frequency.weekly',
23342324
newValue,
2335-
objectPath,
2336-
true,
23372325
);
23382326
},
23392327
});
@@ -2356,15 +2344,11 @@ export const useMqttStore = defineStore('mqtt', () => {
23562344
return plan?.limit.soc_limit;
23572345
},
23582346
set(newValue: number) {
2359-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2360-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2361-
if (planIndex === -1) return;
2362-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.limit.soc_limit`;
2363-
updateTopic(
2364-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2347+
updateScheduledChargingPlanSubtopic(
2348+
chargePointId,
2349+
planId,
2350+
'limit.soc_limit',
23652351
newValue,
2366-
objectPath,
2367-
true,
23682352
);
23692353
},
23702354
});
@@ -2387,15 +2371,11 @@ export const useMqttStore = defineStore('mqtt', () => {
23872371
return plan?.limit.soc_scheduled;
23882372
},
23892373
set(newValue: number) {
2390-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2391-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2392-
if (planIndex === -1) return;
2393-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.limit.soc_scheduled`;
2394-
updateTopic(
2395-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2374+
updateScheduledChargingPlanSubtopic(
2375+
chargePointId,
2376+
planId,
2377+
'limit.soc_scheduled',
23962378
newValue,
2397-
objectPath,
2398-
true,
23992379
);
24002380
},
24012381
});
@@ -2417,15 +2397,11 @@ export const useMqttStore = defineStore('mqtt', () => {
24172397
return plan?.phases_to_use;
24182398
},
24192399
set(newValue: number) {
2420-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2421-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2422-
if (planIndex === -1) return;
2423-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.phases_to_use`;
2424-
updateTopic(
2425-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2400+
updateScheduledChargingPlanSubtopic(
2401+
chargePointId,
2402+
planId,
2403+
'phases_to_use',
24262404
newValue,
2427-
objectPath,
2428-
true,
24292405
);
24302406
},
24312407
});
@@ -2447,15 +2423,11 @@ export const useMqttStore = defineStore('mqtt', () => {
24472423
return plan?.phases_to_use_pv;
24482424
},
24492425
set(newValue: number) {
2450-
const plans = vehicleScheduledChargingPlans.value(chargePointId);
2451-
const planIndex = plans.findIndex((plan) => plan.id === planId);
2452-
if (planIndex === -1) return;
2453-
const objectPath = `chargemode.scheduled_charging.plans.${planIndex}.phases_to_use_pv`;
2454-
updateTopic(
2455-
`openWB/chargepoint/${chargePointId}/set/charge_template`,
2426+
updateScheduledChargingPlanSubtopic(
2427+
chargePointId,
2428+
planId,
2429+
'phases_to_use_pv',
24562430
newValue,
2457-
objectPath,
2458-
true,
24592431
);
24602432
},
24612433
});

0 commit comments

Comments
 (0)