Skip to content

Commit 5afef9b

Browse files
committed
Move to event emitters
1 parent f6945f0 commit 5afef9b

File tree

7 files changed

+465
-331
lines changed

7 files changed

+465
-331
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@teslemetry/api": minor
3+
---
4+
5+
Refactor to use native event emitters, and add emitters for API get requests.

packages/api/examples/sse.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,23 @@ const { TESLEMETRY_ACCESS_TOKEN, TESLEMETRY_VIN } = config().parsed as Record<
99
string
1010
>;
1111
async function main() {
12-
const teslemetry = new Teslemetry(TESLEMETRY_ACCESS_TOKEN, "na");
12+
const teslemetry = new Teslemetry(TESLEMETRY_ACCESS_TOKEN, { region: "na" });
1313

1414
const sonic = teslemetry.getVehicle(TESLEMETRY_VIN);
1515

1616
// Listen for battery level updates
1717
const l1 = sonic.sse.onSignal("ChargerVoltage", (x) => {
1818
console.log(`BChargerVoltage:`, x);
1919
});
20-
const l2 = sonic.sse.on((x) => {
21-
//console.log(`Sonic:`, x);
20+
const l2 = sonic.sse.on("connectivity", (x) => {
21+
console.log(`Sonic:`, x);
2222
});
23-
const l3 = teslemetry.sse.on((x) => {
24-
//console.log(`Enervything:`, x);
23+
const l3 = sonic.sse.data.on("VehicleSpeed", (x) => {
24+
console.log(`VehicleSpeed:`, x);
2525
});
2626
// Listen for connection status changes
27-
const l4 = teslemetry.sse.onConnection((connected: boolean) => {
28-
console.log(
29-
`Stream connection status: ${connected ? "Connected" : "Disconnected"}`,
30-
);
27+
const l4 = teslemetry.sse.on("connect", () => {
28+
console.log(`Stream connection status: Connected`);
3129
});
3230

3331
// Connect to the stream
@@ -40,9 +38,8 @@ async function main() {
4038
process.on("SIGINT", () => {
4139
console.log("Disconnecting from Teslemetry Stream...");
4240
l1();
43-
l2();
44-
l3();
45-
l4();
41+
teslemetry.sse.removeAllListeners();
42+
sonic.sse.removeAllListeners();
4643
teslemetry.sse.disconnect();
4744
process.exit(0);
4845
});

packages/api/src/TeslemetryApi.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import { TeslemetryVehicleApi } from "./TeslemetryVehicleApi.js";
1515

1616
export class TeslemetryApi {
1717
private root: Teslemetry;
18-
public _vehicles: Record<string, TeslemetryVehicleApi> = {};
19-
public _energySites: Record<string, TeslemetryEnergyApi> = {};
18+
public vehicles: Map<string, TeslemetryVehicleApi> = new Map();
19+
public energySites: Map<number, TeslemetryEnergyApi> = new Map();
2020
public user: TeslemetryUserApi;
2121
public charging: TeslemetryChargingApi;
2222

@@ -31,10 +31,10 @@ export class TeslemetryApi {
3131
* @returns The vehicle API instance for the specified VIN.
3232
*/
3333
public getVehicle(vin: string) {
34-
if (!this._vehicles[vin]) {
35-
this._vehicles[vin] = new TeslemetryVehicleApi(this.root, vin);
34+
if (!this.vehicles.has(vin)) {
35+
new TeslemetryVehicleApi(this.root, vin);
3636
}
37-
return this._vehicles[vin];
37+
return this.vehicles.get(vin)!;
3838
}
3939

4040
/**
@@ -43,10 +43,10 @@ export class TeslemetryApi {
4343
* @returns The energy site API instance for the specified ID.
4444
*/
4545
public getEnergySite(id: number) {
46-
if (!this._energySites[id]) {
47-
this._energySites[id] = new TeslemetryEnergyApi(this.root, id);
46+
if (!this.energySites.has(id)) {
47+
new TeslemetryEnergyApi(this.root, id);
4848
}
49-
return this._energySites[id];
49+
return this.energySites.get(id)!;
5050
}
5151

5252
/**
@@ -63,10 +63,10 @@ export class TeslemetryApi {
6363
this.getEnergySite(product.energy_site_id);
6464
}
6565
});
66-
return { vehicles: this._vehicles, energySites: this._energySites };
66+
return { vehicles: this.vehicles, energySites: this.energySites };
6767
}
6868

69-
public async fields() {
69+
public async getFields() {
7070
const { data } = await getFieldsJson({ client: this.root.client });
7171
return data;
7272
}
@@ -76,7 +76,7 @@ export class TeslemetryApi {
7676
* @returns A promise that resolves to an object containing a `response` array and count.
7777
* Each item in the array is a product, which can be a vehicle or an energy site, and a `count` of the products.
7878
*/
79-
public async products() {
79+
public async getProducts() {
8080
const { data } = await getApi1Products({ client: this.root.client });
8181
return data;
8282
}
@@ -95,7 +95,7 @@ export class TeslemetryApi {
9595
* @returns Promise to an object containing metadata about the account,
9696
* including user UID, region, scopes, and lists of vehicles and energy sites.
9797
*/
98-
public async metadata() {
98+
public async getMetadata() {
9999
const { data } = await getApiMetadata({ client: this.root.client });
100100
return data;
101101
}
@@ -106,7 +106,7 @@ export class TeslemetryApi {
106106
* @returns Promise to an object containing lists of paired and unpaired VINs,
107107
* and detailed info for each vehicle.
108108
*/
109-
public async fleetStatus(vins: string[]) {
109+
public async getFleetStatus(vins: string[]) {
110110
const { data } = await postApi1VehiclesFleetStatus({
111111
body: { vins },
112112
client: this.root.client,
@@ -119,7 +119,7 @@ export class TeslemetryApi {
119119
* @returns Promise to an object containing a list of vehicles,
120120
* pagination details, and a total count.
121121
*/
122-
public async vehicles() {
122+
public async getVehicles() {
123123
const { data } = await getApi1Vehicles({ client: this.root.client });
124124
return data;
125125
}

packages/api/src/TeslemetryEnergyApi.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ export class TeslemetryEnergyApi extends EventEmitter {
5858
public siteId: number;
5959

6060
constructor(root: Teslemetry, siteId: number) {
61+
if (root.api.energySites.has(siteId)) {
62+
throw new Error("Energy site already exists");
63+
}
6164
super();
6265
this.root = root;
6366
this.siteId = siteId;
67+
68+
root.api.energySites.set(siteId, this);
6469
}
6570

6671
/**
@@ -218,18 +223,4 @@ export class TeslemetryEnergyApi extends EventEmitter {
218223
});
219224
return data;
220225
}
221-
222-
public onSiteInfo(
223-
callback: (data: GetApi1EnergySitesByIdSiteInfoResponse) => void,
224-
): () => void {
225-
this.on("siteInfo", callback);
226-
return () => this.off("siteInfo", callback);
227-
}
228-
229-
public onLiveStatus(
230-
callback: (data: GetApi1EnergySitesByIdLiveStatusResponse) => void,
231-
): () => void {
232-
this.on("liveStatus", callback);
233-
return () => this.off("liveStatus", callback);
234-
}
235226
}

0 commit comments

Comments
 (0)