@@ -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 ( / ^ d a t a : i m a g e \/ [ a - z ] + ; b a s e 6 4 , / , "" ) ,
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 (
0 commit comments