using Cowain.Bake.BLL; using Cowain.Bake.Common.Enums; using Cowain.Bake.Communication.MOM; using Cowain.Bake.Model; using Newtonsoft.Json; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using System.Windows.Input; using Unity; using static Cowain.Bake.Common.Models.MESModel; namespace Cowain.Bake.UI.UserManagerment.ViewModels { public class DeviceModeViewModel : BindableBase { TDeviceConfig dev; private string _userName; public string UserName { get { return _userName; } set { SetProperty(ref _userName, value); } } private string _userPwa; public string UserPwa { get { return _userPwa; } set { SetProperty(ref _userPwa, value); } } private string _url; public string Url { get { return _url; } set { SetProperty(ref _url, value); } } private int _selectedRadioButtonIndex; public int SelectedRadioButtonIndex { get { return _selectedRadioButtonIndex; } set { SetProperty(ref _selectedRadioButtonIndex, value); } } public class CloseWindowEvent : PubSubEvent { public CloseWindowEvent() { } } private IEventAggregator _eventAggregator; private IUnityContainer _unityContainer; public DeviceModeViewModel(IUnityContainer unityContainer, IEventAggregator eventAggregator) { //初始化数据库连接池 _unityContainer = unityContainer; _eventAggregator = eventAggregator; string v = _unityContainer.Resolve().GetValueByParaID(ESysSetup.MOMMode.ToString()); SelectedRadioButtonIndex = int.Parse(v); dev = _unityContainer.Resolve().GetConfig(EDeviceType.MOM)[0]; dynamic d = JsonConvert.DeserializeObject(dev.Json); _url = d.URL; } public ICommand ExitCommand { get => new DelegateCommand(OnExit); } public ICommand SwitchCommand { get => new DelegateCommand(OnSwitch); } //public ICommand MesIpUpdateCommand //{ // get => new DelegateCommand(OnMesUpdate); //} private void OnExit(object obj ) { _eventAggregator.GetEvent().Publish(); } // _unityContainer.Resolve().GetValueByParaID(ESysSetup.MOMMode.ToString()) public void OnSwitch(object obj) { if (string.IsNullOrEmpty(UserName)|| string.IsNullOrEmpty(this.UserPwa)) { HandyControl.Controls.MessageBox.Info("工号与密码不能为空!请输入后切换"); return; } int deviceMode = int.Parse(_unityContainer.Resolve().GetValueByParaID(ESysSetup.MOMMode.ToString())); _unityContainer.Resolve().UpdateValue(ESysSetup.MOMMode.ToString(), SelectedRadioButtonIndex.ToString()); MESReturnCmdModel mesModel = _unityContainer.Resolve().EqptRun(UserName, this.UserPwa, SelectedRadioButtonIndex.ToString()); string MOMMessage = ""; if (null != mesModel) { MOMMessage = mesModel.Info.MOMMessage; } if (null == mesModel || mesModel.Info.ResultFlag == EResultFlag.NG.ToString()) { _unityContainer.Resolve().UpdateValue(ESysSetup.MOMMode.ToString(), deviceMode.ToString()); HandyControl.Controls.MessageBox.Error($@"切换【{((EMOMMode)SelectedRadioButtonIndex).GetDescription()}】失败 {MOMMessage}!", "切换提示"); return; } HandyControl.Controls.MessageBox.Show($@"切换【{((EMOMMode)SelectedRadioButtonIndex).GetDescription()}】成功!", "切换提示"); _unityContainer.Resolve().AddLog($@"切换【{((EMOMMode)SelectedRadioButtonIndex).GetDescription()}】!", E_LogType.Operate.ToString()); } //public void OnMesUpdate(object obj) //{ // dynamic dc = JsonConvert.DeserializeObject(dev.Json); // if (MessageBoxResult.OK == HandyControl.Controls.MessageBox.Ask("是否修改Mes地址?该操作可能会导致Mes(MOM)无法使用,请仔细确认无误", "操作提示")) // { // dc.URL = _url; // _unityContainer.Resolve().UpdateMesJosn(JsonConvert.SerializeObject(dc)); // //_unityContainer.Resolve().GetJsonParam(JsonConvert.SerializeObject(dc)); // return; // } // Url = dc.URL; //} public void Refresh() { string v = _unityContainer.Resolve().GetValueByParaID(ESysSetup.MOMMode.ToString()); SelectedRadioButtonIndex = int.Parse(v); dev = _unityContainer.Resolve().GetConfig(EDeviceType.MOM)[0]; dynamic d = JsonConvert.DeserializeObject(dev.Json); Url = d.URL; } } }