AIOAIManager.cs 3.3 KB
using Asa.IOModule;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
 public   class AIOAIManager:AIManager
    {
        private   object AILock = "";
        private   List<int> AIValList = null;
        private   int AILength = 4;
        private   Asa.IOModule.AIOBOX AIBox = null;
        public override void ConnectionIP(string ioIp)
        {
            int autoMS = 150;
            try
            {
                AIValList = new List<int>();
                AIBox = new Asa.IOModule.AIOBOX();
                AIBox.IP = ioIp;
                // bool rtn = AIBox.AutoIP(ioIp);
                AIBox.SetInput(Asa.IOModule.Box_Type.AI, AILength);
                AIBox.SetOutput(Asa.IOModule.Box_Type.DO, 0);

                AIBox.AutoReadInput(true, autoMS);
                AIBox.AI_Changed_Event += Box_AI_Changed_Event;
                LogUtil.debug("开始连接AI模块[" + ioIp + "][" + autoMS + "],尝试重连三次");
                for (int i = 1; i <= 3; i++)
                {
                    bool result = AIBox.Connect();
                    if (result)
                    {
                        LogUtil.info("第【" + i + "】次连接IO模块[" + ioIp + "][" + autoMS + "]成功:" + AIBox.ErrInfo);
                        Thread.Sleep(10);
                        break;
                    }
                    else
                    {
                        LogUtil.error("第【" + i + "】次连接IO模块[" + ioIp + "][" + autoMS + "]失败:" + AIBox.ErrInfo + "");
                    }
                    Thread.Sleep(10);
                }
            }
            catch (Exception error)
            {
                LogUtil.error("连接IO模块[" + ioIp + "][" + autoMS + "]出错:" + error.ToString());
            }
        }

        public override void CloseConnect()
        {
            try
            {
                if (AIBox != null)
                {
                    AIBox.Close();
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("断开AI连接出错:" + ex.ToString());
            }
        }

        private   void Box_AI_Changed_Event(AIOBOX box, int[] val)
        {
            try
            {
                if (val != null && val.Length >= AILength)
                {
                    lock (AILock)
                    {
                        AIValList = new List<int>();
                        AIValList.AddRange(val);
                    }

                }
            }
            catch (Exception ex)
            {
                LogUtil.error("Box_AI_Changed_Event出错:" + ex.ToString());
            }
        }


        public override double GetAIValue(string ioiP,int index)
        {
            if (AIValList != null && AIValList.Count > index)
            {
                return AIValList[index];
            }
            return -1;

        }

        //public override double ConvertAI(double aiValue, double defaultValue)
        //{
        //    double xishu = (double)ConfigAppSettings.GetNumValue(Setting_Init.AI_ConvertPosition);
        //    double result = Math.Round((aiValue - defaultValue) / xishu, 2);
        //    return result;
        //}
    }
}