diff --git a/api/src/controllers/recognize.controller.js b/api/src/controllers/recognize.controller.js index 63c511e0..a7070406 100644 --- a/api/src/controllers/recognize.controller.js +++ b/api/src/controllers/recognize.controller.js @@ -52,10 +52,10 @@ module.exports.start = async (req, res) => { }; if (event.type === 'frigate') { - const { type: frigateEventType } = req.body; + const { type: frigateEventType, topic } = req.body; const attributes = req.body.after ? req.body.after : req.body.before; const { id, label, camera, current_zones: zones } = attributes; - event = { id, label, camera, zones, frigateEventType, ...event }; + event = { id, label, camera, zones, frigateEventType, topic, ...event }; } else { const { url, camera } = req.query; @@ -105,7 +105,9 @@ module.exports.start = async (req, res) => { ...config, retries: FRIGATE.ATTEMPTS.LATEST, type: 'latest', - url: `${FRIGATE.URL}/api/${camera}/latest.jpg?h=${FRIGATE.IMAGE.HEIGHT}`, + url: `${frigate.topicURL(event.topic)}/api/${camera}/latest.jpg?h=${ + FRIGATE.IMAGE.HEIGHT + }`, } ) ); @@ -117,7 +119,9 @@ module.exports.start = async (req, res) => { ...config, retries: FRIGATE.ATTEMPTS.SNAPSHOT, type: 'snapshot', - url: `${FRIGATE.URL}/api/events/${id}/snapshot.jpg?crop=1&h=${FRIGATE.IMAGE.HEIGHT}`, + url: `${frigate.topicURL(event.topic)}/api/events/${id}/snapshot.jpg?crop=1&h=${ + FRIGATE.IMAGE.HEIGHT + }`, } ) ); diff --git a/api/src/util/frigate.util.js b/api/src/util/frigate.util.js index eead47b7..c3b3db07 100644 --- a/api/src/util/frigate.util.js +++ b/api/src/util/frigate.util.js @@ -1,13 +1,14 @@ const axios = require('axios'); const sleep = require('./sleep.util'); -const { FRIGATE } = require('../constants'); +const { FRIGATE, MQTT } = require('../constants'); const frigate = this; module.exports.checks = async ({ id, frigateEventType: type, + topic, label, camera, zones, @@ -57,7 +58,7 @@ module.exports.checks = async ({ return `already processed ${id}`; } - await frigate.status(); + await frigate.status(topic); return true; } catch (error) { @@ -65,11 +66,11 @@ module.exports.checks = async ({ } }; -module.exports.status = async () => { +module.exports.status = async (topic) => { try { const request = await axios({ method: 'get', - url: `${FRIGATE.URL}/api/version`, + url: `${this.topicURL(topic)}/api/version`, }); return request.data; } catch (error) { @@ -77,6 +78,16 @@ module.exports.status = async () => { } }; +module.exports.topicURL = (topic) => { + try { + if (typeof FRIGATE.URL === 'string') return FRIGATE.URL; + return FRIGATE.URL[MQTT.TOPICS.FRIGATE.indexOf(topic)]; + } catch (error) { + error.message = `frigate topic url error: ${error.message}`; + throw error; + } +}; + module.exports.snapshotReady = async (id) => { let loop = true; let ready = false; diff --git a/api/src/util/mqtt.util.js b/api/src/util/mqtt.util.js index 5a67eb79..2913c3f2 100644 --- a/api/src/util/mqtt.util.js +++ b/api/src/util/mqtt.util.js @@ -65,6 +65,7 @@ const processMessage = ({ topic, message }) => { headers: AUTH ? { authorization: jwt.sign({ route: 'recognize' }) } : null, data: { ...JSON.parse(message.toString()), + topic, }, }); } catch (error) {