Skip to content

Commit a3d07c0

Browse files
committed
mqtt
1 parent 99783c3 commit a3d07c0

File tree

4 files changed

+41
-71
lines changed

4 files changed

+41
-71
lines changed

app/api/plate-reads/route.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ export async function POST(req) {
274274
plateData.plate_number,
275275
{
276276
...plateData,
277-
imageData: data.Image,
278277
camera_name: camera,
279278
timestamp: timestamp,
280279
}

components/chat/ChatButton.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export function ChatButton() {
3232
<TooltipContent side="right" className="border-0 bg-muted">
3333
<div className="text-center">
3434
<p>AI Assistant (Coming Soon!)</p>
35-
<p className="text-xs text-muted-foreground mt-1">
35+
{/* <p className="text-xs text-muted-foreground mt-1">
3636
Press ⌘K or Ctrl+K
37-
</p>
37+
</p> */}
3838
</div>
3939
</TooltipContent>
4040
</Tooltip>

lib/mqtt-client.js

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -101,69 +101,45 @@ async function buildMqttPayload(
101101
plateData = null,
102102
isTest = false
103103
) {
104-
const timestamp = new Date().toISOString();
104+
if (isTest) {
105+
return "This is a test MQTT notification";
106+
}
105107

106-
let message = {
107-
timestamp,
108-
plate_number: plateNumber,
109-
notification_name: notificationConfig.name,
110-
broker_name: brokerInfo.name,
111-
};
108+
// Get camera name and timestamp from plateData
109+
const cameraName =
110+
plateData?.camera_name || plateData?.camera || "Unknown Camera";
111+
const detectionTime = plateData?.timestamp
112+
? new Date(plateData.timestamp).toLocaleString()
113+
: new Date().toLocaleString();
112114

113-
// Add test flag if this is a test notification
114-
if (isTest) {
115-
message.test = true;
116-
message.message = "This is a test MQTT notification";
117-
} else {
118-
// Add custom message if provided
119-
if (notificationConfig.message) {
120-
message.message = notificationConfig.message;
121-
}
115+
let plateDisplayName = plateNumber;
116+
let tagsText = "";
122117

123-
// Add plate recognition data if available
124-
if (plateData) {
125-
message.recognition_data = {
126-
confidence: plateData.confidence,
127-
camera_name: plateData.camera_name,
128-
timestamp: plateData.timestamp,
129-
...(plateData.crop_coordinates && {
130-
crop_coordinates: plateData.crop_coordinates,
131-
}),
132-
...(plateData.ocr_annotation && {
133-
ocr_annotation: plateData.ocr_annotation,
134-
}),
135-
};
118+
// Fetch known plate info and tags
119+
try {
120+
const knownPlates = await getKnownPlates();
121+
const knownPlate = knownPlates.find(
122+
(kp) => kp.plate_number === plateNumber
123+
);
136124

137-
// Add image data if available (base64 encoded)
138-
if (plateData.imageData) {
139-
message.image = {
140-
type: "base64",
141-
data: plateData.imageData.replace(/^data:image\/[a-z]+;base64,/, ""),
142-
format: "jpeg",
143-
};
144-
}
125+
if (knownPlate && knownPlate.name) {
126+
plateDisplayName = knownPlate.name;
145127
}
128+
129+
if (knownPlate && knownPlate.tags && knownPlate.tags.length > 0) {
130+
const tagNames = knownPlate.tags.map((tag) => tag.name || tag).join(", ");
131+
tagsText = `. Tags: ${tagNames}`;
132+
}
133+
} catch (error) {
134+
console.error("Error fetching known plate info for MQTT:", error);
146135
}
147136

148-
// Add known plate information if requested
149-
if (notificationConfig.includeknownplateinfo && !isTest) {
150-
try {
151-
const knownPlates = await getKnownPlates();
152-
const knownPlate = knownPlates.find(
153-
(kp) => kp.plate_number === plateNumber
154-
);
137+
// Create simple text message
138+
let message = `${plateDisplayName} detected on ${cameraName} at ${detectionTime}${tagsText}`;
155139

156-
if (knownPlate) {
157-
message.known_plate_info = {
158-
name: knownPlate.name,
159-
notes: knownPlate.notes,
160-
tags: knownPlate.tags || [],
161-
flagged: knownPlate.flagged,
162-
};
163-
}
164-
} catch (error) {
165-
console.error("Error fetching known plate info for MQTT:", error);
166-
}
140+
// Add custom message if provided
141+
if (notificationConfig.message && notificationConfig.message.trim()) {
142+
message += `. ${notificationConfig.message.trim()}`;
167143
}
168144

169145
return message;
@@ -224,18 +200,13 @@ export async function sendMqttNotification(
224200

225201
// Publish message
226202
await new Promise((resolve, reject) => {
227-
client.publish(
228-
brokerInfo.topic,
229-
JSON.stringify(payload, null, 2),
230-
{ qos: 1 },
231-
(error) => {
232-
if (error) {
233-
reject(error);
234-
} else {
235-
resolve();
236-
}
203+
client.publish(brokerInfo.topic, payload, { qos: 1 }, (error) => {
204+
if (error) {
205+
reject(error);
206+
} else {
207+
resolve();
237208
}
238-
);
209+
});
239210
});
240211

241212
console.log(

test-payload.json

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)