IOManager.cs 5.3 KB
using OnlineStore.Common;
using OnlineStore.DeviceLibrary.IO;
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=0)
        {
            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=0)
        {
            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=0)
        {
            ConfigIO configIo = GetDI(ioType, subType);
            if (configIo == null)
            {
                configIo = GetDO(ioType, subType);
            }
            return configIo;
        }
        internal static ConfigIO GetDI(string ioType, int subType = 0)
        {
            ConfigIO configIo = null; 
            if (EquipManager.Config.DIList.ContainsKey(ioType))
            {
                return EquipManager.Config.DIList[ioType];
            } 
            return configIo;
        }
        internal static ConfigIO GetDO(string ioType, int subType = 0)
        {
            ConfigIO configIo = null; 
            if (EquipManager.Config.DOList.ContainsKey(ioType))
            {
                return EquipManager.Config.DOList[ioType];
            } 
            return configIo;
        }
        #endregion
        public static void Init()
        {
            bool UseHCBoard = ConfigAppSettings.GetIntValue(Setting_Init.UseHCBoard).Equals(1);
            if (UseHCBoard)
            {
                instance = new HCIOManager();
            }
            else
            {
                bool isAIOBox = ConfigAppSettings.GetIntValue(Setting_Init.UseAIOBOX).Equals(1); 
                if (isAIOBox)
                {
                    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 index);


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

        public abstract IO_VALUE GetIOValue(ConfigIO configIO);

        public abstract void CloseAllDO();


        public abstract void CloseAllConnection();

    }
}