@@ -4,6 +4,7 @@ import PersistedLog from '../scriptable-utils/io/PersistedLog'
44const KEYCHAIN_CACHE_KEY = 'egmp-bluelink-cache'
55export const DEFAULT_STATUS_CHECK_INTERVAL = 3600 * 1000
66const BLUELINK_LOG_FILE = 'egmp-bluelink-log'
7+ const DEFAULT_API_DOMAIN = 'https://mybluelink.ca/tods/api/'
78
89export interface BluelinkTokens {
910 accessToken : string
@@ -77,7 +78,8 @@ export interface ClimateRequest {
7778
7879const carImageHttpURL = 'https://bluelink.andyfase.com/app-assets/car-images/'
7980const carImageMap : Record < string , string > = {
80- ioniq5 : 'ioniq5.png' ,
81+ 'ioniq 5' : 'ioniq5.png' ,
82+ 'ev 6' : 'ev6.png' ,
8183 default : 'ioniq5.png' ,
8284}
8385
@@ -88,37 +90,61 @@ export class Bluelink {
8890 protected cache : Cache
8991 protected vin : string | undefined
9092 protected statusCheckInterval : number
93+ protected apiDomain : string
9194
9295 protected additionalHeaders : Record < string , string >
9396 protected authHeader : string
9497 protected tempLookup : TempConversion | undefined
9598 protected tokens : BluelinkTokens | undefined
9699 protected debugLastRequest : DebugLastRequest | undefined
97100 protected logger : any
101+ protected loginFailure : boolean
98102
99103 constructor ( config : Config , vin ?: string ) {
100104 this . vin = vin
105+ this . apiDomain = DEFAULT_API_DOMAIN
101106 this . statusCheckInterval = DEFAULT_STATUS_CHECK_INTERVAL
102107 this . additionalHeaders = { }
103108 this . authHeader = 'Authentication'
104109 this . tokens = undefined
110+ this . loginFailure = false
105111 this . debugLastRequest = undefined
106112 this . tempLookup = undefined
107113 this . logger = PersistedLog ( BLUELINK_LOG_FILE )
108114 }
109115
110- protected async superInit ( config : Config , vin ?: string , statusCheckInterval ?: number ) {
116+ protected async superInit ( config : Config , statusCheckInterval ?: number ) {
111117 this . config = config
112- this . vin = vin
118+ this . vin = this . config . vin
113119 this . statusCheckInterval = statusCheckInterval || DEFAULT_STATUS_CHECK_INTERVAL
114120
115- this . cache = await this . loadCache ( )
121+ const cache = await this . loadCache ( )
122+ if ( ! cache ) {
123+ this . loginFailure = true
124+ return
125+ }
126+ this . cache = cache
116127 if ( ! this . tokenValid ( ) ) {
117- this . cache . token = await this . login ( )
118- this . saveCache ( )
128+ const tokens = await this . login ( )
129+ if ( ! tokens ) this . loginFailure = true
130+ else {
131+ this . tokens = tokens
132+ this . saveCache ( )
133+ }
119134 }
120135 }
121136
137+ protected getApiDomain ( lookup : string , domains : Record < string , string > , _default : string ) : string {
138+ for ( const [ key , domain ] of Object . entries ( domains ) ) {
139+ if ( key === lookup ) return domain
140+ }
141+ return _default
142+ }
143+
144+ public loginFailed ( ) : boolean {
145+ return this . loginFailure
146+ }
147+
122148 public getCachedStatus ( ) : Status {
123149 return {
124150 car : this . cache . car ,
@@ -216,14 +242,19 @@ export class Bluelink {
216242 Keychain . set ( KEYCHAIN_CACHE_KEY , JSON . stringify ( this . cache ) )
217243 }
218244
219- protected async loadCache ( ) : Promise < Cache > {
245+ protected async loadCache ( ) : Promise < Cache | undefined > {
220246 let cache : Cache | undefined = undefined
221247 if ( Keychain . contains ( KEYCHAIN_CACHE_KEY ) ) {
222248 cache = JSON . parse ( Keychain . get ( KEYCHAIN_CACHE_KEY ) )
223249 }
224250 if ( ! cache ) {
225251 // initial use - load car and status
226- this . tokens = await this . login ( )
252+ const tokens = await this . login ( )
253+ if ( ! tokens ) {
254+ this . loginFailure = true
255+ return
256+ }
257+ this . tokens = tokens
227258 const car = await this . getCar ( )
228259 cache = {
229260 token : this . tokens ,
@@ -314,7 +345,7 @@ export class Bluelink {
314345 } )
315346 }
316347
317- protected async login ( ) : Promise < BluelinkTokens > {
348+ protected async login ( ) : Promise < BluelinkTokens | undefined > {
318349 // implemented in country specific sub-class
319350 throw Error ( 'Not Implemented' )
320351 }
0 commit comments