using Aitex.Common.Util; using Aitex.Core.RT.Log; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using DocumentFormat.OpenXml.Bibliography; using MECF.Framework.Common.Device.PowerSupplier; using MECF.Framework.Common.IOCore; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MECF.Framework.Common.Device.Festo { public class FestoControllerCfgManager : Singleton { #region 内部变量 /// /// do--Modbus设备字典(key-DO名称,value-Modbus设备) /// private Dictionary _doModbusDictionary = new Dictionary(); /// /// 名称数据数组字典(key-festo名称,value-每个festo的byte数组) /// private ConcurrentDictionary _nameDatasDictionary = new ConcurrentDictionary(); /// /// 名称数据数组字典(key-festo名称,value-每个festo的byte数组长度) /// private ConcurrentDictionary _nameDataLengthDictionary = new ConcurrentDictionary(); /// /// do--Festo名称字典(key-DO名称,value-festo名称) /// private Dictionary _doFestoNameDictionary = new Dictionary(); /// /// do对象字典(key-DO名称,value-FestDO对象) /// private Dictionary _doDictionary=new Dictionary(); /// /// do 索引对象字典(key-地址索引-bit,value--do名称) /// private Dictionary _doIndexDictionary = new Dictionary(); #endregion /// /// 初始化 /// public void Initialize(List lst) { bool isSimulate = SC.GetValue("System.IsSimulatorMode"); string xmlPath = ""; try { if (isSimulate) { xmlPath = PathManager.GetCfgDir() + "Devices\\FestoControllerCfg-Simulator.xml"; } else { xmlPath = PathManager.GetCfgDir() + "Devices\\FestoControllerCfg.xml"; } FestoControllerCfg cfg = CustomXmlSerializer.Deserialize(new FileInfo(xmlPath)); if (cfg != null) { foreach (FestoDeviceConfig config in cfg.FestoDeviceConfigs) { FestoModbusDevice modbusDevice = new FestoModbusDevice(config.Name, config.IpAddress, config.Port,(ushort)config.DIStartAddress, (byte)config.Channel); modbusDevice.ReceiveTimeout = config.RecvTimeout; modbusDevice.SendTimeout = config.SendTimeout; InitialDeviceConfig(config, modbusDevice,lst); ushort addressCiount = (ushort)_nameDataLengthDictionary[config.Name]; modbusDevice.InitializeAddressCount(addressCiount); } } } catch { LOG.WriteLog(eEvent.ERR_FESTO, "Festo", "Load festo xml failed"); } } /// /// 初始化DO Modbus设备对象 /// /// /// private void InitialDeviceConfig(FestoDeviceConfig deviceConfig,FestoModbusDevice modbusDevice,List lst) { List addressLst = new List(); int index = -1; foreach(var item in deviceConfig.FestoDoes) { _doModbusDictionary[item.Name] = modbusDevice; if (!addressLst.Contains(item.Address)) { addressLst.Add(item.Address); index++; } item.DataIndex = index; _doFestoNameDictionary[item.Name] = deviceConfig.Name; _doDictionary[item.Name]= item; _doIndexDictionary[$"{deviceConfig.Name}-{item.DataIndex}-{item.Bit}"] = item.Name; if (!lst.Contains(item.Name)) { lst.Add(item.Name); } } _nameDataLengthDictionary[deviceConfig.Name]=addressLst.Count; } /// /// 设置DO数值 /// /// /// /// public bool SetDoValue(string doName,bool value) { if (!_doDictionary.ContainsKey(doName)) { LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{doName} object is null"); return false; } FestoDO festoDO=_doDictionary[doName]; if (!_doFestoNameDictionary.ContainsKey(doName)) { LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{doName} festo name is empty"); return false; } string festName = _doFestoNameDictionary[doName]; if (!_nameDatasDictionary.ContainsKey(festName)) { LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{festName} is not connected"); return false; } byte[] data = _nameDatasDictionary[festName]; if (festoDO.DataIndex >= data.Length) { LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{festName} data index is overflow"); return false; } if (!_doModbusDictionary.ContainsKey(doName)) { LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{doName} festo device is null"); return false; } FestoModbusDevice device = _doModbusDictionary[doName]; try { bool lastValue=festoDO.Invert?!value: value; byte festoValue = GenerateFestoData(festoDO, data[festoDO.DataIndex], lastValue); bool result= device.SetFestoValue((ushort)festoDO.Address, festoValue); if (result) { LOG.WriteLog(eEvent.INFO_FESTO, "Festo", $"{doName} write {value} success"); } else { LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{doName} write {value} failed"); } return result; } catch(Exception ex) { LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"write {doName} value {value} {ex.Message}"); return false; } } /// /// 构建Festo字节数值 /// /// /// /// /// private byte GenerateFestoData(FestoDO festoDO,byte source,bool value) { int bit = festoDO.Bit; bool sourceBit = (source>>bit & 0x01) == 1; if (sourceBit == value) { return source; } else { if (value) { return (byte)(source + Math.Pow(2,bit)); } else { return (byte)(source - Math.Pow(2,bit)); } } } /// /// 更新Festo数组 /// /// /// public void UpdateFestoData(string festoName, byte[] datas) { if (!_nameDataLengthDictionary.ContainsKey(festoName)) { LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"festo device {festoName} is invalid"); return; } int dataLength = _nameDataLengthDictionary[festoName]; byte[] bytes = null; if (!_nameDatasDictionary.ContainsKey(festoName)) { bytes = new byte[dataLength]; FirstUpdateDatas(festoName,datas); Array.Copy(datas,0,bytes,0,dataLength); _nameDatasDictionary[festoName] = bytes; return; } else { bytes=_nameDatasDictionary[festoName]; } if (bytes.Length != datas.Length) { LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"festo device {festoName} array source data length {bytes.Length} is not equal to {datas.Length}"); return; } CheckSourceDataUpdated(festoName,bytes, datas); } /// /// 首次更新数据 /// /// private void FirstUpdateDatas(string festoName,byte[] datas) { for (int i = 0; i < datas.Length; i++) { bool[] sourceBits = GetByteBitValue(datas[i]); for(int j = 0; j < sourceBits.Length; j++) { UpdateIOBitValue(festoName,i, j, sourceBits[j]); } } } /// /// 通知字节数值变化 /// /// /// private void CheckSourceDataUpdated(string festoName,byte[] sourceDatas, byte[] datas) { for (int i = 0; i < datas.Length; i++) { byte data = datas[i]; byte source=sourceDatas[i]; if (data != source) { CheckSourceDOUpdated(festoName,source, data,i); sourceDatas[i] = data; } } } /// /// 通知DO数值变化 /// /// /// private void CheckSourceDOUpdated(string festoName,byte source,byte data,int index) { bool[] sourceBits=GetByteBitValue(source); bool[] dataBits=GetByteBitValue(data); for(int i = 0; i < sourceBits.Length; i++) { if (sourceBits[i] == dataBits[i]) { continue; } UpdateIOBitValue(festoName,index, i, dataBits[i]); } } /// /// 更新IO bit数值 /// /// /// /// private void UpdateIOBitValue(string festoName,int index,int bit,bool value) { string strIndex = $"{festoName}-{index}-{bit}"; if (!_doIndexDictionary.ContainsKey(strIndex)) { return; } string doName = _doIndexDictionary[strIndex]; if (!_doDictionary.ContainsKey(doName)) { return; } FestoDO festoDO = _doDictionary[doName]; if (festoDO.Invert) { IOModuleManager.Instance.UpdateIoValue(_doIndexDictionary[strIndex], !value); } else { IOModuleManager.Instance.UpdateIoValue(_doIndexDictionary[strIndex], value); } } /// /// 获取位数值 /// /// /// private bool[] GetByteBitValue(byte data) { bool[] values = new bool[8]; for(int i = 0; i < values.Length; i++) { if (i == 0) { values[i] = (data & 0x01) == 1; } else { values[i] = ((data >> i) & 0x01) == 1; } } return values; } } }