using BPASmartClient.Helper; using BPASmartClient.Message; using BPASmartClient.Model; using CommunityToolkit.Mvvm.Input; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BPASmartClient.MilkWithTea.ViewModel { partial class PatrameterSettiongViewModel: ObservableObject { string FileName => GLobal.deviceConfig.Count > 0 ? GLobal.deviceConfig[0].ShopName : string.Empty; /// /// 物料通道口列表 /// public ObservableCollection materialPosions { get; set; } = new ObservableCollection(); [ObservableProperty] private int _materialID = 0; /// /// 出料重量 /// [ObservableProperty] private int _outMaterilWeight; /// /// 矫正的通道号 /// [ObservableProperty] private int _CorrectPassway = 0; /// /// 通道是否开启 /// [ObservableProperty] private bool _passwayIsOpen ; /// /// 矫正重量 /// [ObservableProperty] private int _correctMatetailWeight = 0; /// /// 矫正时间 /// [ObservableProperty] private int _outTime = 0; public bool IsEnable { get { return !GLobal.makeEnable; } set { GLobal.makeEnable = !value; OnPropertyChanged(); } } /// /// 出料动作 /// [RelayCommand] private void OutMaterial() { ActionManage.GetInstance.Send("通道口出料", new object[] { MaterialID, OutMaterilWeight }); } /// /// 开始矫正 /// [RelayCommand] private void CheckPassway() { } /// /// 开启通道 /// [RelayCommand] private void OpenPassway() { } /// /// 确认重量 /// [RelayCommand] private void CheckMaterailWeight() { } #region 设备配置 /// /// 店铺名称 /// [ObservableProperty] private string? _shopName = string.Empty; /// /// 店铺ID /// [ObservableProperty] private string? _shopID = string.Empty; /// /// 设备ID /// [ObservableProperty] private string? _deviceID = string.Empty; /// /// PLC地址 /// [ObservableProperty] private string? _pLCAdress = string.Empty; [ObservableProperty] private bool _isPort = true; [ObservableProperty] private Visibility _vsIP = Visibility.Hidden; [ObservableProperty] private Visibility _vsPort = Visibility.Visible; [ObservableProperty] private string[] _ports; [ObservableProperty] private string _prot; [RelayCommand] private void SaveDevices() { SaveDeviceMessage(); } [RelayCommand] private void ChangeCommunation() { if(IsPort) { VsPort = Visibility.Visible; VsIP = Visibility.Hidden; } else { VsIP = Visibility.Visible; VsPort = Visibility.Hidden; } } #endregion public PatrameterSettiongViewModel() { Ports = System.IO.Ports.SerialPort.GetPortNames(); init(); } private void init() { for (int i = 1; i < 14; i++) { materialPosions.Add(i); } if (GLobal.deviceConfig.Count > 0) { ShopName = GLobal.deviceConfig.ElementAtOrDefault(0).ShopName ; ShopID = GLobal.deviceConfig.ElementAtOrDefault(0).ShopId; DeviceID = GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAt(0).DeviceId; PLCAdress = GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAt(0).communicationDevcies.ElementAt(0).communicationPar.IPAddress; } } private void SaveDeviceMessage() { if (GLobal.deviceConfig.Count > 0) { GLobal.deviceConfig.ElementAtOrDefault(0).ShopName = ShopName; GLobal.deviceConfig.ElementAtOrDefault(0).ShopId = ShopID; GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAtOrDefault(0).DeviceId = DeviceID; if(IsPort) { GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAtOrDefault(0).communicationDevcies.ElementAtOrDefault(0).communicationPar.IsSerialPort = true; GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAtOrDefault(0).communicationDevcies.ElementAtOrDefault(0).communicationPar.SerialPort = Prot; GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAtOrDefault(0).communicationDevcies.ElementAtOrDefault(0).communicationPar.BaudRate = 9600; } else { GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAtOrDefault(0).communicationDevcies.ElementAtOrDefault(0).communicationPar.IsSerialPort = false; GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAtOrDefault(0).communicationDevcies.ElementAtOrDefault(0).communicationPar.IsNetworkPort = true; GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAtOrDefault(0).communicationDevcies.ElementAtOrDefault(0).communicationPar.IPAddress = PLCAdress; } } File.WriteAllText($"{LocaPath.GetInstance().GetDeviceConfigPath}MOC.json", JsonConvert.SerializeObject(GLobal.deviceConfig)); } } }