210 lines
6.9 KiB
C#
210 lines
6.9 KiB
C#
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;
|
||
using Prism.Commands;
|
||
using Prism.Events;
|
||
using Prism.Mvvm;
|
||
using Prism.Regions;
|
||
using ScreenRecorderLib;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Media;
|
||
using Unity;
|
||
|
||
namespace DM_Weight.ViewModels
|
||
{
|
||
internal class MainWindowViewModel : BindableBase
|
||
{
|
||
#region 录屏相关
|
||
public Recorder _recorder;
|
||
private string _outputFolder;
|
||
private string _outputFilePath;
|
||
public static MainWindowViewModel vm;
|
||
#endregion
|
||
private readonly ILog logger = LogManager.GetLogger(typeof(MainWindowViewModel));
|
||
|
||
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;
|
||
//}
|
||
private FingerprintUtil _fingerprintUtil;
|
||
IRegionManager _regionManager;
|
||
IUnityContainer _container;
|
||
//private CHKFunction _cHKFunction;
|
||
public MainWindowViewModel(IRegionManager regionManager, IUnityContainer container, IEventAggregator eventAggregator, FingerprintUtil fingerprintUtil, ScreenUtil screenUtil)
|
||
{
|
||
vm = this;
|
||
//_portUtil = portUtil;
|
||
this.eventAggregator = eventAggregator;
|
||
this.eventAggregator.GetEvent<SnackbarEvent>().Subscribe(doMyPrismEvent2);
|
||
this.eventAggregator.GetEvent<PrintScreenEvent>().Subscribe(PrintScreen);
|
||
_screenUtil = screenUtil;
|
||
_fingerprintUtil = fingerprintUtil;
|
||
_regionManager = regionManager;
|
||
_container = container;
|
||
//_cHKFunction = cHKFunction;
|
||
System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
|
||
{
|
||
if (App.DbConnectionFail)
|
||
{
|
||
_regionManager.RequestNavigate("MainRegion", "EmergencyWindow");
|
||
}
|
||
else
|
||
{
|
||
_container.RegisterType<object, LoginWindow>("LoginWindow");
|
||
_regionManager.RequestNavigate("MainRegion", "LoginWindow");
|
||
}
|
||
|
||
}));
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
#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}");
|
||
}
|
||
}
|
||
}
|