Skip to content

Commit 169d4ac

Browse files
committed
feat: 重构组件名称
1 parent e67fab3 commit 169d4ac

4 files changed

Lines changed: 106 additions & 27 deletions

File tree

src/components/BootstrapBlazor.HikVision/Components/HikVision.razor renamed to src/components/BootstrapBlazor.HikVision/Components/HikVisionWebPlugin.razor

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
@namespace BootstrapBlazor.Components
22
@inherits BootstrapModuleComponentBase
33

4-
<div @attributes="AdditionalAttributes" class="@ClassString" id="@Id" style="@StyleString">
5-
</div>
4+
<div @attributes="AdditionalAttributes" class="@ClassString" id="@Id" style="@StyleString"></div>

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

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
namespace BootstrapBlazor.Components;
88

99
/// <summary>
10-
/// 海康威视网络摄像机组件
10+
/// 海康威视网络摄像机组件 (Websdk Plugin 插件版本)
1111
/// </summary>
12-
[JSModuleAutoLoader("./_content/BootstrapBlazor.HikVision/Components/HikVision.razor.js")]
13-
public partial class HikVision
12+
[JSModuleAutoLoader("./_content/BootstrapBlazor.HikVision/Components/HikVisionWebPlugin.razor.js")]
13+
public partial class HikVisionWebPlugin
1414
{
1515
/// <summary>
1616
/// 获得/设置 网络摄像机 IP 地址
@@ -64,6 +64,16 @@ public partial class HikVision
6464
.AddStyleFromAttributes(AdditionalAttributes)
6565
.Build();
6666

67+
/// <summary>
68+
/// 获得 Websdk 插件是否初始化成功
69+
/// </summary>
70+
public bool Inited { get; private set; }
71+
72+
/// <summary>
73+
/// 获得 是否已登录
74+
/// </summary>
75+
public bool IsLogined { get; private set; }
76+
6777
/// <summary>
6878
/// <inheritdoc/>
6979
/// </summary>
@@ -75,6 +85,15 @@ protected override void OnParametersSet()
7585
Height ??= "300px";
7686
}
7787

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+
7897
/// <summary>
7998
/// 登录方法
8099
/// </summary>
@@ -86,7 +105,8 @@ protected override void OnParametersSet()
86105
/// <returns></returns>
87106
public async Task Login(string ip, int port, string userName, string password, LoginType loginType = LoginType.Http)
88107
{
89-
await InvokeVoidAsync("login", Id, ip, port, userName, password, (int)loginType);
108+
ThrowIfNotInited();
109+
IsLogined = await InvokeAsync<bool>("login", Id, ip, port, userName, password, (int)loginType);
90110
}
91111

92112
/// <summary>
@@ -95,6 +115,7 @@ public async Task Login(string ip, int port, string userName, string password, L
95115
/// <returns></returns>
96116
public async Task Logout()
97117
{
118+
IsLogined = false;
98119
await InvokeVoidAsync("logout", Id);
99120
}
100121

@@ -104,7 +125,10 @@ public async Task Logout()
104125
/// <returns></returns>
105126
public async Task StartRealPlay()
106127
{
107-
await InvokeVoidAsync("startRealPlay", Id);
128+
if (IsLogined)
129+
{
130+
await InvokeVoidAsync("startRealPlay", Id);
131+
}
108132
}
109133

110134
/// <summary>
@@ -113,6 +137,17 @@ public async Task StartRealPlay()
113137
/// <returns></returns>
114138
public async Task StopRealPlay()
115139
{
116-
await InvokeVoidAsync("stopRealPlay", Id);
140+
if (IsLogined)
141+
{
142+
await InvokeVoidAsync("stopRealPlay", Id);
143+
}
144+
}
145+
146+
private void ThrowIfNotInited()
147+
{
148+
if (!Inited)
149+
{
150+
throw new InvalidOperationException("HikVision Web Plugin not inited");
151+
}
117152
}
118153
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export async function init(id) {
77
return;
88
}
99

10-
await initVision(id);
10+
return await initVision(id);
1111
}
1212

1313
export { login, logout, startRealPlay, stopRealPlay }

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

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@ export async function init(id) {
1111

1212
const el = document.getElementById(id);
1313
if (el === null) {
14-
return;
14+
return false;
1515
}
1616

1717
const result = await initWindow(id);
1818
if (result.inited === false) {
19-
return;
19+
return false;
2020
}
2121

2222
Data.set(id, {
23-
iWndIndex: result.iWndIndex
23+
iWndIndex: result.iWndIndex,
24+
inited: true
2425
});
26+
27+
return true;
2528
}
2629

2730
const initWindow = id => {
@@ -59,6 +62,14 @@ const initWindow = id => {
5962

6063
export async function login(id, ip, port, userName, password, loginType) {
6164
const vision = Data.get(id);
65+
const { inited, logined } = vision;
66+
if (inited !== true) {
67+
return false;
68+
}
69+
if (logined === true) {
70+
return true;
71+
}
72+
6273
vision.szDeviceIdentify = `${ip}_${port}`;
6374
vision.logined = null;
6475
vision.loginErrorCode = null;
@@ -152,15 +163,19 @@ const getChannelInfo = vision => {
152163
const handler = setInterval(() => {
153164
if (analog_completed && digital_completed && zero_completed) {
154165
clearInterval(handler)
155-
resolve(vision);
166+
resolve();
156167
}
157168
}, 16);
158169
});
159170
}
160171

161172
export function logout(id) {
162173
const vision = Data.get(id);
163-
const { szDeviceIdentify } = vision;
174+
const { szDeviceIdentify, logined } = vision;
175+
if (logined !== true) {
176+
vision.logined = false;
177+
return;
178+
}
164179

165180
let completed = null;
166181
WebVideoCtrl.I_Logout(szDeviceIdentify).then(() => {
@@ -173,13 +188,13 @@ export function logout(id) {
173188
const handler = setInterval(() => {
174189
if (completed !== null) {
175190
clearInterval(handler)
176-
resolve(vision);
191+
resolve();
177192
}
178193
}, 16);
179194
});
180195
}
181196

182-
export async function startRealPlay(id) {
197+
export async function startRealPlay(id, iStreamType, iChannelID) {
183198
const vision = Data.get(id);
184199
const { iWndIndex, szDeviceIdentify } = vision;
185200

@@ -188,26 +203,28 @@ export async function startRealPlay(id) {
188203

189204
const oWndInfo = WebVideoCtrl.I_GetWindowStatus(iWndIndex);
190205
const iRtspPort = vision.devicePort.iRtspPort;
191-
const iChannelID = 1;
192206
const bZeroChannel = false;
193-
const iStreamType = 1;
194-
207+
let completed = null;
195208
const startRealPlay = function () {
196209
WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {
210+
iWndIndex: iWndIndex,
197211
iStreamType: iStreamType,
198212
iChannelID: iChannelID,
199213
bZeroChannel: bZeroChannel,
200214
iPort: iRtspPort,
201215
success: function () {
202-
216+
vision.realPlaying = true;
217+
completed = true;
203218
},
204219
error: function (oError) {
205-
220+
vision.realPlaying = false;
221+
completed = false;
206222
}
207223
});
208224
};
209225

210-
if (oWndInfo != null) {
226+
console.log(oWndInfo);
227+
if (oWndInfo !== null) {
211228
WebVideoCtrl.I_Stop({
212229
success: function () {
213230
startRealPlay();
@@ -217,31 +234,59 @@ export async function startRealPlay(id) {
217234
else {
218235
startRealPlay();
219236
}
237+
238+
return new Promise((resolve, reject) => {
239+
const handler = setInterval(() => {
240+
if (completed !== null) {
241+
clearInterval(handler)
242+
resolve(completed);
243+
}
244+
}, 16);
245+
});
220246
}
221247

222248
export function stopRealPlay(id) {
223249
const vision = Data.get(id);
224250
const { iWndIndex, szDeviceIdentify } = vision;
225251

226252
const oWndInfo = WebVideoCtrl.I_GetWindowStatus(iWndIndex);
253+
let completed = null;
254+
console.log(oWndInfo);
227255
if (oWndInfo !== null) {
228256
WebVideoCtrl.I_Stop({
229257
success: function () {
230-
258+
vision.realPlaying = false;
259+
completed = true;
231260
},
232261
error: function (oError) {
233-
262+
completed = false;
234263
}
235264
});
236265
}
266+
267+
return new Promise((resolve, reject) => {
268+
const handler = setInterval(() => {
269+
if (completed !== null) {
270+
clearInterval(handler)
271+
resolve(completed);
272+
}
273+
}, 16);
274+
});
237275
}
238276

239277
export function dispose(id) {
240-
stopRealPlay(id);
241-
logout(id);
278+
const vision = Data.get(id);
279+
Data.remove(id);
280+
281+
const { realPlaying, logined } = vision;
282+
if (realPlaying === true) {
283+
stopRealPlay(id);
284+
}
285+
if (logined === true) {
286+
logout(id);
287+
}
242288
WebVideoCtrl.I_DestroyPlugin();
243289

244-
Data.remove(id);
245290
}
246291

247292
const getTagNameFirstValue = (xmlDoc, tagName) => {

0 commit comments

Comments
 (0)