123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- using PLCControl;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Shaker.Model;
- using System.Configuration;
- using System.Windows.Input;
- using HandyControl.Interactivity.Commands;
- using System.Diagnostics;
- using ShakerControl.Tools;
- using System.Windows;
- using EventBus;
- using HandyControl.Interactivity;
- using System.Windows.Controls;
- using Shaker.ViewModel;
- using System.Windows.Markup;
- namespace ShakerControl.ViewModel
- {
- internal class MainWindowViewModel:ViewModelBase<ModelBase>
- {
- private bool ismousedown = false;
- private Point lastpoint;
- public ICommand MouseDownCommand => new DelegateCommand<ExCommandParameter>(MouseDown);
- private void MouseDown(ExCommandParameter parameter)
- {
- if(parameter.EventArgs is MouseButtonEventArgs args && args.LeftButton == MouseButtonState.Pressed)
- {
- if (!ismousedown)
- {
- ismousedown = true;
- MoveLenght = new Thickness();
- lastpoint = Mouse.GetPosition(args.Device.Target);
- }
- }
- else
- {
- ismousedown = false;
- MoveLenght = new Thickness();
- }
- }
- public double ControlWidth { get => controlWidth; set =>UpdateProperty(ref controlWidth,value); }
- public Thickness MoveLenght { get => moveLenght; set =>UpdateProperty(ref moveLenght,value); }
- public ICommand MouseMoveCommand=> new DelegateCommand<ExCommandParameter>(MouseMove);
- private void MouseMove(ExCommandParameter parameter)
- {
- if (!ismousedown) return;
- if(parameter.EventArgs is MouseEventArgs args && args.LeftButton == MouseButtonState.Pressed)
- {
- var point = Mouse.GetPosition(args.Device.Target);
- MoveLenght = new Thickness(point.X-lastpoint.X,0,lastpoint.X-point.X,0);
- }
- }
- public ICommand MouseUpCommand => new DelegateCommand<ExCommandParameter>(MouseUp);
- private void MouseUp(ExCommandParameter parameter)
- {
- if(parameter.Sender is ItemsControl items)
- {
- int count = items.Items.Count;
- if (MoveLenght.Left <= -30)
- {
- if (SelectedIndex == count - 1)
- {
- SelectedIndex = 0;
- }
- else
- {
- SelectedIndex++;
- }
- }
- else if(MoveLenght.Left>=30)
- {
- if(SelectedIndex ==0)
- {
- SelectedIndex = count - 1;
- }
- else
- {
- SelectedIndex--;
- }
- }
- }
- ismousedown = false;
- MoveLenght = new Thickness();
- }
- public ICommand MouseLeaveCommand=> new DelegateCommand<ExCommandParameter>(MouseLeave);
- private void MouseLeave(ExCommandParameter parameter)
- {
- if (parameter.Sender is ItemsControl items)
- {
- int count = items.Items.Count;
- if (MoveLenght.Left <= -30)
- {
- if (SelectedIndex == count - 1)
- {
- SelectedIndex = 0;
- }
- else
- {
- SelectedIndex++;
- }
- }
- else if (MoveLenght.Left >= 30)
- {
- if (SelectedIndex == 0)
- {
- SelectedIndex = count - 1;
- }
- else
- {
- SelectedIndex--;
- }
- }
- }
- ismousedown = false;
- MoveLenght = new Thickness();
- }
- private string clientID=string.Empty;
- [AllowNull]
- private EasyMQ.IBus bus;
- private bool isLoggedIn = false;
- private CancellationTokenSource tokenSource = new CancellationTokenSource();
- private double controlWidth;
- private Thickness moveLenght;
- private int selectedIndex=0;
- private string serverIP = "127.0.0.1";
- private int serverPort = 61616;
- private bool mangerIsOnLine = false;
- public const string GET_DATA_EVENT = "GetDataEvent";
- public bool IsOffine { get;private set; } = false;
- private MainWindowViewModel()
- {
- if(ConfigurationManager.AppSettings.AllKeys.Contains(nameof(IsOffine)))
- {
- IsOffine = bool.Parse(ConfigurationManager.AppSettings[nameof(IsOffine)]!.ToString());
- }
- if(ConfigurationManager.AppSettings.AllKeys.Contains(nameof(ServerIP)))
- {
- ServerIP = ConfigurationManager.AppSettings[nameof(ServerIP)]!.ToString();
- }
- else
- {
- ConfigurationManager.AppSettings.Set(nameof(ServerIP), serverIP);
- }
- if(ConfigurationManager.AppSettings.AllKeys.Contains(nameof(ServerPort)))
- {
- try
- {
- ServerPort = int.Parse(ConfigurationManager.AppSettings[nameof(ServerPort)]!.ToString());
- }
- catch
- {
- ConfigurationManager.AppSettings.Set(nameof(ServerPort), serverPort.ToString());
- }
- }
- else
- {
- ConfigurationManager.AppSettings.Set(nameof(ServerPort), serverPort.ToString());
- }
- if(ConfigurationManager.AppSettings.AllKeys.Contains("ClientID"))
- {
- clientID = ConfigurationManager.AppSettings["ClientID"]!.ToString();
- if(string.IsNullOrEmpty(clientID))
- {
- Tools.MessageBoxHelper.Error($"未配置ClientID");
- App.Current.Shutdown();
- return;
- }
- }
- else
- {
- ConfigurationManager.AppSettings.Set("ClientID", "");
- Tools.MessageBoxHelper.Error($"未配置ClientID");
- App.Current.Shutdown();
- return;
- }
- }
-
- static MainWindowViewModel()
- {
- }
- private ManualResetEvent resetEvent = new ManualResetEvent(true);
- public void Continue()
- {
- if (MainTaskIsStart) return;
- resetEvent.Set();
- MainTaskIsStart = true;
- }
- public void Paused()
- {
- if (!MainTaskIsStart) return;
- resetEvent.Reset();
- MainTaskIsStart = false;
- }
- public bool MainTaskIsStart { get; private set; } = true;
- public bool MangerIsOnLine { get => mangerIsOnLine; set =>UpdateProperty(ref mangerIsOnLine,value); }
- [AllowNull]
- public Action InitedActive { get; set; }
- public void StartMainThread()
- {
- InitedActive?.Invoke();
- tokenSource?.Cancel();
- tokenSource = new CancellationTokenSource();
- var dataevent = GetEvent(GET_DATA_EVENT);
- Task.Run(() =>
- {
- while(!tokenSource.IsCancellationRequested)
- {
- dataevent.Publish(this);
- resetEvent.WaitOne();
- Thread.Sleep(SystemConfig.LoopTime);
- }
- });
- Paused();
- }
- public string ServerIP { get => serverIP; set =>UpdateProperty(ref serverIP, value); }
- public int ServerPort { get => serverPort; set =>UpdateProperty(ref serverPort, value); }
- public void Stop()=>tokenSource?.Cancel();
- public ViewModel.Setting.SettingViewModel Setting { get; } = new Setting.SettingViewModel();
- public SystemConfigViewModel SystemConfig { get; } = new SystemConfigViewModel();
- public ICommand ExitCommand => new DelegateCommand(() =>
- {
- PromptViewModel.Default.Init();
- PromptViewModel.Default.Message = "是否退出程序?";
- PromptViewModel.Default.YesAction = () => App.Current.Shutdown(); ;
- PromptViewModel.Default.IconType = IconType.Ask;
- PromptViewModel.Default.IsOpen = true;
- });
- public ICommand ShutDownCommand => new DelegateCommand(() =>App.Current.Shutdown());
- public UserViewModel User { get; } = new UserViewModel();
- public bool IsLoggedIn { get => isLoggedIn; set =>UpdateProperty(ref isLoggedIn,value); }
- public MainPage.MainPageViewModel MainPage { get; } = new MainPage.MainPageViewModel();
- public string ClientID { get => clientID;private set =>UpdateProperty(ref clientID,value); }
- public static MainWindowViewModel Default { get; } = new MainWindowViewModel();
- public EasyMQ.IBus Bus => bus;
- public int SelectedIndex { get => selectedIndex; set =>UpdateProperty(ref selectedIndex, value); }
- public ICommand ClosingCommand => new DelegateCommand(Closing);
- private void Closing()
- {
- SystemLog.Default.WriteLog("客户端退出", LogType.Message);
- if(MainPage?.Shakers!=null && MainPage.Shakers.Count>0)
- {
- foreach (var item in MainPage.Shakers)
- {
- item.Exit();
- }
- }
- tokenSource?.Cancel();
- bus?.Dispose();
- }
- public void Init(bool isOffine)
- {
- IsOffine = isOffine;
- Init();
- }
- public override void Init()
- {
- try
- {
- if (!IsOffine)
- {
- bus = EasyMQ.ActiveHutch.Default.CreateBus($"tcp://{ServerIP}:{ServerPort}?alwaysSessionAsync=true", new EasyMQ.MessagePackSerializer());
- bus.PubSub.Subscribe<Shaker.Model.MangerExitModel>((data, _) =>
- {
- DispatherInovke.Inovke(() =>
- {
- MangerIsOnLine = data.Online;
- if (!data.Online)
- {
- PromptViewModel.Default.Init();
- PromptViewModel.Default.Message = "管理器已离线";
- PromptViewModel.Default.IconType = IconType.Error;
- PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
- PromptViewModel.Default.IsOpen = true;
- }
- });
- });
- bus.PubSub.Subscribe<SystemConfigModel>((data, _) =>
- {
- SystemConfig.LoopTime = data.LoopTime;
- SystemConfig.RequestPermissionsTimeout = data.RequestPermissionsTimeout;
- SystemConfig.CommunicationTimeout = data.CommunicationTimeout;
- });
- }
- }
- catch
- {
- MessageBoxHelper.Error("服务端未运行,程序退出");
- Tools.DispatherInovke.Inovke(() => App.Current.Shutdown());
- return;
- }
- MainPage.Init();
- }
- }
- }
|