@teslemetry/api 0.4.0 → 0.5.0

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
@@ -3245,6 +3245,15 @@ var TeslemetryChargingApi = class {
3245
3245
  var TeslemetryEnergyApi = class extends events.EventEmitter {
3246
3246
  root;
3247
3247
  siteId;
3248
+ refreshDelay = 3e4;
3249
+ refreshInterval = {
3250
+ siteInfo: null,
3251
+ liveStatus: null
3252
+ };
3253
+ refreshClients = {
3254
+ siteInfo: /* @__PURE__ */ new Set(),
3255
+ liveStatus: /* @__PURE__ */ new Set()
3256
+ };
3248
3257
  constructor(root, siteId) {
3249
3258
  if (root.api.energySites.has(siteId)) throw new Error("Energy site already exists");
3250
3259
  super();
@@ -3386,6 +3395,34 @@ var TeslemetryEnergyApi = class extends events.EventEmitter {
3386
3395
  });
3387
3396
  return data;
3388
3397
  }
3398
+ requestPolling(endpoint) {
3399
+ if (!this.refreshInterval[endpoint]) switch (endpoint) {
3400
+ case "siteInfo":
3401
+ this.refreshInterval[endpoint] = setInterval(() => {
3402
+ this.getSiteInfo();
3403
+ }, this.refreshDelay);
3404
+ this.getSiteInfo();
3405
+ break;
3406
+ case "liveStatus":
3407
+ this.refreshInterval[endpoint] = setInterval(() => {
3408
+ this.getLiveStatus();
3409
+ }, this.refreshDelay);
3410
+ this.getLiveStatus();
3411
+ break;
3412
+ default: throw new Error(`Invalid endpoint: ${endpoint}`);
3413
+ }
3414
+ const symbol = Symbol("refreshClient");
3415
+ this.refreshClients[endpoint].add(symbol);
3416
+ return () => {
3417
+ this.refreshClients[endpoint].delete(symbol);
3418
+ if (this.refreshClients[endpoint].size === 0) {
3419
+ if (this.refreshInterval[endpoint]) {
3420
+ clearInterval(this.refreshInterval[endpoint]);
3421
+ this.refreshInterval[endpoint] = null;
3422
+ }
3423
+ }
3424
+ };
3425
+ }
3389
3426
  };
3390
3427
 
3391
3428
  //#endregion
@@ -4907,7 +4944,7 @@ const consoleLogger = {
4907
4944
 
4908
4945
  //#endregion
4909
4946
  //#region package.json
4910
- var version = "0.4.0";
4947
+ var version = "0.5.0";
4911
4948
 
4912
4949
  //#endregion
4913
4950
  //#region src/Teslemetry.ts
@@ -4968,27 +5005,25 @@ var Teslemetry = class {
4968
5005
  * @returns A promise that resolves to an object containing vehicle and energy site names, API, and SSE instances.
4969
5006
  */
4970
5007
  async createProducts() {
4971
- const { data } = await getApi1Products({ client: this.client });
4972
- const result = {
4973
- vehicles: {},
4974
- energySites: {}
5008
+ const { data } = await getApiMetadata({ client: this.client });
5009
+ return {
5010
+ vehicles: Object.fromEntries(Object.entries(data.vehicles).map(([vin, metadata]) => [vin, {
5011
+ name: metadata.name ?? useTeslaModel(vin),
5012
+ vin,
5013
+ api: this.api.getVehicle(vin),
5014
+ sse: this.sse.getVehicle(vin),
5015
+ metadata
5016
+ }])),
5017
+ energySites: Object.fromEntries(Object.entries(data.energy_sites ?? {}).map(([id, metadata]) => {
5018
+ const siteId = Number(id);
5019
+ return [id, {
5020
+ name: metadata.name ?? "Unnamed",
5021
+ id: siteId,
5022
+ api: this.api.getEnergySite(siteId),
5023
+ metadata
5024
+ }];
5025
+ }))
4975
5026
  };
4976
- data.response?.forEach((product) => {
4977
- if (product.device_type === "vehicle") result.vehicles[product.vin] = {
4978
- name: product.display_name ?? useTeslaModel(product.vin),
4979
- vin: product.vin,
4980
- api: this.api.getVehicle(product.vin),
4981
- sse: this.sse.getVehicle(product.vin),
4982
- product
4983
- };
4984
- if (product.device_type === "energy") result.energySites[product.energy_site_id] = {
4985
- name: product.site_name ?? "Unnamed",
4986
- site: product.energy_site_id,
4987
- api: this.api.getEnergySite(product.energy_site_id),
4988
- product
4989
- };
4990
- });
4991
- return result;
4992
5027
  }
4993
5028
  /**
4994
5029
  * Get a vehicle API instance for the specified VIN.