Skip to content

Commit 41d7f07

Browse files
committed
fix json and timestamp
1 parent a3d07c0 commit 41d7f07

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

lib/mqtt-client.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,23 @@ async function buildMqttPayload(
102102
isTest = false
103103
) {
104104
if (isTest) {
105-
return "This is a test MQTT notification";
105+
return JSON.stringify({
106+
plate_number: "TEST123",
107+
camera: "Test Camera",
108+
timestamp: new Date().toLocaleString(),
109+
message: "This is a test MQTT notification",
110+
});
106111
}
107112

108113
// Get camera name and timestamp from plateData
109114
const cameraName =
110115
plateData?.camera_name || plateData?.camera || "Unknown Camera";
111-
const detectionTime = plateData?.timestamp
116+
const timestamp = plateData?.timestamp
112117
? new Date(plateData.timestamp).toLocaleString()
113118
: new Date().toLocaleString();
114119

115120
let plateDisplayName = plateNumber;
116-
let tagsText = "";
121+
let tags = [];
117122

118123
// Fetch known plate info and tags
119124
try {
@@ -127,22 +132,31 @@ async function buildMqttPayload(
127132
}
128133

129134
if (knownPlate && knownPlate.tags && knownPlate.tags.length > 0) {
130-
const tagNames = knownPlate.tags.map((tag) => tag.name || tag).join(", ");
131-
tagsText = `. Tags: ${tagNames}`;
135+
tags = knownPlate.tags.map((tag) => tag.name || tag);
132136
}
133137
} catch (error) {
134138
console.error("Error fetching known plate info for MQTT:", error);
135139
}
136140

137-
// Create simple text message
138-
let message = `${plateDisplayName} detected on ${cameraName} at ${detectionTime}${tagsText}`;
141+
// Create JSON payload similar to Blue Iris format
142+
const payload = {
143+
plate_name: plateDisplayName !== plateNumber ? plateDisplayName : null,
144+
plate_number: plateNumber,
145+
camera: cameraName,
146+
timestamp: timestamp,
147+
};
148+
149+
// Add tags if available
150+
if (tags.length > 0) {
151+
payload.tags = tags;
152+
}
139153

140154
// Add custom message if provided
141155
if (notificationConfig.message && notificationConfig.message.trim()) {
142-
message += `. ${notificationConfig.message.trim()}`;
156+
payload.message = notificationConfig.message.trim();
143157
}
144158

145-
return message;
159+
return JSON.stringify(payload);
146160
}
147161

148162
export async function sendMqttNotification(
@@ -198,7 +212,7 @@ export async function sendMqttNotification(
198212
isTest
199213
);
200214

201-
// Publish message
215+
// Publish message (payload is already a JSON string)
202216
await new Promise((resolve, reject) => {
203217
client.publish(brokerInfo.topic, payload, { qos: 1 }, (error) => {
204218
if (error) {

0 commit comments

Comments
 (0)