11import got from 'got' ;
22import logger from '../logger' ;
33
4- import { REGIONS , DEFAULT_VEHICLE_STATUS_OPTIONS } from '../constants' ;
4+ import {
5+ REGIONS ,
6+ DEFAULT_VEHICLE_STATUS_OPTIONS ,
7+ ChargeTarget ,
8+ POSSIBLE_CHARGE_LIMIT_VALUES ,
9+ } from '../constants' ;
510
611import {
712 VehicleStartOptions ,
@@ -12,11 +17,13 @@ import {
1217 VehicleStatusOptions ,
1318 RawVehicleStatus ,
1419 FullVehicleStatus ,
20+ EVChargeModeTypes ,
1521} from '../interfaces/common.interfaces' ;
1622
1723import { Vehicle } from './vehicle' ;
1824import { celciusToTempCode , parseDate } from '../util' ;
1925import { CanadianController } from '../controllers/canadian.controller' ;
26+ import { ManagedBluelinkyError } from '../tools/common.tools' ;
2027
2128export default class CanadianVehicle extends Vehicle {
2229 public region = REGIONS . CA ;
@@ -40,7 +47,9 @@ export default class CanadianVehicle extends Vehicle {
4047 } ;
4148 logger . debug ( 'Begin status request, polling car: ' + input . refresh ) ;
4249 try {
43- const endpoint = statusConfig . refresh ? this . controller . environment . endpoints . remoteStatus : this . controller . environment . endpoints . status ;
50+ const endpoint = statusConfig . refresh
51+ ? this . controller . environment . endpoints . remoteStatus
52+ : this . controller . environment . endpoints . status ;
4453 const response = await this . request ( endpoint , { } ) ;
4554 const vehicleStatus = response . result ?. status ;
4655
@@ -148,13 +157,19 @@ export default class CanadianVehicle extends Vehicle {
148157 const airTemp = startConfig . airTempvalue ;
149158 // TODO: can we use getTempCode here from util?
150159 if ( airTemp != null ) {
151- body . hvacInfo [ 'airTemp' ] = { value : celciusToTempCode ( REGIONS . CA , airTemp ) , unit : 0 , hvacTempType : 1 } ;
160+ body . hvacInfo [ 'airTemp' ] = {
161+ value : celciusToTempCode ( REGIONS . CA , airTemp ) ,
162+ unit : 0 ,
163+ hvacTempType : 1 ,
164+ } ;
152165 } else if ( ( startConfig . airCtrl ?? false ) || ( startConfig . defrost ?? false ) ) {
153166 throw 'air temperature should be specified' ;
154167 }
155168
156169 const preAuth = await this . getPreAuth ( ) ;
157- const response = await this . request ( this . controller . environment . endpoints . start , body , { pAuth : preAuth } ) ;
170+ const response = await this . request ( this . controller . environment . endpoints . start , body , {
171+ pAuth : preAuth ,
172+ } ) ;
158173
159174 logger . debug ( response ) ;
160175
@@ -197,6 +212,77 @@ export default class CanadianVehicle extends Vehicle {
197212 }
198213 }
199214
215+ /**
216+ * Warning only works on EV vehicles
217+ * @returns
218+ */
219+ public async stopCharge ( ) : Promise < void > {
220+ logger . debug ( 'Begin stopCharge' ) ;
221+ const { stopCharge } = this . controller . environment . endpoints ;
222+ try {
223+ const preAuth = await this . getPreAuth ( ) ;
224+ const response = await this . request ( stopCharge , {
225+ pin : this . controller . userConfig . pin ,
226+ pAuth : preAuth ,
227+ } ) ;
228+ return response ;
229+ } catch ( err ) {
230+ throw 'error: ' + err ;
231+ }
232+ }
233+
234+ /**
235+ * Warning only works on EV vehicles
236+ * @returns
237+ */
238+ public async startCharge ( ) : Promise < void > {
239+ logger . debug ( 'Begin startCharge' ) ;
240+ const { startCharge } = this . controller . environment . endpoints ;
241+ try {
242+ const preAuth = await this . getPreAuth ( ) ;
243+ const response = await this . request ( startCharge , {
244+ pin : this . controller . userConfig . pin ,
245+ pAuth : preAuth ,
246+ } ) ;
247+ return response ;
248+ } catch ( err ) {
249+ throw 'error: ' + err ;
250+ }
251+ }
252+
253+ /**
254+ * Warning only works on EV vehicles
255+ * @param limits
256+ * @returns Promise<void>
257+ */
258+ public async setChargeTargets ( limits : { fast : ChargeTarget ; slow : ChargeTarget } ) : Promise < void > {
259+ logger . debug ( 'Begin setChargeTarget' ) ;
260+ if (
261+ ! POSSIBLE_CHARGE_LIMIT_VALUES . includes ( limits . fast ) ||
262+ ! POSSIBLE_CHARGE_LIMIT_VALUES . includes ( limits . slow )
263+ ) {
264+ throw new ManagedBluelinkyError (
265+ `Charge target values are limited to ${ POSSIBLE_CHARGE_LIMIT_VALUES . join ( ', ' ) } `
266+ ) ;
267+ }
268+
269+ const { setChargeTarget } = this . controller . environment . endpoints ;
270+ try {
271+ const preAuth = await this . getPreAuth ( ) ;
272+ const response = await this . request ( setChargeTarget , {
273+ pin : this . controller . userConfig . pin ,
274+ pAuth : preAuth ,
275+ tsoc : [
276+ { plugType : EVChargeModeTypes . FAST , level : limits . fast } ,
277+ { plugType : EVChargeModeTypes . SLOW , level : limits . slow } ,
278+ ] ,
279+ } ) ;
280+ return response ;
281+ } catch ( err ) {
282+ throw 'error: ' + err ;
283+ }
284+ }
285+
200286 // TODO: @Seb to take a look at doing this
201287 public odometer ( ) : Promise < VehicleOdometer | null > {
202288 throw new Error ( 'Method not implemented.' ) ;
@@ -206,7 +292,11 @@ export default class CanadianVehicle extends Vehicle {
206292 logger . debug ( 'Begin locate request' ) ;
207293 try {
208294 const preAuth = await this . getPreAuth ( ) ;
209- const response = await this . request ( this . controller . environment . endpoints . locate , { } , { pAuth : preAuth } ) ;
295+ const response = await this . request (
296+ this . controller . environment . endpoints . locate ,
297+ { } ,
298+ { pAuth : preAuth }
299+ ) ;
210300 this . _location = response . result as VehicleLocation ;
211301 return this . _location ;
212302 } catch ( err ) {
0 commit comments