AIOBOXManager.cs 9.1 KB
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Asa.AIOBOX;
using System.Threading;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;

namespace OnlineStore.DeviceLibrary
{
    public class AIOBOXManager : IOManager
    {
        
        public readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public Dictionary<string, AIOBOX_32> AIOMap = new Dictionary<string, AIOBOX_32>();
        
        private object DIMapLock = "";
        private object DOMapLock = "";
        public System.Timers.Timer timer = null;
   
        public void ConnectionIP(string ioIp )
        {
            if (timer == null)
            {
                timer = new System.Timers.Timer();
                timer.Interval = 300;
                timer.AutoReset = true;
                timer.Elapsed += timer_Elapsed;
                timer.Enabled = true;
            }
            AIOBOX_32 aioBox = null;
            if (AIOMap.ContainsKey(ioIp))
            {
                aioBox = AIOMap[ioIp];

                if (null != aioBox)
                {
                    aioBox.Close();
                    aioBox = null;
                }
                AIOMap.Remove(ioIp);
            }
            try
            {
                // Create new modbus master and add event functions
                aioBox = new AIOBOX_32();
                aioBox.IP = ioIp;
                bool result = aioBox.Connect();
                if (result)
                {
                    AIOMap.Add(ioIp, aioBox);
                }
                else
                {
                    LogUtil.error("连接IO模块【"+ioIp+"】失败:"+aioBox.ErrInfo);
                }
                Thread.Sleep(10);

                //读取所有的DO
                ReadAllDI(ioIp, 0);
            }
            catch (Exception error)
            {
                LogUtil.error(LOGGER, "连接IO模块[" + ioIp + "]出错:" + error.ToString());
            }
        }
        /// <summary>
        /// 判断Io模块是否连接
        /// </summary> 
        public bool IsConnection(string ip)
        {
            try
            {
                if (AIOMap.ContainsKey(ip))
                {
                    if (AIOMap[ip].IsConn)
                    {
                        return true;
                    }
                }
                
            }
            catch (Exception ex)
            {
                LogUtil.error(LOGGER, "出错啦:" + ex.ToString());
            }
            return false;
        }
        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {

                List<string> list = new List<string>(AIOMap.Keys);
                foreach (string io in list)
                {
                    //判断是否连接,如果没有连接自动重连
                    AIOBOX_32 clinet = AIOMap[io];
                    if (!clinet.IsConn)
                    { 
                        LogUtil.error(LOGGER, io + "当前没有连上:" + clinet.ErrInfo);
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(LOGGER, "出错啦:" + ex.ToString());
            }
            Thread.Sleep(1);
        }

        public override void ConnectionKND(List<string> DIONameList)
        { 
            foreach (string ip in DIONameList)
            { 
                ConnectionIP(ip );
            }
        }

        //关闭所有的DO
        public override void CloseAllDO()
        { 
            foreach (AIOBOX_32 aio in AIOMap.Values)
            { 
                Status[] statuses = new Status[16];
                for(int i = 0; i < 16; i++)
                {
                    statuses[i] = Status.Off;
                }
                aio.WriteDO(Addr.DI_1, statuses);
            } 
        }

        public override void CloseAllConnection()
        {
            foreach (AIOBOX_32 aio in AIOMap.Values)
            {
                aio.Close();
            }
            AIOMap.Clear();
        }
        public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff)
        { 
            try
            {
                
                AIOBOX_32 aioBox = getAIO(ioIp);
                if (aioBox != null)
                {
                    Addr add = GetAddr(StartAddress);
                    aioBox.WriteDO(GetAddr(StartAddress), GetStatus(onOff));
                }
                else
                {
                    LogUtil.error(LOGGER, "ReadSingleDO出错没有连接IO模块:" + ioIp);
                }
            }
            catch (Exception ex)
            {
                LOGGER.Error("出错啦:" + ex.ToString());
            }  
        }
        public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff, int mSeconds)
        {
            try
            {

                AIOBOX_32 aioBox = getAIO(ioIp);
                Status currStatus = GetStatus(onOff);
                if (aioBox != null)
                {
                    Addr add = GetAddr(StartAddress);
                    aioBox.WriteDO(GetAddr(StartAddress), currStatus); 

                    //写入之后,等待指定间隔后回写
                    System.Timers.Timer mytimer = new System.Timers.Timer(mSeconds);
                    mytimer.Elapsed += (o1, e1) =>
                    {
                        try
                        {
                            aioBox.WriteDO(GetAddr(StartAddress), aioBox.ReverseStatus(currStatus));
                            LogUtil.debug(LOGGER, "**********定时回写入 IO【" + ioIp + "," + StartAddress + ",值" + aioBox.ReverseStatus(currStatus) + "】:");
                        }
                        catch (Exception ex)
                        {
                            LogUtil.error(LOGGER, "**********定时回写入 出错:" + ex.StackTrace);
                        }
                    };
                    mytimer.AutoReset = false;//设置是否自动重启,即自动执行多次;
                    mytimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件mytask;
                }
                else
                {
                    LogUtil.error(LOGGER, "WriteSingleDO出错没有连接IO模块:" + ioIp);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("WriteSingleDO出错:" + ioIp);
            }
        }
        public override void ReadAllDI(string ioIp, byte slaveId)
        {
         
        }
        public override void ReadAllDO(string ioIp, byte slaveId)
        { 
        }
        public override IO_VALUE GetDOValue(string ioIP, byte slaveId, ushort StartAddress)
        {
            IO_VALUE value = IO_VALUE.LOW; 
            AIOBOX_32 aioBox = getAIO(ioIP); 
            if (aioBox != null)
            {
                Status status = Status.Off;
                aioBox.ReadDO(GetAddr(StartAddress), out status);
                if (status.Equals(Status.On))
                {
                    value = IO_VALUE.HIGH;
                }
            }
           
            return value;
        }

        public override IO_VALUE GetDIValue(string ioIP, byte slaveId, ushort StartAddress)
        {
            IO_VALUE value = IO_VALUE.LOW;
            AIOBOX_32 aioBox = getAIO(ioIP);
            if (aioBox != null)
            {
                Status status = Status.Off;
                aioBox.ReadDI(GetAddr(StartAddress), out status);
                if (status.Equals(Status.On))
                {
                    value = IO_VALUE.HIGH;
                }
            }

            return value;
        }
        public override IO_VALUE GetIOValue(ConfigIO configIO)
        {
            IO_VALUE value = IO_VALUE.LOW;
            try
            {
                if (configIO.ProType.Equals(ConfigItemType.DI))
                {
                    return GetDIValue(configIO.DeviceName, configIO.SlaveID, configIO.GetIOAddr());
                }
                else if (configIO.ProType.Equals(ConfigItemType.DO))
                {
                    return GetDOValue(configIO.DeviceName, configIO.SlaveID, configIO.GetIOAddr());
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(LOGGER, "获取数据出错:" + ex.ToString());
            }
            return value;
        }
         
        private Addr GetAddr(ushort StartAddress)
        {
            return (Addr)(StartAddress - 1);
        }
        private Status GetStatus(IO_VALUE onOff)
        {
            if (onOff.Equals(IO_VALUE.HIGH))
            {
                return Status.On;
            }
            else
            {
                return Status.Off;
            }
        }

        private AIOBOX_32 getAIO(string ioIp)
        {
            AIOBOX_32 aioBox = null;
            if (AIOMap.ContainsKey(ioIp))
            {
                aioBox = AIOMap[ioIp];
            }
            return aioBox;
        }
    }
}