-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhikvision.js
More file actions
332 lines (293 loc) · 9.86 KB
/
hikvision.js
File metadata and controls
332 lines (293 loc) · 9.86 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
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;
return true;
}
const hackJSResize = function () {
const originalResize = JSVideoPlugin.prototype.JS_Resize;
JSVideoPlugin.prototype.JS_Resize = function (e, t) {
const { szId } = this.oOptions;
if (document.getElementById(szId)) {
return originalResize.call(this, e, t);
}
}
}
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();
}
}
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();
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 function dispose(id) {
const vision = Data.get(id);
Data.remove(id);
const { realPlaying, logined } = vision;
if (realPlaying === true) {
stopRealPlay(id);
}
if (logined === true) {
logout(id);
}
WebVideoCtrl.I_DestroyPlugin();
}
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);
}