node-red-contrib-hik-media-buffer 1.1.2 → 1.1.4
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 +32 -9
- package/package.json +1 -1
package/hik-media-buffer.js
CHANGED
|
@@ -44,15 +44,24 @@ module.exports = function(RED) {
|
|
|
44
44
|
|
|
45
45
|
async function hikRequest(options) {
|
|
46
46
|
const { method, url, data, responseType, user, pass, headers = {} } = options;
|
|
47
|
+
|
|
48
|
+
// Assicuriamoci che data sia una stringa se stiamo mandando XML
|
|
49
|
+
const body = (typeof data === 'object') ? JSON.stringify(data) : data;
|
|
50
|
+
|
|
47
51
|
const res = await urllib.request(url, {
|
|
48
52
|
method: method,
|
|
49
53
|
digestAuth: `${user}:${pass}`,
|
|
50
|
-
content:
|
|
51
|
-
headers:
|
|
54
|
+
content: body,
|
|
55
|
+
headers: {
|
|
56
|
+
'Content-Type': 'application/xml', // Questo dice alla telecamera: "Leggi l'XML"
|
|
57
|
+
'Accept': '*/*',
|
|
58
|
+
...headers
|
|
59
|
+
},
|
|
52
60
|
dataType: responseType === 'arraybuffer' ? 'buffer' : 'text',
|
|
53
61
|
timeout: 15000,
|
|
54
62
|
rejectUnauthorized: false
|
|
55
63
|
});
|
|
64
|
+
|
|
56
65
|
return res;
|
|
57
66
|
}
|
|
58
67
|
|
|
@@ -149,23 +158,33 @@ module.exports = function(RED) {
|
|
|
149
158
|
if (uriMatch) {
|
|
150
159
|
const rawUri = uriMatch[1].replace(/&/g, '&');
|
|
151
160
|
const resDown = await hikRequest({
|
|
152
|
-
method: 'GET',
|
|
161
|
+
method: 'GET',
|
|
162
|
+
url: `${baseUrl}/download`,
|
|
153
163
|
data: `<?xml version="1.0" encoding="UTF-8"?><downloadRequest><playbackURI>${rawUri.replace(/&/g, '&')}</playbackURI></downloadRequest>`,
|
|
154
|
-
responseType: 'arraybuffer',
|
|
164
|
+
responseType: 'arraybuffer',
|
|
165
|
+
user: node.user,
|
|
166
|
+
pass: node.camPass
|
|
155
167
|
});
|
|
156
168
|
|
|
157
169
|
let buffer = Buffer.from(resDown.data);
|
|
170
|
+
|
|
158
171
|
if (t.id === "203") {
|
|
159
|
-
//
|
|
172
|
+
// --- LOGICA FOTO (IDENTICA ALLA TUA) ---
|
|
173
|
+
output.imageBuffer = buffer;
|
|
174
|
+
|
|
175
|
+
// AGGIUNTA: Salviamo fisicamente il file per il sito
|
|
160
176
|
const imgFileName = `img_${timestamp}.jpg`;
|
|
161
177
|
const fullImgPath = path.join(imgDir, imgFileName);
|
|
162
|
-
fs.writeFileSync(fullImgPath, buffer);
|
|
163
|
-
output.
|
|
164
|
-
|
|
178
|
+
fs.writeFileSync(fullImgPath, buffer);
|
|
179
|
+
output.imagePath = fullImgPath;
|
|
180
|
+
|
|
165
181
|
} else {
|
|
182
|
+
// --- LOGICA VIDEO (IDENTICA ALLA TUA) ---
|
|
166
183
|
if (buffer.slice(0, 4).toString() === 'IMKH') buffer = buffer.slice(40);
|
|
184
|
+
|
|
167
185
|
const rawPath = path.join(vidDir, `raw_${timestamp}.mp4`);
|
|
168
186
|
const fixedPath = path.join(vidDir, `hik_v_${channelID}_${timestamp}.mp4`);
|
|
187
|
+
|
|
169
188
|
fs.writeFileSync(rawPath, buffer);
|
|
170
189
|
|
|
171
190
|
await new Promise((resolve) => {
|
|
@@ -173,10 +192,14 @@ module.exports = function(RED) {
|
|
|
173
192
|
if (!err) {
|
|
174
193
|
output.videoPath = fixedPath;
|
|
175
194
|
try { fs.unlinkSync(rawPath); } catch(e) {}
|
|
176
|
-
} else {
|
|
195
|
+
} else {
|
|
196
|
+
output.videoPath = rawPath;
|
|
197
|
+
}
|
|
177
198
|
resolve();
|
|
178
199
|
});
|
|
179
200
|
});
|
|
201
|
+
|
|
202
|
+
setTimeout(() => { if (output.videoPath && fs.existsSync(output.videoPath)) fs.unlinkSync(output.videoPath); }, 180000);
|
|
180
203
|
}
|
|
181
204
|
}
|
|
182
205
|
}
|