-
Notifications
You must be signed in to change notification settings - Fork 4
/
component-camera.js
123 lines (84 loc) · 4.03 KB
/
component-camera.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require('dotenv').config( { silent : process.env.NODE_ENV === 'production' } );
const debug = require('debug')('web-components:component-camera');
const fs = require('fs');
const MAX_SUCCESS_DISPLAY_TIME = 3000;
const wires = {};
module.exports = function(RED) {
RED.nodes.registerType("component-camera", function(config){
debug('Creating node:', this, config);
RED.nodes.createNode(this, config);
const node = this;
debug('CONFIG:', config);
wires[config.id] = config.wires;
if(config.unique && config.unique !== ''){
const uniqueID = config.unique.split('/')[0];
let timeImageWasreceivedAt = 0;
debug('Registering route for component-camera node:', uniqueID)
RED.httpNode.post(`/nr-component-camera/${uniqueID}`, function(req, res) {
new Promise( (resolve, reject) => {
debug('NODE WIRES BEFORE UPDATE:', node.wires);
node.updateWires(wires[node.id]);
debug('NODE WIRES AFTER UPDATE:', node.wires);
const chunks = [];
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET, POST');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
req.on('data', function (data) {
debug('Data:', data);
chunks.push(data);
});
req.on('end', function (data){
debug('Request ended:', data);
res.json({
status : "ok",
message : "Data received successfully"
});
let buf;
if(req.query.type !== 'still' && req.query.type !== 'video'){
reject(`Unknown camera type (should 'still' or video). Refusing to pass on data.`);
} else if(req.query.type === 'still'){
buf = new Buffer( Buffer.concat(chunks).toString(), 'base64' );
} else if(req.query.type === "video"){
buf = Buffer.concat(chunks);
}
resolve(buf);
});
req.on('error:', function(err){
debug('req err:', err);
reject(err);
});
})
.then(imageBuffer => {
debug('Image received. Emitting...', imageBuffer);
timeImageWasreceivedAt = new Date() * 1;
node.status({ fill: "green", shape: "ring", text : 'Data received'});
node.send({
payload: imageBuffer
});
setTimeout(function(){
const currentTime = new Date() * 1;
if(currentTime - timeImageWasreceivedAt > MAX_SUCCESS_DISPLAY_TIME){
node.status({ });
}
}, 3000);
})
.catch(err => {
node.status({ fill: "red", shape: "ring", text: "Node error" });
node.error(err);
});
});
}
});
RED.httpNode.get("/web-components/camera", function(req, res) {
fs.readFile(`${__dirname}/component-scripts/camera.js`, (err, data) => {
if(err){
debug('FS err:', err);
res.status(404);
res.end();
} else {
res.set('Content-Type', 'application/javascript');
res.send(data);
}
});
});
}