OEM初版

This commit is contained in:
2025-10-10 17:54:53 +08:00
parent ccbe0f626f
commit 9036f967fc
16 changed files with 1736 additions and 237 deletions

View File

@@ -1,24 +1,32 @@
using FATrace.Com;
using FATrace.HKNetLib.Hardware;
using FATrace.HKNetLib.Wrapper;
using FATrace.Model;
using LibVLCSharp.Shared;
using ReaLTaiizor.Forms;
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace FATrace.OEMApp
{
public partial class MainApp : MaterialForm
{
/// <summary>
/// 海康Camera 客户端
/// </summary>
public HkCamera HkCameraClient { get; set; }
/// <summary>
/// 通道集合
/// </summary>
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 96, ArraySubType = UnmanagedType.U4)]
private int[] iChannelNum;
// 历史表列头中文映射
private readonly Dictionary<string, string> _historyHeaderMap = new Dictionary<string, string>
{
{ nameof(VideoAction.Id), "编号" },
{ nameof(VideoAction.Code), "条码" },
{ nameof(VideoAction.User), "用户" },
{ nameof(VideoAction.VideoFilePath), "视频路径" },
{ nameof(VideoAction.VideoName), "视频名称" },
{ nameof(VideoAction.StartTime), "开始时间" },
{ nameof(VideoAction.EndTime), "结束时间" },
{ nameof(VideoAction.CreateTime), "创建时间" }
};
public MainApp()
{
@@ -31,8 +39,20 @@ namespace FATrace.OEMApp
HkCameraClient = new HkCamera();
//保存SDK日志
CHCNetSDK.NET_DVR_SetLogToFile(3, "C:\\SdkLog\\", true);
iChannelNum = new int[96];
//读取配置
//HkCameraClient.NVR_IP = ConfigHelper.GetValue("NVRIP");
//HkCameraClient.NVR_Port = ConfigHelper.GetValue("NVRPort");
//HkCameraClient.NVR_UserName = ConfigHelper.GetValue("NVRUserName");
HkCameraClient.NVRVideoSavePath = ConfigHelper.GetValue("NVRVideoSavePath");
InitMediaPlayer();
InitHistoryGridBinding();
//materialListView1.DataBindings
try
{
var systemName = Program.SystemName;
@@ -56,24 +76,43 @@ namespace FATrace.OEMApp
}
}
/// <summary>
/// 当前登录用户
/// </summary>
public string CurUserName { get; set; }
#region
/// <summary>
/// 当前播放的视频
/// </summary>
public string CurrentVideoPath { get; set; }
#endregion
#region
/// <summary>
/// 内包条码
/// </summary>
public string CurInBagCode { get; set; }
/// <summary>
/// 内包条码 原料名称
/// </summary>
public string CurInBagRawName { get; set; }
#endregion
private void btnNVRLogin_Click(object sender, EventArgs e)
{
//var Result = HkCameraClient.Login(new CameraLoginInfo()
//{
// IP = "192.168.1.168",
// Port = 8000,
// UserName = "admin",
// Password = "glico@2025",
//});
HkCameraClient.NVR_IP = ConfigHelper.GetValue("NVRIP");
HkCameraClient.NVR_Port = ushort.Parse(ConfigHelper.GetValue("NVRPort"));
HkCameraClient.NVR_UserName = ConfigHelper.GetValue("NVRUserName");
HkCameraClient.NVR_Pw = ConfigHelper.GetValue("NVRPw");
//if (Result)
//{
// MessageBox.Show("登录成功");
// return;
//}
//MessageBox.Show("登录失败");
var result = HkCameraClient.Sdk_NET_DVR_Login_V30("192.168.1.168", 8000, "admin", "glico@2025");
var result = HkCameraClient.Sdk_NET_DVR_Login_V30(HkCameraClient.NVR_IP, HkCameraClient.NVR_Port, HkCameraClient.NVR_UserName, HkCameraClient.NVR_Pw);
if (result)
{
MessageBox.Show("登录成功");
@@ -83,24 +122,35 @@ namespace FATrace.OEMApp
return;
}
private void btnLoadVideo_Click(object sender, EventArgs e)
{
CurrentVideoPath = NVRCom.GetVideoName(HkCameraClient.NVRVideoSavePath, "CODE");
var Result = HkCameraClient.Sdk_NET_DVR_GetFileByTime_V40(DateTime.Now.AddMinutes(-5), DateTime.Now, $"D:\\Downtest_Channel{DateTime.Now.ToString("yyyy-MM-dd HHmmss")}.mp4");
var Result = HkCameraClient.Sdk_NET_DVR_GetFileByTime_V40(DateTime.Now.AddMinutes(-5), DateTime.Now, CurrentVideoPath);
if (Result.Result)
{
DownTimer.Interval = 2000;
DownTimer.Enabled = true;
HkCameraClient.NVRLoadVideoProcessEventHandler -= HkCameraClient_NVRLoadVideoProcessEventHandler;
HkCameraClient.NVRLoadVideoProcessEventHandler += HkCameraClient_NVRLoadVideoProcessEventHandler;
// 进度轮询已在库方法内部自动启动,这里无需再次调用
HkCameraClient.StartDownloadProgressMonitor();
}
else
{
MessageBox.Show($"[Sdk_NET_DVR_GetFileByTime_V40] 执行失败:{Result.Msg}");
}
//btnStopDownload.Enabled = true;
}
private void HkCameraClient_NVRLoadVideoProcessEventHandler(object? sender, short value)
{
this.BeginInvoke(new Action(() =>
{
DownloadProgressBarMain.Value = value;
}));
}
private void btnDownloadName_Click(object sender, EventArgs e)
{
//if (m_lDownHandle >= 0)
@@ -151,59 +201,266 @@ namespace FATrace.OEMApp
private void btnStopLoadVideo_Click(object sender, EventArgs e)
{
//if (m_lDownHandle < 0)
//{
// return;
//}
//if (!CHCNetSDK.NET_DVR_StopGetFile(m_lDownHandle))
//{
// iLastErr = CHCNetSDK.NET_DVR_GetLastError();
// str = "NET_DVR_StopGetFile failed, error code= " + iLastErr; //下载控制失败,输出错误号
// MessageBox.Show(str);
// return;
//}
//timerDownload.Stop();
//MessageBox.Show("The downloading has been stopped succesfully!");
//m_lDownHandle = -1;
//DownloadProgressBar.Value = 0;
//btnStopDownload.Enabled = true;
var Result = HkCameraClient.Sdk_NET_DVR_StopGetFile();
if (Result.Result)
{
MessageBox.Show($"[暂停成功] {Result.Msg}");
}
else
{
MessageBox.Show($"[暂停失败] {Result.Msg}");
}
}
private void DownTimer_Tick(object sender, EventArgs e)
#region
private LibVLC _libVLC;
private MediaPlayer _mediaPlayer;
/// <summary>
/// 初始化MediaPlay
/// </summary>
private void InitMediaPlayer()
{
DownloadProgressBar.Maximum = 100;
DownloadProgressBar.Minimum = 0;
_libVLC = new LibVLC();
_mediaPlayer = new MediaPlayer(_libVLC);
videoView1.MediaPlayer = _mediaPlayer;
int iPos = 0;
}
//获取下载进度
iPos = HkCameraClient.Sdk_NET_DVR_GetDownloadPos(HkCameraClient.m_lDownHandle);
if ((iPos > DownloadProgressBar.Minimum) && (iPos < DownloadProgressBar.Maximum))
#endregion
/// <summary>
/// 测试手动播放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPlayHistoryVideo_Click(object sender, EventArgs e)
{
string path = "D:\\Downtest_Channel2025-09-12 102454.mp4";
_mediaPlayer.Play(new Media(_libVLC, path));
}
/// <summary>
/// 停止播放当前的视频文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStopHistoryPlay_Click(object sender, EventArgs e)
{
_mediaPlayer.Stop();
}
/// <summary>
/// 清理播放资源
/// </summary>
private void CleanMediaPlay()
{
_mediaPlayer.Stop();
_mediaPlayer.Dispose();
_libVLC.Dispose();
}
#region
/// <summary>
/// 采用 BindingList + BindingSource使数据变化自动触发 UI 刷新
/// </summary>
private BindingSource historyBindingSource { get; set; }
private BindingList<VideoAction> historyVideoBindingList { get; set; }
/// <summary>
/// 初始化历史记录表格的数据绑定
/// </summary>
private void InitHistoryGridBinding()
{
historyBindingSource = new BindingSource();
historyVideoBindingList = new BindingList<VideoAction>();
historyBindingSource.DataSource = historyVideoBindingList;
// 绑定到 WinForms 原生 DataGridView
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = historyBindingSource;
// 绑定完成后统一调整列头与可见性
dataGridView1.DataBindingComplete -= DataGridView1_DataBindingComplete;
dataGridView1.DataBindingComplete += DataGridView1_DataBindingComplete;
// 双击行播放对应视频
dataGridView1.CellDoubleClick -= DataGridView1_CellDoubleClick;
dataGridView1.CellDoubleClick += DataGridView1_CellDoubleClick;
// 若此时已存在列,立即调整一次
ConfigureHistoryGridColumns();
}
private void btnHistoryVideoSearch_Click(object sender, EventArgs e)
{
var query = FSqlContext.FDb.Select<VideoAction>();
if (!string.IsNullOrEmpty(txtSearchCode.Text.Trim()))
{
DownloadProgressBar.Value = iPos;
query = query.Where(a => a.Code!.Contains(txtSearchCode.Text.Trim()));
}
query.Where(a => a.CreateTime >= PdtHistorySearchStart.Value && a.CreateTime <= PdtHistorySearchEnd.Value);
if (iPos == 100) //下载完成
// 拉取结果并刷新绑定列表
var resultList = query.ToList();
// 暂停变更通知,批量更新提高效率
historyVideoBindingList.RaiseListChangedEvents = false;
historyVideoBindingList.Clear();
foreach (var item in resultList)
{
DownloadProgressBar.Value = iPos;
if (!CHCNetSDK.NET_DVR_StopGetFile(HkCameraClient.m_lDownHandle))
historyVideoBindingList.Add(item);
}
historyVideoBindingList.RaiseListChangedEvents = true;
// 通知 UI 刷新
historyVideoBindingList.ResetBindings();
}
// 与历史记录表格列配置相关的成员WinForms DataGridView
private void DataGridView1_DataBindingComplete(object? sender, DataGridViewBindingCompleteEventArgs e)
{
try
{
ConfigureHistoryGridColumns();
}
catch (Exception ex)
{
// 仅记录,不中断界面
System.Diagnostics.Debug.WriteLine($"[DataBindingComplete] 列配置失败: {ex.Message}");
}
}
/// <summary>
/// 双击行,播放该行对应的视频文件
/// </summary>
private void DataGridView1_CellDoubleClick(object? sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0) return; // 双击列头等
try
{
var row = dataGridView1.Rows[e.RowIndex];
if (row?.DataBoundItem is VideoAction va)
{
MessageBox.Show($"[NET_DVR_StopGetFile] 执行失败:{HkCameraClient.GetLastError()}");
return;
var path = va.VideoFilePath;
if (string.IsNullOrWhiteSpace(path))
{
MessageBox.Show("该记录没有可播放的视频路径。", "播放提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (!File.Exists(path))
{
MessageBox.Show($"未找到视频文件:\n{path}", "播放失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
_mediaPlayer?.Stop();
_mediaPlayer?.Play(new LibVLCSharp.Shared.Media(_libVLC, path));
}
}
catch (Exception ex)
{
MessageBox.Show($"播放失败: {ex.Message}", "播放失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 按照 _historyHeaderMap 配置列头、隐藏不需要的列,并做常用格式化
/// </summary>
private void ConfigureHistoryGridColumns()
{
if (dataGridView1 == null || dataGridView1.Columns.Count == 0) return;
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
var propName = col.DataPropertyName;
// 设置列头中文
if (!string.IsNullOrWhiteSpace(propName) && _historyHeaderMap.TryGetValue(propName, out var headerText))
{
col.HeaderText = headerText;
}
// 隐藏 Id 列
if (propName == nameof(VideoAction.Id))
{
col.Visible = false;
continue;
}
//// 常用列宽与格式
//if (propName is nameof(VideoAction.Code))
//{
// col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
//}
//else if (propName is nameof(VideoAction.VideoFilePath))
//{
// col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
//}
// 时间列格式化
if (propName is nameof(VideoAction.StartTime) or nameof(VideoAction.EndTime) or nameof(VideoAction.CreateTime))
{
col.DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
HkCameraClient.m_lDownHandle = -1;
DownTimer.Stop();
}
if (iPos == 200) //网络异常,下载失败
// 选中整行,易于操作
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;
dataGridView1.ReadOnly = true;
dataGridView1.AllowUserToAddRows = false;
}
#endregion
/// <summary>
/// 操作结束
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnTestAction_Click(object sender, EventArgs e)
{
var Result = HkCameraClient.Sdk_NET_DVR_GetFileByTime_V40(DateTime.Now.AddMinutes(-5), DateTime.Now, NVRCom.GetVideoName(HkCameraClient.NVRVideoSavePath, "cODE"));
if (Result.Result)
{
MessageBox.Show("The downloading is abnormal for the abnormal network!");
DownTimer.Stop();
HkCameraClient.NVRLoadVideoProcessEventHandler -= HkCameraClient_NVRLoadVideoProcessEventHandler;
HkCameraClient.NVRLoadVideoProcessEventHandler += HkCameraClient_NVRLoadVideoProcessEventHandler;
HkCameraClient.NVRLoadVideoCompleteEventHandler -= HkCameraClient_NVRLoadVideoCompleteEventHandler;
HkCameraClient.NVRLoadVideoCompleteEventHandler += HkCameraClient_NVRLoadVideoCompleteEventHandler;
// 进度轮询已在库方法内部自动启动,这里无需再次调用
}
else
{
MessageBox.Show($"[Sdk_NET_DVR_GetFileByTime_V40] 执行失败:{Result.Msg}");
}
}
/// <summary>
/// Handles the event triggered when the NVR (Network Video Recorder) completes loading a video.
/// Video下载完成
/// </summary>
/// <param name="sender">The source of the event. This can be <see langword="null"/> if the event is not raised by a specific object.</param>
/// <param name="e">A string containing information about the completed video load operation.</param>
private void HkCameraClient_NVRLoadVideoCompleteEventHandler(object? sender, string e)
{
//先保存当前的信息
var returnValues = FSqlContext.FDb.Insert<OEMRawUse>(new OEMRawUse()
{
InBagCode=CurInBagCode,
RawName=CurInBagRawName,
User= CurUserName,
UrlState=false,
VideoUrl="",//暂时无法获取URL因为服务器还没有扫描到文件信息
}).ExecuteInserted();
}
}
}