|
- //#define Debug
- using BPA.Communication;
- using BPA.Helper;
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.DosingSystem.Model;
- using BPASmartClient.DosingSystem.ViewModel;
- //using BPASmartClient.Modbus;
- using BPASmartClient.Model;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Net.NetworkInformation;
- using System.Reflection;
- using System.Threading;
- using System.Threading.Tasks;
-
- namespace BPASmartClient.DosingSystem
- {
- public class DeviceInquire
- {
- int stockCount;//料仓数
- private volatile static DeviceInquire _Instance;
- public static DeviceInquire GetInstance => _Instance ?? (_Instance = new DeviceInquire());
- private DeviceInquire() { }
-
- string IPSegment = "192.168.0.";
- ConcurrentDictionary<string, DeviceStatus> DeviceLists = new ConcurrentDictionary<string, DeviceStatus>();
- List<string> InvalidIP = new List<string>();//无效 IP 集合
- List<string> IPLists = new List<string>();//启动 Ping 任务IP集合
- ConcurrentQueue<string> IPQueues = new ConcurrentQueue<string>();//pincomplete 完成队列
-
- public ObservableCollection<DeviceCurrentStatus> TopDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
- public ObservableCollection<DeviceCurrentStatus> BottomDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
- public ObservableCollection<Devices> devices { get; set; } = new ObservableCollection<Devices>();
- private void DeviceDataInit()
- {
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- for (int i = 0; i < DeviceLists.Count; i++)
- {
- string deviceName = DeviceLists.ElementAt(i).Value.DeviceName;
- int TopIndex = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
- int BottomIndex = Array.FindIndex(BottomDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
- if (TopIndex >= 0 && TopIndex < TopDeviceCurrentStatuses.Count)
- {
- TopDeviceCurrentStatuses.ElementAt(TopIndex).Weight = DeviceLists.ElementAt(i).Value.deviceStatus.WeightFeedback;
- TopDeviceCurrentStatuses.ElementAt(TopIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
- }
-
- if (BottomIndex >= 0 && BottomIndex < BottomDeviceCurrentStatuses.Count)
- {
- BottomDeviceCurrentStatuses.ElementAt(BottomIndex).Weight = DeviceLists.ElementAt(i).Value.deviceStatus.WeightFeedback;
- BottomDeviceCurrentStatuses.ElementAt(BottomIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
- }
-
- int deviceIndex = Array.FindIndex(devices.ToArray(), p => p.IpAddress == DeviceLists.ElementAt(i).Key/* && p.DeviceName != DeviceLists.ElementAt(i).Value.DeviceName && p.DeviceNum != DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum*/);
- if (deviceIndex >= 0 && deviceIndex < devices.Count)
- {
- devices.ElementAt(deviceIndex).DeviceName = DeviceLists.ElementAt(i).Value.DeviceName;
- devices.ElementAt(deviceIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
- }
- }
- Thread.Sleep(200);
- }), "设备状态监听", true);
- }
-
- private void aa(int num, string name)
- {
- DeviceLists.TryAdd($"192.168.1.{num}", new DeviceStatus() { DeviceName = $"{name}" });
- TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus()
- {
- DeviceName = $"{name}",
- DeviceNum = num,
- Weight = new Random().Next(100, 10000) / 100.0
- });
- Global.DeviceRawMaterials.Add(new RawMaterialModel()
- {
- DeviceIp = $"192.168.1.{num}",
- RawMaterialName = $"{name}",
- });
- devices.Add(new Devices()
- {
- DeviceName = $"{name}",
- DeviceNum = num,
- IpAddress = $"192.168.1.{num}",
- });
- }
-
- private void TestData()
- {
-
- aa(1, "盐");
- aa(2, "郫县豆瓣");
- aa(3, "芥菜");
- aa(4, "调味剂");
- aa(5, "香料");
- aa(6, "鸡精");
- aa(7, "味精");
- for (int i = 0; i < 8; i++)
- {
- DeviceLists.TryAdd($"192.168.1.{i + 1}", new DeviceStatus() { DeviceName = $"测试设备{i + 1}" });
- TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus()
- {
- DeviceName = $"测试设备{i + 1}",
- DeviceNum = i + 1,
- Weight = new Random().Next(100, 10000) / 100.0
- });
- Global.DeviceRawMaterials.Add(new RawMaterialModel()
- {
- DeviceIp = $"192.168.1.{i + 1}",
- RawMaterialName = $"测试设备{i + 1}",
- });
- devices.Add(new Devices()
- {
- DeviceName = $"测试设备{i + 1}",
- DeviceNum = i + 1,
- IpAddress = $"192.168.1.{i + 1}",
- });
- }
-
- for (int i = 8; i < 16; i++)
- {
- DeviceLists.TryAdd($"192.168.1.{i + 1}", new DeviceStatus() { DeviceName = $"测试设备{i + 1}" });
- BottomDeviceCurrentStatuses.Add(new DeviceCurrentStatus()
- {
- DeviceName = $"测试设备{i + 1}",
- DeviceNum = i + 1,
- Weight = new Random().Next(100, 10000) / 100.0
- });
- Global.DeviceRawMaterials.Add(new RawMaterialModel()
- {
- DeviceIp = $"192.168.1.{i + 1}",
- RawMaterialName = $"测试设备{i + 1}",
- });
- devices.Add(new Devices()
- {
- DeviceName = $"测试设备{i + 1}",
- DeviceNum = i + 1,
- IpAddress = $"192.168.1.{i + 1}",
- });
- }
- }
-
- public void Init()
- {
- if (!string.IsNullOrEmpty(Json<DevicePar>.Data.BaseParModel.NetworkSegAddress)) IPSegment = Json<DevicePar>.Data.BaseParModel.NetworkSegAddress;
- else Json<DevicePar>.Data.BaseParModel.NetworkSegAddress = IPSegment;
- //TestData();
- IpAddressLines();
- Task.Factory.StartNew(() =>
- {
- SiemensDevice.GetInstance.Connect(Json<DevicePar>.Data.BaseParModel.DeviceAddress);
- });
- DeviceDataInit();
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- if (IPQueues.Count >= IPLists.Count)
- IpAddressLines();
- Thread.Sleep(5000);
- }), "配料机设备上线监听", true);
- }
-
- public void Rescan()
- {
- InvalidIP.Clear();
- }
-
- public DeviceStatus GetDevice(string ip)
- {
- if (ip != null)
- {
- if (DeviceLists.ContainsKey(ip)) return DeviceLists[ip];
- else return new DeviceStatus();
- //var res = DeviceLists.Values.FirstOrDefault(p => p.IpAddress == ip);
- //if (res != null) return res;
- }
- return new DeviceStatus();
- }
-
- public List<DeviceStatus> GetDevice()
- {
- List<DeviceStatus> deviceStatuses = new List<DeviceStatus>();
- foreach (var device in DeviceLists)
- {
- deviceStatuses.Add(device.Value);
- }
- return deviceStatuses;
- }
-
- private void IpAddressLines()
- {
- IPLists.Clear();
- IPQueues.Clear();
- /*for (int i = 1; i <= 255; i++)
- {
- if (!InvalidIP.Contains($"{IPSegment}{i}") && !DeviceLists.ContainsKey($"{IPSegment}{i}"))
- {
- string pattern = @"^(([1-9]\d?)|(1\d{2})|(2[01]\d)|(22[0-3]))(\.((1?\d\d?)|(2[04]/d)|(25[0-5]))){3}$";
- if (Regex.IsMatch($"{IPSegment}{i}", pattern))
- {
- IPLists.Add($"{IPSegment}{i}");
- }
- }
- }*/
- stockCount = Json<DevicePar>.Data.BaseParModel.StockCount;
- for (int i = 0; i < stockCount; i++)
- {
- IPLists.Add($"{IPSegment}{(i + 1) * 10}");
- }
-
- IPLists.ForEach((item) =>
- {
- Ping myPing = new Ping();
- myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
- myPing.SendAsync(item, 1000, null);
-
- });
- }
-
- private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
- {
- if (e.Reply != null && e.Reply.Status == IPStatus.Success)
- {
- string ip = e.Reply.Address.ToString();
- if (!DeviceLists.ContainsKey(ip))
- {
- DeviceStatus DS = new DeviceStatus();
- DS.modbusTcp.IsReconnect = false;
-
- DS.modbusTcp.Connected = new Action(() =>
- {
- var tt = DS.modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20);
- string DeviceName = DS.modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20).Content?.Trim()?.Replace(" ", "");//读取设备名称
- if (DeviceName.Length > 0)
- {
- DeviceLists.TryAdd(ip, DS);
- DeviceLists[ip].Init(DeviceName);
- DeviceLists[ip].modbusTcp.IsReconnect = false;
- App.Current.Dispatcher.Invoke(new Action(() =>
- {
- devices.Add(new Devices() { DeviceName = DeviceName, IpAddress = ip });
- //2023/7/3调整
- if (stockCount <= 8 || (stockCount > 8 && TopDeviceCurrentStatuses.Count <= stockCount / 2 + 1 && int.TryParse(ip.Split('.')[3], out int res) && res <= (stockCount / 2 + 1) * 10))
- TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = DeviceName });
- else
- BottomDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = DeviceName });
-
-
- 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++)
- {
- if (Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp == ip)
- {
- Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.ElementAt(m).RawMaterialName = DeviceName;
- }
- }
- }
- if (Global.DeviceRawMaterials.Count > 0)
- {
- if (Global.DeviceRawMaterials.FirstOrDefault(p => p.RawMaterialName == DeviceName) == null)
- {
- Global.DeviceRawMaterials.Add(new RawMaterialModel() { RawMaterialName = DeviceName, DeviceIp = ip, RawMaterialSource = 1 });
- }
- }
- else
- {
- Global.DeviceRawMaterials.Add(new RawMaterialModel() { RawMaterialName = DeviceName, DeviceIp = ip, RawMaterialSource = 1 });
- }
-
- }));
- }
- else
- {
- //if (!InvalidIP.Contains(ip)) InvalidIP.Add(ip);
- }
- });
-
- DS.modbusTcp.ConnectFail = new Action(() =>
- {
- //if (!InvalidIP.Contains(ip)) InvalidIP.Add(ip);
- });
-
- DS.modbusTcp.Disconnect = new Action(() =>
- {
- if (InvalidIP.Contains(ip)) InvalidIP.Remove(ip);
- var res = devices.FirstOrDefault(P => P.IpAddress == ip);
- if (res != null && devices.Contains(res))
- {
- App.Current.Dispatcher.Invoke(new Action(() =>
- {
- devices.Remove(res);
- var item = Global.DeviceRawMaterials.FirstOrDefault(P => P.RawMaterialName == res.DeviceName);
- if (item != null) Global.DeviceRawMaterials.Remove(item);
-
- var topRes = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == res.DeviceName);
- var bottomRes = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == res.DeviceName);
- if (topRes != null) TopDeviceCurrentStatuses.Remove(topRes);
- if (bottomRes != null) BottomDeviceCurrentStatuses.Remove(bottomRes);
- }));
- }
-
- if (DeviceLists.ContainsKey(ip))
- {
- DeviceLists[ip].Dispose();
- var res11 = DeviceLists[ip];
- DeviceLists.TryRemove(ip, out res11);
- }
- });
-
- Task.Run(new Action(() =>
- {
- DS.modbusTcp.Connect(new BPA.Communication.Base.ConfigurationOptions() { IpAddress = ip, Port = 502 });//PLC连接
- IPQueues.Enqueue(e.Reply.Address.ToString());
- }));
- }
- else IPQueues.Enqueue(e.Reply.Address.ToString());
- }
- else
- {
- if (e.Reply != null)
- IPQueues.Enqueue(e.Reply.Address.ToString());
- }
- }
- }
-
- public class DeviceStatus
- {
-
- #region 对象属性声明
- public string DeviceName = String.Empty;
- public string IpAddress => modbusTcp.GetConfigPar().IpAddress;
-
- /// <summary>
- /// 设备状态
- /// </summary>
- public RawMaterialDeviceStatus deviceStatus { get; set; } = new RawMaterialDeviceStatus();
-
- public ModbusTcp modbusTcp = new ModbusTcp();
-
- public bool IsConnected => modbusTcp.IsConnected();
- #endregion
-
- public void Init(string DeviceName)
- {
- //modbusTcp.Show = new Action<string>((s) => { MessageNotify.GetInstance.ShowRunLog(s); });
- //modbusTcp.ShowEx = new Action<string>((s) => { MessageNotify.GetInstance.ShowRunLog(s); });
- this.DeviceName = DeviceName;
- AlarmHelper<AlarmInfo>.Init();
- if (IsConnected)
- {
-
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- var dn = modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20);
- if (dn.IsSuccess) { this.DeviceName = dn.Content.Trim().Replace(" ", ""); deviceStatus.DeviceName = this.DeviceName; }
-
- //获取设备运行状态
- var rs = this.modbusTcp.Read<ushort>(DeviceAddress.RunStatus.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (rs.IsSuccess) deviceStatus.RunStatus = rs.Content;
-
- ////获取设备料仓剩余重量
- var wf = this.modbusTcp.Read<uint>(DeviceAddress.WeightFeedback.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (wf.IsSuccess) deviceStatus.WeightFeedback = (float)(wf.Content);
-
- ////获取下料重量
- var nwf = this.modbusTcp.Read<float>(DeviceAddress.CutWeightFeedback.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (nwf.IsSuccess) deviceStatus.NowWeightFeedback = nwf.Content;
-
- ////获取设备编号
- var dnum = this.modbusTcp.Read<ushort>(DeviceAddress.DeviceNum.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (dnum.IsSuccess) deviceStatus.DeviceNum = dnum.Content;
-
- ////获取设备故障编码
- var dac = this.modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (dac.IsSuccess) deviceStatus.DeviceAlarmCode = dac.Content;
-
- //获取原料类型
- var mt = this.modbusTcp.Read<ushort>(DeviceAddress.MaterialDeviceType.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (mt.IsSuccess) deviceStatus.RawMaterialType = mt.Content;
-
- //获取工作模式
- var wm = this.modbusTcp.Read<ushort>(DeviceAddress.WorkModel.ToAdd(),1, BPA.Helper.DataFormat.CDAB);
- if (wm.IsSuccess) deviceStatus.WorkModel = wm.Content.GetBitValue(1);
- //慢加重量
- var ssaw = this.modbusTcp.Read<float>(DeviceAddress.SlowlyAddWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (ssaw.IsSuccess) deviceStatus.SlowAddWeight = ssaw.Content;
- //提前关阀重量
- var pcvw = this.modbusTcp.Read<float>(DeviceAddress.PreCloseValveWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (pcvw.IsSuccess) deviceStatus.PreCloseValueWeight = pcvw.Content;
- //快加速度
- var ra = this.modbusTcp.Read<uint>(DeviceAddress.RapidAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (ra.IsSuccess) deviceStatus.RapidAcceleration = ra.Content;
- //慢加速度
- var sa = this.modbusTcp.Read<uint>(DeviceAddress.SlowAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (sa.IsSuccess) deviceStatus.SlowAcceleration = sa.Content;
- //伺服手动速度
- var sms = this.modbusTcp.Read<uint>(DeviceAddress.ServoManualSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (sms.IsSuccess) deviceStatus.ServoManualSpeed = sms.Content;
- //料仓上限重量
- var ulw = this.modbusTcp.Read<uint>(DeviceAddress.SiloUpperLimitWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (ulw.IsSuccess) deviceStatus.SiloUpperLimitWeight = ulw.Content;
- //料仓下限重量
- var llw = this.modbusTcp.Read<uint>(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (llw.IsSuccess) deviceStatus.SiloLowerLimitWeight = llw.Content;
- //料仓搅拌速度
- var ss = this.modbusTcp.Read<uint>(DeviceAddress.StirringSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (ss.IsSuccess) deviceStatus.StirringSpeed = ss.Content;
- //this.DeviceName = modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20).Content.Trim().Replace(" ", "");
- //deviceStatus.RunStatus = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.RunStatus.ToAdd()).Content; //获取设备运行状态
- //deviceStatus.WeightFeedback = (float)this.modbusTcp.Read<uint>(DeviceAddress.WeightFeedback.ToAdd()).Content;//获取设备料仓剩余重量
- //deviceStatus.NowWeightFeedback = this.modbusTcp.Read<float>(DeviceAddress.CutWeightFeedback.ToAdd()).Content;//获取下料重量
- //deviceStatus.DeviceNum = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.DeviceNum.ToAdd()).Content;//获取设备编号
- //deviceStatus.DeviceAlarmCode = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd()).Content;//获取设备故障编码
-
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop1 = deviceStatus.DeviceAlarmCode.GetBitValue(1);
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Servo = deviceStatus.DeviceAlarmCode.GetBitValue(2);
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Inverter = deviceStatus.DeviceAlarmCode.GetBitValue(3);
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop2 = deviceStatus.DeviceAlarmCode.GetBitValue(7);
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloUpperLimit = deviceStatus.DeviceAlarmCode.GetBitValue(8);
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloLowerLimit = deviceStatus.DeviceAlarmCode.GetBitValue(9);
-
-
- Thread.Sleep(10);
- }), $"{DeviceName} 开始监听", true);
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- Thread.Sleep(1000);
- if (ExcuteControl.GetInstance.mqttCollection.ContainsKey($"Stock{IpAddress.Split(".")[3].Substring(0, 1)}") && ExcuteControl.GetInstance.mqttCollection[$"Stock{IpAddress.Split(".")[3].Substring(0, 1)}"]._mqttClient.IsConnected)
- {
-
- StockPropertyReport();
- StockEventReport();
- }
-
- }),$"{DeviceName}属性实时上报",true);
- }
- }
- /// <summary>
- /// 料仓属性上报
- /// </summary>
- public void StockPropertyReport()
- {
- #if !Debug
- AliyunStockPropertyReportModel model = new AliyunStockPropertyReportModel();
- model.StockOnlineStatus = this.modbusTcp.IsConnected()==true?1:0;
- model.StockRealWeight = (int)deviceStatus.WeightFeedback;
- model.BucketRealWeight = deviceStatus.NowWeightFeedback;
- model.StockDeviceNum = (int)deviceStatus.DeviceNum;
- model.StockDeviceName = deviceStatus.DeviceName;
- model.StockWorkModel = deviceStatus.WorkModel == true ? 1 : 0;
- model.StockSlowlyAddWeight = deviceStatus.SlowAddWeight;
- model.StockPreCloseValueWeight = deviceStatus.PreCloseValueWeight;
- model.StockRapidAcceleration = (int)deviceStatus.RapidAcceleration;
- model.StockSlowAcceleration = (int)deviceStatus.SlowAcceleration;
- model.StockServoManualSpeed = (int)deviceStatus.ServoManualSpeed;
- model.StockUpperLimitWeight = (int)deviceStatus.SiloUpperLimitWeight;
- model.StockLowerLimitWeight = (int)deviceStatus.SiloLowerLimitWeight;
- model.StockStirringSpeed = (int)deviceStatus.StirringSpeed;
- switch (deviceStatus.RawMaterialType)
- {
-
- case 1:
- model.StockMaterialType = "膏体"; break;
- case 2:
- model.StockMaterialType = "液体"; break;
- case 3:
- model.StockMaterialType = "粉体"; break;
- }
- switch (deviceStatus.RunStatus)
- {
- case 0:
- model.StockBatchStatus = "待机"; break;
- case 1:
- model.StockBatchStatus = "等待配料"; break;
- case 2:
- model.StockBatchStatus = "配料中"; break;
- case 3:
- model.StockBatchStatus = "配料完成"; break;
- }
- #endif
- #if Debug
- //本地模拟测试
- AliyunStockPropertyReportModel model = new AliyunStockPropertyReportModel();
- model.StockRealWeight = 1;
- model.BucketRealWeight = 2;
- model.StockDeviceNum = 2;
- model.StockDeviceName ="测试";
- model.StockWorkModel = 0;
- model.StockSlowlyAddWeight = 6;
- model.StockPreCloseValueWeight = 7;
- model.StockRapidAcceleration =46;
- model.StockSlowAcceleration = 24;
- model.StockServoManualSpeed = 35;
- model.StockUpperLimitWeight = 57;
- model.StockLowerLimitWeight = 34;
- model.StockStirringSpeed = 54;
- model.StockMaterialType = "粉体";
- model.StockBatchStatus = "待机";
-
- #endif
- ExcuteControl.GetInstance.StokcPropertyReport(IpAddress, model);
- }
- /// <summary>
- /// 事件上报
- /// </summary>
- public void StockEventReport()
- {
- #if !Debug
- try
- {
- if (RTrig.GetInstance($"stop1:{IpAddress}").Start(AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop1))
- {
- ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockJerk1StatusEvent", AliyunMaterialModelType.StockModel);
- }
- if (RTrig.GetInstance($"Servo:{IpAddress}").Start(AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Servo))
- {
- ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockServoFalutEvent", AliyunMaterialModelType.StockModel);
- }
-
- if (RTrig.GetInstance($"EStop2:{IpAddress}").Start(AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop2))
- {
-
- ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockJerk2StatusEvent", AliyunMaterialModelType.StockModel);
- }
- if (RTrig.GetInstance($"SiloUpperLimit:{IpAddress}").Start(AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloUpperLimit))
- {
- if (DeviceName == "盐")
- MessageNotify.GetInstance.ShowRunLog("料仓1 上限报警");
- if (DeviceName == "A20")
- MessageNotify.GetInstance.ShowRunLog("料仓2 上限报警");
- if (DeviceName == "A30")
- MessageNotify.GetInstance.ShowRunLog("料仓3 上限报警");
- if (DeviceName == "A40")
- MessageNotify.GetInstance.ShowRunLog("料仓4 上限报警");
- if (DeviceName == "A50")
- MessageNotify.GetInstance.ShowRunLog("料仓5 上限报警");
- if (DeviceName == "A60")
- MessageNotify.GetInstance.ShowRunLog("料仓6 上限报警");
- ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockUpLimitEvent", AliyunMaterialModelType.StockModel);
- }
- if (RTrig.GetInstance($"SiloLowerLimit:{IpAddress}").Start(AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloLowerLimit))
- {
- if (DeviceName == "盐")
- MessageNotify.GetInstance.ShowRunLog("料仓1 下限报警");
- if (DeviceName == "A20")
- MessageNotify.GetInstance.ShowRunLog("料仓2 下限报警");
- if (DeviceName == "A30")
- MessageNotify.GetInstance.ShowRunLog("料仓3 下限报警");
- if (DeviceName == "A40")
- MessageNotify.GetInstance.ShowRunLog("料仓4 下限报警");
- if (DeviceName == "A50")
- MessageNotify.GetInstance.ShowRunLog("料仓5 下限报警");
- if (DeviceName == "A60")
- MessageNotify.GetInstance.ShowRunLog("料仓6 下限报警");
- ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockDownLimitEvent", AliyunMaterialModelType.StockModel);
-
- }
- }
- catch (Exception)
- {
-
- //throw;
- }
- #endif
- #if Debug
- ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockJerk1StatusEvent", AliyunMaterialModelType.StockModel);
- ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockServoFalutEvent", AliyunMaterialModelType.StockModel);
- ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockJerk2StatusEvent", AliyunMaterialModelType.StockModel);
- ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockUpLimitEvent", AliyunMaterialModelType.StockModel);
- ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockDownLimitEvent", AliyunMaterialModelType.StockModel);
- #endif
- }
- public void SetDeviceName(string name)
- {
- this.modbusTcp.Write(DeviceAddress.DeviceName.ToAdd(), new ushort[20]);
- this.modbusTcp.Write(DeviceAddress.DeviceName.ToAdd(), name);
- }
-
- public void StatusReset()
- {
- // prop1:
- this.modbusTcp.Write(DeviceAddress.FinfishStatus.ToAdd(), (ushort)1);
- //MessageNotify.GetInstance.ShowRunLog($"料仓复位,值:{1}");
- //var value = this.modbusTcp.Read<ushort>(DeviceAddress.FinfishStatus.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- //if (value != null && value.ToString() != 1.ToString()) goto prop1;
- }
-
- public void Dispose()
- {
- TaskManage.GetInstance.StopTask($"{DeviceName} 开始监听");
- }
-
- public void Start(float Value)
- {
- if (modbusTcp.IsConnected())
- {
- var res = Json<DevicePar>.Data.deviceParModels.FirstOrDefault(p => p.MaterialName == DeviceName);
- if (res != null)
- {
- //modbusTcp.Write(DeviceAddress.SlowlyAddWeight.ToAdd(), res.SlowlyAddWeight);
-
- //modbusTcp.Write(DeviceAddress.PreCloseValveWeight.ToAdd(), res.PreCloseValveWeight);
- //modbusTcp.Write(DeviceAddress.RapidAcceleration.ToAdd(), (uint)res.RapidAcceleration);
- //modbusTcp.Write(DeviceAddress.SlowAcceleration.ToAdd(), (uint)res.SlowAcceleration);
- //modbusTcp.Write(DeviceAddress.ServoManualSpeed.ToAdd(), (uint)res.ServoManualSpeed);
- //modbusTcp.Write(DeviceAddress.SiloUpperLimitWeight.ToAdd(), (uint)res.SiloUpperLimitWeight);
- //modbusTcp.Write(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), (uint)res.LowerLimitWeightOfSilo);
- //modbusTcp.Write(DeviceAddress.StirringSpeed.ToAdd(), (uint)res.StirringSpeed);
-
- //MessageNotify.GetInstance.ShowRunLog($"{res.MaterialName},配料参数下发完成");
-
- prop1:
- modbusTcp.Write(DeviceAddress.SlowlyAddWeight.ToAdd(), res.SlowlyAddWeight);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置慢加重量:{res.SlowlyAddWeight}");
- var value1 = modbusTcp.Read<float>(DeviceAddress.SlowlyAddWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value1 != null && value1.ToString() != res.SlowlyAddWeight.ToString()) goto prop1;
- prop2:
- modbusTcp.Write(DeviceAddress.PreCloseValveWeight.ToAdd(), res.PreCloseValveWeight);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置提前关阀重量:{res.PreCloseValveWeight}");
- var value2 = modbusTcp.Read<float>(DeviceAddress.PreCloseValveWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value2 != null && value2.ToString() != res.PreCloseValveWeight.ToString()) goto prop2;
- prop3:
- modbusTcp.Write(DeviceAddress.RapidAcceleration.ToAdd(), (uint)res.RapidAcceleration);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置快加速度:{res.RapidAcceleration}");
- var value3 = modbusTcp.Read<uint>(DeviceAddress.RapidAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value3 != null && value3.ToString() != res.RapidAcceleration.ToString()) goto prop3;
- prop4:
- modbusTcp.Write(DeviceAddress.SlowAcceleration.ToAdd(), (uint)res.SlowAcceleration);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置慢加速度:{res.SlowAcceleration}");
- var value4 = modbusTcp.Read<uint>(DeviceAddress.SlowAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value4 != null && value4.ToString() != res.SlowAcceleration.ToString()) goto prop4;
- prop5:
- modbusTcp.Write(DeviceAddress.ServoManualSpeed.ToAdd(), (uint)res.ServoManualSpeed);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置伺服手动速度:{res.ServoManualSpeed}");
- var value5 = modbusTcp.Read<uint>(DeviceAddress.ServoManualSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value5 != null && value5.ToString() != res.ServoManualSpeed.ToString()) goto prop5;
-
- prop6:
- modbusTcp.Write(DeviceAddress.SiloUpperLimitWeight.ToAdd(), (uint)res.SiloUpperLimitWeight);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置料仓上限重量:{res.SiloUpperLimitWeight}");
- var value6 = modbusTcp.Read<uint>(DeviceAddress.SiloUpperLimitWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value6 != null && value6.ToString() != res.SiloUpperLimitWeight.ToString()) goto prop6;
- prop7:
- modbusTcp.Write(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), (uint)res.LowerLimitWeightOfSilo);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置料仓下限重量:{res.LowerLimitWeightOfSilo}");
- var value7 = modbusTcp.Read<uint>(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if ((value7 != null && value7.ToString() != res.LowerLimitWeightOfSilo.ToString())) goto prop7;
- prop8:
- modbusTcp.Write(DeviceAddress.StirringSpeed.ToAdd(), (uint)res.StirringSpeed);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置搅拌速度:{res.StirringSpeed}");
- var value8 = modbusTcp.Read<uint>(DeviceAddress.StirringSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value8 != null && value8.ToString() != res.StirringSpeed.ToString()) goto prop8;
- MessageNotify.GetInstance.ShowRunLog($"{res.MaterialName},配料参数下发完成");
- }
- prop9:
- modbusTcp.Write(DeviceAddress.WeightSet.ToAdd(), Value);//写入配方重量
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置重量:{Value}");
-
- var value9 = modbusTcp.Read<float>(DeviceAddress.WeightSet.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if ((value9 != null && value9.ToString() != Value.ToString()) || value9 == 0) goto prop9;
- //prop10:
- //MessageNotify.GetInstance.ShowRunLog($"写入配方重量,值{Value}:{value9}");
- //modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
- //var value10 = modbusTcp.Read<ushort>(DeviceAddress.Start.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- //if ((value10 != null && value10.ToString() != 1.ToString())||value10==0) goto prop10;
- //modbusTcp.Write(DeviceAddress.WeightSet.ToAdd(), Value);//写入配方重量
- modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
- Task.Run(() =>
- {
- while (true)
- {
- ushort c = modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- MessageNotify.GetInstance.ShowRunLog($"{IpAddress}:LW51,值:{modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content}");
- string str = DecToBinary(c);
- if (str[8].ToString() == 0.ToString())
- {
- modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
- }
- else
- return;
- }
-
-
- });
-
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置重量:{Value},味魔方启动配料");
- }
- }
-
- static string DecToBinary(int a)
- {
- int r;
- string s = "";
- do
- {
- r = a % 2;
- a /= 2;
- s = r + s;
- }
- while (a != 0);
- int length = s.Length;
- for (int i = 0; i < 16 - length; i++)
- {
- s = 0 + s;
- }
- return s;
- }
- }
- }
|