@teslemetry/api 0.5.0 → 0.5.2
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 +105 -4
- package/dist/index.d.cts +347 -120
- package/dist/index.d.mts +347 -120
- package/dist/index.mjs +105 -4
- package/package.json +1 -1
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
|
-
|
|
3114
|
+
if (typeof options?.cache === "boolean") {
|
|
3115
|
+
this.cloudCache = options.cache;
|
|
3116
|
+
this.localCache = options.cache;
|
|
3117
|
+
} else {
|
|
3118
|
+
this.cloudCache = options?.cache?.cloud ?? true;
|
|
3119
|
+
this.localCache = options?.cache?.local ?? true;
|
|
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.
|
|
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.
|
|
5048
|
+
var version = "0.5.2";
|
|
4948
5049
|
|
|
4949
5050
|
//#endregion
|
|
4950
5051
|
//#region src/Teslemetry.ts
|