IOManager.cs 5.8 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 HCIOManager instance = null;

        #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 (subType <= 0)
            {
                if (StoreManager.Config.StoreDIList.ContainsKey(ioType))
                {
                    return StoreManager.Config.StoreDIList[ioType];
                }
            }
            else
            {
                if (StoreManager.allConfigMap.ContainsKey(subType))
                {
                    if (StoreManager.allConfigMap[subType].StoreDIList.ContainsKey(ioType))
                    {
                        return StoreManager.allConfigMap[subType].StoreDIList[ioType];
                    }
                }

            }
            if (configIo == null && subType > 0)
            {
                if (StoreManager.Config.StoreDIList.ContainsKey(ioType))
                {
                    return StoreManager.Config.StoreDIList[ioType];
                }
            }
            return configIo;
        }
        internal static ConfigIO GetDO(string ioType, int subType=0)
        {
            ConfigIO configIo = null;
            if (subType <= 0)
            {
                if (StoreManager.Config.StoreDOList.ContainsKey(ioType))
                {
                    return StoreManager.Config.StoreDOList[ioType];
                }
            }
            else
            {
                if (StoreManager.allConfigMap.ContainsKey(subType))
                {
                    if (StoreManager.allConfigMap[subType].StoreDOList.ContainsKey(ioType))
                    {
                        return StoreManager.allConfigMap[subType].StoreDOList[ioType];
                    }
                }
            }
            if (configIo == null && subType > 0)
            {
                if (StoreManager.Config.StoreDOList.ContainsKey(ioType))
                {
                    return StoreManager.Config.StoreDOList[ioType];
                }
            }
            return configIo;
        }

        public static short GetADIOValue(short ioAddr)
        {
            return instance.GetAdVal(ioAddr);
        }
        #endregion
        public static void Init()
        {
            bool UseHCBoard = true;// ConfigAppSettings.GetIntValue(Setting_Init.UseHCBoard).Equals(1);
            if (UseHCBoard)
            {
                instance = new HCIOManager();
            }
        }

    }
}