IOManager.cs 7.0 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace OnlineStore.DeviceLibrary
{
    public abstract class IOManager
    {
        public static IOManager instance = null;

        public abstract void ConnectionIOList(List<string> dIODeviceNameList);


        #region KNDIO

        public static void IOMove(string ioType, IO_VALUE ioValue, int subType = 0)
        {
            if (string.IsNullOrEmpty(ioType))
            {
                return;
            }
            ConfigIO configIo = GetDO(ioType, subType);
            if (configIo != null)
            {
                instance.WriteSingleDO(configIo.IO_IP, configIo.SlaveID, configIo.GetIOAddr(), ioValue);
                Thread.Sleep(10);
            }
            else
            {
                LogUtil.error("未找到DO,[" + ioType + "][" + subType + "]");
            }
        }

        public static IO_VALUE IOValue(string ioType, int subType)
        {
            if (string.IsNullOrEmpty(ioType))
            {
                return IO_VALUE.None;
            }
            IO_VALUE ioValue = IO_VALUE.None;
            ConfigIO configIo = GetDI(ioType, subType);
            if (configIo == null)
            {
                configIo = GetDO(ioType, subType);
            }

            if (configIo != null)
            {
                if (configIo.ProType.Equals(ConfigItemType.DI))
                { 
                    ioValue = instance.GetDIValue(configIo.IO_IP, configIo.SlaveID, configIo.GetIOAddr());
                }
                else
                {
                    ioValue = instance.GetDOValue(configIo.IO_IP, configIo.SlaveID, configIo.GetIOAddr());
                }
            }
            else
            {
                LogUtil.error("未找到IO[" + ioType + "][" + subType + "]");
            }
            return ioValue;
        }
        public static IO_VALUE DOValue(string ioType, int subType = 0)
        {
            if (string.IsNullOrEmpty(ioType))
            {
                return IO_VALUE.None;
            }
            IO_VALUE ioValue = IO_VALUE.None;
            ConfigIO configIo = GetDO(ioType, subType);

            if (configIo != null)
            {
                ioValue = instance.GetDOValue(configIo.IO_IP, configIo.SlaveID, configIo.GetIOAddr());
            }
            else
            {
                LogUtil.error("没有DO=" + ioType);
            }
            return ioValue;
        }
        public static IO_VALUE DIValue(string ioType, int subType)
        {
            if (string.IsNullOrEmpty(ioType))
            {
                return IO_VALUE.None;
            }
            IO_VALUE ioValue = IO_VALUE.None;
            ConfigIO configIo = GetDI(ioType, subType);
            if (configIo != null)
            {
                ioValue = instance.GetDIValue(configIo.IO_IP, configIo.SlaveID, configIo.GetIOAddr());
            }
            else
            {
                LogUtil.error("未找到 DI [" + ioType + "][" + subType + "]");
            }
            return ioValue;
        }
        internal static ConfigIO GetIO(string ioType, int subType)
        {
            ConfigIO configIo = GetDI(ioType, subType);
            if (configIo == null)
            {
                configIo = GetDO(ioType, subType);
            }
            return configIo;
        }
        internal static ConfigIO GetDI(string ioType, int subType)
        {
            ConfigIO configIo = null;
            if (subType <= 0)
            {
                if (RobotManager.Config.DIList.ContainsKey(ioType))
                {
                    return RobotManager.Config.DIList[ioType];
                }
            }
            else
            {
                if (RobotManager.allConfigMap.ContainsKey(subType))
                { 
                    if (RobotManager.allConfigMap[subType].DIList.ContainsKey(ioType))
                    {
                        return RobotManager.allConfigMap[subType].DIList[ioType];
                    }
                }

            }
            if (configIo == null && subType > 0 )
            {
                if (RobotManager.Config.DIList.ContainsKey(ioType))
                {
                    return RobotManager.Config.DIList[ioType];
                }
            }
            return configIo;
        }
        internal static ConfigIO GetDO(string ioType, int subType)
        {
            ConfigIO configIo = null;
            if (subType <= 0)
            {
                if (RobotManager.Config.DOList.ContainsKey(ioType))
                {
                    return RobotManager.Config.DOList[ioType];
                }
            }
            else
            {
                if (RobotManager.allConfigMap.ContainsKey(subType))
                { 
                    if (RobotManager.allConfigMap[subType].DOList.ContainsKey(ioType))
                    {
                        return RobotManager.allConfigMap[subType].DOList[ioType];
                    }
                }
            }
            if (configIo == null && subType > 0)
            {
                if (RobotManager.Config.DOList.ContainsKey(ioType))
                {
                    return RobotManager.Config.DOList[ioType];
                }
            }
            return configIo;
        }
        public static void CloseDeviceDO(List<ConfigIO> DoList)
        {
            foreach (ConfigIO io in DoList)
            {
                instance.WriteSingleDO(io.IO_IP, io.SlaveID, io.GetIOAddr(), IO_VALUE.LOW);
                Thread.Sleep(60);
            }
        }
        public static void CloseDeviceDO(int subType)
        {
            List<ConfigIO> DoList = new List<ConfigIO>();
            if (subType <= 0)
            {
                DoList = new List<ConfigIO>(RobotManager.Config.DOList.Values);
            }
            else
            {
                if (RobotManager.allConfigMap.ContainsKey(subType))
                {
                    DoList = new List<ConfigIO>(RobotManager.allConfigMap[subType].DOList.Values);
                }
            }
            CloseDeviceDO(DoList);
        }
        #endregion
        public static void Init()
        {

            instance = new AIOBOXManager();

        }
        public abstract void ReadAllDI(string deviceName, byte slaveId);

        public abstract void ReadAllDO(string deviceName, byte slaveId);

        public abstract void WriteSingleDO(string deviceName, byte slaveId, ushort index, IO_VALUE value, int time);

        public abstract void WriteSingleDO(string deviceName, byte slaveId, ushort index, IO_VALUE value);

        public abstract IO_VALUE GetDIValue(string deviceName, byte slaveID, ushort v);

        public abstract IO_VALUE GetDOValue(string deviceName, byte slaveID, ushort v);

        public abstract IO_VALUE GetIOValue(ConfigIO configIO);

        public abstract void CloseAllDO();

        public abstract void CloseAllConnection();

        public abstract bool IsConnect(string ip);

    }
}