SettingViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using SHJX.Service.Common.ExtendElement;
  8. using SHJX.Service.Common.ReadXML;
  9. using SHJX.Service.Common.UserDelegate;
  10. using SHJX.Service.Control.ServiceController;
  11. using SHJX.Service.Model.Control;
  12. using SHJX.Service.Model.Dao;
  13. using SHJX.Service.Model.MvvmCommon;
  14. namespace SHJX.Service.ModelView
  15. {
  16. public class SettingViewModel : BindableBase
  17. {
  18. private readonly ReadConfigUtil _config;
  19. private readonly SettingController _controller;
  20. public SettingViewModel(ReadConfigUtil config)
  21. {
  22. _config = config;
  23. _controller = new SettingController(_config);
  24. InitData();
  25. }
  26. #region DataBind
  27. /// <summary>
  28. /// 加热阶段信息
  29. /// </summary>
  30. public ObservableCollection<HeatingSetting> HeatData { get; set; }
  31. /// <summary>
  32. /// 冷却
  33. /// </summary>
  34. public ObservableCollection<CoolingSetting> CoolingData { get; set; }
  35. /// <summary>
  36. /// PID
  37. /// </summary>
  38. public ObservableCollection<PID> PIDs { get; set; }
  39. /// <summary>
  40. /// 速度
  41. /// </summary>
  42. public ObservableCollection<MotorSpeed> SpeedInfos { get; set; }
  43. /// <summary>
  44. /// 液体
  45. /// </summary>
  46. public ObservableCollection<DropLiquid> LiquidWashes { get; set; }
  47. public ObservableCollection<LiquidTotal> LiquidTotals { get; set; }
  48. public ObservableCollection<DropAmount> LiquidAmount { get; set; }
  49. /// <summary>
  50. /// 滴定值
  51. /// </summary>
  52. public ObservableCollection<TitrationValue> TitrationValue { get; set; }
  53. public ObservableCollection<string> Wavekeys { get; set; }
  54. public void InitData()
  55. {
  56. PortName = _config.Port;
  57. AutoFindPort = _config.IsAutoGetPort;
  58. AutoWriteSpeed = _config.SpeedWrite;
  59. isStartHeat = _config.OpenAppStartHeat;
  60. TemplateFilePath = _config.TemplateFilePath;
  61. PreheatTemperature = _config.PreheatTemperature;
  62. AddWaterTemperature = _config.AddWaterTemperature;
  63. SampleResultFilePath = _config.ResultWordFilePath;
  64. CoolingPipeWatingTime = _config.CoolingPipeWatingTime;
  65. DissolveMoveDistance = _config.DissolveMoveDistance;
  66. ManiGrabFeedback = _config.ManiGrabFeedBack;
  67. DripNozzleFeedBack = _config.DripNozzleFeedBack;
  68. IsAddCupFeedBack = _config.IsAddCupFeedBack;
  69. ExportTypeByExcel=_config.ExportTypeByExcel;
  70. ExportTypeByWord = _config.ExportTypeByWord;
  71. IsAddCupBiaoDingBubei = _config.IsAddCupBiaoDingBubei;
  72. SpeedInfos = new ObservableCollection<MotorSpeed>(_config.GetSpeed);
  73. PIDs = new ObservableCollection<PID>(_controller.GetData<PID>());
  74. LiquidAmount = new ObservableCollection<DropAmount>(_config.DropOnceAmounts);
  75. LiquidWashes = new ObservableCollection<DropLiquid>(_controller.GetData<DropLiquid>());
  76. LiquidTotals = new ObservableCollection<LiquidTotal>(_controller.GetData<LiquidTotal>());
  77. HeatData = new ObservableCollection<HeatingSetting>(_controller.GetData<HeatingSetting>());
  78. CoolingData = new ObservableCollection<CoolingSetting>(_controller.GetData<CoolingSetting>());
  79. TitrationValue = new ObservableCollection<TitrationValue>(_controller.GetData<TitrationValue>());
  80. Wavekeys = new ObservableCollection<string>((from wave in _controller.GetWavekeys() select wave.WaveKey).ToList());
  81. WavekeysCurrentShow = Wavekeys.FirstOrDefault();
  82. lowCalibrationValue = _controller.GetBlankCalibrationValue(Wavekeys.FirstOrDefault(),"Low","标定");
  83. lowBlankValue = _controller.GetBlankCalibrationValue(Wavekeys.FirstOrDefault(), "Low","空白");
  84. highCalibrationValue = _controller.GetBlankCalibrationValue(Wavekeys.FirstOrDefault(), "High","标定");
  85. highBlankValue = _controller.GetBlankCalibrationValue(Wavekeys.FirstOrDefault(), "High", "空白");
  86. }
  87. #region titration
  88. public TitrationValue Titration
  89. {
  90. get => TitrationValue.FirstOrDefault();
  91. set
  92. {
  93. TitrationValue.ToArray()[0] = value;
  94. RaisePropertyChanged(nameof(Titration));
  95. }
  96. }
  97. #endregion
  98. #region
  99. private string _portName;
  100. public string PortName
  101. {
  102. get => _portName;
  103. set => SetProperty(ref _portName, value);
  104. }
  105. private bool _autoFindPort;
  106. public bool AutoFindPort
  107. {
  108. get => _autoFindPort;
  109. set => SetProperty(ref _autoFindPort, value);
  110. }
  111. private string wavekeysCurrentShow;
  112. public string WavekeysCurrentShow
  113. {
  114. get => wavekeysCurrentShow;
  115. set => SetProperty(ref wavekeysCurrentShow, value);
  116. }
  117. private double lowCalibrationValue;
  118. public double LowCalibrationValue
  119. {
  120. get => lowCalibrationValue;
  121. set => SetProperty(ref lowCalibrationValue, value);
  122. }
  123. private double lowBlankValue;
  124. public double LowBlankValue
  125. {
  126. get => lowBlankValue;
  127. set => SetProperty(ref lowBlankValue, value);
  128. }
  129. private double highCalibrationValue;
  130. public double HighCalibrationValue
  131. {
  132. get => highCalibrationValue;
  133. set => SetProperty(ref highCalibrationValue, value);
  134. }
  135. private double highBlankValue;
  136. public double HighBlankValue
  137. {
  138. get => highBlankValue;
  139. set => SetProperty(ref highBlankValue, value);
  140. }
  141. #endregion
  142. private bool _maniGrabFeedback;
  143. public bool ManiGrabFeedback
  144. {
  145. get => _maniGrabFeedback;
  146. set => SetProperty(ref _maniGrabFeedback, value);
  147. }
  148. private bool _dripNozzleFeedBack;
  149. public bool DripNozzleFeedBack
  150. {
  151. get => _dripNozzleFeedBack;
  152. set => SetProperty(ref _dripNozzleFeedBack, value);
  153. }
  154. private bool _IsAddCupFeedBack;
  155. public bool IsAddCupFeedBack
  156. {
  157. get => _IsAddCupFeedBack;
  158. set => SetProperty(ref _IsAddCupFeedBack, value);
  159. }
  160. private bool _IsAddCupBiaoDingBubei;
  161. public bool IsAddCupBiaoDingBubei
  162. {
  163. get => _IsAddCupBiaoDingBubei;
  164. set => SetProperty(ref _IsAddCupBiaoDingBubei, value);
  165. }
  166. private bool _IsExportTypeByExcel;
  167. public bool ExportTypeByExcel
  168. {
  169. get => _IsExportTypeByExcel;
  170. set => SetProperty(ref _IsExportTypeByExcel, value);
  171. }
  172. private bool _IsExportTypeByWord;
  173. public bool ExportTypeByWord
  174. {
  175. get => _IsExportTypeByWord;
  176. set => SetProperty(ref _IsExportTypeByWord, value);
  177. }
  178. private bool _autoWriteSpeed;
  179. public bool AutoWriteSpeed
  180. {
  181. get => _autoWriteSpeed;
  182. set => SetProperty(ref _autoWriteSpeed, value);
  183. }
  184. public string templateFilePath;
  185. public string TemplateFilePath
  186. {
  187. get => templateFilePath;
  188. set => SetProperty(ref templateFilePath, value);
  189. }
  190. public string sampleResultFilePath;
  191. public string SampleResultFilePath
  192. {
  193. get => sampleResultFilePath;
  194. set => SetProperty(ref sampleResultFilePath, value);
  195. }
  196. /// <summary>
  197. /// 是否开机自动加热
  198. /// </summary>
  199. private bool isStartHeat;
  200. public bool IsStartHeat
  201. {
  202. get => isStartHeat;
  203. set => SetProperty(ref isStartHeat, value);
  204. }
  205. private double _preheatTemperature;
  206. public double PreheatTemperature
  207. {
  208. get => _preheatTemperature;
  209. set => SetProperty(ref _preheatTemperature, value);
  210. }
  211. private double _addWaterTemperature;
  212. public double AddWaterTemperature
  213. {
  214. get => _addWaterTemperature;
  215. set => SetProperty(ref _addWaterTemperature, value);
  216. }
  217. private double _coolingPipeWatingTime;
  218. public double CoolingPipeWatingTime
  219. {
  220. get => _coolingPipeWatingTime;
  221. set => SetProperty(ref _coolingPipeWatingTime, value);
  222. }
  223. private double _dissolveMoveDistance;
  224. public double DissolveMoveDistance
  225. {
  226. get => _dissolveMoveDistance;
  227. set => SetProperty(ref _dissolveMoveDistance, value);
  228. }
  229. #endregion
  230. #region Command
  231. /// <summary>
  232. /// 更新配置
  233. /// </summary>
  234. public DelegateCommand<object> _updateSettingInfoCommand;
  235. public DelegateCommand<object> UpdateSettingInfoCommand => _updateSettingInfoCommand ?? new DelegateCommand<object>(UpdateSettingInfo);
  236. public DelegateCommand<object> _optTemplatePathCommand;
  237. public DelegateCommand<object> OptTemplatePathCommand => _optTemplatePathCommand ?? new DelegateCommand<object>(OptTemplatePath);
  238. public DelegateCommand<object> _optSampleResultPathCommand;
  239. public DelegateCommand<object> OptSampleResultPathCommand => _optSampleResultPathCommand ?? new DelegateCommand<object>(OptSampleResultPath);
  240. public DelegateCommand<object> _settingSpeedCommand;
  241. public DelegateCommand<object> SettingSpeedCommand => _settingSpeedCommand ?? new DelegateCommand<object>(SettingSpeed);
  242. public DelegateCommand<object> _lowValueUpdateCommand;
  243. public DelegateCommand<object> LowValueUpdateCommand => _lowValueUpdateCommand ?? new DelegateCommand<object>(LowValueUpdate);
  244. public DelegateCommand<object> _highValueUpdateCommand;
  245. public DelegateCommand<object> HighValueUpdateCommand => _highValueUpdateCommand ?? new DelegateCommand<object>(HighValueUpdate);
  246. /// <summary>
  247. /// 低浓度阈值更新
  248. /// </summary>
  249. /// <param name="obj"></param>
  250. private void LowValueUpdate(object obj)
  251. {
  252. Dictionary<string, object> values = new()
  253. {
  254. { "valuetype", "Low" },
  255. { "bd", lowCalibrationValue },
  256. { "kb", lowBlankValue },
  257. { "waveKey", wavekeysCurrentShow }
  258. };
  259. Messager.Send("UpdateResultValue", values);
  260. }
  261. /// <summary>
  262. /// 高浓度阈值更新
  263. /// </summary>
  264. /// <param name="obj"></param>
  265. private void HighValueUpdate(object obj)
  266. {
  267. Dictionary<string, object> values = new()
  268. {
  269. { "valuetype", "High" },
  270. { "bd", highCalibrationValue },
  271. { "kb", highBlankValue },
  272. { "waveKey", wavekeysCurrentShow }
  273. };
  274. Messager.Send("UpdateResultValue", values);
  275. }
  276. /// <summary>
  277. /// 设置速度
  278. /// </summary>
  279. /// <param name="obj"></param>
  280. private void SettingSpeed(object obj)
  281. {
  282. var speed = SpeedInfos.FirstOrDefault(item => item.NodeId.Equals(obj.ToString()));
  283. var res = _controller.SetSpeed(speed);
  284. UMessageBox.InfoTip($"{speed.Description}速度设置{(res ? "成功" : "失败")}");
  285. }
  286. private void OptSampleResultPath(object obj)
  287. {
  288. var dialog = new FolderBrowserDialog
  289. {
  290. SelectedPath = string.IsNullOrWhiteSpace(_config.ResultWordFilePath) ? System.AppDomain.CurrentDomain.BaseDirectory : _config.ResultWordFilePath
  291. };
  292. if (dialog.ShowDialog().Equals(DialogResult.OK))
  293. {
  294. var folderName = dialog.SelectedPath;
  295. SampleResultFilePath = folderName ;
  296. }
  297. }
  298. /// <summary>
  299. /// 更新配置信息
  300. /// </summary>
  301. /// <param name="obj"></param>
  302. private void UpdateSettingInfo(object obj)
  303. {
  304. var pidRes = _controller.UpdateValues(PIDs.ToList());
  305. var heatRes = _controller.UpdateValues(HeatData.ToList());
  306. var washRes = _controller.UpdateValues(LiquidWashes.ToList());
  307. var liquidTotal = _controller.UpdateValues(LiquidTotals.ToList());
  308. var coolingRes = _controller.UpdateValues(CoolingData.ToList());
  309. var titrationRes = _controller.UpdateValues(TitrationValue.ToList());
  310. _config.UpdateAmount(LiquidAmount.ToList());
  311. _config.UpdateTemplateFilePath(TemplateFilePath);
  312. _config.UpdateAppStartHeat(IsStartHeat.ToString());
  313. _config.UpdatePreheatTemperature(PreheatTemperature);
  314. _config.UpdateResultWordFilePath(SampleResultFilePath);
  315. _config.UpdateExportTypeByExcel(ExportTypeByExcel);
  316. _config.UpdateExportTypeByWord(ExportTypeByWord);
  317. _config.UpdateAddWaterTemperature(AddWaterTemperature);
  318. _config.UpdateCoolingPipeWatingTime(CoolingPipeWatingTime);
  319. _config.UpdateDissolveMoveDistance(DissolveMoveDistance);
  320. _config.UpdateManiGrabFeedBack(ManiGrabFeedback);
  321. _config.UpdateDripNozzleFeedBack(DripNozzleFeedBack);
  322. _config.UpdateIsAddCupFeedBack(IsAddCupFeedBack);
  323. _config.UpdateIsAddCupBiaoDingBubei(IsAddCupBiaoDingBubei);
  324. _config.UpdatePort(PortName);
  325. _config.UpdateAutoFindPort(AutoFindPort);
  326. foreach (var item in SpeedInfos)
  327. {
  328. _config.UpdateSpeed(item.NodeId, item.Speed);
  329. _config.UpdateAcSpeed(item.NodeId, item.AcSpeed);
  330. _config.UpdateDeSpeed(item.NodeId, item.DeSpeed);
  331. }
  332. _config.UpdateSpeedWrite(AutoWriteSpeed);
  333. if (heatRes && coolingRes && pidRes && washRes && liquidTotal && titrationRes)
  334. {
  335. UMessageBox.SuccessTip("配置更新成功!");
  336. }
  337. }
  338. public void OptTemplatePath(object obj)
  339. {
  340. var dialog = new FolderBrowserDialog
  341. {
  342. SelectedPath = string.IsNullOrWhiteSpace(_config.TemplateFilePath) ? System.AppDomain.CurrentDomain.BaseDirectory : _config.TemplateFilePath
  343. };
  344. if (dialog.ShowDialog().Equals(DialogResult.OK))
  345. {
  346. var folderName = dialog.SelectedPath;
  347. TemplateFilePath = folderName + "\\WaterTemplate.xlsx";
  348. }
  349. }
  350. #endregion
  351. }
  352. }