Skip to content

Commit

Permalink
feat: sockets for live reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
jakowenko committed Sep 20, 2021
1 parent 6d7ad32 commit 50fef76
Show file tree
Hide file tree
Showing 11 changed files with 406 additions and 135 deletions.
110 changes: 110 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"multer": "^1.4.3",
"node-schedule": "^2.0.0",
"sharp": "^0.28.3",
"socket.io": "^4.2.0",
"traverse": "^0.6.6",
"uuid": "^8.3.2",
"winston": "^3.3.3"
Expand Down
4 changes: 3 additions & 1 deletion api/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('./src/util/logger.util').init();
const http = require('http');
const socket = require('./src/util/socket.util');
const { SERVER } = require('./src/constants');
const { version } = require('./package.json');
const mqtt = require('./src/util/mqtt.util');
Expand All @@ -14,9 +15,10 @@ module.exports.start = async () => {
console.log(`Double Take v${version}`);
console.log(config());
await database.init();
http.Server(app).listen(SERVER.PORT);
const server = http.Server(app).listen(SERVER.PORT);
mqtt.connect();
storage.purge();
socket.connect(server);
};

try {
Expand Down
8 changes: 3 additions & 5 deletions api/src/controllers/recognize.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const recognize = require('../util/recognize.util');
const frigate = require('../util/frigate.util');
const { jwt } = require('../util/auth.util');
const mqtt = require('../util/mqtt.util');
const { emit } = require('../util/socket.util');
const { BAD_REQUEST } = require('../constants/http-status');
const DETECTORS = require('../constants/config').detectors();
const { AUTH, FRIGATE, TOKEN } = require('../constants');
Expand Down Expand Up @@ -167,12 +168,9 @@ module.exports.start = async (req, res) => {
res.send(output);

mqtt.recognize(output);

notify.publish(output, camera, results);

if (output.matches.length) {
IDS.push(id);
}
if (output.matches.length) IDS.push(id);
if (results.length) emit('recognize', true);
} catch (error) {
PROCESSING = false;
res.send(error);
Expand Down
13 changes: 13 additions & 0 deletions api/src/util/socket.util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const socket = require('socket.io');

let io = false;

module.exports.connect = (server) => {
io = socket(server, {
cors: {
origin: true,
},
});
};

module.exports.emit = (event, message) => (io ? io.emit(event, message) : false);
Loading

0 comments on commit 50fef76

Please sign in to comment.