using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DBFactory;
namespace Model
{
    public static class CGetInfo
    {
        static System.Object lockThis = new System.Object();
        public static DBOperator dbo = CommonClassLib.AppSettings.dbo; //20130510
        public static DBOperator dboM = CommonClassLib.AppSettings.dboM; //20130510
        static Dictionary<int, Model.MError> _DeviceErrors;
        static StringBuilder sql = new StringBuilder();
        public static Dictionary<int, Model.MError> DeviceErrors
        {
            get { return _DeviceErrors; }
            set { _DeviceErrors = value; }
        }
        /// <summary>
        /// ³õʼ»¯É豸¹ÊÕÏÐÅÏ¢
        /// </summary>
        public static void AddDeviceErrors()
        {//20160501
            
            lock (lockThis)
            {

                _DeviceErrors = new Dictionary<int, Model.MError>();
                sql.Remove(0, sql.Length);
                sql.Append("SELECT F_ErrorIndex, F_DeviceKindIndex, F_DeviceErrorIndex, F_ErrorName, F_ErrorLevel, F_ManageStatus,F_IfAlarm,F_ErrorKind FROM T_Base_Device_State where F_DeviceErrorIndex>=30");
                DataView dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                for (int i = 0; i < dv.Count; i++)
                {

                    Model.MError errs = new Model.MError();
                    errs.ErrorIndex = Convert.ToInt32(dv[i]["F_ErrorIndex"]);
                    errs.DeviceErrorIndex = Convert.ToInt32(dv[i]["F_DeviceErrorIndex"]);
                    errs.DeviceKindIndex = Convert.ToInt32(dv[i]["F_DeviceKindIndex"]);
                    errs.DeviceErrorIndex = Convert.ToInt32(dv[i]["F_DeviceErrorIndex"]);
                    errs.ErrorName = dv[i]["F_ErrorName"].ToString();
                    errs.ErrorLevel = Convert.ToInt32(dv[i]["F_ErrorLevel"]);
                    errs.ManageStatus = Convert.ToInt32(dv[i]["F_ManageStatus"]);
                    if (dv[i]["F_IfAlarm"] == DBNull.Value)
                    {//20160501
                        errs.IfAlarm ='1';
                    }
                    else
                    {
                        errs.IfAlarm = Convert.ToChar(dv[i]["F_IfAlarm"]);
                    }
                    if (dv[i]["F_ErrorKind"] == DBNull.Value)
                    {//20160501
                        errs.ErrorKind ='O';
                    }
                    else
                    {
                        errs.ErrorKind = Convert.ToChar(dv[i]["F_ErrorKind"]);
                    }
                    
                    if (_DeviceErrors.ContainsKey(Convert.ToInt32(errs.DeviceKindIndex.ToString() + errs.DeviceErrorIndex.ToString())) == false)
                    {
                        _DeviceErrors.Add(Convert.ToInt32(errs.DeviceKindIndex.ToString() + errs.DeviceErrorIndex.ToString()), errs);
                    }
                }
            }
        }
        /// <summary>
        /// ͨ¹ýÉ豸ÀàÐÍ+É豸¹ÊÕÏ´úÂë»ñÈ¡¹ÊÕÏÐÅÏ¢
        /// </summary>
        /// <param name="errID">É豸ÀàÐÍ+É豸¹ÊÕÏ´úÂë</param>
        /// <returns></returns>
        public static Model.MError GetErrorInfo(int errID)
        {
            
            lock (lockThis)
            {
                if (_DeviceErrors.ContainsKey(errID) == true)
                {
                    return _DeviceErrors[errID];
                }
                else
                {
                    return null;
                }
                
            }
        }
        /// <summary>
        /// ¿ÉÒԸĵÀµÄÉ豸ÐÅÏ¢
        /// </summary>
        public static Dictionary<int, Model.MDevice> ModifyPathDeviceInfo { get; set; } 
        /// <summary>
        /// ËùÓÐÉ豸ÐÅÏ¢
        /// </summary>
        static Dictionary<int, Model.MDevice> _DeviceInfo;
        /// <summary>
        /// ¼ÓÔØµ½ÄÚ´æÖеÄÉ豸ÐÅÏ¢¼¯ºÏ
        /// </summary>
        public static Dictionary<int, Model.MDevice> DeviceInfo
        {
            get { return _DeviceInfo; }
            set { _DeviceInfo = value; }
        }
        /// <summary>
        /// ÏòÄÚÖÐÌí¼ÓÉ豸ÐÅÏ¢
        /// </summary>
        public static void AddDeviceInfo()
        {
            
            lock (lockThis)
            {
                DataView dv1 = new DataView();
                _DeviceInfo = new Dictionary<int, Model.MDevice>();
                sql.Remove(0, sql.Length);
                sql.Append("select * from T_Base_Device order by F_DeviceIndex asc");
                DataView dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                for (int i = 0; i < dv.Count; i++)
                {
                    Model.MDevice devinfo = new Model.MDevice();
                    if (dv[i]["F_AppendBarcode"] == DBNull.Value)
                    {
                        devinfo.AppendBarcode = 0;
                    }
                    else
                    {
                        devinfo.AppendBarcode = (int)dv[i]["F_AppendBarcode"];
                    }
                    if (dv[i]["F_BeDetected"] == DBNull.Value)
                    {
                        devinfo.BeDetected = null;
                    }
                    else
                    {
                        devinfo.BeDetected = dv[i]["F_BeDetected"].ToString();
                    }
                    if (dv[i]["F_BindingDevice"] == DBNull.Value)
                    {
                        devinfo.BindingDevice = null;
                    }
                    else
                    {
                        devinfo.BindingDevice = dv[i]["F_BindingDevice"].ToString();
                    }
                    if (dv[i]["F_BindingDeviceOut"] == DBNull.Value)
                    {
                        devinfo.BindingDeviceOut = null;
                    }
                    else
                    {
                        devinfo.BindingDeviceOut = dv[i]["F_BindingDeviceOut"].ToString();
                    }
                    if (dv[i]["F_CommType"] == DBNull.Value)
                    {
                        devinfo.CommType = null;
                    }
                    else
                    {
                        devinfo.CommType = dv[i]["F_CommType"].ToString();
                    }

                    if (dv[i]["F_DBW1Address"] == DBNull.Value)
                    {
                        devinfo.Dbw1Address = -1;
                    }
                    else
                    {
                        devinfo.Dbw1Address = (int)(dv[i]["F_DBW1Address"]);
                    }
                    if (dv[i]["F_DBWSendLength"] == DBNull.Value)
                    {
                        devinfo.Dbw1SendLength = 0;
                    }
                    else
                    {
                        devinfo.Dbw1SendLength = (int)dv[i]["F_DBWSendLength"];
                    }
                    if (dv[i]["F_DBW2Address"] == DBNull.Value)
                    {
                        devinfo.Dbw2Address = -1;
                    }
                    else
                    {
                        devinfo.Dbw2Address = (int)dv[i]["F_DBW2Address"];
                    }
                    if (dv[i]["F_DBWGetLength"] == DBNull.Value)
                    {
                        devinfo.Dbw2Getlength = 0;
                    }
                    else
                    {
                        devinfo.Dbw2Getlength = (decimal)dv[i]["F_DBWGetLength"];
                    }
                    if (dv[i]["F_DeviceIndex"] == DBNull.Value)
                    {
                        devinfo.DeviceIndex = 0;
                    }
                    else
                    {
                        devinfo.DeviceIndex = (int)dv[i]["F_DeviceIndex"];
                    }
                    if (dv[i]["F_DeviceKindIndex"] == DBNull.Value)
                    {
                        devinfo.DeviceKind = 0;
                    }
                    else
                    {
                        devinfo.DeviceKind = (int)dv[i]["F_DeviceKindIndex"];
                    }
                    if (dv[i]["F_DeviceName"] == DBNull.Value)
                    {
                        devinfo.DeviceName = null;
                    }
                    else
                    {
                        devinfo.DeviceName = (string)dv[i]["F_DeviceName"];
                    }
                    if (dv[i]["F_DeviceVisual"] == DBNull.Value)
                    {
                        devinfo.DeviceVisual = 0;
                    }
                    else
                    {
                        devinfo.DeviceVisual = (int)dv[i]["F_DeviceVisual"];
                    }
                    if (dv[i]["F_KeyDevice"] == DBNull.Value)
                    {
                        devinfo.IfKeyDevice = '0';

                    }
                    else
                    {
                        devinfo.IfKeyDevice = Convert.ToChar(dv[i]["F_KeyDevice"]);
                    }
                    if (dv[i]["F_MaxSendCount"] == DBNull.Value)
                    {
                        devinfo.MaxSendCount = 0;
                    }
                    else
                    {
                        devinfo.MaxSendCount = (int)dv[i]["F_MaxSendCount"];
                    }
                    if (dv[i]["F_NeedOptimize"] == DBNull.Value)
                    {
                        devinfo.NeedOptimize = '0';
                    }
                    else
                    {
                        devinfo.NeedOptimize = Convert.ToChar(dv[i]["F_NeedOptimize"]);
                    }
                    if (dv[i]["F_SendInterval"] == DBNull.Value)
                    {
                        devinfo.SendInterval = 0;
                    }
                    else
                    {
                        devinfo.SendInterval = (int)dv[i]["F_SendInterval"];
                    }
                    if (dv[i]["F_LocalIP"] == DBNull.Value)
                    {
                        devinfo.LocalIP = "";
                    }
                    else
                    {
                        devinfo.LocalIP = (string)dv[i]["F_LocalIP"];
                    }
                    if (dv[i]["F_LocalPort"] == DBNull.Value)
                    {
                        devinfo.LocalPort = 0;
                    }
                    else
                    {
                        devinfo.LocalPort = (int)dv[i]["F_LocalPort"];
                    }
                    if (dv[i]["F_RemoteIP"] == DBNull.Value)
                    {
                        devinfo.RemoteIP = "";
                    }
                    else
                    {
                        devinfo.RemoteIP = (string)dv[i]["F_RemoteIP"];
                    }
                    if (dv[i]["F_RemotePort"] == DBNull.Value)
                    {
                        devinfo.RemotePort = 0;
                    }
                    else
                    {
                        devinfo.RemotePort = (int)dv[i]["F_RemotePort"];
                    }
                    if (dv[i]["F_OPCProgID"] == DBNull.Value)
                    {
                        devinfo.OPCProgID = "";
                    }
                    else
                    {
                        devinfo.OPCProgID = (string)dv[i]["F_OPCProgID"];
                    }
                    if (dv[i]["F_ErrorTaskNo"] == DBNull.Value)
                    {
                        devinfo.ErrorTaskNo = 0;
                    }
                    else
                    {
                        devinfo.ErrorTaskNo = (int)dv[i]["F_ErrorTaskNo"];
                    }
                    if (dv[i]["F_SendOutDetect"] == DBNull.Value)
                    {
                        devinfo.SendOutDetect = "";
                    }
                    else
                    {
                        devinfo.SendOutDetect = dv[i]["F_SendOutDetect"].ToString();
                    }
                    //
                    if (dv[i]["F_OnlyDetectIO"] == DBNull.Value)
                    {
                        devinfo.OnlyDetectIO = "0";
                    }
                    else
                    {
                        devinfo.OnlyDetectIO = dv[i]["F_OnlyDetectIO"].ToString();
                    }
                    if (dv[i]["F_UseCommonDB"] == DBNull.Value)
                    {
                        devinfo.UseCommonDB = "0";
                    }
                    else
                    {
                        devinfo.UseCommonDB = dv[i]["F_UseCommonDB"].ToString();
                    }
                    if (dv[i]["F_IfCorrelDoubleFork"] == DBNull.Value)
                    {
                        devinfo.IfCorrelDoubleFork = "0";
                    }
                    else
                    {
                        devinfo.IfCorrelDoubleFork = dv[i]["F_IfCorrelDoubleFork"].ToString();
                    }
                    
                    //20100609
                    if (dv[i]["F_SerialPort"] == DBNull.Value)
                    {
                        devinfo.SerialPort = 0;
                    }
                    else
                    {
                        devinfo.SerialPort = Convert.ToInt32(dv[i]["F_SerialPort"]);
                    }
                    //20100609
                    if (dv[i]["F_CommSettings"] == DBNull.Value)
                    {
                        devinfo.CommSettings = "0";
                    }
                    else
                    {
                        devinfo.CommSettings = dv[i]["F_CommSettings"].ToString();
                    }
                    //20100714
                    if (dv[i]["F_S7Connection"] == DBNull.Value)
                    {
                        devinfo.S7Connection = "S7:[S7 connection_1]";
                    }
                    else
                    {
                        devinfo.S7Connection = dv[i]["F_S7Connection"].ToString();
                    }
                    //20101118
                    if (dv[i]["F_SplitByte"] == DBNull.Value)
                    {
                        devinfo.SplitByte = -1;
                    }
                    else
                    {
                        devinfo.SplitByte = Convert.ToInt32(dv[i]["F_SplitByte"]);
                    }
                    if (dv[i]["F_ManTaskReserve"] == DBNull.Value)
                    {
                        devinfo.ManTaskReserve = 0;
                    }
                    else
                    {
                        devinfo.ManTaskReserve = Convert.ToInt32(dv[i]["F_ManTaskReserve"]);
                    }
                    if (dv[i]["F_HaveGoods"] == DBNull.Value)
                    {
                        devinfo.LogicHaveGoods = false;
                    }
                    else
                    {
                        devinfo.LogicHaveGoods = Convert.ToInt32(dv[i]["F_HaveGoods"]) >0 ? true : false;
                    }
                    if (dv[i]["F_ReadSubscription"] == DBNull.Value)
                    {
                        devinfo.ReadSubscription = "0";
                    }
                    else
                    {
                        devinfo.ReadSubscription = dv[i]["F_ReadSubscription"].ToString();
                    }
                    if (dv[i]["F_UnControl"] == DBNull.Value)
                    {
                        devinfo.UnControl = "0";
                    }
                    else
                    {
                        devinfo.UnControl = dv[i]["F_UnControl"].ToString();
                    }
                    switch (devinfo.DeviceKind)
                    {//20120820
                        case 1://¶Ñ¶â»úµÄÌØ¶¨ÊôÐÔ
                            sql.Clear();
                            sql.Append("SELECT F_ForkAmount, F_Reach,F_Mutex,F_VirtualStack,F_AbendOut,F_ZeroColLayer,F_SpecialZXArea,F_PRIZXArea FROM T_Base_StackInfo WHERE  F_StackIndex = ").Append(devinfo.DeviceIndex);
                            dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                            if (dv1.Count > 0)
                            {
                                devinfo.ForkAmount = Convert.ToInt32(dv1[0]["F_ForkAmount"]);
                                devinfo.StackReach = Convert.ToInt32(dv1[0]["F_Reach"]);
                                if (dv1[0]["F_VirtualStack"] != DBNull.Value)
                                {
                                    devinfo.VirtualStack = Convert.ToInt32(dv1[0]["F_VirtualStack"]);
                                }
                                else
                                {
                                    devinfo.VirtualStack = 0;
                                }
                                if (dv1[0]["F_Mutex"] != DBNull.Value)
                                {
                                    devinfo.MutexStack = Convert.ToInt32(dv1[0]["F_Mutex"]);
                                }
                                else
                                {
                                    devinfo.MutexStack = 0;
                                }
                                if (dv1[0]["F_AbendOut"] != DBNull.Value)
                                {
                                    devinfo.StackAbendOut = Convert.ToInt32(dv1[0]["F_AbendOut"]);
                                }
                                else
                                {
                                    devinfo.StackAbendOut = 0;
                                }
                                if (dv1[0]["F_ZeroColLayer"] != DBNull.Value)
                                {
                                    devinfo.StackZeroColLayer = dv1[0]["F_ZeroColLayer"].ToString();
                                }
                                else
                                {
                                    devinfo.StackZeroColLayer = "";
                                }
                                if (dv1[0]["F_SpecialZXArea"] != DBNull.Value)
                                {
                                    devinfo.SpecialZXArea = dv1[0]["F_SpecialZXArea"].ToString();
                                }
                                else
                                {
                                    devinfo.SpecialZXArea = "";
                                }
                                if (dv1[0]["F_PRIZXArea"] != DBNull.Value)
                                {
                                    devinfo.PRIZXArea = dv1[0]["F_PRIZXArea"].ToString();
                                }
                                else
                                {
                                    devinfo.PRIZXArea = "";
                                }
                            }
                            else
                            {
                                devinfo.ForkAmount = 1;
                                devinfo.StackReach = 1;
                                devinfo.VirtualStack = 0;
                                devinfo.MutexStack =0;
                                devinfo.StackAbendOut = 0;
                                devinfo.StackZeroColLayer ="";
                                devinfo.PRIZXArea = "";
                                devinfo.SpecialZXArea = "";
                            }
                            break;
                        case 2://¼ì²éÊÇ·ñΪÏïµÀ¿Ú¶à²æ¶Ñ¶â»úÈ¡·Å²Ù×÷É豸
                            sql.Clear();
                            sql.Append("SELECT F_UseAwayFork FROM T_Base_Lane_Gate  WHERE F_LaneGateDeviceIndex =").Append(devinfo.DeviceIndex);
                            dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                            if (dv1.Count > 0)
                            {
                                devinfo.DoubleFork = dv1[0]["F_UseAwayFork"].ToString();
                            }
                            else
                            {
                                devinfo.DoubleFork = "0";
                            }
                            break;
                        case 9://¼ì²éÊÇ·ñΪ¶à²æAGVÈ¡·Å¿Ú²Ù×÷É豸
                            sql.Clear();
                            sql.Append("SELECT F_UseAwayFork FROM T_Base_AGV_Gate WHERE  F_AGVGateDeviceIndex= " ).Append(devinfo.DeviceIndex );
                            dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                            if (dv1.Count > 0)
                            {
                                devinfo.DoubleFork = dv1[0]["F_UseAwayFork"].ToString();
                            }
                            else
                            {
                                devinfo.DoubleFork = "0";
                            }
                            break;
                        default:
                            devinfo.DoubleFork = "0";
                            devinfo.ForkAmount = 1;
                            devinfo.StackReach = 1;
                            devinfo.VirtualStack = 0;
                            devinfo.MutexStack =0;
                            devinfo.StackAbendOut = 0;
                            devinfo.StackZeroColLayer ="";
                            break;
                    }
                    if (dv[i]["F_IfClearDB1"] == DBNull.Value)
                    {
                        devinfo.IfClearDB1 = "0";
                    }
                    else
                    {
                        devinfo.IfClearDB1 = dv[i]["F_IfClearDB1"].ToString();
                    }
                    if (dv[i]["F_ScheduledDailyTaskCount"] == DBNull.Value)
                    {//20160501
                        devinfo.ScheduledDailyTaskCount = 320;
                    }
                    else
                    {
                        devinfo.ScheduledDailyTaskCount =Convert.ToInt32( dv[i]["F_ScheduledDailyTaskCount"]);
                    }
                    if (_DeviceInfo.ContainsKey(devinfo.DeviceIndex) == false)
                    {
                        _DeviceInfo.Add(devinfo.DeviceIndex, devinfo);
                    }

                }
            }
        }
        /// <summary>
        /// Ìí¼Ó¿ÉÒԸĵÀÉ豸ÐÅÏ¢
        /// </summary>
        public static void AddModifyPathDeviceInfo()
        {
            
            lock (lockThis)
            {

                ModifyPathDeviceInfo = new Dictionary<int, Model.MDevice>();
                sql.Remove(0, sql.Length);
                sql.Append("select * from T_Base_Device where (F_DeviceKindIndex=1 or F_DeviceKindIndex=2 or F_DeviceKindIndex=4) and F_DBW2Address is not null  order by F_DeviceIndex asc");//20130226richard.liu
                DataView dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                for (int i = 0; i < dv.Count; i++)
                {
                    Model.MDevice devinfo = new Model.MDevice();
                    if (dv[i]["F_AppendBarcode"] == DBNull.Value)
                    {
                        devinfo.AppendBarcode = 0;
                    }
                    else
                    {
                        devinfo.AppendBarcode = (int)dv[i]["F_AppendBarcode"];
                    }
                    if (dv[i]["F_BeDetected"] == DBNull.Value)
                    {
                        devinfo.BeDetected = null;
                    }
                    else
                    {
                        devinfo.BeDetected = dv[i]["F_BeDetected"].ToString();
                    }
                    if (dv[i]["F_BindingDevice"] == DBNull.Value)
                    {
                        devinfo.BindingDevice = null;
                    }
                    else
                    {
                        devinfo.BindingDevice = dv[i]["F_BindingDevice"].ToString();
                    }
                    if (dv[i]["F_BindingDeviceOut"] == DBNull.Value)
                    {
                        devinfo.BindingDeviceOut = null;
                    }
                    else
                    {
                        devinfo.BindingDeviceOut = dv[i]["F_BindingDeviceOut"].ToString();
                    }
                    if (dv[i]["F_CommType"] == DBNull.Value)
                    {
                        devinfo.CommType = null;
                    }
                    else
                    {
                        devinfo.CommType = dv[i]["F_CommType"].ToString();
                    }

                    if (dv[i]["F_DBW1Address"] == DBNull.Value)
                    {
                        devinfo.Dbw1Address = -1;
                    }
                    else
                    {
                        devinfo.Dbw1Address = (int)(dv[i]["F_DBW1Address"]);
                    }
                    if (dv[i]["F_DBWSendLength"] == DBNull.Value)
                    {
                        devinfo.Dbw1SendLength = 0;
                    }
                    else
                    {
                        devinfo.Dbw1SendLength = (int)dv[i]["F_DBWSendLength"];
                    }
                    if (dv[i]["F_DBW2Address"] == DBNull.Value)
                    {
                        devinfo.Dbw2Address = -1;
                    }
                    else
                    {
                        devinfo.Dbw2Address = (int)dv[i]["F_DBW2Address"];
                    }
                    if (dv[i]["F_DBWGetLength"] == DBNull.Value)
                    {
                        devinfo.Dbw2Getlength = 0;
                    }
                    else
                    {
                        devinfo.Dbw2Getlength = (decimal)dv[i]["F_DBWGetLength"];
                    }
                    if (dv[i]["F_DeviceIndex"] == DBNull.Value)
                    {
                        devinfo.DeviceIndex = 0;
                    }
                    else
                    {
                        devinfo.DeviceIndex = (int)dv[i]["F_DeviceIndex"];
                    }
                    if (dv[i]["F_DeviceKindIndex"] == DBNull.Value)
                    {
                        devinfo.DeviceKind = 0;
                    }
                    else
                    {
                        devinfo.DeviceKind = (int)dv[i]["F_DeviceKindIndex"];
                    }
                    if (dv[i]["F_DeviceName"] == DBNull.Value)
                    {
                        devinfo.DeviceName = null;
                    }
                    else
                    {
                        devinfo.DeviceName = (string)dv[i]["F_DeviceName"];
                    }
                    if (dv[i]["F_DeviceVisual"] == DBNull.Value)
                    {
                        devinfo.DeviceVisual = 0;
                    }
                    else
                    {
                        devinfo.DeviceVisual = (int)dv[i]["F_DeviceVisual"];
                    }
                    if (dv[i]["F_KeyDevice"] == DBNull.Value)
                    {
                        devinfo.IfKeyDevice = '0';

                    }
                    else
                    {
                        devinfo.IfKeyDevice = Convert.ToChar(dv[i]["F_KeyDevice"]);
                    }
                    if (dv[i]["F_MaxSendCount"] == DBNull.Value)
                    {
                        devinfo.MaxSendCount = 0;
                    }
                    else
                    {
                        devinfo.MaxSendCount = (int)dv[i]["F_MaxSendCount"];
                    }
                    if (dv[i]["F_NeedOptimize"] == DBNull.Value)
                    {
                        devinfo.NeedOptimize = '0';
                    }
                    else
                    {
                        devinfo.NeedOptimize = Convert.ToChar(dv[i]["F_NeedOptimize"]);
                    }
                    if (dv[i]["F_SendInterval"] == DBNull.Value)
                    {
                        devinfo.SendInterval = 0;
                    }
                    else
                    {
                        devinfo.SendInterval = (int)dv[i]["F_SendInterval"];
                    }
                    if (dv[i]["F_LocalIP"] == DBNull.Value)
                    {
                        devinfo.LocalIP = "";
                    }
                    else
                    {
                        devinfo.LocalIP = (string)dv[i]["F_LocalIP"];
                    }
                    if (dv[i]["F_LocalPort"] == DBNull.Value)
                    {
                        devinfo.LocalPort = 0;
                    }
                    else
                    {
                        devinfo.LocalPort = (int)dv[i]["F_LocalPort"];
                    }
                    if (dv[i]["F_RemoteIP"] == DBNull.Value)
                    {
                        devinfo.RemoteIP = "";
                    }
                    else
                    {
                        devinfo.RemoteIP = (string)dv[i]["F_RemoteIP"];
                    }
                    if (dv[i]["F_RemotePort"] == DBNull.Value)
                    {
                        devinfo.RemotePort = 0;
                    }
                    else
                    {
                        devinfo.RemotePort = (int)dv[i]["F_RemotePort"];
                    }
                    if (dv[i]["F_OPCProgID"] == DBNull.Value)
                    {
                        devinfo.OPCProgID = "";
                    }
                    else
                    {
                        devinfo.OPCProgID = (string)dv[i]["F_OPCProgID"];
                    }
                    if (dv[i]["F_ErrorTaskNo"] == DBNull.Value)
                    {
                        devinfo.ErrorTaskNo = 0;
                    }
                    else
                    {
                        devinfo.ErrorTaskNo = (int)dv[i]["F_ErrorTaskNo"];
                    }
                    if (dv[i]["F_SendOutDetect"] == DBNull.Value)
                    {
                        devinfo.SendOutDetect = "";
                    }
                    else
                    {
                        devinfo.SendOutDetect = dv[i]["F_SendOutDetect"].ToString();
                    }
                    //
                    if (dv[i]["F_OnlyDetectIO"] == DBNull.Value)
                    {
                        devinfo.OnlyDetectIO = "0";
                    }
                    else
                    {
                        devinfo.OnlyDetectIO = dv[i]["F_OnlyDetectIO"].ToString();
                    }
                    if (dv[i]["F_UseCommonDB"] == DBNull.Value)
                    {
                        devinfo.UseCommonDB = "0";
                    }
                    else
                    {
                        devinfo.UseCommonDB = dv[i]["F_UseCommonDB"].ToString();
                    }
                    if (dv[i]["F_IfCorrelDoubleFork"] == DBNull.Value)
                    {
                        devinfo.IfCorrelDoubleFork = "0";
                    }
                    else
                    {
                        devinfo.IfCorrelDoubleFork = dv[i]["F_IfCorrelDoubleFork"].ToString();
                    }

                    //if (dv[i]["F_DoubleFork"] == DBNull.Value)
                    //{
                    //    devinfo.DoubleFork = "0";
                    //}
                    //else
                    //{
                    //    devinfo.DoubleFork = dv[i]["F_DoubleFork"].ToString();
                    //}
                    //20100609
                    if (dv[i]["F_SerialPort"] == DBNull.Value)
                    {
                        devinfo.SerialPort = 0;
                    }
                    else
                    {
                        devinfo.SerialPort = Convert.ToInt32(dv[i]["F_SerialPort"]);
                    }
                    //20100609
                    if (dv[i]["F_CommSettings"] == DBNull.Value)
                    {
                        devinfo.CommSettings = "0";
                    }
                    else
                    {
                        devinfo.CommSettings = dv[i]["F_CommSettings"].ToString();
                    }
                    //20100714
                    if (dv[i]["F_S7Connection"] == DBNull.Value)
                    {
                        devinfo.S7Connection = "S7:[S7 connection_1]";
                    }
                    else
                    {
                        devinfo.S7Connection = dv[i]["F_S7Connection"].ToString();
                    }
                    //20101118
                    if (dv[i]["F_SplitByte"] == DBNull.Value)
                    {
                        devinfo.SplitByte = -1;
                    }
                    else
                    {
                        devinfo.SplitByte = Convert.ToInt32(dv[i]["F_SplitByte"]);
                    }
                    if (dv[i]["F_ManTaskReserve"] == DBNull.Value)
                    {
                        devinfo.ManTaskReserve = 0;
                    }
                    else
                    {
                        devinfo.ManTaskReserve = Convert.ToInt32(dv[i]["F_ManTaskReserve"]);
                    }
                    if (dv[i]["F_HaveGoods"] == DBNull.Value)
                    {
                        devinfo.LogicHaveGoods = false;
                    }
                    else
                    {
                        devinfo.LogicHaveGoods = Convert.ToInt32(dv[i]["F_HaveGoods"])>0 ? true : false;
                    }
                    if (dv[i]["F_ReadSubscription"] == DBNull.Value)
                    {
                        devinfo.ReadSubscription = "0";
                    }
                    else
                    {
                        devinfo.ReadSubscription = dv[i]["F_ReadSubscription"].ToString();
                    }
                    if (dv[i]["F_UnControl"] == DBNull.Value)
                    {
                        devinfo.UnControl = "0";
                    }
                    else
                    {
                        devinfo.UnControl = dv[i]["F_UnControl"].ToString();
                    }
                    if (ModifyPathDeviceInfo.ContainsKey(devinfo.DeviceIndex) == false)
                    {
                        ModifyPathDeviceInfo.Add(devinfo.DeviceIndex, devinfo);
                    }

                }
            }
        }
        public static Model.MDevice GetModifyPathDeviceInfo(int DeviceIndex)
        {
            //20091102
            
            lock (lockThis)
            {

                if (ModifyPathDeviceInfo.ContainsKey(DeviceIndex) == true)
                {
                    Model.MDevice md = ModifyPathDeviceInfo[DeviceIndex];
                    return md;
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// ¸üÐÂÄÚ´æÖеÄÉ豸¿É±äÐÅÏ¢£º¹ÊÕÏÂ룬ÊÇ·ñÓÐÎÔËÐÐ״̬£¬ÈÎÎñºÅ£¬µ±Ç°X¡¢Y×ø±ê£¬¿ØÖÆ·½Ê½
        /// </summary>
        /// <param name="deviceinfo"></param>
        public static void SetModifyPathDeviceInfo(Model.MDevice deviceinfo)
        {
            
            lock (lockThis)
            {

                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].ErrorCode = deviceinfo.ErrorCode;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].HaveGoods = deviceinfo.HaveGoods;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].RunState = deviceinfo.RunState;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].TaskNo = deviceinfo.TaskNo;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].XCoor = deviceinfo.XCoor;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].YCoor = deviceinfo.YCoor;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].ControlMode = deviceinfo.ControlMode;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].Barcode = deviceinfo.Barcode;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].ArrowLocation = deviceinfo.ArrowLocation;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].HaveGoods = deviceinfo.HaveGoods;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].LogicHaveGoods = deviceinfo.LogicHaveGoods;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].SplitByte_0 = deviceinfo.SplitByte_0;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].SplitByte_1 = deviceinfo.SplitByte_1;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].SplitByte_2 = deviceinfo.SplitByte_2;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].SplitByte_3 = deviceinfo.SplitByte_3;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].SplitByte_4 = deviceinfo.SplitByte_4;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].SplitByte_5 = deviceinfo.SplitByte_5;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].SplitByte_6 = deviceinfo.SplitByte_6;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].SplitByte_7 = deviceinfo.SplitByte_7;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].SendMessage = deviceinfo.SendMessage;
                ModifyPathDeviceInfo[deviceinfo.DeviceIndex].ReturnMessage = deviceinfo.ReturnMessage;
            }
        }
        /// <summary>
        /// ͨ¹ýÉ豸Ë÷Òý·´À¡É豸ÐÅÏ¢
        /// </summary>
        /// <param name="DeviceIndex">É豸Ë÷Òý</param>
        /// <returns></returns>
        public static Model.MDevice GetDeviceInfo(int DeviceIndex)
        {
            //20091102
            
            lock (lockThis)
            {

                if (_DeviceInfo.ContainsKey(DeviceIndex) == true)
                {
                    return _DeviceInfo[DeviceIndex];
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// ¸üÐÂÄÚ´æÖеÄÉ豸¿É±äÐÅÏ¢£º¹ÊÕÏÂ룬ÊÇ·ñÓÐÎÔËÐÐ״̬£¬ÈÎÎñºÅ£¬µ±Ç°X¡¢Y×ø±ê£¬¿ØÖÆ·½Ê½
        /// </summary>
        /// <param name="deviceinfo"></param>
        public static void SetDeviceInfo(Model.MDevice deviceinfo)
        {
           
            lock (lockThis)
            {

                _DeviceInfo[deviceinfo.DeviceIndex].ErrorCode = deviceinfo.ErrorCode;
                _DeviceInfo[deviceinfo.DeviceIndex].HaveGoods = deviceinfo.HaveGoods;
                _DeviceInfo[deviceinfo.DeviceIndex].RunState = deviceinfo.RunState;
                _DeviceInfo[deviceinfo.DeviceIndex].TaskNo = deviceinfo.TaskNo;
                _DeviceInfo[deviceinfo.DeviceIndex].XCoor = deviceinfo.XCoor;
                _DeviceInfo[deviceinfo.DeviceIndex].YCoor = deviceinfo.YCoor;
                _DeviceInfo[deviceinfo.DeviceIndex].ControlMode = deviceinfo.ControlMode;
                _DeviceInfo[deviceinfo.DeviceIndex].Barcode = deviceinfo.Barcode;
                _DeviceInfo[deviceinfo.DeviceIndex].ArrowLocation = deviceinfo.ArrowLocation;
                _DeviceInfo[deviceinfo.DeviceIndex].HaveGoods = deviceinfo.HaveGoods;
                _DeviceInfo[deviceinfo.DeviceIndex].LogicHaveGoods = deviceinfo.LogicHaveGoods;
                _DeviceInfo[deviceinfo.DeviceIndex].SplitByte_0 = deviceinfo.SplitByte_0;
                _DeviceInfo[deviceinfo.DeviceIndex].SplitByte_1 = deviceinfo.SplitByte_1;
                _DeviceInfo[deviceinfo.DeviceIndex].SplitByte_2 = deviceinfo.SplitByte_2;
                _DeviceInfo[deviceinfo.DeviceIndex].SplitByte_3 = deviceinfo.SplitByte_3;
                _DeviceInfo[deviceinfo.DeviceIndex].SplitByte_4 = deviceinfo.SplitByte_4;
                _DeviceInfo[deviceinfo.DeviceIndex].SplitByte_5 = deviceinfo.SplitByte_5;
                _DeviceInfo[deviceinfo.DeviceIndex].SplitByte_6 = deviceinfo.SplitByte_6;
                _DeviceInfo[deviceinfo.DeviceIndex].SplitByte_7 = deviceinfo.SplitByte_7;
                _DeviceInfo[deviceinfo.DeviceIndex].SendMessage = deviceinfo.SendMessage;
                _DeviceInfo[deviceinfo.DeviceIndex].ReturnMessage = deviceinfo.ReturnMessage;
            }
        }
        public static void SetDeviceMessage(Model.MDevice deviceinfo)
        {
            
            lock (lockThis)
            {

                _DeviceInfo[deviceinfo.DeviceIndex].ReturnMessage = deviceinfo.ReturnMessage;
            }
        }

        static Dictionary<string, Model.MIOControlStatus> _IOControlStatus;
        static Dictionary<string, Model.MDeviceKind> _DeviceKind;
        /// <summary>
        /// ¼ÓÔØµ½ÄÚ´æÖеÄÓë¹ÜÀíÈÎÎñ½»»¥µÄµ÷¶ÈÈÎÎñ״̬ÐÅÏ¢¼¯ºÏ
        /// </summary>
        public static Dictionary<string, Model.MIOControlStatus> IOControlStatus
        {
            get { return CGetInfo._IOControlStatus; }
            set { CGetInfo._IOControlStatus = value; }
        }
        public static void AddIOControlStatus()
        {
           
            lock (lockThis)
            {

                _IOControlStatus = new Dictionary<string, Model.MIOControlStatus>();
                DataView dv = dbo.ExceSQL("SELECT * FROM T_Base_IOControlStatus ").Tables[0].DefaultView;
                for (int i = 0; i < dv.Count; i++)
                {

                    Model.MIOControlStatus ioStatus = new Model.MIOControlStatus();
                    ioStatus.StatusCode = dv[i]["F_StatusCode"].ToString();
                    if (dv[i]["F_StatusID"] == DBNull.Value)
                    {
                        ioStatus.StatusID = 0;
                    }
                    else
                    {
                        ioStatus.StatusID = Convert.ToInt32(dv[i]["F_StatusID"]);
                    }
                    if (dv[i]["F_Remark"] == DBNull.Value)
                    {
                        ioStatus.Remark = "-";
                    }
                    else
                    {
                        ioStatus.Remark = dv[i]["F_Remark"].ToString();
                    }
                    if (dv[i]["F_ForkNo"] == DBNull.Value)
                    {
                        ioStatus.ForkNo = "-";
                    }
                    else
                    {
                        ioStatus.ForkNo = dv[i]["F_ForkNo"].ToString();
                    }
                    if (_IOControlStatus.ContainsKey(ioStatus.StatusCode) == false)
                    {
                        _IOControlStatus.Add(ioStatus.StatusCode, ioStatus);
                    }

                }
            }
        }
        public static Model.MIOControlStatus GetIOControlStatus(string StatusCode)
        {
            
            lock (lockThis)
            {

                if (_IOControlStatus.ContainsKey(StatusCode) == true)
                {
                    return _IOControlStatus[StatusCode];
                }
                else
                {
                    return null;
                }
            }
        }
        public static Model.MIOControlStatus GetIOControlStatus(int errid)
        {

            lock (lockThis)
            {
                DataView dv = dbo.ExceSQL(string.Format("SELECT F_StatusCode FROM T_Base_IOControlStatus where F_StatusID={0}",errid)).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    if (_IOControlStatus.ContainsKey(dv[0]["F_StatusCode"].ToString()) == true)
                    {
                        return _IOControlStatus[dv[0]["F_StatusCode"].ToString()];
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
        }
        public static Model.MDeviceKind GetDeviceKind(string DeviceKindCode)
        {
            
            lock (lockThis)
            {

                if (_DeviceKind.ContainsKey(DeviceKindCode) == true)
                {
                    return _DeviceKind[DeviceKindCode];
                }
                else
                {
                    return null;
                }
            }
        }
        public static void AddDeviceKindInfo()
        {
            
            lock (lockThis)
            {

                _DeviceKind = new Dictionary<string, Model.MDeviceKind>();
                DataView dv = dbo.ExceSQL("SELECT * FROM T_Base_Device_Kind ").Tables[0].DefaultView;
                for (int i = 0; i < dv.Count; i++)
                {

                    Model.MDeviceKind dk = new Model.MDeviceKind();
                    dk.DeviceKindCode = dv[i]["F_DeviceKindCode"].ToString();
                    if (dv[i]["F_DeviceKindIndex"] == DBNull.Value)
                    {
                        dk.DeviceKindIndex = 0;
                    }
                    else
                    {
                        dk.DeviceKindIndex = Convert.ToInt32(dv[i]["F_DeviceKindIndex"]);
                    }
                    if (dv[i]["F_GoodsMoveKindIndex"] == DBNull.Value)
                    {
                        dk.GoodsMoveKindIndex = 0;
                    }
                    else
                    {
                        dk.GoodsMoveKindIndex = Convert.ToInt32(dv[i]["F_GoodsMoveKindIndex"]);
                    }

                    if (dv[i]["F_DeviceKindName"] == DBNull.Value)
                    {
                        dk.DeviceKindName = "-";
                    }
                    else
                    {
                        dk.DeviceKindName = dv[i]["F_DeviceKindName"].ToString();
                    }
                    if (_DeviceKind.ContainsKey(dk.DeviceKindCode) == false)
                    {
                        _DeviceKind.Add(dk.DeviceKindCode, dk);
                    }

                }
            }
        }
    }
}