using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace DoWhatPlc
{
    public class EngineValue
    {
        public string OpName { set; get; }
        public string EngineID { set; get; }
        public string EngineType { set; get; }
        public int EngineTypeID { set; get; }
        public int PalletCode { set; get; }
        public int ProcessCode { set; get; }
        public int PartPalletCode { set; get; }
        public string PalletInfo { set; get; }
        /// <summary>
        /// 显示返修标志
        /// </summary>
        public int RepairPartStatusPLC { set; get; }
        /// <summary>
        /// 1 合格；2 不合格
        /// </summary>
        public int QualityMask { set; get; }

        public string OrderForm { set; get; }
    }
    public class DoWhat
    {
        public const string ReadPLC_TagID = "ReadPLC_TagID";
        public const string WritePLC_TagID = "WritePLC_TagID";
        public const string ReadPLC_TagTypeCodeID = "ReadPLC_TagTypeCodeID";
        public const string WritePLC_TagTypeCodeID = "WritePLC_TagTypeCodeID";
        public const string ReadPLC_Cmd = "ReadPLC_Cmd";
        public const string WritePLC_Cmd = "WritePLC_Cmd";
        public const string ReadPLC_GroupData = "ReadPLC_GroupData";
        public const string ReadPLC_GroupMonitor = "ReadPLC_GroupMonitor";
        public const string ReadPLC_IpTag = "ReadPLC_IpTag";
        public const string ReadPLC_IpMonitorTag = "ReadPLC_IpMonitorTag";
        public const string ReadQualityDataList = "ReadQualityDataList";
        public const string ReadByteData_Op = "ReadByteData_Op";
        public const string ReadByteData_Ip = "ReadByteData_Ip";
        public const string WritePLC_Bytes_Op = "WritePLC_Bytes_Op";
        public const string WritePLC_Bytes_Ip = "WritePLC_Bytes_Ip";
        public const string IpAllTag = "IpAllTag";
        public const string IpMonitorTag = "IpMonitorTag";
        public const string RegionRead = "RegionRead";
        public const string ReadBytes = "ReadBytes";
        public const string WriteBytes = "WriteBytes";
    }
    /// <summary>
    /// object 类型数据转换成字符串变量表达
    /// </summary>
    public class ReturnValueString
    {
        public string TagID { set; get; }
        public string TagValue { set; get; }
        public string ValueType { set; get; }
        public string OpName { set; get; }
        public string TimeStamp { set; get; }
        public string TagTypeCodeID { set; get; }
        public string TagTypeID { set; get; }
        /// <summary>
        /// 测量项目   TEXT2 报警的 工步内容
        /// </summary>
        public string ItemName { set; get; }
        /// <summary>
        /// 测量位置 TEXT1 报警的 工步号
        /// </summary>
        public string ShaftID { set; get; }
        /// <summary>
        /// 列位置  工步  工位工步分类
        /// </summary>
        public string TagOrderBy { set; get; }

        
        public string TagValueBefore { get; set; }
        public string TimeStampBefore { get; set; }
        public string TagName { get; set; }


        /// <summary>
        /// 变量变化的持续时间，单位 毫秒
        /// </summary>
        public string TagKeepTime;
        public string FunName;
        public string FunCmd;
        public string MesServerCode;
        public string CmdPlcReadWrite;
        public ReturnValueString()
        {

        }
        public ReturnValueString(string tagID, object tagValue, string opName = "", string tagTypeCodeID = "", string tagTypeID = "", string tagKeepTime = "",
            string funName = "", string funCmd = "", string mesServerCode = "", string timeStamp = "", 
            string timeStampBefore = "", string tagValueBefore = "", string tagName = "",string  itemName="", 
            string shaftID = "", string tagOrderBy = "")
        {
            TagTypeCodeID = tagTypeCodeID;
            TagTypeID = tagTypeID;
            TagValue = tagValue.ToString();
            ValueType = tagValue.GetType().Name;
            TagID = tagID;
            OpName = opName;
            TagKeepTime = tagKeepTime;
            FunName = funName;
            FunCmd = funCmd;
            MesServerCode = mesServerCode;
            TimeStamp = timeStamp;
            TimeStampBefore = timeStampBefore;
            TagName = tagName;
            TagValueBefore = tagValueBefore;
            ItemName = itemName;
            ShaftID = shaftID;
            TagOrderBy = tagOrderBy;
        }

        public ReturnValueObject ReturnValueToObject()
        {
            ReturnValueObject ro = new ReturnValueObject(TagID, TagValue, OpName, TagTypeCodeID, ValueType, TimeStamp, TimeStampBefore, TagValueBefore, TagName, ItemName, ShaftID);

            return ro;
        }

    }
    /// <summary>
    /// 字符串变量转变成object类型变量
    /// </summary>
    public class ReturnValueObject
    {
        public string TagID { set; get; }
        public object TagValue { set; get; }
        public string ValueType { set; get; }
        public string OpName { set; get; }
        public DateTime TimeStamp { set; get; }
        public string TagTypeCodeID { set; get; }
        /// <summary>
        /// 变量变化的持续时间，单位 毫秒
        /// </summary>
        public int TagKeepTime;

        public object TagValueBefore { get; set; }
        public DateTime TimeStampBefore { get; set; }
        public string TagName { get; set; }
        public string ItemName { set; get; }
        public string ShaftID { set; get; }
        public string TagOrderBy { set; get; }
        public ReturnValueObject(string tagID, string tagValue, string opName = "", string tagTypeCodeID = "", string valueType = "", 
            string timeStamp = "", string timeStampBefore = "", string tagValueBefore = "", 
            string tagName = "", string itemName = "", string shaftID = "", string tagOrderBy = "")
        {
            TagValue = GetTypeValue(valueType, tagValue);
            if (tagValueBefore != null)
            {
                TagValueBefore = GetTypeValue(valueType, tagValueBefore);
            }
            ValueType = valueType;
            TagID = tagID;
            OpName = opName;
            try
            {
                TimeStamp = Convert.ToDateTime(timeStamp);
            }
            catch
            {
                TimeStamp = DateTime.Now;

            }
            try
            {
                TimeStampBefore = Convert.ToDateTime(timeStampBefore);
            }
            catch
            {
                TimeStampBefore = DateTime.Now;

            }
            TagTypeCodeID = tagTypeCodeID;
            TagName = tagName;
            ItemName = itemName;
            ShaftID = shaftID;
            TagOrderBy = tagOrderBy;

        }
       

        public ReturnValueString ReturnValueToString()
        {
            ReturnValueString rs = new ReturnValueString(TagID, TagValue, OpName, TagTypeCodeID);
            return rs;
        }
        public static object GetTypeValue(string type, string tagValue)
        {
            object tagV = "-1";
            if (type == null)
            {
                tagV = tagValue;
            }
            switch (type.ToLower())
            {
                case "bool":
                case "boolean":
                    tagV = false;
                    switch (tagValue.ToLower())
                    {
                        case "1":
                        case "true":
                            tagV = true;
                            break;
                        case "0":
                        case "false":
                            tagV = false;
                            break;
                    }
                    return tagV;
                //return Type.GetType(" System.Boolean ", true, true);
                case "byte ":
                    return Convert.ToByte(tagValue);
                case "sbyte":
                    return Convert.ToSByte(tagValue);
                case "char":
                    return Convert.ToChar(tagValue);
                case "decimal":
                    return Convert.ToDecimal(tagValue);
                case "double":
                    return Convert.ToDouble(tagValue);
                case "float":
                    return Convert.ToSingle(tagValue);
                case "int":
                    return Convert.ToInt32(tagValue);
                case "int16":
                    return Convert.ToInt32(tagValue);
                case "uint":
                    return Convert.ToUInt32(tagValue);
                case "long":
                    return Convert.ToInt64(tagValue);
                case "ulong":
                    return Convert.ToUInt64(tagValue);
                case "object":
                    return tagValue;
                case "short":
                    return Convert.ToInt16(tagValue);
                case "ushort":
                    return Convert.ToUInt16(tagValue);
                case "string":
                    return tagValue;
                case "date":
                case "datetime":
                    return Convert.ToDateTime(tagValue);
                case "guid":
                    return tagValue;
                default:
                    return tagValue;
            }
        }


    }

    public class TagIDTagFormat
    {
        public string TagID;
        public TagFormat TF;

        public TagIDTagFormat(string tagID, TagFormat tF)
        {
            TagID = tagID;
            TF = tF;
        }
    }


    public class QualityDataSaveList
    {
        public Hashtable valuList;
        public DataTable BasicDataTagValue;
        public DataTable BasicDataTagValueUpDown;
        public Hashtable hashtable_EngineTypeID;
        public Hashtable hashtable_tagTable_OpName_SaveData;
    }



    public struct ControlData
    {
        public int TagType;
        public string opName;
        public Control btn; public ControlData(Control Ctrl, string OpName)
        {
            string[] Array_NameSplit;
            Array_NameSplit = Ctrl.Name.Split('_');
            TagType = Convert.ToInt32(Array_NameSplit[2]);
            opName = OpName;
            btn = Ctrl;

        }
    }
    public struct ControlChangeByTagType
    {
        public ControlChangeByTagType(int tagType, string value)
        {
            TagType = tagType;
            Value = value;
        }

        public int TagType;
        public string Value;
    }
    /// <summary>
    /// tag格式化结构
    /// </summary>
    public struct TagFormat
    {
        /// <summary>
        /// TagID
        /// </summary>
        public string TagID;
        /// <summary>
        /// TagID
        /// </summary>
        public string TagName;
        /// <summary>
        /// 工位号
        /// </summary>
        public string OpName;
        /// <summary>
        /// 数据来源
        /// </summary>
        public string DataSources;
        /// <summary>
        /// IP
        /// </summary>
        public string IP;
        /// <summary>
        /// OPC的URL
        /// </summary>
        public string OpcIpPort;
        /// <summary>
        /// 组
        /// </summary>
        public string Group;
        /// <summary>
        /// 监控时间间隔
        /// </summary>
        public string MonitorInterval;
        /// <summary>
        /// DB号加起始地址，DB1001.30
        /// </summary>
        public string TagDbAndStartAdr;
        /// <summary>
        /// DB号加起始地址，DB1001.30.1,为了bool写入专用
        /// </summary>
        public string TagDbAndStartAdrWrite;
        /// <summary>
        /// OPC协议地址
        /// 例如：S7:[192.168.1.211|VFD1|S7ONLINE|01.00,192.168.1.211,02.02,1]DB1001,DINT154
        /// </summary>
        public string TagOPCAdrStr;
        /// <summary>
        /// DB地址位置
        /// 变量起始地址，例如  0，1，2，56，80...
        /// </summary>
        public int Index;
        /// <summary>
        /// 变量字节长度
        /// 变量字节长度,l例如  1，2，4...       
        /// </summary>
        public int Length;
        /// <summary>
        /// X  0,1,2,3,4,5,6,7
        /// 例如  X  0,1,2,3,4,5,6,7
        /// </summary>
        public int BitIndex;
        /// <summary>
        /// 以连续Byte数组方式读取的DB地址实际位置
        /// 变量起始地址，例如  0，1，2，56，80...
        /// </summary>
        public int IndexBytes;
        /// <summary>
        /// 绝对地址差分，应用区域读取的拆分20200101_LLX
        /// </summary>
        public int AbsAddrDiff;



        /// <summary>
        /// 点类型代码
        /// 比如bool是11，String是8
        /// </summary>
        public int TagTypeID;
        /// <summary>
        /// 点类型
        /// 比如Bool,INT,STRING等
        /// </summary>
        public string TagType;
        /// <summary>
        /// 变量类型代码
        /// 比如：允许保存是19
        /// </summary>
        public int TagTypeCodeID;
        /// <summary>
        /// Value
        /// </summary>
        public object TagValue;
        /// <summary>
        /// 是否监控
        /// </summary>
        public int IsMonitor;





        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime TimeStamp;
        /// <summary>
        /// 标签质量  192 , -1    
        /// </summary>
        public string TagQuality;
        /// <summary>
        /// 测量位置
        /// </summary>
        public string ShaftID;
        /// <summary>
        /// 测量项目   
        /// </summary>
        public string ItemName;
        /// <summary>
        /// 测量单位
        /// </summary>
        public string Unit;
        /// <summary>
        /// 上限值
        /// </summary>
        public string TagValueUp;
        /// <summary>
        /// 下限值
        /// </summary>
        public string TagValueDown;
        /// <summary>
        /// 理论值
        /// </summary>
        public string TagValeNormal;
        /// <summary>
        /// 合格标志
        /// </summary>
        public string IsGoodBad;
        /// <summary>
        /// 列位置
        /// </summary>
        public string TagOrderBy;
        /// <summary>
        /// 变量排序
        /// </summary>
        public string TagItemOrderBy;
        /// <summary>
        /// 是保存到数据库
        /// </summary>
        public int IsSaveToSql;
        /// <summary>
        /// 是发送到WebSocket
        /// </summary>
        public int IsWebSocket;
        /// <summary>
        /// 是程序启动时候的值
        /// </summary>
        public int IsFirstValue;
        /// <summary>
        /// 变量特点
        /// </summary>
        public int TagProperty;
        /// <summary>
        /// 变量变化的持续时间，单位 毫秒
        /// </summary>
        public int TagKeepTime;
        /// <summary>
        /// 变量变化前的值
        /// </summary>
        public object TagValueBefore;

    }

    public class CustomeEvetnArgs : EventArgs
    {
        public int TagKeepTime { get; set; }
        public object TagProperty { get; set; }
        public object TagItemOrderBy { get; set; }
        public object TagOrderBy { get; set; }
        public object IsGoodBad { get; set; }
        public object TagValeNormal { get; set; }
        public object TagValueDown { get; set; }
        public object TagValueUp { get; set; }
        public object Unit { get; set; }
        public object ItemName { get; set; }
        public object ShaftID { get; set; }
        public object IsMonitor { get; set; }
        public object TagTypeActual { get; set; }
        public object TagValueBefore { get; set; }
        public object IsFirstValue { get; set; }
        public object IsSaveToSql { get; set; }
        public object DataDources { get; set; }
        public object TagTypeID { get; set; }
        public object TagTypeCodeID { get; set; }
        public object TagID { get; set; }
        public string TimeStamp { get; set; }
        public object TagQuality { get; set; }
        public object OpName { get; set; }
        public object TagValue { get; set; }
        public object TagValueType { get; set; }
        public object TagType { get; set; }
        public object TagName { get; set; }
        public object TagKey { get; set; }
        public object IsWebSocket { get; set; }
        public string TimeStampBefore { get; set; }
    }

    public class QualityDataType
    {
        public int tagValueType;
        public int tagTypeID;
        public string opName;
        public string tagID;
        public object tagValue;
        public int tagQuality;
        public int tagOrderBy;
        public int tagItemOrderBy;
        public int tagProperty;
        public int isGoodBad;
    }

    public class HashtableList
    {
        /// <summary>
        /// MesServerCode
        /// </summary>
        static public Hashtable hashtable_MesServerCode_Temp;
        /// <summary>
        /// MesDll
        /// </summary>
        static public Hashtable hashtable_MesDll_Temp;
        static public Hashtable hashtable_TagTypeDataType_Temp;
        static public Hashtable hashtable_TagType_Temp;

        static public Hashtable hashtable_EngineGetOver_EngineTypeID_Temp;
        static public Hashtable hashtable_EngineGetOver_EngineType_Temp;
        static public Hashtable hashtable_EngineGetOver_EngineID_Temp;
        static public Hashtable hashtable_EngineGetOver_OrderForm_Temp;

        static public Hashtable hashtable_OpNameRepair_Temp;

        static public Hashtable hashtable_WorkGroupIndex_Temp;
        static public Hashtable hashtable_workStepProperty_Temp;
        static public Hashtable hashtable_QualityMesRead_Temp;
        static public Hashtable hashtable_IsAllowWork_Temp;
        /// <summary>
        /// 工位号与工位上下线类型
        /// （0普通工位）（1正常上线）（2正常下线）（3返修上线）（4返修下线）
        /// 5 分线上线（总线） 6 分线上线（分线）
        /// </summary>
        static public Hashtable hashtableOpName_OpType_Temp;

        /// <summary>
        /// <summary>
        /// 工位号与工位上下线类型
        /// （0普通工位）（1正常上线）（2正常下线）（3返修上线）（4返修下线）
        /// 5 分线上线（总线） 6 分线上线（分线）        
        /// </summary>
        public Hashtable hashtableOpName_OpType;
        public Hashtable hashtable_IsAllowWork;
        public Hashtable hashtable_QualityMesRead;
        public Hashtable hashtable_workStepProperty;
        public Hashtable hashtable_OpNameRepair;
        /// <summary>
        /// MesServerCode
        /// </summary>
        public Hashtable hashtable_MesServerCode;
        /// <summary>
        /// MesDll
        /// </summary>
        public Hashtable hashtable_MesDll;
        public Hashtable hashtable_TagTypeDataType;
        public Hashtable hashtable_TagType;


        public Hashtable hashtable_EngineGetOver_EngineTypeID;
        public Hashtable hashtable_EngineGetOver_EngineType;
        public Hashtable hashtable_EngineGetOver_EngineID;
        public Hashtable hashtable_EngineGetOver_OrderForm;

        public Hashtable hashtable_WorkGroupIndex;
    }

    public struct QualityDataQueryStruct
    {

        public QualityDataQueryStruct(string opName, Control ctrl)
        {
            OpName = opName;
            Ctrl = ctrl;
            tagType = 0;
            tagValue = "";
            tagID = "";
            tagTypeID = 0;
            tempForm = null;
            tagValue2 = null;

        }

        public QualityDataQueryStruct(string opName, int TagType)
        {

            OpName = opName;
            Ctrl = null;
            tagType = TagType;
            tagValue = "";
            tagID = "";
            tagTypeID = 0;
            tempForm = null;
            tagValue2 = null;
        }

        public QualityDataQueryStruct(string opName, int TagType, Control ctrl)
        {

            OpName = opName;
            Ctrl = ctrl;
            tagType = TagType;
            tagValue = "";
            tagID = "";
            tagTypeID = 0;
            tempForm = null;
            tagValue2 = null;
        }
        public QualityDataQueryStruct(string opName, int TagType, object TagValue)
        {

            OpName = opName;
            Ctrl = null;
            tagType = TagType;
            tagValue = TagValue;
            tagID = "";
            tagTypeID = 0;
            tempForm = null;
            tagValue2 = null;
        }
        public QualityDataQueryStruct(string TagID, int TagTypeID, object TagValue, object TagValue2, Form TempForm)
        {

            OpName = "";
            Ctrl = null;
            tagType = 0;
            tagValue = TagValue;
            tagID = TagID;
            tagTypeID = TagTypeID;
            tempForm = TempForm;
            tagValue2 = TagValue2;
        }


        public string OpName;
        public Control Ctrl;
        public int tagType;
        public object tagValue;
        public object tagValue2;
        public string tagID;
        public int tagTypeID;
        public Form tempForm;


    }
}



namespace DoWhatPlc1
{
    public class EngineValue
    {
        public string OpName { set; get; }
        public string EngineID { set; get; }
        public string EngineType { set; get; }
        public int EngineTypeID { set; get; }
        public int PalletCode { set; get; }
        public int ProcessCode { set; get; }
        public int PartPalletCode { set; get; }
        public string PalletInfo { set; get; }
        /// <summary>
        /// 显示返修标志
        /// </summary>
        public int RepairPartStatusPLC { set; get; }
        /// <summary>
        /// 1 合格；2 不合格
        /// </summary>
        public int QualityMask { set; get; }

        public string OrderForm { set; get; }
    }
    public class DoWhat
    {
        public const string ReadPLC_TagID = "ReadPLC_TagID";
        public const string WritePLC_TagID = "WritePLC_TagID";
        public const string ReadPLC_TagTypeCodeID = "ReadPLC_TagTypeCodeID";
        public const string WritePLC_TagTypeCodeID = "WritePLC_TagTypeCodeID";
        public const string ReadPLC_Cmd = "ReadPLC_Cmd";
        public const string WritePLC_Cmd = "WritePLC_Cmd";
        public const string ReadPLC_GroupData = "ReadPLC_GroupData";
        public const string ReadPLC_GroupMonitor = "ReadPLC_GroupMonitor";
        public const string ReadPLC_IpTag = "ReadPLC_IpTag";
        public const string ReadPLC_IpMonitorTag = "ReadPLC_IpMonitorTag";
        public const string ReadQualityDataList = "ReadQualityDataList";
        public const string ReadByteData_Op = "ReadByteData_Op";
        public const string ReadByteData_Ip = "ReadByteData_Ip";
        public const string WritePLC_Bytes_Op = "WritePLC_Bytes_Op";
        public const string WritePLC_Bytes_Ip = "WritePLC_Bytes_Ip";
        public const string IpAllTag = "IpAllTag";
        public const string IpMonitorTag = "IpMonitorTag";
        public const string RegionRead = "RegionRead";
        public const string ReadBytes = "ReadBytes";
        public const string WriteBytes = "WriteBytes";
    }
    /// <summary>
    /// object 类型数据转换成字符串变量表达
    /// </summary>
    public class ReturnValueString
    {
        public string TagID { set; get; }
        public string TagValue { set; get; }
        public string ValueType { set; get; }
        public string OpName { set; get; }
        public string TimeStamp { set; get; }
        public string TagTypeCodeID { set; get; }
        public string TagTypeID { set; get; }
        /// <summary>
        /// 变量变化的持续时间，单位 毫秒
        /// </summary>
        public string TagKeepTime;
        public string FunName;
        public string FunCmd;
        public string MesServerCode;
        public string CmdPlcReadWrite;
        public ReturnValueString()
        {

        }
        public ReturnValueString(string tagID, object tagValue, string opName = "", string tagTypeCodeID = "", string tagTypeID = "", string tagKeepTime = "", string funName = "", string funCmd = "", string mesServerCode = "")
        {
            TagTypeCodeID = tagTypeCodeID;
            TagTypeID = tagTypeID;
            TagValue = tagValue.ToString();
            ValueType = tagValue.GetType().Name;
            TagID = tagID;
            OpName = opName;
            TagKeepTime = tagKeepTime;
            FunName = funName;
            FunCmd = funCmd;
            MesServerCode = mesServerCode;
            TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
        }

        public ReturnValueObject ReturnValueToObject()
        {
            ReturnValueObject ro = new ReturnValueObject(TagID, TagValue, OpName, TagTypeCodeID, ValueType, TimeStamp);

            return ro;
        }

    }
    /// <summary>
    /// 字符串变量转变成object类型变量
    /// </summary>
    public class ReturnValueObject
    {
        public string TagID { set; get; }
        public object TagValue { set; get; }
        public string ValueType { set; get; }
        public string OpName { set; get; }
        public DateTime TimeStamp { set; get; }
        public string TagTypeCodeID { set; get; }
        /// <summary>
        /// 变量变化的持续时间，单位 毫秒
        /// </summary>
        public int TagKeepTime;

        public ReturnValueObject(string tagID, string tagValue, string opName = "", string tagTypeCodeID = "", string valueType = "", string timeStamp = "")
        {
            TagValue = GetTypeValue(valueType, tagValue);
            ValueType = valueType;
            TagID = tagID;
            OpName = opName;
            try
            {
                TimeStamp = Convert.ToDateTime(timeStamp);
            }
            catch
            {
                TimeStamp = DateTime.Now;

            }
            TagTypeCodeID = tagTypeCodeID;

        }

        public ReturnValueString ReturnValueToString()
        {
            ReturnValueString rs = new ReturnValueString(TagID, TagValue, OpName, TagTypeCodeID);
            return rs;
        }
        public static object GetTypeValue(string type, string tagValue)
        {
            object tagV = "-1";
            if (type == null)
            {
                tagV = tagValue;
            }
            switch (type.ToLower())
            {
                case "bool":
                case "boolean":
                    tagV = false;
                    switch (tagValue.ToLower())
                    {
                        case "1":
                        case "true":
                            tagV = true;
                            break;
                        case "0":
                        case "false":
                            tagV = false;
                            break;
                    }
                    return tagV;
                //return Type.GetType(" System.Boolean ", true, true);
                case "byte ":
                    return Convert.ToByte(tagValue);
                case "sbyte":
                    return Convert.ToSByte(tagValue);
                case "char":
                    return Convert.ToChar(tagValue);
                case "decimal":
                    return Convert.ToDecimal(tagValue);
                case "double":
                    return Convert.ToDouble(tagValue);
                case "float":
                    return Convert.ToSingle(tagValue);
                case "int":
                    return Convert.ToInt32(tagValue);
                case "int16":
                    return Convert.ToInt32(tagValue);
                case "uint":
                    return Convert.ToUInt32(tagValue);
                case "long":
                    return Convert.ToInt64(tagValue);
                case "ulong":
                    return Convert.ToUInt64(tagValue);
                case "object":
                    return tagValue;
                case "short":
                    return Convert.ToInt16(tagValue);
                case "ushort":
                    return Convert.ToUInt16(tagValue);
                case "string":
                    return tagValue;
                case "date":
                case "datetime":
                    return Convert.ToDateTime(tagValue);
                case "guid":
                    return tagValue;
                default:
                    return tagValue;
            }
        }


    }

    public class TagIDTagFormat
    {
        public string TagID;
        public TagFormat TF;

        public TagIDTagFormat(string tagID, TagFormat tF)
        {
            TagID = tagID;
            TF = tF;
        }
    }


    public class QualityDataSaveList
    {
        public Hashtable valuList;
        public DataTable BasicDataTagValue;
        public DataTable BasicDataTagValueUpDown;
        public Hashtable hashtable_EngineTypeID;
        public Hashtable hashtable_tagTable_OpName_SaveData;
    }



    public struct ControlData
    {
        public int TagType;
        public string opName;
        public Control btn; public ControlData(Control Ctrl, string OpName)
        {
            string[] Array_NameSplit;
            Array_NameSplit = Ctrl.Name.Split('_');
            TagType = Convert.ToInt32(Array_NameSplit[2]);
            opName = OpName;
            btn = Ctrl;

        }
    }
    public struct ControlChangeByTagType
    {
        public ControlChangeByTagType(int tagType, string value)
        {
            TagType = tagType;
            Value = value;
        }

        public int TagType;
        public string Value;
    }
    /// <summary>
    /// tag格式化结构
    /// </summary>
    public struct TagFormat
    {
        /// <summary>
        /// TagID
        /// </summary>
        public string TagID;
        /// <summary>
        /// TagID
        /// </summary>
        public string TagName;
        /// <summary>
        /// 工位号
        /// </summary>
        public string OpName;
        /// <summary>
        /// 数据来源
        /// </summary>
        public string DataSources;
        /// <summary>
        /// IP
        /// </summary>
        public string IP;
        /// <summary>
        /// OPC的URL
        /// </summary>
        public string OpcIpPort;
        /// <summary>
        /// 组
        /// </summary>
        public string Group;
        /// <summary>
        /// 监控时间间隔
        /// </summary>
        public string MonitorInterval;
        /// <summary>
        /// DB号加起始地址，DB1001.30
        /// </summary>
        public string TagDbAndStartAdr;
        /// <summary>
        /// DB号加起始地址，DB1001.30.1,为了bool写入专用
        /// </summary>
        public string TagDbAndStartAdrWrite;
        /// <summary>
        /// OPC协议地址
        /// 例如：S7:[192.168.1.211|VFD1|S7ONLINE|01.00,192.168.1.211,02.02,1]DB1001,DINT154
        /// </summary>
        public string TagOPCAdrStr;
        /// <summary>
        /// DB地址位置
        /// 变量起始地址，例如  0，1，2，56，80...
        /// </summary>
        public int Index;
        /// <summary>
        /// 变量字节长度
        /// 变量字节长度,l例如  1，2，4...       
        /// </summary>
        public int Length;
        /// <summary>
        /// X  0,1,2,3,4,5,6,7
        /// 例如  X  0,1,2,3,4,5,6,7
        /// </summary>
        public int BitIndex;
        /// <summary>
        /// 以连续Byte数组方式读取的DB地址实际位置
        /// 变量起始地址，例如  0，1，2，56，80...
        /// </summary>
        public int IndexBytes;
        /// <summary>
        /// 绝对地址差分，应用区域读取的拆分20200101_LLX
        /// </summary>
        public int AbsAddrDiff;



        /// <summary>
        /// 点类型代码
        /// 比如bool是11，String是8
        /// </summary>
        public int TagTypeID;
        /// <summary>
        /// 点类型
        /// 比如Bool,INT,STRING等
        /// </summary>
        public string TagType;
        /// <summary>
        /// 变量类型代码
        /// 比如：允许保存是19
        /// </summary>
        public int TagTypeCodeID;
        /// <summary>
        /// Value
        /// </summary>
        public object TagValue;
        /// <summary>
        /// 是否监控
        /// </summary>
        public int IsMonitor;





        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime TimeStamp;
        /// <summary>
        /// 标签质量  192 , -1    
        /// </summary>
        public string TagQuality;
        /// <summary>
        /// 测量位置
        /// </summary>
        public string ShaftID;
        /// <summary>
        /// 测量项目   
        /// </summary>
        public string ItemName;
        /// <summary>
        /// 测量单位
        /// </summary>
        public string Unit;
        /// <summary>
        /// 上限值
        /// </summary>
        public string TagValueUp;
        /// <summary>
        /// 下限值
        /// </summary>
        public string TagValueDown;
        /// <summary>
        /// 理论值
        /// </summary>
        public string TagValeNormal;
        /// <summary>
        /// 合格标志
        /// </summary>
        public string IsGoodBad;
        /// <summary>
        /// 列位置
        /// </summary>
        public string TagOrderBy;
        /// <summary>
        /// 变量排序
        /// </summary>
        public string TagItemOrderBy;
        /// <summary>
        /// 是保存到数据库
        /// </summary>
        public int IsSaveToSql;
        /// <summary>
        /// 是发送到WebSocket
        /// </summary>
        public int IsWebSocket;
        /// <summary>
        /// 是程序启动时候的值
        /// </summary>
        public int IsFirstValue;
        /// <summary>
        /// 变量特点
        /// </summary>
        public int TagProperty;
        /// <summary>
        /// 变量变化的持续时间，单位 毫秒
        /// </summary>
        public int TagKeepTime;
        /// <summary>
        /// 变量变化前的值
        /// </summary>
        public object TagValueBefore;

    }

    public class CustomeEvetnArgs : EventArgs
    {
        public int TagKeepTime { get; set; }
        public object TagProperty { get; set; }
        public object TagItemOrderBy { get; set; }
        public object TagOrderBy { get; set; }
        public object IsGoodBad { get; set; }
        public object TagValeNormal { get; set; }
        public object TagValueDown { get; set; }
        public object TagValueUp { get; set; }
        public object Unit { get; set; }
        public object ItemName { get; set; }
        public object ShaftID { get; set; }
        public object IsMonitor { get; set; }
        public object TagTypeActual { get; set; }
        public object TagValueBefore { get; set; }
        public object IsFirstValue { get; set; }
        public object IsSaveToSql { get; set; }
        public object DataDources { get; set; }
        public object TagTypeID { get; set; }
        public object TagTypeCodeID { get; set; }
        public object TagID { get; set; }
        public string TimeStamp { get; set; }
        public object TagQuality { get; set; }
        public object OpName { get; set; }
        public object TagValue { get; set; }
        public object TagValueType { get; set; }
        public object TagType { get; set; }
        public object TagName { get; set; }
        public object TagKey { get; set; }
        public object IsWebSocket { get; set; }
        public string TimeStampBefore { get; set; }
    }

    public class QualityDataType
    {
        public int tagValueType;
        public int tagTypeID;
        public string opName;
        public string tagID;
        public object tagValue;
        public int tagQuality;
        public int tagOrderBy;
        public int tagItemOrderBy;
        public int tagProperty;
        public int isGoodBad;
    }

    public class HashtableList
    {
        /// <summary>
        /// MesServerCode
        /// </summary>
        static public Hashtable hashtable_MesServerCode_Temp;
        /// <summary>
        /// MesDll
        /// </summary>
        static public Hashtable hashtable_MesDll_Temp;
        static public Hashtable hashtable_TagTypeDataType_Temp;
        static public Hashtable hashtable_TagType_Temp;

        static public Hashtable hashtable_EngineGetOver_EngineTypeID_Temp;
        static public Hashtable hashtable_EngineGetOver_EngineType_Temp;
        static public Hashtable hashtable_EngineGetOver_EngineID_Temp;
        static public Hashtable hashtable_EngineGetOver_OrderForm_Temp;

        static public Hashtable hashtable_OpNameRepair_Temp;

        static public Hashtable hashtable_WorkGroupIndex_Temp;
        static public Hashtable hashtable_workStepProperty_Temp;
        static public Hashtable hashtable_QualityMesRead_Temp;
        static public Hashtable hashtable_IsAllowWork_Temp;
        /// <summary>
        /// 工位号与工位上下线类型
        /// （0普通工位）（1正常上线）（2正常下线）（3返修上线）（4返修下线）
        /// 5 分线上线（总线） 6 分线上线（分线）
        /// </summary>
        static public Hashtable hashtableOpName_OpType_Temp;

        /// <summary>
        /// <summary>
        /// 工位号与工位上下线类型
        /// （0普通工位）（1正常上线）（2正常下线）（3返修上线）（4返修下线）
        /// 5 分线上线（总线） 6 分线上线（分线）        
        /// </summary>
        public Hashtable hashtableOpName_OpType;
        public Hashtable hashtable_IsAllowWork;
        public Hashtable hashtable_QualityMesRead;
        public Hashtable hashtable_workStepProperty;
        public Hashtable hashtable_OpNameRepair;
        /// <summary>
        /// MesServerCode
        /// </summary>
        public Hashtable hashtable_MesServerCode;
        /// <summary>
        /// MesDll
        /// </summary>
        public Hashtable hashtable_MesDll;
        public Hashtable hashtable_TagTypeDataType;
        public Hashtable hashtable_TagType;


        public Hashtable hashtable_EngineGetOver_EngineTypeID;
        public Hashtable hashtable_EngineGetOver_EngineType;
        public Hashtable hashtable_EngineGetOver_EngineID;
        public Hashtable hashtable_EngineGetOver_OrderForm;

        public Hashtable hashtable_WorkGroupIndex;
    }

    public struct QualityDataQueryStruct
    {

        public QualityDataQueryStruct(string opName, Control ctrl)
        {
            OpName = opName;
            Ctrl = ctrl;
            tagType = 0;
            tagValue = "";
            tagID = "";
            tagTypeID = 0;
            tempForm = null;
            tagValue2 = null;

        }

        public QualityDataQueryStruct(string opName, int TagType)
        {

            OpName = opName;
            Ctrl = null;
            tagType = TagType;
            tagValue = "";
            tagID = "";
            tagTypeID = 0;
            tempForm = null;
            tagValue2 = null;
        }

        public QualityDataQueryStruct(string opName, int TagType, Control ctrl)
        {

            OpName = opName;
            Ctrl = ctrl;
            tagType = TagType;
            tagValue = "";
            tagID = "";
            tagTypeID = 0;
            tempForm = null;
            tagValue2 = null;
        }
        public QualityDataQueryStruct(string opName, int TagType, object TagValue)
        {

            OpName = opName;
            Ctrl = null;
            tagType = TagType;
            tagValue = TagValue;
            tagID = "";
            tagTypeID = 0;
            tempForm = null;
            tagValue2 = null;
        }
        public QualityDataQueryStruct(string TagID, int TagTypeID, object TagValue, object TagValue2, Form TempForm)
        {

            OpName = "";
            Ctrl = null;
            tagType = 0;
            tagValue = TagValue;
            tagID = TagID;
            tagTypeID = TagTypeID;
            tempForm = TempForm;
            tagValue2 = TagValue2;
        }


        public string OpName;
        public Control Ctrl;
        public int tagType;
        public object tagValue;
        public object tagValue2;
        public string tagID;
        public int tagTypeID;
        public Form tempForm;


    }
}


