@@ -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 ) ]
1313public 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}
0 commit comments