| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 | 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;using System.Windows.Media.Animation;namespace MECF.Framework.Common.Device.Festo{    public class FestoControllerCfgManager : Singleton<FestoControllerCfgManager>    {        #region 内部变量        /// <summary>        /// do--Modbus设备字典(key-DO名称,value-Modbus设备)        /// </summary>        private Dictionary<string, FestoModbusDevice> _doModbusDictionary = new Dictionary<string, FestoModbusDevice>();        /// <summary>        /// 名称数据数组字典(key-festo名称,value-每个festo的byte数组)        /// </summary>        private ConcurrentDictionary<string, byte[]> _nameDatasDictionary = new ConcurrentDictionary<string, byte[]>();        /// <summary>        /// 名称锁字典(key-fest名称,value-locker)        /// </summary>        private Dictionary<string, object> _nameLockerDictionary = new Dictionary<string, object>();        /// <summary>        /// 名称数据数组字典(key-festo名称,value-每个festo的byte数组长度)        /// </summary>        private ConcurrentDictionary<string, int> _nameDataLengthDictionary = new ConcurrentDictionary<string, int>();        /// <summary>        /// do--Festo名称字典(key-DO名称,value-festo名称)        /// </summary>        private Dictionary<string,string> _doFestoNameDictionary = new Dictionary<string, string>();        /// <summary>        /// do对象字典(key-DO名称,value-FestDO对象)        /// </summary>        private Dictionary<string,FestoDO> _doDictionary=new Dictionary<string, FestoDO>();        /// <summary>        /// do 索引对象字典(key-地址索引-bit,value--do名称)        /// </summary>        private Dictionary<string, string> _doIndexDictionary = new Dictionary<string, string>();                #endregion        /// <summary>        /// 初始化        /// </summary>        public void Initialize(List<string> lst)        {            bool isSimulate = SC.GetValue<bool>("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<FestoControllerCfg>(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;                        _nameLockerDictionary[config.Name] = new object();                        InitialDeviceConfig(config, modbusDevice,lst);                        ushort addressCiount = (ushort)_nameDataLengthDictionary[config.Name];                        modbusDevice.InitializeAddressCount(addressCiount);                    }                }            }            catch            {                LOG.WriteLog(eEvent.ERR_FESTO, "Festo", "Load festo xml failed");            }        }        /// <summary>        /// 初始化DO Modbus设备对象        /// </summary>        /// <param name="deviceConfig"></param>        /// <param name="modbusDevice"></param>        private void InitialDeviceConfig(FestoDeviceConfig deviceConfig,FestoModbusDevice modbusDevice,List<string> lst)        {            List<int> addressLst = new List<int>();            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;        }        /// <summary>        /// 设置DO数值        /// </summary>        /// <param name="doName"></param>        /// <param name="value"></param>        /// <returns></returns>        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;            }            if (!_nameLockerDictionary.ContainsKey(festName))            {                LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{festName} has no locker");                return false;            }            lock (_nameLockerDictionary[festName])            {                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)                    {                        _nameDatasDictionary[festName][festoDO.DataIndex] = festoValue;                            IOModuleManager.Instance.UpdateIoValue(festoDO.Name, value);                        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;                }            }        }        /// <summary>        /// 构建Festo字节数值        /// </summary>        /// <param name="festoDO"></param>        /// <param name="source"></param>        /// <param name="value"></param>        /// <returns></returns>        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));                }            }        }        /// <summary>        /// 更新Festo数组        /// </summary>        /// <param name="festoName"></param>        /// <param name="datas"></param>        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);        }        /// <summary>        /// 首次更新数据        /// </summary>        /// <param name="datas"></param>        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]);                }            }        }        /// <summary>        /// 通知字节数值变化        /// </summary>        /// <param name="source"></param>        /// <param name="datas"></param>        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;                }            }        }        /// <summary>        /// 通知DO数值变化        /// </summary>        /// <param name="source"></param>        /// <param name="data"></param>        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]);            }        }        /// <summary>        /// 更新IO bit数值        /// </summary>        /// <param name="index"></param>        /// <param name="bit"></param>        /// <param name="value"></param>        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);            }        }        /// <summary>        /// 获取位数值        /// </summary>        /// <param name="data"></param>        /// <returns></returns>        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;        }    }}
 |