Skip to content

Commit dd8769f

Browse files
authored
Merge pull request #26 from EVNotify/distance
✨ Distance calculation within logs sync
2 parents fc3e6b2 + 8ba2ddd commit dd8769f

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/logs/handler/metadata.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { STATUS } from '../entities/status.entity';
1111
import { Log } from '../schemas/log.schema';
1212
import { Sync } from '../schemas/sync.schema';
1313
import { TYPE } from '../entities/type.entity';
14+
import { buffer, distance, lineString, point, pointToLineDistance } from "@turf/turf";
1415

1516
@Injectable()
1617
export class MetadataHandler {
@@ -93,9 +94,13 @@ export class MetadataHandler {
9394
log.history.forEach((entry) => {
9495
if (entry.dcBatteryPower != null) {
9596
averageKWs.push(entry.dcBatteryPower);
96-
} else if (entry.speed != null) {
97+
}
98+
99+
if (entry.speed != null) {
97100
averageSpeeds.push(entry.speed);
98-
} else if (entry.latitude != null && entry.longitude != null) {
101+
}
102+
103+
if (entry.latitude != null && entry.longitude != null) {
99104
distances.push({
100105
latitude: entry.latitude,
101106
longitude: entry.longitude,
@@ -114,7 +119,28 @@ export class MetadataHandler {
114119
averageSpeeds.reduce((a, b) => a + b, sync.speed) /
115120
(averageSpeeds.length + 1);
116121
}
117-
// TODO calculate distance
122+
123+
let totalDistance = 0;
124+
125+
if (sync.latitude != null && sync.longitude != null) {
126+
distances.push({
127+
latitude: sync.latitude,
128+
longitude: sync.longitude,
129+
});
130+
}
131+
132+
distances.forEach((coord, index) => {
133+
if (index === 0) {
134+
return;
135+
}
136+
137+
const currentCoord = point([coord.longitude, coord.latitude]);
138+
const previousCoord = point([distances[index - 1].longitude, distances[index - 1].latitude]);
139+
140+
totalDistance += distance(previousCoord, currentCoord);
141+
});
142+
143+
log.distance = parseFloat(totalDistance.toFixed(2));
118144

119145
if (sync.cec != null && log.startCEC) {
120146
const amount = parseFloat((sync.cec - log.startCEC).toFixed(1));

src/logs/logs.controller.spec.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,10 @@ describe('LogsController', () => {
357357

358358
dto.dcBatteryPower = 2;
359359
dto.cec = 30069.1;
360+
dto.ced = 30059;
360361
dto.socDisplay = 77;
362+
dto.latitude = 32.125;
363+
dto.longitude = 34.112;
361364

362365
await controller.syncData(testAccount.akey, dto);
363366

@@ -370,7 +373,8 @@ describe('LogsController', () => {
370373
expect(response).toHaveProperty('rechargedKWh', 0);
371374
expect(response).toHaveProperty('startSOC', 75);
372375
expect(response).toHaveProperty('currentSOC', 77);
373-
expect(response).toHaveProperty('dischargedKWh', undefined);
376+
expect(response).toHaveProperty('dischargedKWh', 0);
377+
expect(response).toHaveProperty('distance', 0);
374378
});
375379

376380
it('should update rechargedKWh metadata when adding new cec value', async () => {
@@ -388,6 +392,37 @@ describe('LogsController', () => {
388392
expect(response).toHaveProperty('rechargedKWh', 0.2);
389393
});
390394

395+
it('should update dischargedKWh metadata when adding new ced value', async () => {
396+
const dto = new SyncDto();
397+
398+
dto.ced = 30062.4;
399+
400+
await controller.syncData(testAccount.akey, dto);
401+
402+
await new Promise((resolve) => setTimeout(resolve, 1000));
403+
404+
const response = await controller.findOne(testAccount.akey, chargeLogId);
405+
406+
expect(response).toBeInstanceOf(LogDto);
407+
expect(response).toHaveProperty('dischargedKWh', 3.4);
408+
});
409+
410+
it('should update distance metadata when adding new location', async () => {
411+
const dto = new SyncDto();
412+
413+
dto.latitude = 32.135;
414+
dto.longitude = 34.125;
415+
416+
await controller.syncData(testAccount.akey, dto);
417+
418+
await new Promise((resolve) => setTimeout(resolve, 1000));
419+
420+
const response = await controller.findOne(testAccount.akey, chargeLogId);
421+
422+
expect(response).toBeInstanceOf(LogDto);
423+
expect(response).toHaveProperty('distance', 1.65);
424+
});
425+
391426
it('should find current running log', async () => {
392427
const response = await controller.findRunning(testAccount.akey);
393428

0 commit comments

Comments
 (0)