IOManager.cs 6.7 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 )
        {
            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)
        {
            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 (StoreManager.Config.DIList.ContainsKey(ioType))
                {
                    return StoreManager.Config.DIList[ioType];
                }
            }
            else
            {
                if (StoreManager.allConfigMap.ContainsKey(subType))
                {
                    if (StoreManager.allConfigMap[subType].DIList.ContainsKey(ioType))
                    {
                        return StoreManager.allConfigMap[subType].DIList[ioType];
                    }
                }

            }
            if (configIo == null && subType > 0)
            {
                if (StoreManager.Config.DIList.ContainsKey(ioType))
                {
                    return StoreManager.Config.DIList[ioType];
                }
            }
            return configIo;
        }
        internal static ConfigIO GetDO(string ioType, int subType)
        {
            ConfigIO configIo = null;
            if (subType <= 0)
            {
                if (StoreManager.Config.DOList.ContainsKey(ioType))
                {
                    return StoreManager.Config.DOList[ioType];
                }
            }
            else
            {
                if (StoreManager.allConfigMap.ContainsKey(subType))
                {
                    if (StoreManager.allConfigMap[subType].DOList.ContainsKey(ioType))
                    {
                        return StoreManager.allConfigMap[subType].DOList[ioType];
                    }
                }
            }
            if (configIo == null && subType > 0)
            {
                if (StoreManager.Config.DOList.ContainsKey(ioType))
                {
                    return StoreManager.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();
                }
                //else
                //{
                //    instance = new KNDManager();
                //}
            }
        }
        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();

    }
}