@teslemetry/api 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -3009,6 +3009,10 @@ var TeslemetryVehicleStream = class extends events.EventEmitter {
3009
3009
  this.fields = event.config.fields;
3010
3010
  });
3011
3011
  }
3012
+ on(event, listener) {
3013
+ this.root.sse.sendCache(this.vin, event, listener);
3014
+ return super.on(event, listener);
3015
+ }
3012
3016
  /** Get the current configuration for the vehicle */
3013
3017
  async getConfig() {
3014
3018
  const { data, response } = await getApiConfigByVin({ path: { vin: this.vin } });
@@ -3080,6 +3084,8 @@ var TeslemetryVehicleStream = class extends events.EventEmitter {
3080
3084
  this.addField(field).catch((error) => {
3081
3085
  this.logger.error(`Failed to add field ${field}:`, error);
3082
3086
  });
3087
+ const data = this.root.sse.cache?.[this.vin]?.data?.[field];
3088
+ if (data !== void 0) callback(data);
3083
3089
  this.data.on(field, callback);
3084
3090
  return () => this.data.off(field, callback);
3085
3091
  }
@@ -3096,16 +3102,69 @@ var TeslemetryStream = class extends events.EventEmitter {
3096
3102
  active = false;
3097
3103
  connected = false;
3098
3104
  vin;
3099
- cache;
3105
+ cache = {};
3106
+ cloudCache;
3107
+ localCache;
3100
3108
  logger;
3101
3109
  vehicles = /* @__PURE__ */ new Map();
3102
3110
  constructor(root, options) {
3103
3111
  super();
3104
3112
  this.root = root;
3105
3113
  this.vin = options?.vin;
3106
- this.cache = options?.cache;
3114
+ if (typeof options?.cache === "boolean") {
3115
+ this.cloudCache = options.cache;
3116
+ this.localCache = options.cache;
3117
+ } else {
3118
+ this.cloudCache = options?.cache?.cloud;
3119
+ this.localCache = options?.cache?.local;
3120
+ }
3107
3121
  this.logger = root.logger;
3108
3122
  if (this.vin) this.getVehicle(this.vin);
3123
+ if (this.localCache) this.startLocalCache();
3124
+ }
3125
+ sendCache(vin, event, listener) {
3126
+ if (this.cache) {
3127
+ const vehicleCache = this.cache[vin];
3128
+ if (event === "connectivity" && vehicleCache.connectivity) for (const networkInterface in vehicleCache.connectivity) {
3129
+ const typedNetworkInterface = networkInterface;
3130
+ const status = vehicleCache.connectivity[typedNetworkInterface];
3131
+ if (status !== void 0) listener({
3132
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3133
+ vin,
3134
+ networkInterface: typedNetworkInterface,
3135
+ status,
3136
+ isCache: true
3137
+ });
3138
+ }
3139
+ else if (event === "state" && vehicleCache.state) listener({
3140
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3141
+ vin,
3142
+ state: vehicleCache.state,
3143
+ isCache: true
3144
+ });
3145
+ else if (event === "data" && vehicleCache.data) listener({
3146
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3147
+ vin,
3148
+ data: vehicleCache.data,
3149
+ isCache: true
3150
+ });
3151
+ else if (event === "errors" && vehicleCache.errors) listener({
3152
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3153
+ vin,
3154
+ errors: vehicleCache.errors,
3155
+ isCache: true
3156
+ });
3157
+ else if (event === "alerts" && vehicleCache.alerts) listener({
3158
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3159
+ vin,
3160
+ alerts: vehicleCache.alerts,
3161
+ isCache: true
3162
+ });
3163
+ }
3164
+ }
3165
+ on(event, listener) {
3166
+ for (const vin in this.cache) this.sendCache(vin, event, listener);
3167
+ return super.on(event, listener);
3109
3168
  }
3110
3169
  getVehicle(vin) {
3111
3170
  if (!this.vehicles.has(vin)) new TeslemetryVehicleStream(this.root, vin);
@@ -3122,7 +3181,7 @@ var TeslemetryStream = class extends events.EventEmitter {
3122
3181
  const sse = await getSseByVin_({
3123
3182
  client: this.root.client,
3124
3183
  path: { vin: this.vin || "" },
3125
- query: { cache: this.cache }
3184
+ query: { cache: this.cloudCache }
3126
3185
  });
3127
3186
  this.logger.info(`Connected to stream`);
3128
3187
  retries = 0;
@@ -3181,6 +3240,48 @@ var TeslemetryStream = class extends events.EventEmitter {
3181
3240
  else if ("config" in event) vehicle.emit("config", event);
3182
3241
  }
3183
3242
  }
3243
+ cacheState(event) {
3244
+ this.cache[event.vin] ??= {};
3245
+ this.cache[event.vin].state = event.state;
3246
+ }
3247
+ cacheData(event) {
3248
+ this.cache[event.vin] ??= { data: {} };
3249
+ this.cache[event.vin].data = {
3250
+ ...this.cache[event.vin].data,
3251
+ ...event.data
3252
+ };
3253
+ }
3254
+ cacheErrors(event) {
3255
+ this.cache[event.vin] ??= {};
3256
+ this.cache[event.vin].errors = event.errors;
3257
+ }
3258
+ cacheAlerts(event) {
3259
+ this.cache[event.vin] ??= {};
3260
+ this.cache[event.vin].alerts = event.alerts;
3261
+ }
3262
+ cacheConnectivity(event) {
3263
+ this.cache[event.vin] ??= {};
3264
+ this.cache[event.vin].connectivity ??= {};
3265
+ this.cache[event.vin].connectivity[event.networkInterface] = event.status;
3266
+ }
3267
+ startLocalCache() {
3268
+ this.localCache = true;
3269
+ this.on("state", this.cacheState);
3270
+ this.on("data", this.cacheData);
3271
+ this.on("errors", this.cacheErrors);
3272
+ this.on("alerts", this.cacheAlerts);
3273
+ this.on("connectivity", this.cacheConnectivity);
3274
+ this.logger.info(`Started local cache`);
3275
+ }
3276
+ stopLocalCache() {
3277
+ this.localCache = false;
3278
+ this.off("state", this.cacheState);
3279
+ this.off("data", this.cacheData);
3280
+ this.off("errors", this.cacheErrors);
3281
+ this.off("alerts", this.cacheAlerts);
3282
+ this.off("connectivity", this.cacheConnectivity);
3283
+ this.logger.info(`Stopped local cache`);
3284
+ }
3184
3285
  };
3185
3286
 
3186
3287
  //#endregion
@@ -4944,7 +5045,7 @@ const consoleLogger = {
4944
5045
 
4945
5046
  //#endregion
4946
5047
  //#region package.json
4947
- var version = "0.5.0";
5048
+ var version = "0.5.1";
4948
5049
 
4949
5050
  //#endregion
4950
5051
  //#region src/Teslemetry.ts
package/dist/index.d.cts CHANGED
@@ -13909,7 +13909,10 @@ declare class TeslemetryVehicleStream extends EventEmitter {
13909
13909
  //#region src/TeslemetryStream.d.ts
13910
13910
  interface TeslemetryStreamOptions {
13911
13911
  vin?: string;
13912
- cache?: boolean;
13912
+ cache?: boolean | {
13913
+ cloud: boolean;
13914
+ local: boolean;
13915
+ };
13913
13916
  }
13914
13917
  type TeslemetryStreamEventMap = {
13915
13918
  state: SseState;
@@ -13929,15 +13932,26 @@ declare interface TeslemetryStream {
13929
13932
  off<K$1 extends keyof TeslemetryStreamEventMap>(event: K$1, listener: (data: TeslemetryStreamEventMap[K$1]) => void): this;
13930
13933
  emit<K$1 extends keyof TeslemetryStreamEventMap>(event: K$1, ...args: TeslemetryStreamEventMap[K$1] extends void ? [] : [TeslemetryStreamEventMap[K$1]]): boolean;
13931
13934
  }
13935
+ interface VehicleCache {
13936
+ state?: SseState["state"];
13937
+ data?: SseData["data"];
13938
+ alerts?: SseAlerts["alerts"];
13939
+ errors?: SseErrors["errors"];
13940
+ connectivity?: Partial<Record<SseConnectivity["networkInterface"], SseConnectivity["status"]>>;
13941
+ }
13942
+ type Cache = Record<string, VehicleCache>;
13932
13943
  declare class TeslemetryStream extends EventEmitter {
13933
13944
  private root;
13934
13945
  active: boolean;
13935
13946
  connected: boolean;
13936
13947
  private vin;
13937
- private cache;
13948
+ cache: Cache;
13949
+ private cloudCache;
13950
+ private localCache;
13938
13951
  logger: Logger;
13939
13952
  vehicles: Map<string, TeslemetryVehicleStream>;
13940
13953
  constructor(root: Teslemetry, options?: TeslemetryStreamOptions);
13954
+ sendCache<K$1 extends keyof TeslemetryStreamEventMap>(vin: string, event: K$1, listener: (data: any) => void): void;
13941
13955
  getVehicle(vin: string): TeslemetryVehicleStream;
13942
13956
  connect(): Promise<void>;
13943
13957
  private _connectLoop;
@@ -13945,6 +13959,13 @@ declare class TeslemetryStream extends EventEmitter {
13945
13959
  close(): void;
13946
13960
  parseCreatedAt(event: SseEvent): Date;
13947
13961
  private _dispatch;
13962
+ private cacheState;
13963
+ private cacheData;
13964
+ private cacheErrors;
13965
+ private cacheAlerts;
13966
+ private cacheConnectivity;
13967
+ startLocalCache(): void;
13968
+ stopLocalCache(): void;
13948
13969
  }
13949
13970
  //#endregion
13950
13971
  //#region src/TeslemetryChargingApi.d.ts
package/dist/index.d.mts CHANGED
@@ -13909,7 +13909,10 @@ declare class TeslemetryVehicleStream extends EventEmitter {
13909
13909
  //#region src/TeslemetryStream.d.ts
13910
13910
  interface TeslemetryStreamOptions {
13911
13911
  vin?: string;
13912
- cache?: boolean;
13912
+ cache?: boolean | {
13913
+ cloud: boolean;
13914
+ local: boolean;
13915
+ };
13913
13916
  }
13914
13917
  type TeslemetryStreamEventMap = {
13915
13918
  state: SseState;
@@ -13929,15 +13932,26 @@ declare interface TeslemetryStream {
13929
13932
  off<K$1 extends keyof TeslemetryStreamEventMap>(event: K$1, listener: (data: TeslemetryStreamEventMap[K$1]) => void): this;
13930
13933
  emit<K$1 extends keyof TeslemetryStreamEventMap>(event: K$1, ...args: TeslemetryStreamEventMap[K$1] extends void ? [] : [TeslemetryStreamEventMap[K$1]]): boolean;
13931
13934
  }
13935
+ interface VehicleCache {
13936
+ state?: SseState["state"];
13937
+ data?: SseData["data"];
13938
+ alerts?: SseAlerts["alerts"];
13939
+ errors?: SseErrors["errors"];
13940
+ connectivity?: Partial<Record<SseConnectivity["networkInterface"], SseConnectivity["status"]>>;
13941
+ }
13942
+ type Cache = Record<string, VehicleCache>;
13932
13943
  declare class TeslemetryStream extends EventEmitter {
13933
13944
  private root;
13934
13945
  active: boolean;
13935
13946
  connected: boolean;
13936
13947
  private vin;
13937
- private cache;
13948
+ cache: Cache;
13949
+ private cloudCache;
13950
+ private localCache;
13938
13951
  logger: Logger;
13939
13952
  vehicles: Map<string, TeslemetryVehicleStream>;
13940
13953
  constructor(root: Teslemetry, options?: TeslemetryStreamOptions);
13954
+ sendCache<K$1 extends keyof TeslemetryStreamEventMap>(vin: string, event: K$1, listener: (data: any) => void): void;
13941
13955
  getVehicle(vin: string): TeslemetryVehicleStream;
13942
13956
  connect(): Promise<void>;
13943
13957
  private _connectLoop;
@@ -13945,6 +13959,13 @@ declare class TeslemetryStream extends EventEmitter {
13945
13959
  close(): void;
13946
13960
  parseCreatedAt(event: SseEvent): Date;
13947
13961
  private _dispatch;
13962
+ private cacheState;
13963
+ private cacheData;
13964
+ private cacheErrors;
13965
+ private cacheAlerts;
13966
+ private cacheConnectivity;
13967
+ startLocalCache(): void;
13968
+ stopLocalCache(): void;
13948
13969
  }
13949
13970
  //#endregion
13950
13971
  //#region src/TeslemetryChargingApi.d.ts
package/dist/index.mjs CHANGED
@@ -3009,6 +3009,10 @@ var TeslemetryVehicleStream = class extends EventEmitter {
3009
3009
  this.fields = event.config.fields;
3010
3010
  });
3011
3011
  }
3012
+ on(event, listener) {
3013
+ this.root.sse.sendCache(this.vin, event, listener);
3014
+ return super.on(event, listener);
3015
+ }
3012
3016
  /** Get the current configuration for the vehicle */
3013
3017
  async getConfig() {
3014
3018
  const { data, response } = await getApiConfigByVin({ path: { vin: this.vin } });
@@ -3080,6 +3084,8 @@ var TeslemetryVehicleStream = class extends EventEmitter {
3080
3084
  this.addField(field).catch((error) => {
3081
3085
  this.logger.error(`Failed to add field ${field}:`, error);
3082
3086
  });
3087
+ const data = this.root.sse.cache?.[this.vin]?.data?.[field];
3088
+ if (data !== void 0) callback(data);
3083
3089
  this.data.on(field, callback);
3084
3090
  return () => this.data.off(field, callback);
3085
3091
  }
@@ -3096,16 +3102,69 @@ var TeslemetryStream = class extends EventEmitter {
3096
3102
  active = false;
3097
3103
  connected = false;
3098
3104
  vin;
3099
- cache;
3105
+ cache = {};
3106
+ cloudCache;
3107
+ localCache;
3100
3108
  logger;
3101
3109
  vehicles = /* @__PURE__ */ new Map();
3102
3110
  constructor(root, options) {
3103
3111
  super();
3104
3112
  this.root = root;
3105
3113
  this.vin = options?.vin;
3106
- this.cache = options?.cache;
3114
+ if (typeof options?.cache === "boolean") {
3115
+ this.cloudCache = options.cache;
3116
+ this.localCache = options.cache;
3117
+ } else {
3118
+ this.cloudCache = options?.cache?.cloud;
3119
+ this.localCache = options?.cache?.local;
3120
+ }
3107
3121
  this.logger = root.logger;
3108
3122
  if (this.vin) this.getVehicle(this.vin);
3123
+ if (this.localCache) this.startLocalCache();
3124
+ }
3125
+ sendCache(vin, event, listener) {
3126
+ if (this.cache) {
3127
+ const vehicleCache = this.cache[vin];
3128
+ if (event === "connectivity" && vehicleCache.connectivity) for (const networkInterface in vehicleCache.connectivity) {
3129
+ const typedNetworkInterface = networkInterface;
3130
+ const status = vehicleCache.connectivity[typedNetworkInterface];
3131
+ if (status !== void 0) listener({
3132
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3133
+ vin,
3134
+ networkInterface: typedNetworkInterface,
3135
+ status,
3136
+ isCache: true
3137
+ });
3138
+ }
3139
+ else if (event === "state" && vehicleCache.state) listener({
3140
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3141
+ vin,
3142
+ state: vehicleCache.state,
3143
+ isCache: true
3144
+ });
3145
+ else if (event === "data" && vehicleCache.data) listener({
3146
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3147
+ vin,
3148
+ data: vehicleCache.data,
3149
+ isCache: true
3150
+ });
3151
+ else if (event === "errors" && vehicleCache.errors) listener({
3152
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3153
+ vin,
3154
+ errors: vehicleCache.errors,
3155
+ isCache: true
3156
+ });
3157
+ else if (event === "alerts" && vehicleCache.alerts) listener({
3158
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3159
+ vin,
3160
+ alerts: vehicleCache.alerts,
3161
+ isCache: true
3162
+ });
3163
+ }
3164
+ }
3165
+ on(event, listener) {
3166
+ for (const vin in this.cache) this.sendCache(vin, event, listener);
3167
+ return super.on(event, listener);
3109
3168
  }
3110
3169
  getVehicle(vin) {
3111
3170
  if (!this.vehicles.has(vin)) new TeslemetryVehicleStream(this.root, vin);
@@ -3122,7 +3181,7 @@ var TeslemetryStream = class extends EventEmitter {
3122
3181
  const sse = await getSseByVin_({
3123
3182
  client: this.root.client,
3124
3183
  path: { vin: this.vin || "" },
3125
- query: { cache: this.cache }
3184
+ query: { cache: this.cloudCache }
3126
3185
  });
3127
3186
  this.logger.info(`Connected to stream`);
3128
3187
  retries = 0;
@@ -3181,6 +3240,48 @@ var TeslemetryStream = class extends EventEmitter {
3181
3240
  else if ("config" in event) vehicle.emit("config", event);
3182
3241
  }
3183
3242
  }
3243
+ cacheState(event) {
3244
+ this.cache[event.vin] ??= {};
3245
+ this.cache[event.vin].state = event.state;
3246
+ }
3247
+ cacheData(event) {
3248
+ this.cache[event.vin] ??= { data: {} };
3249
+ this.cache[event.vin].data = {
3250
+ ...this.cache[event.vin].data,
3251
+ ...event.data
3252
+ };
3253
+ }
3254
+ cacheErrors(event) {
3255
+ this.cache[event.vin] ??= {};
3256
+ this.cache[event.vin].errors = event.errors;
3257
+ }
3258
+ cacheAlerts(event) {
3259
+ this.cache[event.vin] ??= {};
3260
+ this.cache[event.vin].alerts = event.alerts;
3261
+ }
3262
+ cacheConnectivity(event) {
3263
+ this.cache[event.vin] ??= {};
3264
+ this.cache[event.vin].connectivity ??= {};
3265
+ this.cache[event.vin].connectivity[event.networkInterface] = event.status;
3266
+ }
3267
+ startLocalCache() {
3268
+ this.localCache = true;
3269
+ this.on("state", this.cacheState);
3270
+ this.on("data", this.cacheData);
3271
+ this.on("errors", this.cacheErrors);
3272
+ this.on("alerts", this.cacheAlerts);
3273
+ this.on("connectivity", this.cacheConnectivity);
3274
+ this.logger.info(`Started local cache`);
3275
+ }
3276
+ stopLocalCache() {
3277
+ this.localCache = false;
3278
+ this.off("state", this.cacheState);
3279
+ this.off("data", this.cacheData);
3280
+ this.off("errors", this.cacheErrors);
3281
+ this.off("alerts", this.cacheAlerts);
3282
+ this.off("connectivity", this.cacheConnectivity);
3283
+ this.logger.info(`Stopped local cache`);
3284
+ }
3184
3285
  };
3185
3286
 
3186
3287
  //#endregion
@@ -4944,7 +5045,7 @@ const consoleLogger = {
4944
5045
 
4945
5046
  //#endregion
4946
5047
  //#region package.json
4947
- var version = "0.5.0";
5048
+ var version = "0.5.1";
4948
5049
 
4949
5050
  //#endregion
4950
5051
  //#region src/Teslemetry.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teslemetry/api",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "API client for Teslemetry",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",