Skip to content

Commit c11f496

Browse files
committed
feat: 增加 OnInitedAsync 回调方法
1 parent 169d4ac commit c11f496

3 files changed

Lines changed: 61 additions & 43 deletions

File tree

src/components/BootstrapBlazor.HikVision/Components/HikVisionWebPlugin.razor.cs

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace BootstrapBlazor.Components;
99
/// <summary>
1010
/// 海康威视网络摄像机组件 (Websdk Plugin 插件版本)
1111
/// </summary>
12-
[JSModuleAutoLoader("./_content/BootstrapBlazor.HikVision/Components/HikVisionWebPlugin.razor.js")]
12+
[JSModuleAutoLoader("./_content/BootstrapBlazor.HikVision/Components/HikVisionWebPlugin.razor.js", JSObjectReference = true)]
1313
public partial class HikVisionWebPlugin
1414
{
1515
/// <summary>
@@ -54,6 +54,12 @@ public partial class HikVisionWebPlugin
5454
[Parameter]
5555
public string? Height { get; set; }
5656

57+
/// <summary>
58+
/// 获得/设置 插件初始化完成后回调方法
59+
/// </summary>
60+
[Parameter]
61+
public Func<bool, Task> OnInitedAsync { get; set; }
62+
5763
private string? ClassString => CssBuilder.Default("bb-hik")
5864
.AddClassFromAttributes(AdditionalAttributes)
5965
.Build();
@@ -74,6 +80,11 @@ public partial class HikVisionWebPlugin
7480
/// </summary>
7581
public bool IsLogined { get; private set; }
7682

83+
/// <summary>
84+
/// 获得 是否正在实时预览
85+
/// </summary>
86+
public bool IsRealPlaying { get; private set; }
87+
7788
/// <summary>
7889
/// <inheritdoc/>
7990
/// </summary>
@@ -85,15 +96,6 @@ protected override void OnParametersSet()
8596
Height ??= "300px";
8697
}
8798

88-
/// <summary>
89-
/// <inheritdoc/>
90-
/// </summary>
91-
/// <returns></returns>
92-
protected override async Task InvokeInitAsync()
93-
{
94-
Inited = await InvokeAsync<bool?>("init", Id) ?? false;
95-
}
96-
9799
/// <summary>
98100
/// 登录方法
99101
/// </summary>
@@ -103,10 +105,11 @@ protected override async Task InvokeInitAsync()
103105
/// <param name="password"></param>
104106
/// <param name="loginType"></param>
105107
/// <returns></returns>
106-
public async Task Login(string ip, int port, string userName, string password, LoginType loginType = LoginType.Http)
108+
public async Task<bool> Login(string ip, int port, string userName, string password, LoginType loginType = LoginType.Http)
107109
{
108110
ThrowIfNotInited();
109-
IsLogined = await InvokeAsync<bool>("login", Id, ip, port, userName, password, (int)loginType);
111+
IsLogined = await InvokeAsync<bool?>("login", Id, ip, port, userName, password, (int)loginType) ?? false;
112+
return IsLogined;
110113
}
111114

112115
/// <summary>
@@ -115,19 +118,22 @@ public async Task Login(string ip, int port, string userName, string password, L
115118
/// <returns></returns>
116119
public async Task Logout()
117120
{
121+
if (IsLogined)
122+
{
123+
await InvokeVoidAsync("logout", Id);
124+
}
118125
IsLogined = false;
119-
await InvokeVoidAsync("logout", Id);
120126
}
121127

122128
/// <summary>
123129
/// 开始实时预览方法
124130
/// </summary>
125131
/// <returns></returns>
126-
public async Task StartRealPlay()
132+
public async Task StartRealPlay(int streamType, int channelId)
127133
{
128-
if (IsLogined)
134+
if (IsLogined && !IsRealPlaying)
129135
{
130-
await InvokeVoidAsync("startRealPlay", Id);
136+
IsRealPlaying = await InvokeAsync<bool?>("startRealPlay", Id, streamType, channelId) ?? false;
131137
}
132138
}
133139

@@ -137,9 +143,13 @@ public async Task StartRealPlay()
137143
/// <returns></returns>
138144
public async Task StopRealPlay()
139145
{
140-
if (IsLogined)
146+
if (IsLogined && IsRealPlaying)
141147
{
142-
await InvokeVoidAsync("stopRealPlay", Id);
148+
var result = await InvokeAsync<bool?>("stopRealPlay", Id) ?? false;
149+
if (result)
150+
{
151+
IsRealPlaying = false;
152+
}
143153
}
144154
}
145155

@@ -150,4 +160,19 @@ private void ThrowIfNotInited()
150160
throw new InvalidOperationException("HikVision Web Plugin not inited");
151161
}
152162
}
163+
164+
/// <summary>
165+
/// 触发 <see cref="OnInitedAsync"/> 回调方法由 JavaScript 调用
166+
/// </summary>
167+
/// <returns></returns>
168+
[JSInvokable]
169+
public async Task TriggerInited(bool inited)
170+
{
171+
Inited = inited;
172+
173+
if (OnInitedAsync != null)
174+
{
175+
await OnInitedAsync(inited);
176+
}
177+
}
153178
}

src/components/BootstrapBlazor.HikVision/Components/HikVisionWebPlugin.razor.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { init as initVision, login, logout, startRealPlay, stopRealPlay, dispose as disposeVision } from '../hikvision.js';
22
import EventHandler from '../../BootstrapBlazor/modules/event-handler.js';
33

4-
export async function init(id) {
4+
export async function init(id, invoke) {
55
const el = document.getElementById(id);
66
if (el === null) {
77
return;
88
}
99

10-
return await initVision(id);
10+
const inited = await initVision(id);
11+
await invoke.invokeMethodAsync('TriggerInited', inited);
1112
}
1213

1314
export { login, logout, startRealPlay, stopRealPlay }

src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const initWindow = id => {
3333
bWndFull: true,
3434
iWndowType: 1,
3535
cbSelWnd: function (xmlDoc) {
36-
result.iWndIndex = getTagNameFirstValue(xmlDoc, "SelectWnd")
36+
result.iWndIndex = parseInt(getTagNameFirstValue(xmlDoc, "SelectWnd"));
3737
},
3838
cbDoubleClickWnd: function (iWndIndex, bFullScreen) {
3939

@@ -63,7 +63,7 @@ const initWindow = id => {
6363
export async function login(id, ip, port, userName, password, loginType) {
6464
const vision = Data.get(id);
6565
const { inited, logined } = vision;
66-
if (inited !== true) {
66+
if (inited !== true || ip.length === 0 || port <= 0 || userName.length === 0 || password.length === 0) {
6767
return false;
6868
}
6969
if (logined === true) {
@@ -95,9 +95,9 @@ export async function login(id, ip, port, userName, password, loginType) {
9595

9696
return new Promise((resolve, reject) => {
9797
const handler = setInterval(async () => {
98-
if (vision.logined !== void 0) {
98+
if (vision.logined !== null) {
9999
clearInterval(handler)
100-
resolve(vision);
100+
resolve(vision.logined);
101101
}
102102
}, 16);
103103
});
@@ -169,29 +169,18 @@ const getChannelInfo = vision => {
169169
});
170170
}
171171

172-
export function logout(id) {
172+
export async function logout(id) {
173173
const vision = Data.get(id);
174174
const { szDeviceIdentify, logined } = vision;
175175
if (logined !== true) {
176176
vision.logined = false;
177177
return;
178178
}
179179

180-
let completed = null;
181-
WebVideoCtrl.I_Logout(szDeviceIdentify).then(() => {
182-
completed = true;
183-
}, () => {
184-
completed = false;
185-
});
180+
stopRealPlay(id);
186181

187-
return new Promise((resolve, reject) => {
188-
const handler = setInterval(() => {
189-
if (completed !== null) {
190-
clearInterval(handler)
191-
resolve();
192-
}
193-
}, 16);
194-
});
182+
await WebVideoCtrl.I_Logout(szDeviceIdentify);
183+
vision.logined = false;
195184
}
196185

197186
export async function startRealPlay(id, iStreamType, iChannelID) {
@@ -247,11 +236,14 @@ export async function startRealPlay(id, iStreamType, iChannelID) {
247236

248237
export function stopRealPlay(id) {
249238
const vision = Data.get(id);
250-
const { iWndIndex, szDeviceIdentify } = vision;
239+
const { iWndIndex, realPlaying } = vision;
240+
241+
if (realPlaying !== true) {
242+
return true;
243+
}
251244

252245
const oWndInfo = WebVideoCtrl.I_GetWindowStatus(iWndIndex);
253246
let completed = null;
254-
console.log(oWndInfo);
255247
if (oWndInfo !== null) {
256248
WebVideoCtrl.I_Stop({
257249
success: function () {
@@ -289,12 +281,12 @@ export function dispose(id) {
289281

290282
}
291283

292-
const getTagNameFirstValue = (xmlDoc, tagName) => {
284+
const getTagNameFirstValue = (xmlDoc, tagName, defaultValue = '0') => {
293285
const tags = xmlDoc.getElementsByTagName(tagName);
294286
if (tags.length > 0) {
295287
return tags[0].textContent;
296288
}
297-
return null;
289+
return defaultValue;
298290
}
299291

300292
const getTagNameValues = (xmlDoc, tagName) => {

0 commit comments

Comments
 (0)