-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathPlansSettings.vue
More file actions
341 lines (336 loc) Β· 9.45 KB
/
Copy pathPlansSettings.vue
File metadata and controls
341 lines (336 loc) Β· 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<template>
<div class="mt-4">
<div class="form-group d-lg-flex align-items-baseline justify-content-between">
<div class="container px-0">
<ChargingPlanStaticSettings
:id="`lp${id}-1`"
class="mb-2"
v-bind="staticPlan || {}"
:capacity="capacity"
:range-per-soc="rangePerSoc"
:soc-per-kwh="socPerKwh"
:soc-based-planning="socBasedPlanning"
:multiple-plans="multiplePlans"
@static-plan-updated="updateStaticPlan"
@static-plan-removed="removeStaticPlan"
@plan-preview="previewStaticPlan"
/>
<div v-if="socBasedPlanning">
<div v-if="multiplePlans" class="d-none d-lg-block">
<hr class="mt-5" />
<h5>
<div class="inner mb-3" data-testid="repeating-plan-title">
{{ $t("main.chargingPlan.repeatingPlans") }}
</div>
</h5>
</div>
<ChargingPlansRepeatingSettings
:id="id"
:rangePerSoc="rangePerSoc"
:plans="repeatingPlans"
@updated="updateRepeatingPlans"
/>
</div>
</div>
</div>
<hr />
<h5>
<div class="inner d-flex align-items-center gap-1" data-testid="plan-preview-title">
<span v-if="!multiplePlans">
{{ $t(`main.targetCharge.${noActivePlan ? "preview" : "currentPlan"}`) }}
</span>
<span v-else-if="noActivePlan">{{ $t("main.targetCharge.preview") }} #1</span>
<span v-else-if="alreadyReached">{{ $t("main.targetCharge.goalReached") }}</span>
<span v-else>{{ nextPlanTitle }}</span>
<button
type="button"
class="btn btn-sm"
:class="strategyOpen ? 'btn-secondary' : 'evcc-gray'"
:aria-label="$t('main.chargingPlan.strategySettings')"
tabindex="0"
@click="strategyOpen = !strategyOpen"
>
<shopicon-regular-adjust size="s"></shopicon-regular-adjust>
</button>
</div>
</h5>
<ChargingPlanStrategy
:id="id"
:precondition="effectivePlanStrategy?.precondition"
:continuous="effectivePlanStrategy?.continuous"
:disabled="strategyDisabled"
:show="strategyOpen"
@update="updatePlanStrategy"
/>
<ChargingPlanPreview v-bind="chargingPlanPreviewProps" />
<ChargingPlanWarnings v-bind="chargingPlanWarningsProps" />
</div>
</template>
<script lang="ts">
import "@h2d2/shopicons/es/regular/plus";
import Preview from "./Preview.vue";
import PlanStaticSettings from "./PlanStaticSettings.vue";
import ChargingPlanStrategy from "./PlanStrategy.vue";
import RepeatingSettings from "./PlansRepeatingSettings.vue";
import Warnings from "./Warnings.vue";
import formatter from "@/mixins/formatter";
import collector from "@/mixins/collector";
import api from "@/api";
import deepEqual from "@/utils/deepEqual";
import convertRates from "@/utils/convertRates";
import { defineComponent, type PropType } from "vue";
import type { Vehicle, Timeout, CURRENCY, Forecast } from "@/types/evcc";
import type {
StaticPlan,
RepeatingPlan,
PlanWrapper,
StaticSocPlan,
StaticEnergyPlan,
PlanResponse,
PlanStrategy,
} from "./types";
export default defineComponent({
name: "ChargingPlansSettings",
components: {
ChargingPlanPreview: Preview,
ChargingPlanStaticSettings: PlanStaticSettings,
ChargingPlanStrategy,
ChargingPlansRepeatingSettings: RepeatingSettings,
ChargingPlanWarnings: Warnings,
},
mixins: [formatter, collector],
props: {
id: [String, Number],
staticPlan: Object as PropType<StaticPlan>,
repeatingPlans: { type: Array as PropType<RepeatingPlan[]>, default: () => [] },
effectiveLimitSoc: Number,
effectivePlanTime: String,
effectivePlanSoc: Number,
effectivePlanStrategy: Object as PropType<PlanStrategy>,
planEnergy: Number,
limitEnergy: Number,
socBasedPlanning: Boolean,
socPerKwh: Number,
rangePerSoc: Number,
smartCostType: String,
currency: String as PropType<CURRENCY>,
mode: String,
capacity: Number,
vehicle: Object as PropType<Vehicle>,
vehicleLimitSoc: Number,
planOverrun: Number,
forecast: Object as PropType<Forecast>,
},
emits: [
"static-plan-removed",
"static-plan-updated",
"repeating-plans-updated",
"plan-strategy-updated",
],
data() {
return {
staticPlanPreview: {} as StaticPlan,
plan: {} as PlanWrapper,
activeTab: "time",
debounceTimer: null as Timeout,
nextPlanId: 0,
strategyOpen: false,
};
},
computed: {
noActivePlan(): boolean {
return !this.staticPlan && this.repeatingPlans.every((plan) => !plan.active);
},
multiplePlans(): boolean {
return this.repeatingPlans.length !== 0;
},
chargingPlanWarningsProps(): any {
return this.collectProps(Warnings);
},
chargingPlanPreviewProps(): any {
const forecastSlots = this.forecast?.planner || [];
const rates = convertRates(forecastSlots);
const { duration, plan, power, planTime } = this.plan;
const targetTime = planTime ? new Date(planTime) : null;
const { currency, smartCostType } = this;
return rates
? { duration, plan, power, rates, targetTime, currency, smartCostType }
: null;
},
alreadyReached(): boolean {
return this.plan.duration === 0;
},
nextPlanTitle(): string {
return `${this.$t("main.targetCharge.nextPlan")} #${this.nextPlanId}`;
},
strategyDisabled(): boolean {
// options only make sense if there are variable prices
// TODO: make this logic more robust (api fails, missing data)
const slots = this.forecast?.planner || [];
const values = new Set(slots.map(({ value }) => value));
return values.size <= 1;
},
},
watch: {
effectivePlanTime(newValue: string) {
if (null !== newValue) {
this.updatePlanDebounced();
}
},
effectivePlanStrategy: {
deep: true,
handler() {
this.updatePlanDebounced();
},
},
staticPlan: {
deep: true,
handler(vNew: StaticPlan, vOld: StaticPlan) {
if (!deepEqual(vNew, vOld)) {
this.updatePlanDebounced();
}
},
},
repeatingPlans: {
deep: true,
handler(vNew: RepeatingPlan[], vOld: RepeatingPlan[]) {
if (!deepEqual(vNew, vOld)) {
this.updatePlanDebounced();
}
},
},
},
mounted(): void {
this.updatePlanDebounced();
},
methods: {
async updatePlanDebounced() {
if (this.noActivePlan) {
await this.updatePlanPreviewDebounced();
} else {
await this.updateActivePlanDebounced();
}
},
async updateActivePlan(): Promise<void> {
try {
const res = await this.apiFetchPlan(`loadpoints/${this.id}/plan`);
this.plan = res?.data ?? ({} as PlanWrapper);
this.nextPlanId = this.plan.planId;
} catch (e) {
console.error(e);
}
},
async fetchStaticPreviewSoc(plan: StaticSocPlan): Promise<PlanResponse | undefined> {
const timeISO = plan.time.toISOString();
const params: Record<string, unknown> = {};
return await this.apiFetchPlan(
`loadpoints/${this.id}/plan/static/preview/soc/${plan.soc}/${timeISO}`,
params
);
},
async fetchStaticPreviewEnergy(plan: StaticEnergyPlan): Promise<PlanResponse | undefined> {
const timeISO = plan.time.toISOString();
const params: Record<string, unknown> = {};
return await this.apiFetchPlan(
`loadpoints/${this.id}/plan/static/preview/energy/${plan.energy}/${timeISO}`,
params
);
},
async apiFetchPlan(
url: string,
params?: Record<string, unknown>
): Promise<PlanResponse | undefined> {
try {
const res = (await api.get(url, {
validateStatus: (code) => [200, 404].includes(code),
params,
})) as PlanResponse;
if (res.status === 404) {
return { data: {} } as PlanResponse;
}
return res;
} catch (e) {
console.error(e);
return;
}
},
async updatePreviewPlan(): Promise<void> {
// only show preview if no plan is active
if (!this.noActivePlan) return;
try {
let planRes: PlanResponse | undefined = undefined;
if (this.staticPlanPreview) {
// static plan
let plan = this.staticPlanPreview;
if (this.socBasedPlanning) {
plan = plan as StaticSocPlan;
planRes = await this.fetchStaticPreviewSoc({
soc: plan.soc,
time: plan.time,
});
} else {
plan = plan as StaticEnergyPlan;
planRes = await this.fetchStaticPreviewEnergy({
energy: plan.energy,
time: plan.time,
});
}
}
this.plan = planRes?.data ?? ({} as PlanWrapper);
} catch (e) {
console.error(e);
}
},
async updatePlanPreviewDebounced(): Promise<void> {
if (!this.debounceTimer) {
await this.updatePreviewPlan();
return;
}
clearTimeout(this.debounceTimer);
this.debounceTimer = setTimeout(async () => await this.updatePreviewPlan(), 1000);
},
async updateActivePlanDebounced(): Promise<void> {
if (!this.debounceTimer) {
await this.updateActivePlan();
return;
}
clearTimeout(this.debounceTimer);
this.debounceTimer = setTimeout(async () => await this.updateActivePlan(), 1000);
},
removeStaticPlan(): void {
this.$emit("static-plan-removed");
},
updateStaticPlan(plan: StaticPlan): void {
this.$emit("static-plan-updated", plan);
},
updateRepeatingPlans(plans: RepeatingPlan[]): void {
this.$emit("repeating-plans-updated", plans);
},
previewStaticPlan(plan: StaticPlan): void {
this.staticPlanPreview = plan;
this.updatePlanPreviewDebounced();
},
updatePlanStrategy(strategy: PlanStrategy): void {
this.$emit("plan-strategy-updated", strategy);
},
},
});
</script>
<style scoped>
h5 {
position: relative;
display: flex;
top: -33px;
margin-bottom: -0.5rem;
padding: 0 0.5rem;
justify-content: center;
}
h5 .inner {
padding: 0 1rem;
background-color: var(--evcc-box);
font-weight: normal;
color: var(--evcc-gray);
text-transform: uppercase;
text-align: center;
}
</style>