|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using BPA.Helper;
- using BPASmartClient.CustomResource.Pages.Model;
-
- namespace BPASmartClient.DosingSystem.ViewModel
- {
- public class ChangeDeviceNameViewModel : ViewModelBase
- {
- public ChangeDeviceNameViewModel()
- {
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- if (o != null && o is string str) IpAddress = str;
- }), "ChangeDeviceNameViewOpen");
-
- CancelCommand = new BPARelayCommand(() => { ActionManage.GetInstance.Send("ChangeDeviceNameViewClose"); });
- AddCommand = new BPARelayCommand(() =>
- {
- if (string.IsNullOrEmpty(DeviceName))
- {
- ErrorInfo = "设备名称不能为空";
- return;
- }
- int index = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.IpAddress == IpAddress);
- if (index >= 0 && index < DeviceInquire.GetInstance.devices.Count)
- {
- if (DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == DeviceName) != null)
- ErrorInfo = "设备名称已存在";
- else
- {
- var res = Global.DeviceRawMaterials.FirstOrDefault(p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(index).DeviceName);
- if (res != null)
- {
- res.RawMaterialName = DeviceName;
- DeviceInquire.GetInstance.devices.ElementAt(index).DeviceName = DeviceName;
- DeviceInquire.GetInstance.GetDevice(IpAddress).SetDeviceName(DeviceName);//设置PLC名称
- for (int i = 0; i < Json<LocalRecipe>.Data.Recipes.Count; i++)
- {
- for (int m = 0; m < Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.Count; m++)
- {
- Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.ElementAt(m).RawMaterialName = DeviceName;
- }
- }
- ActionManage.GetInstance.Send("ChangeDeviceNameViewClose");
- }
- }
- }
- });
- }
-
- private static string IpAddress = string.Empty;
-
- public string DeviceName { get { return _mDeviceName; } set { _mDeviceName = value; OnPropertyChanged(); } }
- private string _mDeviceName;
-
- }
- }
|