初级的功能
This commit is contained in:
@@ -37,6 +37,57 @@ namespace FATrace.OEMApp
|
||||
{ nameof(VideoAction.CreateTime), "创建时间" }
|
||||
};
|
||||
|
||||
private int _lvLogMaxItems = 1000;
|
||||
private int _lastProgressLogged = -1;
|
||||
|
||||
private void InitLvLog()
|
||||
{
|
||||
try
|
||||
{
|
||||
LvLog.BeginUpdate();
|
||||
LvLog.Clear();
|
||||
LvLog.View = View.Details;
|
||||
LvLog.FullRowSelect = true;
|
||||
LvLog.GridLines = true;
|
||||
LvLog.HeaderStyle = ColumnHeaderStyle.Nonclickable;
|
||||
LvLog.Columns.Add("时间", 150);
|
||||
LvLog.Columns.Add("级别", 60);
|
||||
LvLog.Columns.Add("消息", 720);
|
||||
}
|
||||
finally
|
||||
{
|
||||
LvLog.EndUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendLog(string level, string message, Color? color = null)
|
||||
{
|
||||
if (LvLog == null || LvLog.IsDisposed) return;
|
||||
if (InvokeRequired)
|
||||
{
|
||||
try { BeginInvoke(new Action(() => AppendLog(level, message, color))); } catch { }
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
var item = new ListViewItem(DateTime.Now.ToString("HH:mm:ss.fff"));
|
||||
item.SubItems.Add(level);
|
||||
item.SubItems.Add(message ?? string.Empty);
|
||||
if (color.HasValue) item.ForeColor = color.Value;
|
||||
LvLog.Items.Add(item);
|
||||
try { LvLog.EnsureVisible(LvLog.Items.Count - 1); } catch { }
|
||||
if (LvLog.Items.Count > _lvLogMaxItems)
|
||||
{
|
||||
for (int i = 0; i < 200; i++) { if (LvLog.Items.Count == 0) break; LvLog.Items.RemoveAt(0); }
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
private void LogInfo(string msg) => AppendLog("INFO", msg, Color.FromArgb(33, 33, 33));
|
||||
private void LogWarn(string msg) => AppendLog("WARN", msg, Color.DarkOrange);
|
||||
private void LogError(string msg) => AppendLog("ERROR", msg, Color.DarkRed);
|
||||
|
||||
public MainApp()
|
||||
{
|
||||
InitializeComponent();
|
||||
@@ -46,6 +97,7 @@ namespace FATrace.OEMApp
|
||||
/// PLC数据服务
|
||||
/// </summary>
|
||||
private PLCDataService PLCDataService { get; set; }
|
||||
private TimeClearDataService TimeClearService { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 主窗体加载:
|
||||
@@ -56,10 +108,18 @@ namespace FATrace.OEMApp
|
||||
/// </summary>
|
||||
private void MainApp_Load(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
InitLvLog();
|
||||
LogInfo("主界面初始化");
|
||||
HkCameraClient = new HkCamera();
|
||||
//保存SDK日志
|
||||
CHCNetSDK.NET_DVR_SetLogToFile(3, "C:\\SdkLog\\", true);
|
||||
LogInfo("已启用海康SDK日志输出 C:\\SdkLog\\");
|
||||
|
||||
try
|
||||
{
|
||||
HkCameraClient.NVRLoadVideoProcessEventHandler += HkCameraClient_NVRLoadVideoProcessEventHandler;
|
||||
}
|
||||
catch { }
|
||||
|
||||
//读取配置
|
||||
//HkCameraClient.NVR_IP = ConfigHelper.GetValue("NVRIP");
|
||||
@@ -71,6 +131,7 @@ namespace FATrace.OEMApp
|
||||
PLCDataService.ScanCodeEventHandler += PLCDataService_ScanCodeEventHandler;
|
||||
|
||||
HkCameraClient.NVRVideoSavePath = ConfigHelper.GetValue("NVRVideoSavePath");
|
||||
|
||||
//NVR登录
|
||||
NVRLogin();
|
||||
|
||||
@@ -81,11 +142,18 @@ namespace FATrace.OEMApp
|
||||
try
|
||||
{
|
||||
DownloadTaskWorker.Instance.Start(HkCameraClient);
|
||||
LogInfo("下载队列服务已启动");
|
||||
JellyfinMonitorQueueService.Instance.Start();
|
||||
LogInfo("Jellyfin 监控服务已启动");
|
||||
TimeClearService = new TimeClearDataService();
|
||||
TimeClearService.Info += (m) => LogInfo($"[清理]{m}");
|
||||
TimeClearService.Start();
|
||||
LogInfo("定时清理服务已启动");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"[TaskServices] 启动失败: {ex.Message}");
|
||||
LogError($"后台服务启动失败: {ex.Message}");
|
||||
}
|
||||
|
||||
// 初始化 gridRULog 并启动 UI 定时刷新
|
||||
@@ -115,6 +183,7 @@ namespace FATrace.OEMApp
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"初始化界面失败: {ex.Message}");
|
||||
LogError($"初始化界面失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +194,7 @@ namespace FATrace.OEMApp
|
||||
/// <param name="Code"></param>
|
||||
private void PLCDataService_ScanCodeEventHandler(object? sender, string Code)
|
||||
{
|
||||
|
||||
LogInfo($"扫码: {Code}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -135,7 +204,7 @@ namespace FATrace.OEMApp
|
||||
/// <param name="e"></param>
|
||||
private void PLCDataService_PlcConnectedEventHandler(object? sender, string e)
|
||||
{
|
||||
|
||||
LogInfo($"PLC连接: {e}");
|
||||
}
|
||||
|
||||
private void NVRLogin()
|
||||
@@ -145,13 +214,16 @@ namespace FATrace.OEMApp
|
||||
HkCameraClient.NVR_UserName = ConfigHelper.GetValue("NVRUserName");
|
||||
HkCameraClient.NVR_Pw = ConfigHelper.GetValue("NVRPw");
|
||||
|
||||
LogInfo($"尝试登录NVR {HkCameraClient.NVR_IP}:{HkCameraClient.NVR_Port} 用户 {HkCameraClient.NVR_UserName}");
|
||||
var result = HkCameraClient.Sdk_NET_DVR_Login_V30(HkCameraClient.NVR_IP, HkCameraClient.NVR_Port, HkCameraClient.NVR_UserName, HkCameraClient.NVR_Pw);
|
||||
if (result)
|
||||
{
|
||||
MessageBox.Show("登录成功");
|
||||
LogInfo("NVR 登录成功");
|
||||
return;
|
||||
}
|
||||
MessageBox.Show($"登录失败:{HkCameraClient.LastMsgErr}");
|
||||
LogError($"NVR 登录失败: {HkCameraClient.LastMsgErr}");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -197,13 +269,16 @@ namespace FATrace.OEMApp
|
||||
HkCameraClient.NVR_UserName = ConfigHelper.GetValue("NVRUserName");
|
||||
HkCameraClient.NVR_Pw = ConfigHelper.GetValue("NVRPw");
|
||||
|
||||
LogInfo($"尝试登录NVR {HkCameraClient.NVR_IP}:{HkCameraClient.NVR_Port} 用户 {HkCameraClient.NVR_UserName}");
|
||||
var result = HkCameraClient.Sdk_NET_DVR_Login_V30(HkCameraClient.NVR_IP, HkCameraClient.NVR_Port, HkCameraClient.NVR_UserName, HkCameraClient.NVR_Pw);
|
||||
if (result)
|
||||
{
|
||||
MessageBox.Show("登录成功");
|
||||
LogInfo("NVR 登录成功");
|
||||
return;
|
||||
}
|
||||
MessageBox.Show($"登录失败:{HkCameraClient.LastMsgErr}");
|
||||
LogError($"NVR 登录失败: {HkCameraClient.LastMsgErr}");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -219,10 +294,11 @@ namespace FATrace.OEMApp
|
||||
code: CurInBagCode,
|
||||
rawName: CurInBagRawName,
|
||||
user: CurUserName,
|
||||
start: DateTime.Now.AddMinutes(-5),
|
||||
start: DateTime.Now.AddSeconds(-30),
|
||||
end: DateTime.Now
|
||||
);
|
||||
MessageBox.Show($"已入队下载任务,Id={taskId}");
|
||||
LogInfo($"下载任务入队 Id={taskId} Code={CurInBagCode}");
|
||||
}
|
||||
|
||||
private void HkCameraClient_NVRLoadVideoProcessEventHandler(object? sender, short value)
|
||||
@@ -231,6 +307,11 @@ namespace FATrace.OEMApp
|
||||
{
|
||||
DownloadProgressBarMain.Value = value;
|
||||
}));
|
||||
if (value >= 100 && _lastProgressLogged != 100)
|
||||
{
|
||||
_lastProgressLogged = 100;
|
||||
LogInfo("下载进度 100%");
|
||||
}
|
||||
}
|
||||
|
||||
private void btnDownloadName_Click(object sender, EventArgs e)
|
||||
@@ -287,10 +368,12 @@ namespace FATrace.OEMApp
|
||||
if (Result.Result)
|
||||
{
|
||||
MessageBox.Show($"[暂停成功] :{Result.Msg}");
|
||||
LogInfo($"下载暂停: {Result.Msg}");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"[暂停失败] :{Result.Msg}");
|
||||
LogWarn($"下载暂停失败: {Result.Msg}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -512,7 +595,7 @@ namespace FATrace.OEMApp
|
||||
code: CurInBagCode,
|
||||
rawName: CurInBagRawName,
|
||||
user: CurUserName,
|
||||
start: DateTime.Now.AddMinutes(-5),
|
||||
start: DateTime.Now.AddSeconds(-100),
|
||||
end: DateTime.Now
|
||||
);
|
||||
MessageBox.Show($"[Test] 已入队下载任务,Id={taskId}");
|
||||
@@ -526,10 +609,16 @@ namespace FATrace.OEMApp
|
||||
private void HkCameraClient_NVRLoadVideoCompleteEventHandler(object? sender, string e)
|
||||
{
|
||||
// 计算用于去重的 key(文件路径或文件名,小写)
|
||||
if (string.IsNullOrWhiteSpace(e) && string.IsNullOrWhiteSpace(this.CurrentVideoPath))
|
||||
{
|
||||
LogWarn("下载完成事件未提供文件名,忽略");
|
||||
return;
|
||||
}
|
||||
var localNameOrPath = (!string.IsNullOrWhiteSpace(e) && (e.Contains("\\") || e.Contains("/") || e.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)))
|
||||
? e
|
||||
: this.CurrentVideoPath;
|
||||
var key = (System.IO.Path.GetFileName(localNameOrPath) ?? localNameOrPath).ToLowerInvariant();
|
||||
var safePath = localNameOrPath ?? string.Empty;
|
||||
var key = (System.IO.Path.GetFileName(safePath) ?? safePath).ToLowerInvariant();
|
||||
|
||||
// 若同一 key 已在处理中,则忽略本次回调
|
||||
if (!_downloadProcessingKeys.TryAdd(key, 0))
|
||||
@@ -538,6 +627,8 @@ namespace FATrace.OEMApp
|
||||
return;
|
||||
}
|
||||
|
||||
LogInfo($"下载完成: {localNameOrPath}");
|
||||
|
||||
// 先保存当前的信息,记录主键 Id
|
||||
var rawUse = new OEMRawUse()
|
||||
{
|
||||
@@ -853,5 +944,11 @@ namespace FATrace.OEMApp
|
||||
{
|
||||
HkCameraClient.Sdk_NET_DVR_StopGetFile();
|
||||
}
|
||||
|
||||
protected override void OnFormClosing(FormClosingEventArgs e)
|
||||
{
|
||||
try { TimeClearService?.Stop(); } catch { }
|
||||
base.OnFormClosing(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user