node-red-contrib-hik-media-buffer 1.1.7 → 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hik-media-buffer.js +11 -26
- package/package.json +1 -1
package/hik-media-buffer.js
CHANGED
|
@@ -11,7 +11,7 @@ module.exports = function(RED) {
|
|
|
11
11
|
RED.nodes.createNode(this, config);
|
|
12
12
|
const node = this;
|
|
13
13
|
|
|
14
|
-
node.name = config.name || "TEST";
|
|
14
|
+
node.name = config.name || "TEST";
|
|
15
15
|
node.host = config.host;
|
|
16
16
|
node.port = config.port || "80";
|
|
17
17
|
node.protocol = config.protocol || "http";
|
|
@@ -29,12 +29,10 @@ module.exports = function(RED) {
|
|
|
29
29
|
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
|
|
30
30
|
const EventList = ["FieldDetection", "LineDetection"];
|
|
31
31
|
|
|
32
|
-
// ---
|
|
32
|
+
// --- QUESTE RIGHE SERVONO PER IL SITO ---
|
|
33
33
|
const baseStorage = `C:\\Users\\APerucca\\Documents\\progetto-docker\\storage\\${node.name}`;
|
|
34
34
|
const imgDir = path.join(baseStorage, "allarmi");
|
|
35
35
|
const vidDir = path.join(baseStorage, "video");
|
|
36
|
-
|
|
37
|
-
// Crea le cartelle se non esistono
|
|
38
36
|
if (!fs.existsSync(imgDir)) fs.mkdirSync(imgDir, { recursive: true });
|
|
39
37
|
if (!fs.existsSync(vidDir)) fs.mkdirSync(vidDir, { recursive: true });
|
|
40
38
|
|
|
@@ -120,20 +118,18 @@ module.exports = function(RED) {
|
|
|
120
118
|
method: 'GET',
|
|
121
119
|
url: `${baseUrl}/download`,
|
|
122
120
|
data: `<?xml version="1.0" encoding="UTF-8"?><downloadRequest><playbackURI>${rawUri.replace(/&/g, '&')}</playbackURI></downloadRequest>`,
|
|
123
|
-
responseType: 'arraybuffer'
|
|
124
|
-
headers: { "Content-Type": "application/xml" }
|
|
121
|
+
responseType: 'arraybuffer'
|
|
125
122
|
});
|
|
126
123
|
|
|
127
124
|
let buffer = Buffer.from(resDown.data);
|
|
128
|
-
|
|
129
125
|
if (t.id === "203") {
|
|
130
|
-
// ---
|
|
126
|
+
// --- SALVA FOTO ---
|
|
131
127
|
const fullImgPath = path.join(imgDir, `img_${timestamp}.jpg`);
|
|
132
128
|
fs.writeFileSync(fullImgPath, buffer);
|
|
133
|
-
output.imageBuffer = buffer;
|
|
129
|
+
output.imageBuffer = buffer;
|
|
134
130
|
output.imagePath = fullImgPath;
|
|
135
131
|
} else {
|
|
136
|
-
// ---
|
|
132
|
+
// --- SALVA VIDEO ---
|
|
137
133
|
if (buffer.slice(0, 4).toString() === 'IMKH') buffer = buffer.slice(40);
|
|
138
134
|
const rawPath = path.join(vidDir, `raw_${timestamp}.mp4`);
|
|
139
135
|
const fixedPath = path.join(vidDir, `hik_v_${channelID}_${timestamp}.mp4`);
|
|
@@ -159,22 +155,16 @@ module.exports = function(RED) {
|
|
|
159
155
|
updateNodeStatus();
|
|
160
156
|
}
|
|
161
157
|
|
|
158
|
+
// --- ALERT STREAM (TUTTO COME PRIMA) ---
|
|
162
159
|
function startAlertStream() {
|
|
163
160
|
if (isClosing) return;
|
|
164
161
|
const nvrAuth = new AxiosDigestAuth({ username: node.user, password: node.pass });
|
|
165
162
|
const url = `${node.protocol}://${node.host}:${node.port}/ISAPI/Event/notification/alertStream`;
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
method: 'GET', url: url, responseType: 'stream',
|
|
169
|
-
httpsAgent: node.protocol === "https" ? httpsAgent : undefined
|
|
170
|
-
}).then(response => {
|
|
163
|
+
nvrAuth.request({ method: 'GET', url: url, responseType: 'stream', httpsAgent: node.protocol === "https" ? httpsAgent : undefined })
|
|
164
|
+
.then(response => {
|
|
171
165
|
streamRequest = response;
|
|
172
|
-
if (!nvrOnline) {
|
|
173
|
-
node.send({ payload: { status: "online", ip: node.host, msg: "NVR Online" } });
|
|
174
|
-
nvrOnline = true;
|
|
175
|
-
}
|
|
166
|
+
if (!nvrOnline) { node.send({ payload: { status: "online", ip: node.host, msg: "NVR Online" } }); nvrOnline = true; }
|
|
176
167
|
updateNodeStatus();
|
|
177
|
-
|
|
178
168
|
response.data.on('data', (chunk) => {
|
|
179
169
|
const data = chunk.toString().toLowerCase();
|
|
180
170
|
if (data.includes("active")) {
|
|
@@ -186,24 +176,19 @@ module.exports = function(RED) {
|
|
|
186
176
|
}
|
|
187
177
|
}
|
|
188
178
|
});
|
|
189
|
-
|
|
190
179
|
response.data.on('error', () => handleNvrError());
|
|
191
180
|
response.data.on('end', () => !isClosing && setTimeout(startAlertStream, 5000));
|
|
192
181
|
}).catch(() => handleNvrError());
|
|
193
182
|
}
|
|
194
183
|
|
|
195
184
|
function handleNvrError() {
|
|
196
|
-
if (nvrOnline) {
|
|
197
|
-
node.send({ payload: { status: "offline", ip: node.host, msg: "NVR Offline" } });
|
|
198
|
-
nvrOnline = false;
|
|
199
|
-
}
|
|
185
|
+
if (nvrOnline) { node.send({ payload: { status: "offline", ip: node.host, msg: "NVR Offline" } }); nvrOnline = false; }
|
|
200
186
|
updateNodeStatus();
|
|
201
187
|
if (!isClosing) setTimeout(startAlertStream, 10000);
|
|
202
188
|
}
|
|
203
189
|
|
|
204
190
|
startAlertStream();
|
|
205
191
|
setTimeout(checkCameras, 2000);
|
|
206
|
-
|
|
207
192
|
node.on('close', (done) => {
|
|
208
193
|
isClosing = true;
|
|
209
194
|
clearInterval(heartbeatInterval);
|