Get frame from MotionEye
Need to install npm nodeFetch
and follow this guide: https://nodered.org/docs/user-guide/context
const nodeFetch = context.global.get('nodeFetch');
const fs = context.global.get('fs');
const url = 'http://my.motion.eye.url./picture/2/current';
const getRandomIntInclusive = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
};
const pictureFilename = `${new Date().getTime()}_${getRandomIntInclusive(0, 9999).toString().padStart(4, 0)}.jpg`;
const savePath = "/tmp/";
const fullPath = `${savePath}${pictureFilename}`;
const res = await nodeFetch(url);
const fileStream = fs.createWriteStream(`${savePath}${pictureFilename}`);
await new Promise((resolve, reject) => {
res.body.pipe(fileStream);
res.body.on("error", reject);
fileStream.on("finish", resolve);
});
// Do what you want with it the photo. Image path is: fullPath
// Delete image after 3 seconds
setTimeout(() => {
fs.unlink(fullPath, () => {});
}, 3000);
return msg;
[{"id":"767da24912b61bf4","type":"function","z":"b1dc1d46e5c77129","name":"","func":"const nodeFetch = context.global.get('nodeFetch');\nconst fs = context.global.get('fs');\nconst url = 'http://my.motion.eye.url./picture/2/current';\n\nconst getRandomIntInclusive = (min, max) => {\n min = Math.ceil(min);\n max = Math.floor(max);\n return Math.floor(Math.random() * (max - min + 1) + min);\n};\n\nconst pictureFilename = `${new Date().getTime()}_${getRandomIntInclusive(0, 9999).toString().padStart(4, 0)}.jpg`;\nconst savePath = \"/tmp/\";\nconst fullPath = `${savePath}${pictureFilename}`;\n\nconst res = await nodeFetch(url);\nconst fileStream = fs.createWriteStream(`${savePath}${pictureFilename}`);\n\nawait new Promise((resolve, reject) => {\n res.body.pipe(fileStream);\n res.body.on(\"error\", reject);\n fileStream.on(\"finish\", resolve);\n});\n\n// Do what you want with it the photo. Image path is: fullPath\n\n// Delete image after 3 seconds\nsetTimeout(() => {\n fs.unlink(fullPath, () => {});\n}, 3000);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1780,"y":720,"wires":[[]]}]