generated from pandosme/node-red-axis-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
axis-acap.js
163 lines (149 loc) · 4.58 KB
/
axis-acap.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
//Copyright (c) 2021-2022 Fred Juhlin
const fs = require("fs");
const VapixWrapper = require('./vapix-wrapper');
module.exports = function(RED) {
function Axis_ACAP(config) {
RED.nodes.createNode(this,config);
this.preset = config.preset;
this.action = config.action;
this.acap = config.acap;
var node = this;
node.on('input', function(msg) {
node.status({});
var device = {
address: null,
user: null,
password: null,
protocol: "http"
}
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"
}
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;
}
var action = msg.action || node.action;
var acap = node.acap || msg.payload;
if( typeof acap !== "string" || acap.length < 2 )
acap = null;
switch( action ) {
case "ACAP Status":
VapixWrapper.ACAP_List( device, function( error, response ) {
msg.payload = response;
if( error ) {
node.error("ACAP Status failed on " + device.address, msg);
return;
}
if( acap ) {
var selectedACAP = null;
response.forEach( function( item ){
if( item.Name === acap )
selectedACAP = item;
});
if( selectedACAP ) {
msg.payload = selectedACAP;
node.send(msg);
return;
} else {
msg.payload = acap + " is not installed";
node.error("ACAP Status failed on " + device.address, msg);
return;
}
}
node.send(msg);
});
break;
case "Start ACAP":
if(!acap) {
msg.payload = "Undefined ACAP package id";
node.error("Start ACAP failed on " + device.address,msg);
return;
}
VapixWrapper.ACAP_Control( device, "start", acap, function(error, response){
msg.payload = response;
if( error ) {
node.error("Start ACAP failed on " + device.address,msg);
return;
}
msg.payload = acap;
node.send(msg);
});
break;
case "Stop ACAP":
if(!acap) {
msg.payload = "Undefined ACAP package id";
node.error("Stop ACAP failed on " + device.address,msg);
return;
}
VapixWrapper.ACAP_Control( device, "stop", acap, function(error, response){
msg.payload = response;
if( error ) {
msg.payload.action = action;
msg.payload.address = device.address;
node.error("Stop ACAP failed on " + device.address,msg);
return;
}
msg.payload = acap;
node.send(msg);
});
break;
case "Remove ACAP":
if(!acap) {
msg.payload = "Undefined ACAP package id";
node.error("Remove ACAP failed on " + device.address,msg);
return;
}
VapixWrapper.ACAP_Control( device, "remove", acap, function(error, response){
msg.payload = response;
if( error ) {
node.error("Remove ACAP failed on " + device.address,msg);
return;
}
msg.payload = acap;
node.send(msg);
});
break;
case "Install ACAP":
var filename = msg.filename || node.filename;
if( !fs.existsSync(filename) ) {
msg.payload = filename + " does not exist";
node.error("Install ACAP failed on " + device.address, msg);
return;
}
node.status({fill:"blue",shape:"dot",text:"Installing..."});
VapixWrapper.Upload_ACAP( device, filename, function(error, response){
msg.payload = response;
if( error ) {
node.status({fill:"red",shape:"dot",text:"Failed"});
node.error("Install ACAP failed on " + device.address, msg);
return;
}
if( typeof response === "object" && response.hasOwnProperty("data") ) {
msg.payload = response.data.id;
} else
msg.payload = acap;
node.status({fill:"green",shape:"dot",text:"Success"});
node.send(msg);
});
break;
default:
node.error("Invalid action", msg);
break;
}
});
}
RED.nodes.registerType("Axis ACAP", Axis_ACAP,{
defaults: {
action: { type:"text" },
preset: {type:"Device Access"},
action: { type:"text" },
acap: { type:"text" }
}
});
}