Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for getting/setting charge schedule #228

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 93 additions & 5 deletions src/interfaces/common.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { REGION } from '../constants';
import { ChargeTarget, REGION } from '../constants';

export type Brand = 'kia' | 'hyundai';

Expand Down Expand Up @@ -51,7 +51,7 @@ export interface VehicleStatus {
range: number;
rangeGas?: number;
rangeEV?: number;
plugedTo? : EVPlugTypes;
plugedTo?: EVPlugTypes;
estimatedCurrentChargeDuration?: number;
estimatedFastChargeDuration?: number;
estimatedPortableChargeDuration?: number;
Expand Down Expand Up @@ -87,7 +87,7 @@ export interface VehicleStatus {
all: boolean;
};
};
lastupdate: Date|null
lastupdate: Date | null
}

// TODO: fix/update
Expand Down Expand Up @@ -127,7 +127,7 @@ export interface FullVehicleStatus {
batteryCharge: boolean;
batteryStatus: number;
batteryPlugin: number;
remainTime2: {
remainTime2: {
etc1: { value: number; unit: number; };
etc2: { value: number; unit: number; };
etc3: { value: number; unit: number; };
Expand Down Expand Up @@ -506,4 +506,92 @@ export interface VehicleDayTrip {
};
distance: number;
}[];
}
}

export const enum OffPeakPowerFlag {
OffPeakPreferred = 1,
OffPeakOnly = 2,
}

export const enum RangeUnit {
Kilometers = 1,
//TODO determine enum value for miles
}

export interface Range {
value: number,
unit: RangeUnit,
}

export interface SocTarget {
targetSOClevel: ChargeTarget,
plugType: EVChargeModeTypes,
dte?: {
rangeByFuel: {
evModeRange: Range,
totalAvailableRange: Range
}
type: number //TODO determine the meaning of this
}
}

export const enum Weekday {
Monday = 1,
Tyesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 0,

Unknown = 9
}
/** AM or PM */
export const enum TimeSection {
AM = 0,
PM = 1
}

/** A time of day, in the vehicle's time zone. */
export interface Time {
/** String of 4 digits, '1200' through '1259' and '0100' through '1159' */
time: string,
timeSection: TimeSection
}

export interface ReservChargeInfoDetail {
reservInfo: {
day: Weekday[],
time: Time,
}
reservChargeSet: boolean,
reservFatcSet: {
defrost: boolean,
airTemp: { //TODO decode the deeper meaning of this.
value: string,
unit: number,
hvacTempType: number
},
airCtrl: number,
heating1: number

}
}

export interface ReservationCharge {
reservChargeInfo: { reservChargeInfoDetail: ReservChargeInfoDetail },
reserveChargeInfo2: { reservChargeInfoDetail: ReservChargeInfoDetail },
offpeakPowerInfo: {
offPeakPowerTime1: {
starttime: Time,
endtime: Time
},
offPeakPowerFlag: OffPeakPowerFlag
},
reservFlag: number, //TODO determine the meaning of this
ect: {
start: { day: Weekday, time: Time },
end: { day: Weekday, time: Time }
},
targetSOClist: SocTarget[]
}
106 changes: 69 additions & 37 deletions src/vehicles/european.vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
EVChargeModeTypes,
VehicleDayTrip,
VehicleMonthTrip,
ReservationCharge,
} from '../interfaces/common.interfaces';

import logger from '../logger';
Expand All @@ -43,9 +44,9 @@ export default class EuropeanVehicle extends Vehicle {
reset?: Date;
updatedAt?: Date;
} = {
max: -1,
current: -1,
};
max: -1,
current: -1,
};

constructor(public vehicleConfig: VehicleRegisterOptions, public controller: EuropeanController) {
super(vehicleConfig, controller);
Expand Down Expand Up @@ -371,23 +372,23 @@ export default class EuropeanVehicle extends Vehicle {
breakdown: rawData.breakdown,
driving: rawData.driving
? {
distance: rawData.driving?.runDistance,
startCount: rawData.driving?.engineStartCount,
durations: {
idle: rawData.driving?.engineIdleTime,
drive: rawData.driving?.engineOnTime,
},
}
distance: rawData.driving?.runDistance,
startCount: rawData.driving?.engineStartCount,
durations: {
idle: rawData.driving?.engineIdleTime,
drive: rawData.driving?.engineOnTime,
},
}
: undefined,
vehicleStatus: rawData.vehicleStatus
? {
tpms: rawData.vehicleStatus?.tpmsSupport
? Boolean(rawData.vehicleStatus?.tpmsSupport)
: undefined,
tirePressure: {
all: rawData.vehicleStatus?.tirePressure?.tirePressureLampAll == '1',
},
}
tpms: rawData.vehicleStatus?.tpmsSupport
? Boolean(rawData.vehicleStatus?.tpmsSupport)
: undefined,
tirePressure: {
all: rawData.vehicleStatus?.tirePressure?.tirePressureLampAll == '1',
},
}
: undefined,
};
}
Expand Down Expand Up @@ -432,10 +433,10 @@ export default class EuropeanVehicle extends Vehicle {
return {
days: Array.isArray(rawData?.tripDayList)
? rawData?.tripDayList.map(day => ({
dayRaw: day.tripDayInMonth,
date: day.tripDayInMonth ? parseDate(day.tripDayInMonth) : undefined,
tripsCount: day.tripCntDay,
}))
dayRaw: day.tripDayInMonth,
date: day.tripDayInMonth ? parseDate(day.tripDayInMonth) : undefined,
tripsCount: day.tripCntDay,
}))
: [],
durations: {
drive: rawData?.tripDrvTime,
Expand Down Expand Up @@ -464,22 +465,22 @@ export default class EuropeanVehicle extends Vehicle {
},
trips: Array.isArray(day.tripList)
? day.tripList.map(trip => {
const start = parseDate(`${day.tripDay}${trip.tripTime}`);
return {
timeRaw: trip.tripTime,
start,
end: addMinutes(start, trip.tripDrvTime),
durations: {
drive: trip.tripDrvTime,
idle: trip.tripIdleTime,
},
speed: {
avg: trip.tripAvgSpeed,
max: trip.tripMaxSpeed,
},
distance: trip.tripDist,
};
})
const start = parseDate(`${day.tripDay}${trip.tripTime}`);
return {
timeRaw: trip.tripTime,
start,
end: addMinutes(start, trip.tripDrvTime),
durations: {
drive: trip.tripDrvTime,
idle: trip.tripIdleTime,
},
speed: {
avg: trip.tripAvgSpeed,
max: trip.tripMaxSpeed,
},
distance: trip.tripDist,
};
})
: [],
}));
}
Expand Down Expand Up @@ -608,6 +609,37 @@ export default class EuropeanVehicle extends Vehicle {
}
}


public async getChargeSchedule(): Promise<ReservationCharge> {
const http = await this.controller.getVehicleHttpService();
try {
const response = this.updateRates(await http.get(
`/api/v2/spa/vehicles/${this.vehicleConfig.id}/reservation/charge`));
const result = response.body.resMsg as ReservationCharge;
return result;
} catch (err) {
throw manageBluelinkyError(err, 'EuropeVehicle.getChargeSchedule');
}
}

/** Warning: doesn't work yet.
* Returns a 503 error when called,
* even when Android app works fine */
public async setChargeSchedule(schedule: ReservationCharge): Promise<unknown> {
const http = await this.controller.getVehicleHttpService();
try {
const response = this.updateRates(await http.post(
`/api/v2/spa/vehicles/${this.vehicleConfig.id}/reservation/charge`, {
body: schedule
})
);
return response;
} catch (err) {
throw manageBluelinkyError(err, 'EuropeVehicle.setChargeSchedule');

}
}

private updateRates<T extends Record<string, unknown>>(resp: got.Response<T>): got.Response<T> {
if (resp.headers?.['x-ratelimit-limit']) {
this.serverRates.max = Number(resp.headers?.['x-ratelimit-limit']);
Expand Down