using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows.Forms; using Prism.Commands; using Prism.Mvvm; using SHJX.Service.Common.ExtendElement; using SHJX.Service.Common.ReadXML; using SHJX.Service.Common.UserDelegate; using SHJX.Service.Control.ServiceController; using SHJX.Service.Model.Control; using SHJX.Service.Model.Dao; using SHJX.Service.Model.MvvmCommon; namespace SHJX.Service.ModelView { public class SettingViewModel : BindableBase { private readonly ReadConfigUtil _config; private readonly SettingController _controller; public SettingViewModel(ReadConfigUtil config) { _config = config; _controller = new SettingController(_config); InitData(); } #region DataBind /// /// 加热阶段信息 /// public ObservableCollection HeatData { get; set; } /// /// 冷却 /// public ObservableCollection CoolingData { get; set; } /// /// PID /// public ObservableCollection PIDs { get; set; } /// /// 速度 /// public ObservableCollection SpeedInfos { get; set; } /// /// 液体 /// public ObservableCollection LiquidWashes { get; set; } public ObservableCollection LiquidTotals { get; set; } public ObservableCollection LiquidAmount { get; set; } /// /// 滴定值 /// public ObservableCollection TitrationValue { get; set; } public ObservableCollection Wavekeys { get; set; } public void InitData() { PortName = _config.Port; AutoFindPort = _config.IsAutoGetPort; AutoWriteSpeed = _config.SpeedWrite; isStartHeat = _config.OpenAppStartHeat; TemplateFilePath = _config.TemplateFilePath; PreheatTemperature = _config.PreheatTemperature; AddWaterTemperature = _config.AddWaterTemperature; SampleResultFilePath = _config.ResultWordFilePath; CoolingPipeWatingTime = _config.CoolingPipeWatingTime; DissolveMoveDistance = _config.DissolveMoveDistance; ManiGrabFeedback = _config.ManiGrabFeedBack; DripNozzleFeedBack = _config.DripNozzleFeedBack; IsAddCupFeedBack = _config.IsAddCupFeedBack; ExportTypeByExcel=_config.ExportTypeByExcel; ExportTypeByWord = _config.ExportTypeByWord; IsAddCupBiaoDingBubei = _config.IsAddCupBiaoDingBubei; SpeedInfos = new ObservableCollection(_config.GetSpeed); PIDs = new ObservableCollection(_controller.GetData()); LiquidAmount = new ObservableCollection(_config.DropOnceAmounts); LiquidWashes = new ObservableCollection(_controller.GetData()); LiquidTotals = new ObservableCollection(_controller.GetData()); HeatData = new ObservableCollection(_controller.GetData()); CoolingData = new ObservableCollection(_controller.GetData()); TitrationValue = new ObservableCollection(_controller.GetData()); Wavekeys = new ObservableCollection((from wave in _controller.GetWavekeys() select wave.WaveKey).ToList()); WavekeysCurrentShow = Wavekeys.FirstOrDefault(); lowCalibrationValue = _controller.GetBlankCalibrationValue(Wavekeys.FirstOrDefault(),"Low","标定"); lowBlankValue = _controller.GetBlankCalibrationValue(Wavekeys.FirstOrDefault(), "Low","空白"); highCalibrationValue = _controller.GetBlankCalibrationValue(Wavekeys.FirstOrDefault(), "High","标定"); highBlankValue = _controller.GetBlankCalibrationValue(Wavekeys.FirstOrDefault(), "High", "空白"); } #region titration public TitrationValue Titration { get => TitrationValue.FirstOrDefault(); set { TitrationValue.ToArray()[0] = value; RaisePropertyChanged(nameof(Titration)); } } #endregion #region private string _portName; public string PortName { get => _portName; set => SetProperty(ref _portName, value); } private bool _autoFindPort; public bool AutoFindPort { get => _autoFindPort; set => SetProperty(ref _autoFindPort, value); } private string wavekeysCurrentShow; public string WavekeysCurrentShow { get => wavekeysCurrentShow; set => SetProperty(ref wavekeysCurrentShow, value); } private double lowCalibrationValue; public double LowCalibrationValue { get => lowCalibrationValue; set => SetProperty(ref lowCalibrationValue, value); } private double lowBlankValue; public double LowBlankValue { get => lowBlankValue; set => SetProperty(ref lowBlankValue, value); } private double highCalibrationValue; public double HighCalibrationValue { get => highCalibrationValue; set => SetProperty(ref highCalibrationValue, value); } private double highBlankValue; public double HighBlankValue { get => highBlankValue; set => SetProperty(ref highBlankValue, value); } #endregion private bool _maniGrabFeedback; public bool ManiGrabFeedback { get => _maniGrabFeedback; set => SetProperty(ref _maniGrabFeedback, value); } private bool _dripNozzleFeedBack; public bool DripNozzleFeedBack { get => _dripNozzleFeedBack; set => SetProperty(ref _dripNozzleFeedBack, value); } private bool _IsAddCupFeedBack; public bool IsAddCupFeedBack { get => _IsAddCupFeedBack; set => SetProperty(ref _IsAddCupFeedBack, value); } private bool _IsAddCupBiaoDingBubei; public bool IsAddCupBiaoDingBubei { get => _IsAddCupBiaoDingBubei; set => SetProperty(ref _IsAddCupBiaoDingBubei, value); } private bool _IsExportTypeByExcel; public bool ExportTypeByExcel { get => _IsExportTypeByExcel; set => SetProperty(ref _IsExportTypeByExcel, value); } private bool _IsExportTypeByWord; public bool ExportTypeByWord { get => _IsExportTypeByWord; set => SetProperty(ref _IsExportTypeByWord, value); } private bool _autoWriteSpeed; public bool AutoWriteSpeed { get => _autoWriteSpeed; set => SetProperty(ref _autoWriteSpeed, value); } public string templateFilePath; public string TemplateFilePath { get => templateFilePath; set => SetProperty(ref templateFilePath, value); } public string sampleResultFilePath; public string SampleResultFilePath { get => sampleResultFilePath; set => SetProperty(ref sampleResultFilePath, value); } /// /// 是否开机自动加热 /// private bool isStartHeat; public bool IsStartHeat { get => isStartHeat; set => SetProperty(ref isStartHeat, value); } private double _preheatTemperature; public double PreheatTemperature { get => _preheatTemperature; set => SetProperty(ref _preheatTemperature, value); } private double _addWaterTemperature; public double AddWaterTemperature { get => _addWaterTemperature; set => SetProperty(ref _addWaterTemperature, value); } private double _coolingPipeWatingTime; public double CoolingPipeWatingTime { get => _coolingPipeWatingTime; set => SetProperty(ref _coolingPipeWatingTime, value); } private double _dissolveMoveDistance; public double DissolveMoveDistance { get => _dissolveMoveDistance; set => SetProperty(ref _dissolveMoveDistance, value); } #endregion #region Command /// /// 更新配置 /// public DelegateCommand _updateSettingInfoCommand; public DelegateCommand UpdateSettingInfoCommand => _updateSettingInfoCommand ?? new DelegateCommand(UpdateSettingInfo); public DelegateCommand _optTemplatePathCommand; public DelegateCommand OptTemplatePathCommand => _optTemplatePathCommand ?? new DelegateCommand(OptTemplatePath); public DelegateCommand _optSampleResultPathCommand; public DelegateCommand OptSampleResultPathCommand => _optSampleResultPathCommand ?? new DelegateCommand(OptSampleResultPath); public DelegateCommand _settingSpeedCommand; public DelegateCommand SettingSpeedCommand => _settingSpeedCommand ?? new DelegateCommand(SettingSpeed); public DelegateCommand _lowValueUpdateCommand; public DelegateCommand LowValueUpdateCommand => _lowValueUpdateCommand ?? new DelegateCommand(LowValueUpdate); public DelegateCommand _highValueUpdateCommand; public DelegateCommand HighValueUpdateCommand => _highValueUpdateCommand ?? new DelegateCommand(HighValueUpdate); /// /// 低浓度阈值更新 /// /// private void LowValueUpdate(object obj) { Dictionary values = new() { { "valuetype", "Low" }, { "bd", lowCalibrationValue }, { "kb", lowBlankValue }, { "waveKey", wavekeysCurrentShow } }; Messager.Send("UpdateResultValue", values); } /// /// 高浓度阈值更新 /// /// private void HighValueUpdate(object obj) { Dictionary values = new() { { "valuetype", "High" }, { "bd", highCalibrationValue }, { "kb", highBlankValue }, { "waveKey", wavekeysCurrentShow } }; Messager.Send("UpdateResultValue", values); } /// /// 设置速度 /// /// private void SettingSpeed(object obj) { var speed = SpeedInfos.FirstOrDefault(item => item.NodeId.Equals(obj.ToString())); var res = _controller.SetSpeed(speed); UMessageBox.InfoTip($"{speed.Description}速度设置{(res ? "成功" : "失败")}"); } private void OptSampleResultPath(object obj) { var dialog = new FolderBrowserDialog { SelectedPath = string.IsNullOrWhiteSpace(_config.ResultWordFilePath) ? System.AppDomain.CurrentDomain.BaseDirectory : _config.ResultWordFilePath }; if (dialog.ShowDialog().Equals(DialogResult.OK)) { var folderName = dialog.SelectedPath; SampleResultFilePath = folderName ; } } /// /// 更新配置信息 /// /// private void UpdateSettingInfo(object obj) { var pidRes = _controller.UpdateValues(PIDs.ToList()); var heatRes = _controller.UpdateValues(HeatData.ToList()); var washRes = _controller.UpdateValues(LiquidWashes.ToList()); var liquidTotal = _controller.UpdateValues(LiquidTotals.ToList()); var coolingRes = _controller.UpdateValues(CoolingData.ToList()); var titrationRes = _controller.UpdateValues(TitrationValue.ToList()); _config.UpdateAmount(LiquidAmount.ToList()); _config.UpdateTemplateFilePath(TemplateFilePath); _config.UpdateAppStartHeat(IsStartHeat.ToString()); _config.UpdatePreheatTemperature(PreheatTemperature); _config.UpdateResultWordFilePath(SampleResultFilePath); _config.UpdateExportTypeByExcel(ExportTypeByExcel); _config.UpdateExportTypeByWord(ExportTypeByWord); _config.UpdateAddWaterTemperature(AddWaterTemperature); _config.UpdateCoolingPipeWatingTime(CoolingPipeWatingTime); _config.UpdateDissolveMoveDistance(DissolveMoveDistance); _config.UpdateManiGrabFeedBack(ManiGrabFeedback); _config.UpdateDripNozzleFeedBack(DripNozzleFeedBack); _config.UpdateIsAddCupFeedBack(IsAddCupFeedBack); _config.UpdateIsAddCupBiaoDingBubei(IsAddCupBiaoDingBubei); _config.UpdatePort(PortName); _config.UpdateAutoFindPort(AutoFindPort); foreach (var item in SpeedInfos) { _config.UpdateSpeed(item.NodeId, item.Speed); _config.UpdateAcSpeed(item.NodeId, item.AcSpeed); _config.UpdateDeSpeed(item.NodeId, item.DeSpeed); } _config.UpdateSpeedWrite(AutoWriteSpeed); if (heatRes && coolingRes && pidRes && washRes && liquidTotal && titrationRes) { UMessageBox.SuccessTip("配置更新成功!"); } } public void OptTemplatePath(object obj) { var dialog = new FolderBrowserDialog { SelectedPath = string.IsNullOrWhiteSpace(_config.TemplateFilePath) ? System.AppDomain.CurrentDomain.BaseDirectory : _config.TemplateFilePath }; if (dialog.ShowDialog().Equals(DialogResult.OK)) { var folderName = dialog.SelectedPath; TemplateFilePath = folderName + "\\WaterTemplate.xlsx"; } } #endregion } }