generated from pandosme/node-red-axis-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
axis-camera.js
304 lines (283 loc) · 10.2 KB
/
axis-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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//Copyright (c) 2021-2024 Fred Juhlin
const VapixWrapper = require('./vapix-wrapper');
const xml2js = require('xml2js');
const fs = require("fs");
module.exports = function(RED) {
function Axis_Camera(config) {
RED.nodes.createNode(this,config);
this.preset = config.preset;
this.action = config.action;
this.resolution = config.resolution;
this.output = config.output;
this.options = config.options;
this.filename = config.filename;
var node = this;
node.on('input', function(msg) {
node.status({});
var preset = RED.nodes.getNode(node.preset);
var device = {
address: msg.address || preset.address,
user: msg.user || preset.credentials.user,
password: msg.password || preset.credentials.password,
protocol: "http"
}
var action = msg.action || node.action;
var filename = msg.filename || node.filename;
var options = node.options || msg.payload;
if( !device.address || device.address.length === 0 || !device.user || device.user.length === 0 || !device.password || device.password.length === 0 ) {
msg.payload = "Missing one or more attributes (address,user,password)";
node.error("Access failed",msg);
return;
}
msg.error = false;
switch( action ) {
case "JPEG Image":
var resolution = "resolution=" + node.resolution;
if( msg.resolution )
resolution = "resolution=" + msg.resolution;
VapixWrapper.JPEG( device, resolution, function( error, response) {
msg.payload = response;
if( error ) {
node.error("JPEG Imaged failed on " + device.address, msg);
return;
}
if( node.output === "Base64" )
msg.payload = response.toString('base64');
node.send(msg);
});
break;
case "Start recording":
var cgi = "/axis-cgi/record/record.cgi?diskid=SD_DISK";
VapixWrapper.HTTP_Get( device, cgi, "text", function( error, response) {
msg.payload = response;
if( error ) {
node.error("Start recording failed on " + device.address, msg);
return;
}
var parser = new xml2js.Parser({
explicitArray: false,
mergeAttrs: true
});
parser.parseString(response, function (err, result) {
if( err ) {
msg.payload = "Invalid XML response from camera";
node.error("Start recording failed on " + device.address, msg);
return;
}
if( result.hasOwnProperty("root")
&& result.root.hasOwnProperty("record")
&& result.root.record.hasOwnProperty("result")
&& result.root.record.result === "OK" ) {
msg.payload = result.root.record.recordingid;
node.send(msg);
return;
}
msg.payload = result;
node.error( "Start recording failed on " + device.address, msg);
});
});
break;
case "Stop recording":
if( typeof msg.payload !== "string" || msg.payload.length < 10 ) {
msg.payload = "Input must be a valid recording ID string";
node.error("Stop recording failed on " + device.address,msg);
return;
}
var id = msg.payload;
var cgi = "/axis-cgi/record/stop.cgi?recordingid=" + msg.payload;
VapixWrapper.HTTP_Get( device, cgi, "text", function( error, response) {
if( error ) {
msg.payload = response;
node.error("Stop recording failed on " + device.address,msg);
return;
}
msg.payload = id;
node.send(msg);
});
break;
case "Camera Info":
VapixWrapper.Param_Get( device, "properties", function( error, response ) {
msg.payload = response;
if( error ) {
node.error("Camera Info failed on " + device.address, msg);
return;
}
var info = {};
if( !response.hasOwnProperty("Image") || !response.Image.hasOwnProperty("Format")) {
msg.payload = "No camera info";
node.error("Camera Info failed on " + device.address, msg);
return;
}
info.formats = response.Image.Format.split(","),
info.resolutions = response.Image.Resolution.split(",")
info.largest = info.resolutions[0];
info.medium = "640x480";
info.smallest = info.resolutions[info.resolutions.length-1];
info.aspect = "4:3";
info.rotation = 0;
VapixWrapper.Param_Get( device, "ImageSource.I0", function( error, response ) {
if( error ) {
msg.payload = response;
node.error("Camera Info failed on " + device.address, msg);
return;
}
if( response.hasOwnProperty("I0") ) {
if( response.I0.hasOwnProperty("Sensor") && response.I0.Sensor.hasOwnProperty("AspectRatio") ) {
info.aspect = response.I0.Sensor.AspectRatio;
if( info.aspect === "16:9")
info.medium = "640x360";
if( info.aspect === "4:3")
info.medium = "640x480";
if( info.aspect === "1:1")
info.medium = "640x640";
if( info.aspect === "16:10")
info.medium = "640x400";
}
if( response.I0.hasOwnProperty("Rotation") )
info.rotation = parseInt(response.I0.Rotation);
}
msg.payload = info;
node.send(msg);
});
});
break;
case "Get image settings":
VapixWrapper.Param_Get( device, "ImageSource.I0.Sensor", function( error, response ) {
if( error ) {
msg.payload = response;
node.error("Get image settings failed on " + device.address, msg);
return;
}
var settings = {
Brightness: parseInt(response.I0.Sensor.Brightness),
ColorLevel: parseInt(response.I0.Sensor.ColorLevel),
Contrast: parseInt(response.I0.Sensor.Contrast),
Exposure: response.I0.Sensor.Exposure,
WhiteBalance: response.I0.Sensor.WhiteBalance,
WDR: response.I0.Sensor.WDR === "on"
}
VapixWrapper.Param_Get( device, "ImageSource.I0.DayNight", function( error, response ) {
if( error ) {
msg.payload = response;
node.error("Get image settings failed on " + device.address, msg);
return;
}
settings.DayLevel = parseInt(response.I0.DayNight.ShiftLevel);
if( response.I0.IrCutFilter === "yes")
settings.DayLevel = 100;
if( response.I0.IrCutFilter === "no")
settings.DayLevel = 0;
msg.payload = settings;
node.send(msg);
});
});
break;
case "Set image settings":
if( typeof options === "string" )
options = JSON.parse(options);
var sensor = JSON.parse( JSON.stringify(options) );
sensor.WDR = sensor.WDR ? "on":"off";
delete sensor.DayLevel;
VapixWrapper.Param_Set( device, "ImageSource.I0.Sensor", sensor, function( error, response ) {
if( error ) {
msg.payload = response;
node.error("Set image settings failed on " + device.address, msg);
return;
}
if( !options.hasOwnProperty("DayLevel") ) {
msg.payload = "Missing propertry DayLevel";
node.error("Set image settings failed on " + device.address, msg);
return;
}
var DayNight = {
IrCutFilter: "auto",
ShiftLevel: options.DayLevel
}
if( options.DayLevel === 0 )
DayNight.IrCutFilter = "no";
if( options.DayLevel === 100 )
DayNight.IrCutFilter = "yes";
VapixWrapper.Param_Set( device, "ImageSource.I0.DayNight", DayNight, function( error, response ) {
msg.payload = response;
if( error ) {
msg.payload = response;
node.error("Set image settings failed on " + device.address, msg);
return;
}
msg.payload = options;
node.send(msg);
});
});
break;
case "Recordings":
VapixWrapper.Recordings( device, msg.payload, function(error,response ) {
msg.payload = response;
if( error ) {
node.error("List Recordings failed on " + device.address, msg);
return;
}
msg.payload.forEach( function(item){
item.url = device.protocol + "://" + device.address;
item.url += "/axis-cgi/record/export/exportrecording.cgi?schemaversion=1&exportformat=matroska";
item.url += "&recordingid=" + item.id;
item.url += "&diskid=" + item.storage;
})
node.send( msg );
});
break;
case "Export Recording":
if( !msg.payload.hasOwnProperty("storage") || !msg.payload.hasOwnProperty("id") ) {
msg.payload = "Missing property storage or id";
node.error("Export Recording failed on " + device.address, msg );
return;
}
var cgi = "/axis-cgi/record/export/exportrecording.cgi?schemaversion=1&exportformat=matroska";
cgi += "&recordingid=" + msg.payload.id;
cgi += "&diskid=" + msg.payload.storage;
VapixWrapper.HTTP_Get( device, cgi, "blob", function(error, response) {
msg.payload = response;
if( error ) {
node.error("Export Recording failed on " + device.address, msg );
return;
}
node.send( msg );
});
break;
case "Upload overlay":
node.status({fill:"blue",shape:"dot",text:"Uploading image..."});
if( typeof options === "string" )
options = JSON.parse( options );
VapixWrapper.Upload_Overlay( device, filename, options, function(error, response){
msg.payload = response;
if( error ) {
node.status({fill:"red",shape:"dot",text:"Upload failed"});
node.error("Upload overlay failed on " + device.address, msg);
return;
}
if( msg.payload.hasOwnProperty("error") ) {
msg.payload = msg.payload.error;
node.error("Upload overlay failed on " + device.address, msg);
return;
}
node.status({fill:"green",shape:"dot",text:"Upload success"});
node.send(msg);
});
break;
default:
node.warn( action + " is not yet implemented");
break;
}
});
}
RED.nodes.registerType("Axis camera",Axis_Camera,{
defaults: {
name: { type:"text" },
preset: {type:"Device Access"},
action: { type:"text" },
resolution: { type:"text" },
output: { type:"text" },
options: { type:"text" },
filename: { type:"text" }
}
});
}