You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.2 KiB

3 years ago
using EC.Entity.Warning;
using EC.UsingEventAggregator;
using EC.Utils;
using EC.Utils.ZMQ;
using ECMonitor.Code;
using ECMonitor.Code.Models;
using ECMonitor.Manager;
using ECMonitor.Page.Name;
using ECMonitor.Views;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using System;
using System.Windows;
namespace ECMonitor
{
public class MainWindowViewModel : BaseModel
{
private MainWindow _w;
private ZMQManager _mqManager { get; set; }
private WebSocketManager _webSocketManager = null;
private FrmVideoPlayer _frmVideoPlayer;
private FrmVideoWanging _frmVideoWanging;
public DelegateCommand<Window> LoadCommand { get; private set; }
public DelegateCommand<string> NavigateCommand { get; private set; }
public MainWindowViewModel(IRegionManager regionManager, IEventAggregator ea) : base(regionManager, ea)
{
MessageManager.Init(ea);
LoadCommand = new DelegateCommand<Window>(Load);
NavigateCommand = new DelegateCommand<string>(Navigate);
regionManager.RegisterViewWithRegion(RegionsName.MainWindowCenter, typeof(Page.CameraMonitor.Main));
_mqManager = new ZMQManager();
_mqManager.OnDataReceived += ZMQDataReceived;
_webSocketManager = new WebSocketManager();
_webSocketManager.OnDataReceived += WebSocketDataReceived;
StartServer();
_frmVideoPlayer = new FrmVideoPlayer(ea);
_frmVideoWanging = new FrmVideoWanging(ea);
}
private void Navigate(string navigatePath)
{
if (navigatePath != null)
{
_regionManager.RequestNavigate(RegionsName.MainWindowCenter, navigatePath);
}
}
//窗口载入
private void Load(Window w)
{
_w = (MainWindow)w;
w.Closed += (sender, e) =>
{
StopServer();
};
}
private void StartServer()
{
_mqManager.Start();
_webSocketManager.Start();
}
private void StopServer()
{
_mqManager?.Stop();
_webSocketManager.Stop();
}
//关闭窗口
private void CloseWindow()
{
// canClose = true;
_w.Close();
}
#region ZMQ WebSocket
/// <summary>
/// ZMQ 消息
/// </summary>
/// <param name="sender"></param>
/// <param name="msgModel"></param>
private void ZMQDataReceived(object sender, PubSubModel msgModel)
{
SubMessageIn(msgModel);
}
private void SubMessageIn(PubSubModel msgModel)
{
if (msgModel.TopicLower().StartsWith(ZMQTopic.Show_Single_Video))//单个视频显示
{
_frmVideoPlayer.Play(msgModel.msg);
}
else if (msgModel.TopicLower().StartsWith(ZMQTopic.MainFrom_Show_Video))//在主窗体显示
{
_ea.GetEvent<ShowVoideInMainEvent>().Publish(msgModel.msg);
_w.ShowWindow();
}
//else if (msgModel.TopicLower() == ZMQTopic.Warning_Show_Video)//预警视频只是显示
//{
// _frmVideoWanging.NewWarningPlay(msgModel.msg);
//}
else if (msgModel.TopicLower() == ZMQTopic.AI_Detect_Warning_Data)//视频预警输入
{
try
{
WarningModel warningModel = msgModel.msg.ToObject<WarningModel>();
_frmVideoWanging.NewWarningPlay(warningModel.ip);
_ea.GetEvent<WaningVoideEvent>().Publish(warningModel);
}
catch (Exception ex)
{
LogFactory.GetLogger().Error(ex);
}
}
}
private void WebSocketDataReceived(object sender, PubSubModel pubSubModel)
{
SubMessageIn(pubSubModel);
}
#endregion ZMQ WebSocket
}
}