-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhikvision.js
More file actions
613 lines (536 loc) · 16.3 KB
/
hikvision.js
File metadata and controls
613 lines (536 loc) · 16.3 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
import { addScript, registerBootstrapBlazorModule } from '../BootstrapBlazor/modules/utility.js';
import Data from '../BootstrapBlazor/modules/data.js';
export async function init(id) {
await addScript('./_content/BootstrapBlazor.HikVision/webVideoCtrl.js');
if (window.$ === void 0) {
await addScript('./_content/BootstrapBlazor.HikVision/jquery-1.7.1.min.js');
}
const el = document.getElementById(id);
if (el === null) {
return false;
}
const result = await initWindow(id);
if (result.inited === false) {
return false;
}
const vision = Data.get(id);
vision.iWndIndex = result.iWndIndex;
vision.inited = true;
const observer = new IntersectionObserver(() => {
if (checkVisibility(el)) {
WebVideoCtrl.I_Resize(el.offsetWidth, el.offsetHeight);
}
else {
WebVideoCtrl.I_HidPlugin();
}
});
observer.observe(el);
vision.observer = observer;
return true;
}
const hackJSResize = function () {
const originalResize = JSVideoPlugin.prototype.JS_Resize;
JSVideoPlugin.prototype.JS_Resize = function (e, t) {
const { szId } = this.oOptions;
const el = document.getElementById(szId);
if (el) {
const visible = checkVisibility(el);
if (visible) {
return originalResize.call(this, e, t);
}
else {
WebVideoCtrl.I_HidPlugin();
}
}
}
}
const hackJSShowWnd = function () {
const originalShowWnd = JSVideoPlugin.prototype.JS_ShowWnd;
JSVideoPlugin.prototype.JS_ShowWnd = function () {
const { szId } = this.oOptions;
const el = document.getElementById(szId);
if (el) {
const visible = checkVisibility(el);
if (visible) {
return originalShowWnd.call(this);
}
else {
return new Promise((resolve, reject) => {
resolve();
});
}
}
}
}
const hackJSDestroyPlugin = function () {
const originalDestroy = JSVideoPlugin.prototype.JS_DestroyPlugin;
JSVideoPlugin.prototype.JS_DestroyPlugin = function (n) {
const origianlSendRequestProxy = this.oPlugin.oRequest.oRequest.sendRequest;
this.oPlugin.oRequest.oRequest.sendRequest = function (r) {
if (this.oWebSocket && WebSocket.OPEN === this.oWebSocket.readyState) {
return origianlSendRequestProxy.call(this, r);
}
}
this.oPlugin.JS_DestroyPlugin(true);
JSVideoPlugin = null;
delete window.JSVideoPlugin;
removePlugin();
return new Promise((resolve, reject) => {
resolve();
});
}
}
const removePlugin = () => {
const scripts = [...document.head.querySelectorAll('script')]
const nodes = scripts.filter(function (link) {
return link.src.indexOf('/jsVideoPlugin-1.0.0.min.js') > -1
})
for (let index = 0; index < nodes.length; index++) {
document.head.removeChild(nodes[index])
}
}
const initWindow = id => {
const result = { inited: null, iWndIndex: -1 };
WebVideoCtrl.I_InitPlugin({
szBasePath: './_content/BootstrapBlazor.HikVision',
bWndFull: true,
iWndowType: 1,
cbSelWnd: function (xmlDoc) {
result.iWndIndex = parseInt(getTagNameFirstValue(xmlDoc, "SelectWnd"));
},
cbDoubleClickWnd: function (iWndIndex, bFullScreen) {
},
cbEvent: function (iEventType, iParam1, iParam2) {
},
cbInitPluginComplete: function () {
WebVideoCtrl.I_InsertOBJECTPlugin(id).then(() => {
result.inited = true;
}, () => {
result.inited = false;
});
}
});
return new Promise((resolve, reject) => {
const handler = setInterval(() => {
if (result.inited === false || (result.inited && result.iWndIndex !== -1)) {
clearInterval(handler);
hackJSResize();
hackJSShowWnd();
hackJSDestroyPlugin();
resolve(result);
}
}, 16);
});
}
export async function login(id, ip, port, userName, password, loginType) {
const vision = Data.get(id);
const { inited, logined } = vision;
if (inited !== true || ip.length === 0 || port <= 0 || userName.length === 0 || password.length === 0) {
vision.logined = false;
return false;
}
if (logined === true) {
return true;
}
vision.szDeviceIdentify = `${ip}_${port}`;
vision.logined = null;
vision.loginErrorCode = null;
vision.loginErrorMsg = null;
WebVideoCtrl.I_Login(ip, loginType, port, userName, password, {
timeout: 3000,
success: function (xmlDoc) {
getChannelList(vision).then(() => {
vision.logined = true;
});
},
error: function (oError) {
const ERROR_CODE_LOGIN_REPEATLOGIN = 2001;
if (oError.errorCode === ERROR_CODE_LOGIN_REPEATLOGIN) {
vision.logined = true;
return true;
}
vision.logined = false;
vision.loginErrorCode = oError.errorCode;
vision.loginErrorMsg = oError.errorMsg;
}
});
return new Promise((resolve, reject) => {
const handler = setInterval(async () => {
if (vision.logined !== null) {
clearInterval(handler)
resolve(vision.logined);
}
}, 16);
});
}
const getChannelList = vision => {
const { szDeviceIdentify, logined } = vision;
let analog_completed = false;
WebVideoCtrl.I_GetAnalogChannelInfo(szDeviceIdentify, {
success: function (xmlDoc) {
const channels = [...getTagNameValues(xmlDoc, "VideoInputChannel")];
vision.analogChannels = channels.map(channel => {
return {
id: parseInt(getTagNameFirstValue(channel, "id")),
inputPort: parseInt(getTagNameFirstValue(channel, "inputPort")),
name: getTagNameFirstValue(channel, "name"),
videoFormat: getTagNameFirstValue(channel, "videoFormat")
};
});
analog_completed = true;
},
error: function (oError) {
analog_completed = true;
}
});
let digital_completed = false;
WebVideoCtrl.I_GetDigitalChannelInfo(szDeviceIdentify, {
success: function (xmlDoc) {
const channels = [...getTagNameValues(xmlDoc, "InputProxyChannelStatus")];
vision.digitalChannels = channels.map(channel => {
return {
id: parseInt(getTagNameFirstValue(channel, "id")),
online: getTagNameFirstValue(channel, "online")
};
});
digital_completed = true;
},
error: function (oError) {
digital_completed = true;
}
});
let zero_completed = false;
WebVideoCtrl.I_GetZeroChannelInfo(szDeviceIdentify, {
success: function (xmlDoc) {
const channels = [...getTagNameValues(xmlDoc, "ZeroVideoChannel")];
vision.zeroChannels = channels.map(channel => {
return {
id: parseInt(getTagNameFirstValue(channel, "id")),
inputPort: parseInt(getTagNameFirstValue(channel, "inputPort")),
enabled: getTagNameFirstValue(channel, "enabled") === 'true',
};
});
zero_completed = true;
},
error: function (oError) {
zero_completed = true;
}
});
return new Promise((resolve, reject) => {
const handler = setInterval(() => {
if (analog_completed && digital_completed && zero_completed) {
clearInterval(handler)
resolve();
}
}, 16);
});
}
export async function logout(id) {
const vision = Data.get(id);
const { szDeviceIdentify, logined } = vision;
if (logined !== true) {
vision.logined = false;
return;
}
stopRealPlay(id);
await WebVideoCtrl.I_Logout(szDeviceIdentify);
vision.logined = false;
}
export async function startRealPlay(id, iStreamType, iChannelID) {
const vision = Data.get(id);
const { iWndIndex, szDeviceIdentify } = vision;
vision.devicePort = await WebVideoCtrl.I_GetDevicePort(szDeviceIdentify);
const { iRtspPort } = vision.devicePort;
const bZeroChannel = false;
let completed = null;
const startRealPlay = function () {
WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {
iWndIndex: iWndIndex,
iStreamType: iStreamType,
iChannelID: iChannelID,
bZeroChannel: bZeroChannel,
iPort: iRtspPort,
success: function () {
vision.realPlaying = true;
completed = true;
},
error: function (oError) {
vision.realPlaying = false;
completed = false;
}
});
};
const oWndInfo = WebVideoCtrl.I_GetWindowStatus(iWndIndex);
if (oWndInfo !== null) {
WebVideoCtrl.I_Stop({
success: function () {
startRealPlay();
}
});
}
else {
startRealPlay();
}
return new Promise((resolve, reject) => {
const handler = setInterval(() => {
if (completed !== null) {
clearInterval(handler)
resolve(completed);
}
}, 16);
});
}
export function stopRealPlay(id) {
const vision = Data.get(id);
const { iWndIndex, realPlaying } = vision;
if (realPlaying !== true) {
return true;
}
const oWndInfo = WebVideoCtrl.I_GetWindowStatus(iWndIndex);
let completed = null;
if (oWndInfo !== null) {
WebVideoCtrl.I_Stop({
success: function () {
vision.realPlaying = false;
completed = true;
},
error: function (oError) {
completed = false;
}
});
}
return new Promise((resolve, reject) => {
const handler = setInterval(() => {
if (completed !== null) {
clearInterval(handler)
resolve(completed);
}
}, 16);
});
}
export async function openSound(id) {
const vision = Data.get(id);
const { iWndIndex, realPlaying } = vision;
if (realPlaying !== true) {
return 101;
}
let code = 100;
try {
await WebVideoCtrl.I_OpenSound(iWndIndex);
}
catch (ex) {
code = 102;
console.log(ex);
}
return code;
}
export async function closeSound(id) {
const vision = Data.get(id);
const { iWndIndex, realPlaying } = vision;
if (realPlaying !== true) {
return 101;
}
let code = 100;
try {
await WebVideoCtrl.I_CloseSound(iWndIndex);
}
catch (ex) {
code = 102;
console.log(ex);
}
return code;
}
export async function setVolume(id, value) {
const vision = Data.get(id);
const { iWndIndex, realPlaying } = vision;
if (realPlaying !== true) {
return 101;
}
let v = parseInt(value);
if (isNaN(v)) {
v = 50;
}
let code = 100;
try {
await WebVideoCtrl.I_SetVolume(Math.min(100, Math.max(0, v)));
}
catch (ex) {
code = 102;
console.log(ex);
}
return code;
}
export async function capturePicture(id) {
const vision = Data.get(id);
const { realPlaying } = vision;
if (realPlaying !== true) {
return "";
}
try {
const base64 = await WebVideoCtrl.I_CapturePicData();
if (base64) {
const bytes = base64ToArray(base64);
return DotNet.createJSStreamReference(bytes.buffer);
}
}
catch (ex) {
console.log(ex);
return null;
}
}
const base64ToArray = base64String => {
const base64Data = base64String.split(',')[1] || base64String;
const binaryString = atob(base64Data);
const length = binaryString.length;
const bytes = new Uint8Array(length);
for (let i = 0; i < length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
export async function capturePictureAndDownload(id) {
const vision = Data.get(id);
const { realPlaying } = vision;
if (realPlaying !== true) {
return;
}
try {
const base64 = await WebVideoCtrl.I_CapturePicData();
if (base64) {
const anchorElement = document.createElement('a');
anchorElement.href = `data:image/jpg;base64,${base64}`;
anchorElement.download = `capture_${new Date().getTime()}.jpg`;
document.body.appendChild(anchorElement);
anchorElement.click();
document.body.removeChild(anchorElement);
}
}
catch (ex) {
console.log(ex);
}
}
export async function startRecord(id) {
const vision = Data.get(id);
const { realPlaying } = vision;
if (realPlaying !== true) {
return false;
}
let completed = false;
let error = null;
try {
WebVideoCtrl.I_StartRecord(`record_${new Date().getTime()}`, {
success: function () {
completed = true;
},
error: function (oError) {
completed = true;
error = oError;
}
});
}
catch (ex) {
console.log(ex);
}
return new Promise((resolve, reject) => {
const handler = setInterval(() => {
if (completed) {
clearTimeout(handler);
if (error === null) {
resolve(true);
}
else {
reject(error);
}
}
}, 16);
});
}
export async function stopRecord(id) {
const vision = Data.get(id);
const { realPlaying } = vision;
if (realPlaying !== true) {
return false;
}
let completed = false;
let error = null;
try {
WebVideoCtrl.I_StopRecord({
success: function () {
completed = true;
},
error: function (oError) {
completed = true;
error = oError;
}
});
}
catch (ex) {
}
return new Promise((resolve, reject) => {
const handler = setInterval(() => {
if (completed) {
clearTimeout(handler);
if (error === null) {
resolve(true);
}
else {
reject(error);
}
}
}, 16);
});
}
export function dispose(id) {
const vision = Data.get(id);
const { realPlaying, logined, observer } = vision;
if (observer) {
observer.disconnect();
}
WebVideoCtrl.I_HidPlugin();
if (realPlaying === true) {
stopRealPlay(id);
}
if (logined === true) {
logout(id);
}
WebVideoCtrl.I_DestroyPlugin();
Data.remove(id);
}
const getTagNameFirstValue = (xmlDoc, tagName, defaultValue = '0') => {
const tags = xmlDoc.getElementsByTagName(tagName);
if (tags.length > 0) {
return tags[0].textContent;
}
return defaultValue;
}
const getTagNameValues = (xmlDoc, tagName) => {
return xmlDoc.getElementsByTagName(tagName);
}
const checkVisibility = el => {
if (el.checkVisibility) {
return el.checkVisibility();
}
else {
return isVisible(el);
}
}
const isVisible = (element) => {
if (!element) return false;
const style = window.getComputedStyle(element);
if (style.display === 'none' || style.visibility === 'hidden' || parseFloat(style.opacity) < 0.01) {
return false;
}
const rect = element.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0) {
return false;
}
let parent = element.parentElement;
while (parent) {
const parentStyle = window.getComputedStyle(parent);
if (parentStyle.display === 'none' || parentStyle.visibility === 'hidden') {
return false;
}
parent = parent.parentElement;
}
return true;
}