2025-07-10 11:52:24 +08:00
|
|
|
|
using DM_Weight.Finger;
|
|
|
|
|
using DM_Weight.HIKVISION;
|
|
|
|
|
using DM_Weight.msg;
|
|
|
|
|
using DM_Weight.Port;
|
|
|
|
|
using DM_Weight.util;
|
|
|
|
|
using DM_Weight.Views;
|
|
|
|
|
using log4net;
|
|
|
|
|
using log4net.Repository.Hierarchy;
|
|
|
|
|
using MaterialDesignThemes.Wpf;
|
2024-12-03 13:22:42 +08:00
|
|
|
|
using Prism.Commands;
|
|
|
|
|
using Prism.Events;
|
|
|
|
|
using Prism.Mvvm;
|
|
|
|
|
using Prism.Regions;
|
2025-07-10 11:52:24 +08:00
|
|
|
|
using ScreenRecorderLib;
|
2024-12-03 13:22:42 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2025-07-10 11:52:24 +08:00
|
|
|
|
using System.IO;
|
2024-12-03 13:22:42 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using Unity;
|
|
|
|
|
|
|
|
|
|
namespace DM_Weight.ViewModels
|
|
|
|
|
{
|
|
|
|
|
internal class MainWindowViewModel : BindableBase
|
|
|
|
|
{
|
2025-07-10 11:52:24 +08:00
|
|
|
|
#region 录屏相关
|
|
|
|
|
public Recorder _recorder;
|
|
|
|
|
private string _outputFolder;
|
|
|
|
|
private string _outputFilePath;
|
|
|
|
|
public static MainWindowViewModel vm;
|
|
|
|
|
#endregion
|
|
|
|
|
private readonly ILog logger = LogManager.GetLogger(typeof(MainWindowViewModel));
|
|
|
|
|
|
2024-12-03 13:22:42 +08:00
|
|
|
|
private string _title = "Prism App"; //标题
|
|
|
|
|
|
|
|
|
|
private ISnackbarMessageQueue _snackbarMessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3));
|
|
|
|
|
|
|
|
|
|
private SolidColorBrush _colorBrush;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//private PortUtil _portUtil;
|
|
|
|
|
|
|
|
|
|
private ScreenUtil _screenUtil;
|
|
|
|
|
|
|
|
|
|
public SolidColorBrush Background
|
|
|
|
|
{
|
|
|
|
|
get => _colorBrush;
|
|
|
|
|
set => SetProperty(ref _colorBrush, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ISnackbarMessageQueue SnackbarMessageQueue
|
|
|
|
|
{
|
|
|
|
|
get => _snackbarMessageQueue;
|
|
|
|
|
set => SetProperty(ref _snackbarMessageQueue, value);
|
|
|
|
|
}
|
|
|
|
|
public string Title
|
|
|
|
|
{
|
|
|
|
|
get { return _title; }
|
|
|
|
|
set { SetProperty(ref _title, value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IEventAggregator eventAggregator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//public MainWindowViewModel(IEventAggregator eventAggregator, PortUtil portUtil, ScreenUtil screenUtil)
|
|
|
|
|
//{
|
|
|
|
|
// _portUtil = portUtil;
|
|
|
|
|
// this.eventAggregator = eventAggregator;
|
|
|
|
|
// this.eventAggregator.GetEvent<SnackbarEvent>().Subscribe(doMyPrismEvent2);
|
|
|
|
|
// _screenUtil = screenUtil;
|
|
|
|
|
//}
|
2025-03-04 13:13:44 +08:00
|
|
|
|
private FingerprintUtil _fingerprintUtil;
|
2024-12-03 13:22:42 +08:00
|
|
|
|
IRegionManager _regionManager;
|
|
|
|
|
IUnityContainer _container;
|
|
|
|
|
//private CHKFunction _cHKFunction;
|
2025-03-04 13:13:44 +08:00
|
|
|
|
public MainWindowViewModel(IRegionManager regionManager, IUnityContainer container, IEventAggregator eventAggregator, FingerprintUtil fingerprintUtil, ScreenUtil screenUtil)
|
2024-12-03 13:22:42 +08:00
|
|
|
|
{
|
2025-07-10 11:52:24 +08:00
|
|
|
|
vm = this;
|
2024-12-03 13:22:42 +08:00
|
|
|
|
//_portUtil = portUtil;
|
|
|
|
|
this.eventAggregator = eventAggregator;
|
|
|
|
|
this.eventAggregator.GetEvent<SnackbarEvent>().Subscribe(doMyPrismEvent2);
|
2025-07-10 11:52:24 +08:00
|
|
|
|
this.eventAggregator.GetEvent<PrintScreenEvent>().Subscribe(PrintScreen);
|
2025-03-04 13:13:44 +08:00
|
|
|
|
_screenUtil = screenUtil;
|
|
|
|
|
_fingerprintUtil = fingerprintUtil;
|
2024-12-03 13:22:42 +08:00
|
|
|
|
_regionManager = regionManager;
|
|
|
|
|
_container = container;
|
|
|
|
|
//_cHKFunction = cHKFunction;
|
2025-01-15 18:57:17 +08:00
|
|
|
|
System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
|
|
|
|
|
{
|
2025-03-03 16:07:21 +08:00
|
|
|
|
if (App.DbConnectionFail)
|
|
|
|
|
{
|
|
|
|
|
_regionManager.RequestNavigate("MainRegion", "EmergencyWindow");
|
|
|
|
|
}
|
|
|
|
|
else
|
2025-03-04 13:13:44 +08:00
|
|
|
|
{
|
|
|
|
|
_container.RegisterType<object, LoginWindow>("LoginWindow");
|
2025-03-03 16:07:21 +08:00
|
|
|
|
_regionManager.RequestNavigate("MainRegion", "LoginWindow");
|
2025-03-04 13:13:44 +08:00
|
|
|
|
}
|
2024-12-03 13:22:42 +08:00
|
|
|
|
|
2025-01-15 18:57:17 +08:00
|
|
|
|
}));
|
2024-12-03 13:22:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void doMyPrismEvent2(AlertMsg msg)
|
|
|
|
|
{
|
|
|
|
|
switch (msg.Type)
|
|
|
|
|
{
|
|
|
|
|
case MsgType.INFO:
|
|
|
|
|
this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00e676"));
|
|
|
|
|
break;
|
|
|
|
|
case MsgType.ERROR:
|
|
|
|
|
this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c"));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00e676"));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
SnackbarMessageQueue.Enqueue(msg.Message);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-10 11:52:24 +08:00
|
|
|
|
#region 录屏
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 录屏事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="type">0停止录屏;1开始录屏;2退出软件清进程</param>
|
|
|
|
|
void PrintScreen(int type)
|
|
|
|
|
{
|
|
|
|
|
if(type==0)
|
|
|
|
|
{
|
|
|
|
|
StopPrintScreen();
|
|
|
|
|
//删除7天前的录屏文件
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(_outputFolder))
|
|
|
|
|
{
|
|
|
|
|
var files = Directory.GetFiles(_outputFolder);
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
var fileInfo = new FileInfo(file);
|
|
|
|
|
if (fileInfo.CreationTime < DateTime.Now.AddDays(-7))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
fileInfo.Delete();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"删除录屏文件失败: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (type == 1)
|
|
|
|
|
{
|
|
|
|
|
StartPrintScreen();
|
|
|
|
|
}
|
|
|
|
|
else if (type == 2)
|
|
|
|
|
{
|
|
|
|
|
_recorder?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
void StopPrintScreen()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//退出登录结束录屏
|
|
|
|
|
_recorder?.Stop();
|
|
|
|
|
}
|
|
|
|
|
//录屏
|
|
|
|
|
void StartPrintScreen()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// 创建输出目录
|
|
|
|
|
_outputFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Log", "ScreenRecordings");
|
|
|
|
|
if (!Directory.Exists(_outputFolder))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(_outputFolder);
|
|
|
|
|
}
|
|
|
|
|
// 生成输出文件名
|
|
|
|
|
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
|
|
|
|
_outputFilePath = Path.Combine(_outputFolder, $"{timestamp}.mp4");
|
|
|
|
|
|
|
|
|
|
// 设置录制选项
|
|
|
|
|
var options = new RecorderOptions();
|
|
|
|
|
|
|
|
|
|
// 创建录制器实例
|
|
|
|
|
_recorder = Recorder.CreateRecorder(options);
|
|
|
|
|
|
|
|
|
|
// 设置事件处理
|
|
|
|
|
_recorder.OnRecordingComplete += Recorder_OnRecordingComplete;
|
|
|
|
|
_recorder.OnRecordingFailed += Recorder_OnRecordingFailed;
|
|
|
|
|
|
|
|
|
|
// 开始录制
|
|
|
|
|
_recorder.Record(_outputFilePath);
|
|
|
|
|
}
|
|
|
|
|
//录制失败
|
|
|
|
|
private void Recorder_OnRecordingFailed(object sender, RecordingFailedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
logger.Info($"录制失败: {e.Error}");
|
|
|
|
|
}
|
|
|
|
|
//录制完成
|
|
|
|
|
private void Recorder_OnRecordingComplete(object sender, RecordingCompleteEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
logger.Info($"录制完成: {e.FilePath}");
|
|
|
|
|
}
|
2024-12-03 13:22:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|