ShakerControlViewModel.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. using HandyControl.Interactivity;
  2. using HandyControl.Interactivity.Commands;
  3. using OxyPlot;
  4. using Shaker.Model;
  5. using Shaker.ViewModel;
  6. using ShakerManger.Data;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.ComponentModel.DataAnnotations;
  11. using System.Diagnostics.CodeAnalysis;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. namespace ShakerManger.ViewModel
  20. {
  21. internal class ShakerControlViewModel:Shaker.ViewModel.DisplayViewModel<Shaker.Model.ShakerControlModel>
  22. {
  23. private object locker = new object();
  24. public string ClientID { get => Model.ClientID; set => UpdateProperty(ref Model.ClientID, value); }
  25. public int MaxItemCount => 1000;
  26. private ObservableCollection<DataMangerItemViewModel> Items { get; } = new ObservableCollection<DataMangerItemViewModel>();
  27. public OxyPlot.PlotModel MinPlotModel { get; } = new PlotModel();
  28. private List<OxyPlot.Series.LineSeries> _MinLineSeries = new List<OxyPlot.Series.LineSeries>();
  29. private List<OxyPlot.Series.LineSeries> _LineSeries = new List<OxyPlot.Series.LineSeries>();
  30. public string[] PropertiyNames { get; }= new string[]
  31. {
  32. nameof(DataMangerItemViewModel.OperatingStatus),
  33. nameof(DataMangerItemViewModel.OutputCurrent),
  34. nameof(DataMangerItemViewModel.OutputVoltage),
  35. nameof(DataMangerItemViewModel.ExcitationCurrent),
  36. nameof(DataMangerItemViewModel.ExcitationVoltage),
  37. nameof(DataMangerItemViewModel.ThreePhaseVoltage),
  38. nameof(DataMangerItemViewModel.AbutmentTemperature0),
  39. nameof(DataMangerItemViewModel.AbutmentTemperature1),
  40. nameof(DataMangerItemViewModel.DCVoltage1),
  41. nameof(DataMangerItemViewModel.DCVoltage2),
  42. nameof(DataMangerItemViewModel.DCVoltage3),
  43. nameof(DataMangerItemViewModel.DCVoltage4),
  44. nameof(DataMangerItemViewModel.ExternalCirculatingWaterPressure),
  45. nameof(DataMangerItemViewModel.ExternalCirculatingWaterFlow),
  46. nameof(DataMangerItemViewModel.ExternalCirculatingWaterTemperature),
  47. nameof(DataMangerItemViewModel.MovingCoilWaterSupplyPressure),
  48. nameof(DataMangerItemViewModel.MovingCoilWaterSupplyFlow),
  49. nameof(DataMangerItemViewModel.MovingCoilWaterSupplyTemperature),
  50. nameof(DataMangerItemViewModel.ExcitationWaterSupplyPressure),
  51. nameof(DataMangerItemViewModel.ExcitationWaterSupplyFlow),
  52. nameof(DataMangerItemViewModel.ExcitationWaterSupplyTemperature),
  53. nameof(DataMangerItemViewModel.CoolingWaterStatus),
  54. nameof(DataMangerItemViewModel.Gain),
  55. };
  56. public ICommand MenuShowCommand => new DelegateCommand<ExCommandParameter>(MenuShow);
  57. private void MenuShow(ExCommandParameter parameter)
  58. {
  59. if(parameter.EventArgs is MouseButtonEventArgs args)
  60. {
  61. if (args.ChangedButton == MouseButton.Right)
  62. {
  63. MenuIsOpen = true;
  64. }
  65. }
  66. }
  67. public ICommand DisConnectCommand=> new DelegateCommand(DisConnect);
  68. private void DisConnect()
  69. {
  70. PromptViewModel.Default.Init();
  71. PromptViewModel.Default.IconType = IconType.Ask;
  72. PromptViewModel.Default.Message = "是否断开功放连接?";
  73. PromptViewModel.Default.YesAction = async ()=>
  74. {
  75. var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl, Shaker.Model.Result>(new SysControl()
  76. {
  77. Cmd = SysCmd.DisConnect,
  78. }, Properties);
  79. if (result == null ||!result.Success)
  80. {
  81. PromptViewModel.Default.Init();
  82. PromptViewModel.Default.IconType = IconType.Error;
  83. PromptViewModel.Default.Message = result == null? "通信超时" : result.Message;
  84. PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
  85. PromptViewModel.Default.IsOpen = true;
  86. }
  87. };
  88. PromptViewModel.Default.IsOpen = true;
  89. }
  90. public ICommand ConnectCommand => new DelegateCommand(Connect);
  91. private async void Connect()
  92. {
  93. var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl, Shaker.Model.Result>(new SysControl()
  94. {
  95. Cmd = SysCmd.Connect,
  96. },Properties);
  97. if(result == null || !result.Success)
  98. {
  99. PromptViewModel.Default.Init();
  100. PromptViewModel.Default.IconType = IconType.Error;
  101. PromptViewModel.Default.Message = result == null?"通信超时": result.Message;
  102. PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
  103. PromptViewModel.Default.IsOpen= true;
  104. }
  105. }
  106. public bool IsOnline { get => isOnline; set =>UpdateProperty(ref isOnline, value); }
  107. public DataMangerItemViewModel CurrentData { get => currentData; set =>UpdateProperty(ref currentData, value); }
  108. public Boolean SetPlotConfigIsOpen
  109. {
  110. get => setPlotConfigIsOpen;
  111. set
  112. {
  113. UpdateProperty(ref setPlotConfigIsOpen, value);
  114. if(value)
  115. {
  116. foreach(var item in PlotConfig)
  117. {
  118. item.Init();
  119. }
  120. }
  121. }
  122. }
  123. public bool MenuIsOpen { get => menuIsOpen; set =>UpdateProperty(ref menuIsOpen , value); }
  124. private Data.ShakerViewType lastviewType = ShakerViewType.ShakerControl;
  125. public Data.ShakerViewType ShakerView
  126. {
  127. get => shakerView;
  128. set
  129. {
  130. if (shakerView == value) return;
  131. MenuIsOpen = false;
  132. UpdateProperty(ref shakerView, value);
  133. }
  134. }
  135. public bool IsDeviceControl { get => isDeviceControl; set =>UpdateProperty(ref isDeviceControl , value); }
  136. public bool IsConnected
  137. {
  138. get => isConnected;
  139. set
  140. {
  141. if (isConnected == value) return;
  142. UpdateProperty(ref isConnected, value);
  143. if (value)
  144. {
  145. ShakerView = lastviewType;
  146. }
  147. else
  148. {
  149. lastviewType = ShakerView;
  150. ShakerView = ShakerViewType.None;
  151. }
  152. }
  153. }
  154. private bool isVisible = false;
  155. public bool IsVisible { get => isVisible; set => UpdateProperty(ref isVisible, value); }
  156. public string[] AnalogNames { get; } = new string[Shaker.Model.DataMangerItemModel.ANALOG_DATA_LENGTH];
  157. public string[] Units { get; } = new string[Shaker.Model.DataMangerItemModel.ANALOG_DATA_LENGTH];
  158. private Dictionary<string, string> Properties { get; init; } = new Dictionary<string, string>();
  159. public ObservableCollection<PlotConfigViewModel> PlotConfig { get; } = new ObservableCollection<PlotConfigViewModel>();
  160. public ShakerControlViewModel(Shaker.Model.ShakerControlModel model)
  161. {
  162. Model = model;
  163. Values.Add(new PLCAnlogViewModel(model.ExcitationVoltage));
  164. Values.Add(new PLCAnlogViewModel(model.ExcitationCurrent));
  165. Values.Add(new PLCAnlogViewModel(model.ThreePhaseVoltage));
  166. Values.Add(new PLCAnlogViewModel(model.AbutmentTemperature0));
  167. Values.Add(new PLCAnlogViewModel(model.AbutmentTemperature1));
  168. Values.Add(new PLCAnlogViewModel(model.DCVoltage1));
  169. Values.Add(new PLCAnlogViewModel(model.DCVoltage2));
  170. Values.Add(new PLCAnlogViewModel(model.DCVoltage3));
  171. Values.Add(new PLCAnlogViewModel(model.DCVoltage4));
  172. CoolingWaterValues.Add(new PLCAnlogViewModel(model.ExternalCirculatingWaterPressure));
  173. CoolingWaterValues.Add(new PLCAnlogViewModel(model.ExternalCirculatingWaterFlow));
  174. CoolingWaterValues.Add(new PLCAnlogViewModel(model.ExternalCirculatingWaterTemperature));
  175. CoolingWaterValues.Add(new PLCAnlogViewModel(model.MovingCoilWaterSupplyPressure));
  176. CoolingWaterValues.Add(new PLCAnlogViewModel(model.MovingCoilWaterSupplyFlow));
  177. CoolingWaterValues.Add(new PLCAnlogViewModel(model.MovingCoilWaterSupplyTemperature));
  178. CoolingWaterValues.Add(new PLCAnlogViewModel(model.ExcitationWaterSupplyPressure));
  179. CoolingWaterValues.Add(new PLCAnlogViewModel(model.ExcitationWaterSupplyFlow));
  180. CoolingWaterValues.Add(new PLCAnlogViewModel(model.ExcitationWaterSupplyTemperature));
  181. CurrentData = new DataMangerItemViewModel(new DataMangerItemModel());
  182. CurrentData.PropertyChanged += (sender, args) =>
  183. {
  184. if(args.PropertyName == nameof(DataMangerItemViewModel.OperatingStatus))
  185. {
  186. OnPropertyChanged(nameof(GainEnabled));
  187. OnPropertyChanged(nameof(GainSwitchEnabled));
  188. OnPropertyChanged(nameof(StartEnabled));
  189. OnPropertyChanged(nameof(StopEnabled));
  190. OnPropertyChanged(nameof(ResetEnabled));
  191. }
  192. };
  193. Properties[Shaker.Model.GlobalVariable.ShakerIDKey] = model.Id;
  194. AnalogNames[0] = Model.OperatingStatus.Name;
  195. AnalogNames[1] = Model.OutputCurrent.Name;
  196. AnalogNames[2] = Model.OutputVoltage.Name;
  197. AnalogNames[3] = Model.ExcitationCurrent.Name;
  198. AnalogNames[4] = Model.ExcitationVoltage.Name;
  199. AnalogNames[5] = Model.ThreePhaseVoltage.Name;
  200. Units[0] = Model.OperatingStatus.Unit;
  201. Units[1] = Model.OutputCurrent.Unit;
  202. Units[2] = Model.OutputVoltage.Unit;
  203. Units[3] = Model.ExcitationCurrent.Unit;
  204. Units[4] = Model.ExcitationVoltage.Unit;
  205. Units[5] = Model.ThreePhaseVoltage.Unit;
  206. if (!string.IsNullOrEmpty(model.AbutmentTemperature0.Name))
  207. {
  208. AnalogNames[6] = Model.AbutmentTemperature0.Name;
  209. Units[6] = Model.AbutmentTemperature0.Unit;
  210. }
  211. if (!string.IsNullOrEmpty(model.AbutmentTemperature1.Name))
  212. {
  213. AnalogNames[7] = Model.AbutmentTemperature1.Name;
  214. Units[7] = Model.AbutmentTemperature1.Unit;
  215. }
  216. if (!string.IsNullOrEmpty(model.DCVoltage1.Name))
  217. {
  218. AnalogNames[8] = Model.DCVoltage1.Name;
  219. Units[8] = Model.DCVoltage1.Unit;
  220. }
  221. if (!string.IsNullOrEmpty(model.DCVoltage2.Name))
  222. {
  223. AnalogNames[9] = Model.DCVoltage2.Name;
  224. Units[9] = Model.DCVoltage2.Unit;
  225. }
  226. if (!string.IsNullOrEmpty(model.DCVoltage3.Name))
  227. {
  228. AnalogNames[10] = Model.DCVoltage3.Name;
  229. Units[10] = Model.DCVoltage3.Unit;
  230. }
  231. if (!string.IsNullOrEmpty(model.DCVoltage4.Name))
  232. {
  233. AnalogNames[11] = Model.DCVoltage4.Name;
  234. Units[11] = Model.DCVoltage4.Unit;
  235. }
  236. AnalogNames[12] = Model.ExternalCirculatingWaterPressure.Name;
  237. Units[12] = Model.ExternalCirculatingWaterPressure.Unit;
  238. AnalogNames[13] = Model.ExternalCirculatingWaterFlow.Name;
  239. Units[13] = Model.ExternalCirculatingWaterFlow.Unit;
  240. AnalogNames[14] = Model.ExternalCirculatingWaterTemperature.Name;
  241. Units[14] = Model.ExternalCirculatingWaterTemperature.Unit;
  242. AnalogNames[15] = Model.MovingCoilWaterSupplyPressure.Name;
  243. Units[15] = Model.MovingCoilWaterSupplyPressure.Unit;
  244. AnalogNames[16] = Model.MovingCoilWaterSupplyFlow.Name;
  245. Units[16] = Model.MovingCoilWaterSupplyFlow.Unit;
  246. AnalogNames[17] = Model.MovingCoilWaterSupplyTemperature.Name;
  247. Units[17] = Model.MovingCoilWaterSupplyTemperature.Unit;
  248. AnalogNames[18] = Model.ExcitationWaterSupplyPressure.Name;
  249. Units[18] = Model.ExcitationWaterSupplyPressure.Unit;
  250. AnalogNames[19] = Model.ExcitationWaterSupplyFlow.Name;
  251. Units[19] = Model.ExcitationWaterSupplyFlow.Unit;
  252. AnalogNames[20] = Model.ExcitationWaterSupplyTemperature.Name;
  253. Units[20] = Model.ExcitationWaterSupplyTemperature.Unit;
  254. AnalogNames[21] = Model.CoolingWaterOperatingStatus.Name;
  255. Units[21] = Model.CoolingWaterOperatingStatus.Unit;
  256. PlotModel.TextColor = OxyColors.White;
  257. PlotModel.TitleColor = OxyColors.White;
  258. PlotModel.PlotAreaBorderColor = OxyColors.White;
  259. PlotModel.Axes.Add(new OxyPlot.Axes.DateTimeAxis()
  260. {
  261. Title = "时间",
  262. Position = OxyPlot.Axes.AxisPosition.Bottom,
  263. Key = "X",
  264. MaximumPadding = 0,
  265. MajorGridlineColor = OxyPlot.OxyColors.Gray,
  266. MajorGridlineStyle = LineStyle.Dot,
  267. MajorGridlineThickness = 1,
  268. MinimumPadding = 0,
  269. ExtraGridlineColor = OxyPlot.OxyColors.White,
  270. TextColor = OxyColors.White,
  271. TitleColor = OxyColors.White,
  272. AxislineColor = OxyColors.White,
  273. TicklineColor = OxyColors.White,
  274. });
  275. PlotModel.Axes.Add(new OxyPlot.Axes.LinearAxis()
  276. {
  277. Title = "值",
  278. Position = OxyPlot.Axes.AxisPosition.Left,
  279. Key = "Y",
  280. MajorGridlineStyle = LineStyle.Dot,
  281. MajorGridlineColor = OxyPlot.OxyColors.Gray,
  282. MajorGridlineThickness = 1,
  283. ExtraGridlineColor = OxyPlot.OxyColors.White,
  284. TextColor = OxyColors.White,
  285. TitleColor = OxyColors.White,
  286. AxislineColor = OxyColors.White,
  287. TicklineColor = OxyColors.White,
  288. });
  289. for (int i = 0; i < AnalogNames.Length-1; i++)
  290. {
  291. if (string.IsNullOrEmpty(AnalogNames[i]) || i == 0) continue;
  292. OxyPlot.Series.LineSeries lineSeries = new OxyPlot.Series.LineSeries();
  293. lineSeries.Title = AnalogNames[i];
  294. lineSeries.StrokeThickness = 1;
  295. lineSeries.XAxisKey = "X";
  296. lineSeries.YAxisKey = "Y";
  297. lineSeries.TrackerFormatString = "{1}:{2}\n{0}:{4}" + Units[i];
  298. lineSeries.CanTrackerInterpolatePoints = false;
  299. lineSeries.DataFieldX = nameof(DataMangerItemModel.Time);
  300. lineSeries.DataFieldY = PropertiyNames[i];
  301. lineSeries.IsVisible = PropertiyNames[i] == nameof(DataMangerItemModel.OutputCurrent) || PropertiyNames[i] == nameof(DataMangerItemModel.OutputVoltage);
  302. _LineSeries.Add(lineSeries);
  303. PlotModel.Series.Add(lineSeries);
  304. PlotConfig.Add(new PlotConfigViewModel(lineSeries));
  305. PlotConfig[^1].PropertyChanged += (_, _) =>
  306. {
  307. PlotModel.InvalidatePlot(false);
  308. PlotModel.InvalidatePlot(true);
  309. };
  310. }
  311. {
  312. OxyPlot.Series.LineSeries lineSeries = new OxyPlot.Series.LineSeries();
  313. lineSeries.Title = "增益";
  314. lineSeries.XAxisKey = "X";
  315. lineSeries.YAxisKey = "Y";
  316. lineSeries.IsVisible = false;
  317. lineSeries.StrokeThickness = 1;
  318. lineSeries.TrackerFormatString = "{1}:{2}\n{0}:{4}%";
  319. lineSeries.CanTrackerInterpolatePoints = false;
  320. lineSeries.DataFieldX = nameof(DataMangerItemModel.Time);
  321. lineSeries.DataFieldY = PropertiyNames[^1];
  322. _LineSeries.Add(lineSeries);
  323. PlotModel.Series.Add(lineSeries);
  324. PlotConfig.Add(new PlotConfigViewModel(lineSeries));
  325. PlotConfig[^1].PropertyChanged += (_, _) =>
  326. {
  327. PlotModel.InvalidatePlot(false);
  328. PlotModel.InvalidatePlot(true);
  329. };
  330. }
  331. PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
  332. {
  333. IsLegendVisible = true,
  334. ShowInvisibleSeries = false,
  335. });
  336. BusManger.Defaut.Bus.PubSub.Subscribe<Shaker.Model.ShakerConnectState>((data, _) =>
  337. {
  338. Tools.DispatherInovke.Inovke(() =>
  339. {
  340. IsConnected = data.IsConnected;
  341. });
  342. }, $"{Shaker.Model.GlobalVariable.ShakerIDKey}='{Model.Id}'");
  343. BusManger.Defaut.Bus.PubSub.Subscribe<Shaker.Model.ShakerStatusModel>((data, _) =>
  344. {
  345. Tools.DispatherInovke.Inovke(() =>
  346. {
  347. GainSwitch = data.GainSwich;
  348. });
  349. }, $"{Shaker.Model.GlobalVariable.ShakerIDKey}='{Model.Id}'");
  350. BusManger.Defaut.Bus.PubSub.Subscribe<Shaker.Model.ClientExitModel>((data, _) =>
  351. {
  352. Tools.DispatherInovke.Inovke(() =>
  353. {
  354. IsOnline = data.Online;
  355. });
  356. }, $"{Shaker.Model.GlobalVariable.ShakerIDKey}='{Model.Id}'");
  357. BusManger.Defaut.Bus.PubSub.Subscribe<DataMangerItemModel>((model, property) =>
  358. {
  359. if(MainWindowViewModel.Default.IsSaveData) Sql.Default.Insert(model, Model.Id);
  360. Tools.DispatherInovke.Inovke(() =>
  361. {
  362. if (CurrentData == null)
  363. {
  364. CurrentData = new DataMangerItemViewModel(model);
  365. }
  366. else
  367. {
  368. CurrentData.UpDateModel(model);
  369. }
  370. lock (locker)
  371. {
  372. gain = model.Gain;
  373. OnPropertyChanged(nameof(Gain));
  374. }
  375. Items.Add(new DataMangerItemViewModel(model));
  376. if (Items.Count > MaxItemCount) Items.RemoveAt(0);
  377. PlotModel.InvalidatePlot(false);
  378. _LineSeries.ForEach(x => x.ItemsSource = Items);
  379. PlotModel.InvalidatePlot(true);
  380. MinPlotModel.InvalidatePlot(false);
  381. _MinLineSeries.ForEach(x => x.ItemsSource = Items);
  382. MinPlotModel.InvalidatePlot(true);
  383. });
  384. }, $"{Shaker.Model.GlobalVariable.ShakerIDKey}='{Model.Id}'");
  385. BusManger.Defaut.Bus.PubSub.Subscribe<Shaker.Model.AllowRemoteControl>((model, property) =>
  386. {
  387. Tools.DispatherInovke.Inovke(() =>
  388. {
  389. IsRemoteControl = model == AllowRemoteControl.Remote;
  390. });
  391. }, $"{Shaker.Model.GlobalVariable.ShakerIDKey}='{Model.Id}'");
  392. MinPlotModel.Axes.Add(new OxyPlot.Axes.DateTimeAxis()
  393. {
  394. Title = "时间",
  395. Position = OxyPlot.Axes.AxisPosition.Bottom,
  396. Key = "X",
  397. MaximumPadding = 0,
  398. MajorGridlineColor = OxyPlot.OxyColors.Gray,
  399. MajorGridlineStyle = LineStyle.Dot,
  400. MajorGridlineThickness = 1,
  401. MinimumPadding = 0,
  402. ExtraGridlineColor = OxyPlot.OxyColors.White,
  403. TextColor = OxyColors.White,
  404. TitleColor = OxyColors.White,
  405. AxislineColor = OxyColors.White,
  406. TicklineColor = OxyColors.White,
  407. });
  408. MinPlotModel.Axes.Add(new OxyPlot.Axes.LinearAxis()
  409. {
  410. Title = "电流",
  411. Position = OxyPlot.Axes.AxisPosition.Left,
  412. Key = "Y",
  413. Unit = "A",
  414. MajorGridlineStyle = LineStyle.Dot,
  415. MajorGridlineColor = OxyPlot.OxyColors.Gray,
  416. MajorGridlineThickness = 1,
  417. ExtraGridlineColor = OxyPlot.OxyColors.White,
  418. TextColor = OxyColors.White,
  419. TitleColor = OxyColors.White,
  420. AxislineColor = OxyColors.White,
  421. TicklineColor = OxyColors.White,
  422. });
  423. {
  424. OxyPlot.Series.LineSeries lineSeries = new OxyPlot.Series.LineSeries();
  425. lineSeries.Title = AnalogNames[1];
  426. lineSeries.StrokeThickness = 1;
  427. lineSeries.XAxisKey = "X";
  428. lineSeries.YAxisKey = "Y";
  429. lineSeries.TrackerFormatString = "{1}:{2}\n{0}:{4}" + Units[1];
  430. lineSeries.CanTrackerInterpolatePoints = false;
  431. lineSeries.DataFieldX = nameof(DataMangerItemModel.Time);
  432. lineSeries.DataFieldY = PropertiyNames[1];
  433. _MinLineSeries.Add(lineSeries);
  434. MinPlotModel.Series.Add(lineSeries);
  435. }
  436. PlotController.BindMouseDown(OxyMouseButton.Right, new DelegatePlotCommand<OxyMouseDownEventArgs>((v, c, h) =>
  437. {
  438. h.Handled = true;
  439. MenuIsOpen = true;
  440. }));
  441. }
  442. public OxyPlot.IPlotController PlotController { get; } = new OxyPlot.PlotController();
  443. public View.ShakerView Content { get; } = new View.ShakerView();
  444. public bool GainSwitch
  445. {
  446. get => gainSwitch;
  447. set
  448. {
  449. if (value == gainSwitch) return;
  450. UpdateProperty(ref gainSwitch, value);
  451. OnPropertyChanged(nameof(GainEnabled));
  452. }
  453. }
  454. public bool AllowCoolingWater => Model.AllowCoolingWater;
  455. [AllowNull]
  456. public PowerAmplifierViewModel PowerAmplifier { get; init; }
  457. public ObservableCollection<PLCAnlogViewModel> CoolingWaterValues { get; } = new ObservableCollection<PLCAnlogViewModel>();
  458. public ObservableCollection<PLCAnlogViewModel> Values { get; } = new ObservableCollection<PLCAnlogViewModel>();
  459. public override Type View { get; } = typeof(View.ShakerView);
  460. public ICommand OpenCommand => new DelegateCommand(Open);
  461. public ICommand CloseCommand => new DelegateCommand(Close);
  462. public ICommand StartCommand => new DelegateCommand(Start);
  463. public ICommand StopCommand => new DelegateCommand(Stop);
  464. public ICommand ResetCommand => new DelegateCommand(Reset);
  465. public ICommand CoolingWaterStartCommand => new DelegateCommand(CoolingWaterStart);
  466. public ICommand CoolingWaterStopCommand => new DelegateCommand(CoolingWaterStop);
  467. public ICommand CoolingWaterResetCommand => new DelegateCommand(CoolingWaterReset);
  468. public async void CoolingWaterStartNoResult()
  469. {
  470. await BusManger.Defaut.Bus.PubSub.PublishAsync<SysControl>(new SysControl()
  471. {
  472. Cmd = SysCmd.CoolingWaterStart,
  473. }, Properties);
  474. }
  475. public async void CoolingWaterStopNoResult()
  476. {
  477. await BusManger.Defaut.Bus.PubSub.PublishAsync<SysControl>(new SysControl()
  478. {
  479. Cmd = SysCmd.CoolingWaterStop,
  480. }, Properties);
  481. }
  482. public async void CoolingWaterResetNoResult()
  483. {
  484. await BusManger.Defaut.Bus.PubSub.PublishAsync<SysControl>(new SysControl()
  485. {
  486. Cmd = SysCmd.CoolingWaterReset,
  487. }, Properties);
  488. }
  489. public async void StartNoResult()
  490. {
  491. await BusManger.Defaut.Bus.PubSub.PublishAsync<SysControl>(new SysControl()
  492. {
  493. Cmd = SysCmd.Start,
  494. }, Properties);
  495. }
  496. public async void StopNoResult()
  497. {
  498. await BusManger.Defaut.Bus.PubSub.PublishAsync<SysControl>(new SysControl()
  499. {
  500. Cmd = SysCmd.Stop,
  501. }, Properties);
  502. }
  503. public async void ResetNoResult()
  504. {
  505. await BusManger.Defaut.Bus.PubSub.PublishAsync<SysControl>(new SysControl()
  506. {
  507. Cmd = SysCmd.Reset,
  508. }, Properties);
  509. }
  510. public async void GainSwitchControlNoResult(bool switchon)
  511. {
  512. await BusManger.Defaut.Bus.PubSub.PublishAsync<Shaker.Model.SysControl>(new Shaker.Model.SysControl()
  513. {
  514. Cmd = switchon ? SysCmd.OpenGain: Shaker.Model.SysCmd.CloseGain,
  515. }, Properties);
  516. }
  517. private bool isOpen = false;
  518. public OxyPlot.PlotModel PlotModel { get; } = new OxyPlot.PlotModel();
  519. public bool IsOpen { get => isOpen; set => UpdateProperty(ref isOpen, value); }
  520. private async void Close()
  521. {
  522. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}关闭功放"));
  523. var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl,Shaker.Model.Result>(new Shaker.Model.SysControl()
  524. {
  525. Cmd = Shaker.Model.SysCmd.CloseGain,
  526. }, Properties);
  527. if(result == null || !result.Success)
  528. {
  529. PromptViewModel.Default.Init();
  530. PromptViewModel.Default.IconType = IconType.Error;
  531. PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
  532. PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
  533. PromptViewModel.Default.IsOpen = true;
  534. }
  535. }
  536. public void SetGainNoResult(short gain)
  537. {
  538. lock (locker)
  539. {
  540. BusManger.Defaut.Bus.PubSub.PublishAsync(new SetGain()
  541. {
  542. Gain = gain
  543. }, Properties).Wait();
  544. this.gain = gain;
  545. OnPropertyChanged(nameof(Gain));
  546. }
  547. }
  548. public ICommand GainSwitchCommand => new DelegateCommand<ExCommandParameter>(GainSwitchControl);
  549. public void GainSwitchControl(ExCommandParameter parameter)
  550. {
  551. if (parameter.Parameter is bool switchon)
  552. {
  553. if (switchon) Open();
  554. else Close();
  555. }
  556. }
  557. private void SetGain(short gain)
  558. {
  559. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}设置功放增益为{Gain}%"));
  560. var result = BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SetGain, Shaker.Model.Result>(new Shaker.Model.SetGain()
  561. {
  562. Gain = gain,
  563. }, Properties).Result;
  564. if (result == null || !result.Success)
  565. {
  566. PromptViewModel.Default.Init();
  567. PromptViewModel.Default.IconType = IconType.Error;
  568. PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
  569. PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
  570. PromptViewModel.Default.IsOpen = true;
  571. }
  572. }
  573. public bool GainSwitchEnabled => CurrentData?.OperatingStatus == OperatingStatus.Run;
  574. public bool GainEnabled => CurrentData != null && (CurrentData.OperatingStatus == OperatingStatus.Run && GainSwitch);
  575. public bool StartEnabled => CurrentData?.OperatingStatus == OperatingStatus.Wait;
  576. public bool StopEnabled => CurrentData?.OperatingStatus == OperatingStatus.Run;
  577. public bool ResetEnabled => CurrentData?.OperatingStatus != OperatingStatus.Run;
  578. public bool CoolingWaterStartEnabled => CurrentData?.CoolingWaterStatus == CoolingWaterOperatingStatus.Wait && AllowCoolingWater;
  579. public bool CoolingWaterStopEnabled => CurrentData?.CoolingWaterStatus == CoolingWaterOperatingStatus.Run && AllowCoolingWater;
  580. public bool CoolingWaterResetEnabled => CurrentData?.CoolingWaterStatus != CoolingWaterOperatingStatus.Run && AllowCoolingWater;
  581. private async void CoolingWaterStart()
  582. {
  583. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}冷却开始"));
  584. var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl, Shaker.Model.Result>(new Shaker.Model.SysControl()
  585. {
  586. Cmd = Shaker.Model.SysCmd.CoolingWaterStart,
  587. }, Properties);
  588. if (result == null || !result.Success)
  589. {
  590. PromptViewModel.Default.Init();
  591. PromptViewModel.Default.IconType = IconType.Error;
  592. PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
  593. PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
  594. PromptViewModel.Default.IsOpen = true;
  595. }
  596. }
  597. private async void CoolingWaterStop()
  598. {
  599. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}停止冷却"));
  600. var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl, Shaker.Model.Result>(new Shaker.Model.SysControl()
  601. {
  602. Cmd = Shaker.Model.SysCmd.CoolingWaterStop,
  603. }, Properties);
  604. }
  605. private async void CoolingWaterReset()
  606. {
  607. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}冷却复位"));
  608. var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl, Shaker.Model.Result>(new Shaker.Model.SysControl()
  609. {
  610. Cmd = Shaker.Model.SysCmd.CoolingWaterReset,
  611. }, Properties);
  612. if (result == null || !result.Success)
  613. {
  614. PromptViewModel.Default.Init();
  615. PromptViewModel.Default.IconType = IconType.Error;
  616. PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
  617. PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
  618. PromptViewModel.Default.IsOpen = true;
  619. }
  620. }
  621. private async void Start()
  622. {
  623. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}功放开始"));
  624. var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl,Shaker.Model.Result>(new Shaker.Model.SysControl()
  625. {
  626. Cmd = Shaker.Model.SysCmd.Start,
  627. }, Properties);
  628. if (result == null || !result.Success)
  629. {
  630. PromptViewModel.Default.Init();
  631. PromptViewModel.Default.IconType = IconType.Error;
  632. PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
  633. PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
  634. PromptViewModel.Default.IsOpen = true;
  635. }
  636. }
  637. private async void Stop()
  638. {
  639. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}停止功放"));
  640. var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl,Shaker.Model.Result>(new Shaker.Model.SysControl()
  641. {
  642. Cmd = Shaker.Model.SysCmd.Stop,
  643. }, Properties);
  644. }
  645. private async void Reset()
  646. {
  647. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}功放复位"));
  648. var result= await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl,Shaker.Model.Result>(new Shaker.Model.SysControl()
  649. {
  650. Cmd = Shaker.Model.SysCmd.Reset,
  651. }, Properties);
  652. if (result == null || !result.Success)
  653. {
  654. PromptViewModel.Default.Init();
  655. PromptViewModel.Default.IconType = IconType.Error;
  656. PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
  657. PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
  658. PromptViewModel.Default.IsOpen = true;
  659. }
  660. }
  661. private async void Open()
  662. {
  663. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}打开功放"));
  664. var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl,Shaker.Model.Result>(new Shaker.Model.SysControl()
  665. {
  666. Cmd = Shaker.Model.SysCmd.OpenGain,
  667. }, Properties);
  668. if (result == null || !result.Success)
  669. {
  670. PromptViewModel.Default.Init();
  671. PromptViewModel.Default.IconType = IconType.Error;
  672. PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
  673. PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
  674. PromptViewModel.Default.IsOpen = true;
  675. }
  676. }
  677. private short gain;
  678. private bool setPlotConfigIsOpen = false;
  679. [AllowNull]
  680. private DataMangerItemViewModel currentData;
  681. private bool isConnected = false;
  682. private bool isRemoteControl = false;
  683. private bool isOnline = false;
  684. private bool gainSwitch = false;
  685. private bool isDeviceControl = false;
  686. private ShakerViewType shakerView = Data.ShakerViewType.None;
  687. private bool menuIsOpen = false;
  688. public string Title => string.IsNullOrEmpty(Description) ? Name : Description;
  689. public string Name
  690. {
  691. get => Model.Name;
  692. set
  693. {
  694. UpdateProperty(ref Model.Name, value);
  695. OnPropertyChanged(nameof(Title));
  696. }
  697. }
  698. public string Description
  699. {
  700. get => Model.Description;
  701. set
  702. {
  703. if (value == Model.Description) return;
  704. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}别名修改为{value}"));
  705. UpdateProperty(ref Model.Description, value);
  706. Sql.Default.UpDate<Shaker.Model.ShakerControlModel, string, string>(p => p.Id, Model.Id, p => p.Description, Model.Description);
  707. OnPropertyChanged(nameof(Title));
  708. }
  709. }
  710. public byte SlaveID { get=>Model.SlaveID; set=>UpdateProperty(ref Model.SlaveID, value); }
  711. public string IPAddress
  712. {
  713. get => Model.IPAddress;
  714. set
  715. {
  716. if (Model.Id == value) return;
  717. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}功放IP地址修改为{value}"));
  718. UpdateProperty(ref Model.IPAddress, value);
  719. Sql.Default.UpDate<Shaker.Model.ShakerControlModel, string, string>(p => p.Id, Model.Id, p => p.IPAddress, Model.IPAddress);
  720. }
  721. }
  722. public int Port
  723. {
  724. get => Model.Port;
  725. set
  726. {
  727. if (value != Model.Port)
  728. {
  729. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}功放端口号修改为{value}"));
  730. UpdateProperty(ref Model.Port, value);
  731. Sql.Default.UpDate<Shaker.Model.ShakerControlModel, string, int>(p => p.Id, Model.Id, p => p.Port, Model.Port);
  732. }
  733. }
  734. }
  735. public ICommand RequestPermissionsCommand => new DelegateCommand(RequestPermissions);
  736. private async void RequestPermissions()
  737. {
  738. if (IsRemoteControl) return;
  739. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"申请振动台{Name}功放控制权限"));
  740. var result = await BusManger.Defaut.Bus.RPC.RequestAsync<Shaker.Model.SysControl, Shaker.Model.Result>(new Shaker.Model.SysControl()
  741. {
  742. Cmd = Shaker.Model.SysCmd.ApplicationControl,
  743. }, Properties,CancellationToken.None,MainWindowViewModel.Default.SystemConfig.SystemPage.RequestPermissionsTimeout);
  744. if (result == null || !result.Success)
  745. {
  746. PromptViewModel.Default.Init();
  747. PromptViewModel.Default.IconType = IconType.Error;
  748. PromptViewModel.Default.Message = result == null ? "通信超时" : result.Message;
  749. PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
  750. PromptViewModel.Default.IsOpen = true;
  751. }
  752. }
  753. public short Gain
  754. {
  755. get => gain;
  756. set
  757. {
  758. if (gain == value) return;
  759. lock (locker)
  760. {
  761. UpdateProperty(ref gain, value);
  762. }
  763. SetGain(value);
  764. }
  765. }
  766. public bool IsRemoteControl { get => isRemoteControl; set =>UpdateProperty(ref isRemoteControl, value); }
  767. public ICommand SaveCommand => new DelegateCommand(() => Save());
  768. public ICommand CancelCommand => new DelegateCommand(() => Cancel());
  769. private void Save()
  770. {
  771. GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"修改了振动台{Name}配置参数"));
  772. Sql.Default.Replace(p => p.Id, Model.Id, Model);
  773. }
  774. private void Cancel()
  775. {
  776. Model = Sql.Default.FindFirst<Shaker.Model.ShakerControlModel>(p => p.Id == Model.Id)!;
  777. RefreshUI();
  778. }
  779. public bool IsMax { get; set; }
  780. public bool IsSelected { get; set; }
  781. }
  782. }