node-red-contrib-hik-media-buffer 1.1.7 → 1.1.9
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 -27
- 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`);
|
|
@@ -148,7 +144,6 @@ module.exports = function(RED) {
|
|
|
148
144
|
resolve();
|
|
149
145
|
});
|
|
150
146
|
});
|
|
151
|
-
setTimeout(() => { if (output.videoPath && fs.existsSync(output.videoPath)) fs.unlinkSync(output.videoPath); }, 180000);
|
|
152
147
|
}
|
|
153
148
|
}
|
|
154
149
|
}
|
|
@@ -159,22 +154,16 @@ module.exports = function(RED) {
|
|
|
159
154
|
updateNodeStatus();
|
|
160
155
|
}
|
|
161
156
|
|
|
157
|
+
// --- ALERT STREAM (TUTTO COME PRIMA) ---
|
|
162
158
|
function startAlertStream() {
|
|
163
159
|
if (isClosing) return;
|
|
164
160
|
const nvrAuth = new AxiosDigestAuth({ username: node.user, password: node.pass });
|
|
165
161
|
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 => {
|
|
162
|
+
nvrAuth.request({ method: 'GET', url: url, responseType: 'stream', httpsAgent: node.protocol === "https" ? httpsAgent : undefined })
|
|
163
|
+
.then(response => {
|
|
171
164
|
streamRequest = response;
|
|
172
|
-
if (!nvrOnline) {
|
|
173
|
-
node.send({ payload: { status: "online", ip: node.host, msg: "NVR Online" } });
|
|
174
|
-
nvrOnline = true;
|
|
175
|
-
}
|
|
165
|
+
if (!nvrOnline) { node.send({ payload: { status: "online", ip: node.host, msg: "NVR Online" } }); nvrOnline = true; }
|
|
176
166
|
updateNodeStatus();
|
|
177
|
-
|
|
178
167
|
response.data.on('data', (chunk) => {
|
|
179
168
|
const data = chunk.toString().toLowerCase();
|
|
180
169
|
if (data.includes("active")) {
|
|
@@ -186,24 +175,19 @@ module.exports = function(RED) {
|
|
|
186
175
|
}
|
|
187
176
|
}
|
|
188
177
|
});
|
|
189
|
-
|
|
190
178
|
response.data.on('error', () => handleNvrError());
|
|
191
179
|
response.data.on('end', () => !isClosing && setTimeout(startAlertStream, 5000));
|
|
192
180
|
}).catch(() => handleNvrError());
|
|
193
181
|
}
|
|
194
182
|
|
|
195
183
|
function handleNvrError() {
|
|
196
|
-
if (nvrOnline) {
|
|
197
|
-
node.send({ payload: { status: "offline", ip: node.host, msg: "NVR Offline" } });
|
|
198
|
-
nvrOnline = false;
|
|
199
|
-
}
|
|
184
|
+
if (nvrOnline) { node.send({ payload: { status: "offline", ip: node.host, msg: "NVR Offline" } }); nvrOnline = false; }
|
|
200
185
|
updateNodeStatus();
|
|
201
186
|
if (!isClosing) setTimeout(startAlertStream, 10000);
|
|
202
187
|
}
|
|
203
188
|
|
|
204
189
|
startAlertStream();
|
|
205
190
|
setTimeout(checkCameras, 2000);
|
|
206
|
-
|
|
207
191
|
node.on('close', (done) => {
|
|
208
192
|
isClosing = true;
|
|
209
193
|
clearInterval(heartbeatInterval);
|