Skip to content

Commit 1c0bf89

Browse files
DIMO-Networkjaggedbytes
authored andcommitted
Update vehicle location data retrieval to match API schema changes
Adjust DIMO service to use new `currentLocationCoordinates` field, replacing separate latitude and longitude fields, and update historical data querying and processing logic accordingly. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 0c4f817a-ba25-473f-b38d-fa2e8fbded69 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: ab440250-bbc4-4606-acb9-c1c8541189fa Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/0b0e3706-744d-42a8-93dc-5271e81245b9/0c4f817a-ba25-473f-b38d-fa2e8fbded69/XGXQaqq Replit-Helium-Checkpoint-Created: true
1 parent f4cc66f commit 1c0bf89

1 file changed

Lines changed: 34 additions & 24 deletions

File tree

server/dimo-service.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,13 @@ export class DimoService {
8383
const query = `
8484
{
8585
signalsLatest(tokenId: ${tokenId}) {
86-
currentLocationLatitude {
86+
currentLocationCoordinates {
8787
timestamp
88-
value
89-
}
90-
currentLocationLongitude {
91-
timestamp
92-
value
93-
}
94-
dimoAftermarketHDOP {
95-
timestamp
96-
value
88+
value {
89+
latitude
90+
longitude
91+
hdop
92+
}
9793
}
9894
lastSeen
9995
}
@@ -108,9 +104,10 @@ export class DimoService {
108104
console.log("DIMO Telemetry API response:", locationData);
109105

110106
const signalsData = locationData?.data?.signalsLatest;
111-
const latitude = signalsData?.currentLocationLatitude?.value;
112-
const longitude = signalsData?.currentLocationLongitude?.value;
113-
const hdop = signalsData?.dimoAftermarketHDOP?.value;
107+
const coords = signalsData?.currentLocationCoordinates?.value;
108+
const latitude = coords?.latitude;
109+
const longitude = coords?.longitude;
110+
const hdop = coords?.hdop;
114111

115112
if (!latitude || !longitude) {
116113
throw new Error("No location data available for this vehicle");
@@ -121,7 +118,7 @@ export class DimoService {
121118
lat: parseFloat(latitude),
122119
lng: parseFloat(longitude),
123120
hdop: hdop ? parseFloat(hdop) : 1.0,
124-
timestamp: signalsData?.lastSeen || new Date().toISOString(),
121+
timestamp: signalsData?.currentLocationCoordinates?.timestamp || signalsData?.lastSeen || new Date().toISOString(),
125122
};
126123
} catch (error) {
127124
console.error("Error fetching DIMO vehicle location:", error);
@@ -139,7 +136,7 @@ export class DimoService {
139136
const from = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString();
140137
const to = new Date().toISOString();
141138

142-
// Query telemetry API for latest location data
139+
// Query telemetry API for historical location data
143140
const query = `
144141
{
145142
signals(
@@ -148,8 +145,12 @@ export class DimoService {
148145
to: "${to}",
149146
interval: "6h"
150147
) {
151-
currentLocationLatitude (agg: LAST)
152-
currentLocationLongitude (agg: LAST)
148+
timestamp
149+
currentLocationCoordinates (agg: LAST) {
150+
latitude
151+
longitude
152+
hdop
153+
}
153154
}
154155
}
155156
`;
@@ -169,25 +170,34 @@ export class DimoService {
169170
throw new Error("No location data available for this vehicle");
170171
}
171172

173+
// Filter out entries without valid coordinates
174+
const validPoints = signalsData.filter(
175+
(point: any) => point.currentLocationCoordinates?.latitude && point.currentLocationCoordinates?.longitude
176+
);
177+
178+
if (validPoints.length === 0) {
179+
throw new Error("No location data available for this vehicle");
180+
}
181+
172182
// Average lat/lng
173-
const { totalLat, totalLng } = signalsData.reduce(
174-
(acc, point) => {
175-
acc.totalLat += point.currentLocationLatitude;
176-
acc.totalLng += point.currentLocationLongitude;
183+
const { totalLat, totalLng } = validPoints.reduce(
184+
(acc: any, point: any) => {
185+
acc.totalLat += point.currentLocationCoordinates.latitude;
186+
acc.totalLng += point.currentLocationCoordinates.longitude;
177187
return acc;
178188
},
179189
{ totalLat: 0, totalLng: 0 },
180190
);
181191

182-
const avgLat = totalLat / signalsData.length;
183-
const avgLng = totalLng / signalsData.length;
192+
const avgLat = totalLat / validPoints.length;
193+
const avgLng = totalLng / validPoints.length;
184194

185195
// Convert to GPS format for your app
186196
return {
187197
lat: avgLat,
188198
lng: avgLng,
189199
hdop: 1000.0,
190-
datapoints: signalsData.length,
200+
datapoints: validPoints.length,
191201
};
192202
} catch (error) {
193203
console.error("Error fetching DIMO vehicle location:", error);

0 commit comments

Comments
 (0)