Skip to content

Commit 0be3715

Browse files
authored
Merge pull request #134 from HSLdevcom/feat/vehicle-mode-into-suffix
feat: Add vehicle mode into topicSuffix
2 parents e6867c1 + d424d64 commit 0be3715

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/transformer.test.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ import Pulsar from "pulsar-client";
33
import * as expandedApc from "./quicktype/expandedApc";
44
import { passengerCount } from "./protobuf/passengerCount";
55
import {
6-
getUniqueVehicleIdFromMqttTopic,
6+
getVehicleModeAndUniqueVehicleIdFromMqttTopic,
77
initializeTransformer,
88
wrapDefined,
99
} from "./transformer";
1010

11-
describe("Get unique vehicle IDs", () => {
12-
test("Get a unique vehicle ID from a valid MQTT topic", () => {
11+
describe("Get modes and unique vehicle IDs", () => {
12+
test("Get the mode and the unique vehicle ID from a valid MQTT topic", () => {
1313
const mqttTopic = "/hfp/v2/journey/ongoing/apc/bus/0022/00758";
14-
const uniqueVehicleId = "0022/00758";
15-
expect(getUniqueVehicleIdFromMqttTopic(mqttTopic)).toBe(uniqueVehicleId);
14+
const modeAndUniqueVehicleId = "bus/0022/00758";
15+
expect(getVehicleModeAndUniqueVehicleIdFromMqttTopic(mqttTopic)).toBe(
16+
modeAndUniqueVehicleId,
17+
);
1618
});
1719

18-
test("Get undefined instead of a unique vehicle ID from an invalid MQTT topic", () => {
20+
test("Get undefined instead of the mode and the unique vehicle ID from an invalid MQTT topic", () => {
1921
const mqttTopic = "/hfp/v2/journey/ongoing/foobar/0022/00758";
20-
expect(getUniqueVehicleIdFromMqttTopic(mqttTopic)).toBeUndefined();
22+
expect(
23+
getVehicleModeAndUniqueVehicleIdFromMqttTopic(mqttTopic),
24+
).toBeUndefined();
2125
});
2226
});
2327

@@ -150,7 +154,7 @@ describe("Transformer", () => {
150154
},
151155
},
152156
properties: {
153-
"mqtt-topic": "0018/00817",
157+
"mqtt-topic": "bus/0018/00817",
154158
},
155159
eventTimestamp: 1660731500000,
156160
});
@@ -236,7 +240,7 @@ describe("Transformer", () => {
236240
},
237241
},
238242
properties: {
239-
"mqtt-topic": "0018/01028",
243+
"mqtt-topic": "bus/0018/01028",
240244
},
241245
eventTimestamp: 1707664053746,
242246
});
@@ -314,7 +318,7 @@ describe("Transformer", () => {
314318
},
315319
},
316320
properties: {
317-
"mqtt-topic": "0018/01028",
321+
"mqtt-topic": "bus/0018/01028",
318322
},
319323
eventTimestamp: 1707664053746,
320324
});

src/transformer.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import type Pulsar from "pulsar-client";
44
import type * as expandedApc from "./quicktype/expandedApc";
55
import { passengerCount } from "./protobuf/passengerCount";
66

7-
export const getUniqueVehicleIdFromMqttTopic = (
7+
export const getVehicleModeAndUniqueVehicleIdFromMqttTopic = (
88
topic: string,
99
): string | undefined => {
1010
const parts = topic.split("/");
1111
if (parts.length >= 9) {
12-
return parts.slice(7, 9).join("/");
12+
return parts.slice(6, 9).join("/");
1313
}
1414
return undefined;
1515
};
@@ -149,7 +149,9 @@ export const initializeTransformer = (
149149
"APC data is missing topic and thus the owning operator and its vehicle number",
150150
);
151151
} else {
152-
const mqttTopicSuffix = getUniqueVehicleIdFromMqttTopic(apcData.topic);
152+
const mqttTopicSuffix = getVehicleModeAndUniqueVehicleIdFromMqttTopic(
153+
apcData.topic,
154+
);
153155
if (mqttTopicSuffix === undefined) {
154156
logger.warn(
155157
{ topic: apcData.topic },

0 commit comments

Comments
 (0)