Skip to content

Commit 524f998

Browse files
committed
Only remove manual schedules when vehicle leaves a known location
This should allow you to set a manual charge before reaching a destination
1 parent 4ce2eea commit 524f998

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

server/logic.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ export class Logic {
7979
[was.vehicle_uuid, vehicle.location_uuid, vehicle.odometer]
8080
);
8181
if (!connection) {
82-
// Remove manual charge entries if we have any
83-
await this.db.pg.none(
84-
`DELETE FROM schedule WHERE vehicle_uuid = $1 AND schedule_type = $2;`,
85-
[was.vehicle_uuid, ScheduleType.Manual]
86-
);
87-
8882
connection = (await this.db.pg.one(
8983
`INSERT INTO connected($[this:name]) VALUES($[this:csv]) RETURNING *;`,
9084
{
@@ -298,6 +292,17 @@ export class Logic {
298292
}
299293
}
300294

295+
if (vehicle.location_uuid && vehicle.location_uuid !== was.location_uuid) {
296+
// Remove all manual charge schedules if we move from a known location to anything else
297+
const removed = await this.db.pg.oneOrNone(
298+
`DELETE FROM schedule WHERE vehicle_uuid = $1 AND schedule_type = $2 RETURNING *;`,
299+
[vehicle.vehicle_uuid, ScheduleType.Manual]
300+
);
301+
if (removed) {
302+
log(LogLevel.Trace, `Removed manual schedules for vehicle ${vehicle.vehicle_uuid}, because it moved from ${was.location_uuid} to ${vehicle.location_uuid}`);
303+
}
304+
}
305+
301306
if (was.charge_id !== null && vehicle.charge_id === null) {
302307
// We stopped charging
303308
log(LogLevel.Debug, `Ending charge ${was.charge_id}`);
@@ -874,9 +879,9 @@ export class Logic {
874879

875880
// Cleanup schedule remove all entries 1 hour after the end time
876881
const schedule: DBSchedule[] = await this.db.pg.manyOrNone(
877-
`DELETE FROM schedule WHERE vehicle_uuid = $1 AND schedule_ts + interval '1 hour' < NOW();
882+
`DELETE FROM schedule WHERE vehicle_uuid = $1 AND schedule_type <> $2 AND schedule_ts + interval '1 hour' < NOW();
878883
SELECT * FROM schedule WHERE vehicle_uuid = $1;`,
879-
[vehicle.vehicle_uuid]
884+
[vehicle.vehicle_uuid, ScheduleType.Manual]
880885
);
881886

882887
// Reduce the array to a map with only the first upcoming event of each type

0 commit comments

Comments
 (0)