Skip to content

Commit

Permalink
feat: support for multiple frigate urls and topics
Browse files Browse the repository at this point in the history
  • Loading branch information
jakowenko committed Aug 4, 2021
1 parent 5961306 commit 4ead9f7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
12 changes: 8 additions & 4 deletions api/src/controllers/recognize.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
}`,
}
)
);
Expand All @@ -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
}`,
}
)
);
Expand Down
19 changes: 15 additions & 4 deletions api/src/util/frigate.util.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -57,26 +58,36 @@ module.exports.checks = async ({
return `already processed ${id}`;
}

await frigate.status();
await frigate.status(topic);

return true;
} catch (error) {
throw new Error(error.message);
}
};

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) {
throw new Error(`frigate status error: ${error.message}`);
}
};

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;
Expand Down
1 change: 1 addition & 0 deletions api/src/util/mqtt.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const processMessage = ({ topic, message }) => {
headers: AUTH ? { authorization: jwt.sign({ route: 'recognize' }) } : null,
data: {
...JSON.parse(message.toString()),
topic,
},
});
} catch (error) {
Expand Down

0 comments on commit 4ead9f7

Please sign in to comment.