123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- using HandyControl.Interactivity.Commands;
- using OxyPlot;
- using Shaker.Model;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel.DataAnnotations;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace ShakerManger.ViewModel
- {
- internal class ShakerControlViewModel:Shaker.ViewModel.DisplayViewModel<Shaker.Model.ShakerControlModel>
- {
- public string ClientID { get => Model.ClientID; set => UpdateProperty(ref Model.ClientID, value); }
- public int MaxItemCount => 1000;
- private ObservableCollection<DataMangerItemViewModel> Items { get; } = new ObservableCollection<DataMangerItemViewModel>();
- private List<OxyPlot.Series.LineSeries> _LineSeries = new List<OxyPlot.Series.LineSeries>();
- public string[] PropertiyNames { get; }= new string[]
- {
- nameof(DataMangerItemModel.OperatingStatus),
- nameof(DataMangerItemModel.OutputCurrent),
- nameof(DataMangerItemModel.OutputVoltage),
- nameof(DataMangerItemModel.ExcitationCurrent),
- nameof(DataMangerItemModel.ExcitationVoltage),
- nameof(DataMangerItemModel.ThreePhaseVoltage),
- nameof(DataMangerItemModel.AbutmentTemperature0),
- nameof(DataMangerItemModel.AbutmentTemperature1),
- nameof(DataMangerItemModel.DCVoltage1),
- nameof(DataMangerItemModel.DCVoltage2),
- nameof(DataMangerItemModel.DCVoltage3),
- nameof(DataMangerItemModel.DCVoltage4),
- nameof(DataMangerItemModel.Gain),
- };
- public ICommand DisConnectCommand=> new DelegateCommand(DisConnect);
- private void DisConnect()
- {
- PromptViewModel.Default.Init();
- PromptViewModel.Default.IconType = IconType.Ask;
- PromptViewModel.Default.Message = "是否断开功放连接?";
- PromptViewModel.Default.YesAction = async ()=>
- {
- var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl, Shaker.Model.Result>(new SysControl()
- {
- Cmd = SysCmd.DisConnect,
- }, Properties);
- if (result == null ||!result.Success)
- {
- PromptViewModel.Default.Init();
- PromptViewModel.Default.IconType = IconType.Error;
- PromptViewModel.Default.Message = result == null? "通信超时" : result.Message;
- PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
- PromptViewModel.Default.IsOpen = true;
- }
- };
- PromptViewModel.Default.IsOpen = true;
- }
- public ICommand ConnectCommand => new DelegateCommand(Connect);
- private async void Connect()
- {
- var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl, Shaker.Model.Result>(new SysControl()
- {
- Cmd = SysCmd.Connect,
- },Properties);
- if(result == null || !result.Success)
- {
- PromptViewModel.Default.Init();
- PromptViewModel.Default.IconType = IconType.Error;
- PromptViewModel.Default.Message = result == null?"通信超时": result.Message;
- PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
- PromptViewModel.Default.IsOpen= true;
- }
- }
- public bool IsOnline { get => isOnline; set =>UpdateProperty(ref isOnline, value); }
- public DataMangerItemViewModel CurrentData { get => currentData; set =>UpdateProperty(ref currentData, value); }
- public Boolean SetPlotConfigIsOpen
- {
- get => setPlotConfigIsOpen;
- set
- {
- UpdateProperty(ref setPlotConfigIsOpen, value);
- if(value)
- {
- foreach(var item in PlotConfig)
- {
- item.Init();
- }
- }
- }
- }
- public bool IsConnected { get => isConnected; set =>UpdateProperty(ref isConnected, value); }
- public string[] AnalogNames { get; } = new string[12];
- public string[] Units { get; } = new string[12];
- private Dictionary<string, string> Properties { get; init; } = new Dictionary<string, string>();
- public ObservableCollection<PlotConfigViewModel> PlotConfig { get; } = new ObservableCollection<PlotConfigViewModel>();
- public ShakerControlViewModel(Shaker.Model.ShakerControlModel model)
- {
- Model = model;
- CurrentData = new DataMangerItemViewModel(new DataMangerItemModel());
- Properties[Shaker.Model.GlobalVariable.ShakerIDKey] = model.Id;
- AnalogNames[0] = Model.OperatingStatus.Name;
- AnalogNames[1] = Model.OutputCurrent.Name;
- AnalogNames[2] = Model.OutputVoltage.Name;
- AnalogNames[3] = Model.ExcitationCurrent.Name;
- AnalogNames[4] = Model.ExcitationVoltage.Name;
- AnalogNames[5] = Model.ThreePhaseVoltage.Name;
- Units[0] = Model.OperatingStatus.Unit;
- Units[1] = Model.OutputCurrent.Unit;
- Units[2] = Model.OutputVoltage.Unit;
- Units[3] = Model.ExcitationCurrent.Unit;
- Units[4] = Model.ExcitationVoltage.Unit;
- Units[5] = Model.ThreePhaseVoltage.Unit;
- if (!string.IsNullOrEmpty(model.AbutmentTemperature0.Name))
- {
- AnalogNames[6] = Model.AbutmentTemperature0.Name;
- Units[6] = Model.AbutmentTemperature0.Unit;
- }
- if (!string.IsNullOrEmpty(model.AbutmentTemperature1.Name))
- {
- AnalogNames[7] = Model.AbutmentTemperature1.Name;
- Units[7] = Model.AbutmentTemperature1.Unit;
- }
- if (!string.IsNullOrEmpty(model.DCVoltage1.Name))
- {
- AnalogNames[8] = Model.DCVoltage1.Name;
- Units[8] = Model.DCVoltage1.Unit;
- }
- if (!string.IsNullOrEmpty(model.DCVoltage2.Name))
- {
- AnalogNames[9] = Model.DCVoltage2.Name;
- Units[9] = Model.DCVoltage2.Unit;
- }
- if (!string.IsNullOrEmpty(model.DCVoltage3.Name))
- {
- AnalogNames[10] = Model.DCVoltage3.Name;
- Units[10] = Model.DCVoltage3.Unit;
- }
- if (!string.IsNullOrEmpty(model.DCVoltage4.Name))
- {
- AnalogNames[11] = Model.DCVoltage4.Name;
- Units[11] = Model.DCVoltage4.Unit;
- }
- PlotModel.TextColor = OxyColors.White;
- PlotModel.TitleColor = OxyColors.White;
- PlotModel.PlotAreaBorderColor = OxyColors.White;
- PlotModel.Axes.Add(new OxyPlot.Axes.DateTimeAxis()
- {
- Title = "时间",
- Position = OxyPlot.Axes.AxisPosition.Bottom,
- Key = "X",
- MaximumPadding = 0,
- MajorGridlineColor = OxyPlot.OxyColors.Gray,
- MajorGridlineStyle = LineStyle.Dot,
- MajorGridlineThickness = 1,
- MinimumPadding = 0,
- ExtraGridlineColor = OxyPlot.OxyColors.White,
- TextColor = OxyColors.White,
- TitleColor = OxyColors.White,
- AxislineColor = OxyColors.White,
- TicklineColor = OxyColors.White,
- });
- PlotModel.Axes.Add(new OxyPlot.Axes.LinearAxis()
- {
- Title = "值",
- Position = OxyPlot.Axes.AxisPosition.Left,
- Key = "Y",
- MajorGridlineStyle = LineStyle.Dot,
- MajorGridlineColor = OxyPlot.OxyColors.Gray,
- MajorGridlineThickness = 1,
- ExtraGridlineColor = OxyPlot.OxyColors.White,
- TextColor = OxyColors.White,
- TitleColor = OxyColors.White,
- AxislineColor = OxyColors.White,
- TicklineColor = OxyColors.White,
- });
- for (int i = 0; i < AnalogNames.Length; i++)
- {
- if (string.IsNullOrEmpty(AnalogNames[i]) || i == 0) continue;
- OxyPlot.Series.LineSeries lineSeries = new OxyPlot.Series.LineSeries();
- lineSeries.Title = AnalogNames[i];
- lineSeries.StrokeThickness = 1;
- lineSeries.XAxisKey = "X";
- lineSeries.YAxisKey = "Y";
- lineSeries.TrackerFormatString = "{1}:{2}\n{0}:{4}" + Units[i];
- lineSeries.CanTrackerInterpolatePoints = false;
- lineSeries.DataFieldX = nameof(DataMangerItemModel.Time);
- lineSeries.DataFieldY = PropertiyNames[i];
- _LineSeries.Add(lineSeries);
- PlotModel.Series.Add(lineSeries);
- PlotConfig.Add(new PlotConfigViewModel(lineSeries));
- PlotConfig[^1].PropertyChanged += (_, _) =>
- {
- PlotModel.InvalidatePlot(false);
- PlotModel.InvalidatePlot(true);
- };
- }
- {
- OxyPlot.Series.LineSeries lineSeries = new OxyPlot.Series.LineSeries();
- lineSeries.Title = "增益";
- lineSeries.XAxisKey = "X";
- lineSeries.YAxisKey = "Y";
- lineSeries.StrokeThickness = 1;
- lineSeries.TrackerFormatString = "{1}:{2}\n{0}:{4}%";
- lineSeries.CanTrackerInterpolatePoints = false;
- lineSeries.DataFieldX = nameof(DataMangerItemModel.Time);
- lineSeries.DataFieldY = PropertiyNames[^1];
- _LineSeries.Add(lineSeries);
- PlotModel.Series.Add(lineSeries);
- PlotConfig.Add(new PlotConfigViewModel(lineSeries));
- PlotConfig[^1].PropertyChanged += (_, _) =>
- {
- PlotModel.InvalidatePlot(false);
- PlotModel.InvalidatePlot(true);
- };
- }
- PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
- {
- IsLegendVisible = true,
- ShowInvisibleSeries = true,
- });
-
- BusManger.Defaut.Bus.PubSub.Subscribe<Shaker.Model.ShakerConnectState>((data, _) =>
- {
- if (Thread.CurrentThread == Application.Current?.Dispatcher?.Thread)
- {
- IsConnected = data.IsConnected;
- }
- else
- {
- App.Current?.Dispatcher?.BeginInvoke(() =>
- {
- IsConnected = data.IsConnected;
- });
- }
- }, $"{Shaker.Model.GlobalVariable.ShakerIDKey}='{Model.Id}'");
- BusManger.Defaut.Bus.PubSub.Subscribe<Shaker.Model.ShakerStatusModel>((data, _) =>
- {
- if (Thread.CurrentThread == Application.Current?.Dispatcher?.Thread)
- {
- GainSwich = data.GainSwich;
- }
- else
- {
- App.Current?.Dispatcher?.BeginInvoke(() =>
- {
- GainSwich = data.GainSwich;
- });
- }
- }, $"{Shaker.Model.GlobalVariable.ShakerIDKey}='{Model.Id}'");
- BusManger.Defaut.Bus.PubSub.Subscribe<Shaker.Model.ClientExitModel>((data, _) =>
- {
- if (Thread.CurrentThread == Application.Current?.Dispatcher?.Thread)
- {
- IsOnline = data.Online;
- }
- else
- {
- App.Current?.Dispatcher?.BeginInvoke(() =>
- {
- IsOnline = data.Online;
- });
- }
- }, $"{Shaker.Model.GlobalVariable.ShakerIDKey}='{Model.Id}'");
- BusManger.Defaut.Bus.PubSub.Subscribe<DataMangerItemModel>((model, property) =>
- {
- if(MainWindowViewModel.Default.IsSaveData) Sql.Default.Insert(model, Model.Id);
- if (Thread.CurrentThread == Application.Current?.Dispatcher?.Thread)
- {
- if(CurrentData==null)
- {
- CurrentData = new DataMangerItemViewModel(model);
- }
- else
- {
- CurrentData.UpDateModel(model);
- }
- Items.Add(new DataMangerItemViewModel(model));
- if (Items.Count > MaxItemCount) Items.RemoveAt(0);
- PlotModel.InvalidatePlot(false);
- _LineSeries.ForEach(x => x.ItemsSource = Items);
- PlotModel.InvalidatePlot(true);
- }
- else
- {
- App.Current?.Dispatcher?.BeginInvoke(() =>
- {
- if (CurrentData == null)
- {
- CurrentData = new DataMangerItemViewModel(model);
- }
- else
- {
- CurrentData.UpDateModel(model);
- }
- Items.Add(new DataMangerItemViewModel(model));
- if (Items.Count > MaxItemCount) Items.RemoveAt(0);
- PlotModel.InvalidatePlot(false);
- _LineSeries.ForEach(x => x.ItemsSource = Items);
- PlotModel.InvalidatePlot(true);
- });
- }
- }, $"{Shaker.Model.GlobalVariable.ShakerIDKey}='{Model.Id}'");
- BusManger.Defaut.Bus.PubSub.Subscribe<Shaker.Model.AllowRemoteControl>((model, property) =>
- {
- if (Thread.CurrentThread == Application.Current?.Dispatcher?.Thread)
- {
- IsRemoteControl = model == AllowRemoteControl.Remote;
- }
- else
- {
- App.Current?.Dispatcher?.BeginInvoke(() =>
- {
- IsRemoteControl = model == AllowRemoteControl.Remote;
- });
- }
- }, $"{Shaker.Model.GlobalVariable.ShakerIDKey}='{Model.Id}'");
- }
- public View.ShakerView Content { get; } = new View.ShakerView();
- public bool GainSwich { get => gainSwich; set =>UpdateProperty(ref gainSwich, value); }
- public override Type View { get; } = typeof(View.ShakerView);
- public ICommand OpenCommand => new DelegateCommand(Open);
- public ICommand CloseCommand => new DelegateCommand(Close);
- public ICommand StartCommand => new DelegateCommand(Start);
- public ICommand StopCommand => new DelegateCommand(Stop);
- public ICommand ResetCommand => new DelegateCommand(Reset);
- private bool isOpen = false;
- public bool ShakerControlIsOpen { get => shakerControlIsOpen; set => UpdateProperty(ref shakerControlIsOpen, value); }
- public OxyPlot.PlotModel PlotModel { get; } = new OxyPlot.PlotModel();
- public bool IsOpen { get => isOpen; set => UpdateProperty(ref isOpen, value); }
- private async void Close()
- {
- GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}关闭功放"));
- var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl,Shaker.Model.Result>(new Shaker.Model.SysControl()
- {
- Cmd = Shaker.Model.SysCmd.CloseGain,
- }, Properties);
- if(result == null || !result.Success)
- {
- PromptViewModel.Default.Init();
- PromptViewModel.Default.IconType = IconType.Error;
- PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
- PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
- PromptViewModel.Default.IsOpen = true;
- }
- }
- private async void SetGain(short gain)
- {
- GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}设置功放增益为{Gain}%"));
- var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SetGain,Shaker.Model.Result>(new Shaker.Model.SetGain()
- {
- Gain = gain,
- }, Properties);
- if (result == null || !result.Success)
- {
- PromptViewModel.Default.Init();
- PromptViewModel.Default.IconType = IconType.Error;
- PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
- PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
- PromptViewModel.Default.IsOpen = true;
- }
- }
- public bool GainSwitchEnabled => CurrentData?.OperatingStatus == OperatingStatus.Run;
- public bool GainEnabled=>CurrentData?.OperatingStatus == OperatingStatus.Run && CurrentData?.
- private async void Start()
- {
- GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}功放开始"));
- var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl,Shaker.Model.Result>(new Shaker.Model.SysControl()
- {
- Cmd = Shaker.Model.SysCmd.Start,
- }, Properties);
- if (result == null || !result.Success)
- {
- PromptViewModel.Default.Init();
- PromptViewModel.Default.IconType = IconType.Error;
- PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
- PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
- PromptViewModel.Default.IsOpen = true;
- }
- }
- private async void Stop()
- {
- GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}停止功放"));
- var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl,Shaker.Model.Result>(new Shaker.Model.SysControl()
- {
- Cmd = Shaker.Model.SysCmd.Stop,
- }, Properties);
- }
- private async void Reset()
- {
- GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}功放复位"));
- var result= await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl,Shaker.Model.Result>(new Shaker.Model.SysControl()
- {
- Cmd = Shaker.Model.SysCmd.Reset,
- }, Properties);
- if (result == null || !result.Success)
- {
- PromptViewModel.Default.Init();
- PromptViewModel.Default.IconType = IconType.Error;
- PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
- PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
- PromptViewModel.Default.IsOpen = true;
- }
- }
- private async void Open()
- {
- GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}打开功放"));
- var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl,Shaker.Model.Result>(new Shaker.Model.SysControl()
- {
- Cmd = Shaker.Model.SysCmd.OpenGain,
- }, Properties);
- if (result == null || !result.Success)
- {
- PromptViewModel.Default.Init();
- PromptViewModel.Default.IconType = IconType.Error;
- PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
- PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
- PromptViewModel.Default.IsOpen = true;
- }
- }
- private short gain;
- private bool shakerControlIsOpen = false;
- private bool setPlotConfigIsOpen = false;
- [AllowNull]
- private DataMangerItemViewModel currentData;
- private bool isConnected = false;
- private bool isRemoteControl = false;
- private bool isOnline = false;
- private bool gainSwich = false;
- public string Title => string.IsNullOrEmpty(Description) ? Name : Description;
- public string Name
- {
- get => Model.Name;
- set
- {
- UpdateProperty(ref Model.Name, value);
- OnPropertyChanged(nameof(Title));
- }
- }
- public string Description
- {
- get => Model.Description;
- set
- {
- if (value == Model.Description) return;
- GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}别名修改为{value}"));
- UpdateProperty(ref Model.Description, value);
- Sql.Default.UpDate<Shaker.Model.ShakerControlModel, string, string>(p => p.Id, Model.Id, p => p.Description, Model.Description);
- OnPropertyChanged(nameof(Title));
- }
- }
- public byte SlaveID { get=>Model.SlaveID; set=>UpdateProperty(ref Model.SlaveID, value); }
- public string IPAddress
- {
- get => Model.IPAddress;
- set
- {
- if (Model.Id == value) return;
- GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}功放IP地址修改为{value}"));
- UpdateProperty(ref Model.IPAddress, value);
- Sql.Default.UpDate<Shaker.Model.ShakerControlModel, string, string>(p => p.Id, Model.Id, p => p.IPAddress, Model.IPAddress);
- }
- }
- public int Port
- {
- get => Model.Port;
- set
- {
- if (value != Model.Port)
- {
- GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}功放端口号修改为{value}"));
- UpdateProperty(ref Model.Port, value);
- Sql.Default.UpDate<Shaker.Model.ShakerControlModel, string, int>(p => p.Id, Model.Id, p => p.Port, Model.Port);
- }
- }
- }
- public ICommand RequestPermissionsCommand => new DelegateCommand(RequestPermissions);
- private async void RequestPermissions()
- {
- if (IsRemoteControl) return;
- GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"申请振动台{Name}功放控制权限"));
- var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl, Shaker.Model.Result>(new Shaker.Model.SysControl()
- {
- Cmd = Shaker.Model.SysCmd.ApplicationControl,
- }, Properties,CancellationToken.None,MainWindowViewModel.Default.SystemConfig.SystemPage.RequestPermissionsTimeout);
- if (result == null || !result.Success)
- {
- PromptViewModel.Default.Init();
- PromptViewModel.Default.IconType = IconType.Error;
- PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
- PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
- PromptViewModel.Default.IsOpen = true;
- }
- }
- public short Gain
- {
- get => gain;
- set
- {
- if (gain == value) return;
- UpdateProperty(ref gain, value);
- SetGain(value);
- }
- }
- public bool IsRemoteControl { get => isRemoteControl; set =>UpdateProperty(ref isRemoteControl, value); }
- public ICommand SaveCommand => new DelegateCommand(() => Save());
- public ICommand CancelCommand => new DelegateCommand(() => Cancel());
- private void Save()
- {
- GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"修改了振动台{Name}配置参数"));
- Sql.Default.Replace(p => p.Id, Model.Id, Model);
- }
- private void Cancel()
- {
- Model = Sql.Default.FindFirst<Shaker.Model.ShakerControlModel>(p => p.Id == Model.Id)!;
- RefreshUI();
- }
- public bool IsMax { get; set; }
- public bool IsSelected { get; set; }
- }
- }
|