using System;
//using SystemFramework;


namespace MesServerWork
{

    public partial class MIS_BASE
    {


        //static public void WritePLC(int tagType, string opName, object tagValue)
        //{
        //    Write_TagTypeID(tagType, opName, tagValue);
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public delegate void DemoHandler(object sender, DeviceDriver_BasicData.CustomeEvetnArgs e);
        /// <summary>
        /// 
        /// </summary>
        public static event DemoHandler OPCTagDataDemo;

        /// <summary>
        /// 根据tagType,工位号写入PLC对应值
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        static private void Write_TagValueByTagTypeName(string cmd, string opName,object tagValue)
        {
            int tagTypeID = 0;
            string msg;
            try
            {
                tagTypeID = Convert.ToInt32(MIS_BASE.hashtable_TagType[cmd]);
                MIS_BASE.WriteTagValue(tagTypeID, opName, tagValue);
            }
            catch (Exception err) 
            {
                ////ApplicationLog.WriteLog(err, "Write_TagValueByTagTypeName_FUNC");
            }
            
        }

        /// <summary>
        /// 根据TagTypeID,工位号写入PLC对应值
        /// </summary>
        /// <param name="TagTypeID"></param>
        /// <param name="tagValue"></param>
        static public void Write_TagValueByTagTypeID(int tagTypeID, string opName, object tagValue)
        {
            string msg;
            object tagValueOb;
            object newTagValue= tagValue;
            try
            {
               MIS_BASE.WriteTagValue(tagTypeID, opName, tagValue);

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Write_TagValueByTagTypeID_FUNC");
            }

        }

        /// <summary>
        /// 通过TagID写入PLC数据
        /// </summary>
        /// <param name="TagID"></param>
        /// <param name="tagValue"></param>
        public static void Write_TagID(string TagID, object tagValue)
        {
            try
            {
                MIS_BASE.WritePLC(TagID, tagValue);
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Write_TagValueByTagID_FUNC");
            }
        }


        /// <summary>
        /// 清空MES信号 MES传递机型(5) 允许工作(66) 合格放行(51) 不合格放行(8) 拧紧结束(67) 工作完成(7) 保存完成(20) 加工时间保存完成(54) 匹配完成(68) 默认false
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="false"></param> 
        public static void Write_ClearMesMoby(string opName)
        {
            MIS_BASE.Write_EngineTypeGetOver_MES(opName, false);
            MIS_BASE.Write_FixAllow(opName, false);
            MIS_BASE.Write_ConfirmOkGo(opName, false);
            MIS_BASE.Write_FalseGO(opName, false);
            MIS_BASE.Write_FixOver(opName, false);
            MIS_BASE.Write_MesWorkFinish(opName, false);
            MIS_BASE.Write_MesReadOver(opName, false);
            MIS_BASE.Write_MachineTime_ReadOver(opName, false);
            MIS_BASE.Write_MatchingCompleted(opName, false);
        }


        /// <summary>
        /// 订单号(string)(25)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_OrderForm(string opName, string tagValue)
        {
            string cmd = "OrderForm";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }
        /// <summary>
        /// 总成编号(string)(26)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_AssemblyCode(string opName, string tagValue)
        {
            string cmd = "AssemblyCode";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }
        /// <summary>
        /// 配方号1(dint)(27)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_Recipe1(string opName, int tagValue)
        {
            string cmd = "Recipe1";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }
        /// <summary>
        /// 配方号2(dint)(28)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_Recipe2(string opName, int tagValue)
        {
            tagValue = 0;
            string cmd = "Recipe2";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }



        /// <summary>
        /// 总成编号(string)(62)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_AssemblyCode_MES(string opName, string tagValue)
        {

            string cmd = "AssemblyCode_MES";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }
        /// <summary>
        /// 配方号1(dint)(63)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_Recipe1_MES(string opName, int tagValue)
        {
            string cmd = "Recipe1_MES";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }
        /// <summary>
        /// 配方号2(dint)(64)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_Recipe2_MES(string opName, int tagValue)
        {
            string cmd = "Recipe2_MES";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }


        /// <summary>
        /// 各个工位返修状态(4)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_RepairPathString(string opName, string tagValue)
        {
            string cmd = "RepairPathString";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }

        
        /// <summary>
        /// 加工时间保存完数据 DBX102.1 (54)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_MachineTime_ReadOver(string opName, bool tagValue)
        {

            int tagTypeID = 54;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }
       
        /// <summary>
        /// 工位三种状态：在线、离线、屏蔽（0，1，2）(DBB116)(79)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_OnLineState(string opName, int tagValue)
        {
            string cmd = "OnLineState";
            Write_TagValueByTagTypeName(cmd, opName,Convert .ToInt16 ( tagValue));
        }

        /// <summary>
        /// 托盘编号（PLC)(80)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_PalletCode(string opName, int tagValue)
        {
            string cmd = "PartPalletCode";
            Write_TagValueByTagTypeName(cmd, opName, Convert.ToInt16(tagValue));
        }
        
        /// <summary>
        /// MES向PLC传递机型(5)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_EngineTypeGetOver_MES(string opName, bool tagValue)
        {
            int tagTypeID = 5;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }

        /// <summary>
        /// PLC向MES传递机型(44)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_EngineTypeGetOver_PLC(string opName, bool tagValue)
        {
            string cmd = "EngineTypeGetOverPLC";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }

        /// <summary>
        /// PLC向MES质量数据可以读 允许保存(19) DBX587.0
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_MesRead_PLC(string opName, bool tagValue)
        {
            string cmd = "MesRead";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }


        /// <summary>
        /// 工件到位 
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_WorkStart(string opName, bool tagValue)
        {
            string cmd = "WorkStart";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }

        /// <summary>
        /// 拧紧结束确认(67)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_FixOver(string opName, bool tagValue)
        {

            int tagTypeID = 67;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }

        /// <summary>
        ///返修上线,在上线工位，MES依据工件情况，设定为返修件，PLC见到MES设定为1后，将返修标志写入到DBX0.2为1
        ///MES设定完返修返修上线后，MES清空该信号
        ///DBX3.7(72)  
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_RepairUpline_MES(string opName, bool tagValue)
        {
            int tagTypeID = 72;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }

        /// <summary>
        /// 匹配完成(68)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_MatchingCompleted(string opName, bool tagValue)
        {
            int tagTypeID = 68;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }


       

        /// <summary>
        /// 匹配结果(69)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_Match(string opName, bool tagValue)
        {
            int tagTypeID = 69;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);

        }

        /// <summary>
        /// 空托盘放行(DBX2.2)(6)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_PartPalletGoOver(string opName, int tagValue)
        {
            int tagTypeID = 6;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }


        /// <summary>
        /// 自动手动申请工件编号,0是自动方式；1：手动方式。切换时，(DBX2.1)(71)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_IsAutoGetEngineID(string opName, int tagValue)
        {
            string cmd = "IsAutoGetEngineID";
            Write_TagValueByTagTypeName(cmd, opName, Convert.ToInt16(tagValue));
        }


        /// <summary>
        /// 正常拧紧时选择(DBX100.3)(81)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_Normal(string opName, int tagValue)
        {
            string cmd = "Normal";
            Write_TagValueByTagTypeName(cmd, opName, Convert.ToInt16(tagValue));
        }
        /// <summary>
        /// 备用拧紧时选择(DBX100.4)(82)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_UnNormal(string opName, int tagValue)
        {
            string cmd = "UnNormal";
            Write_TagValueByTagTypeName(cmd, opName, Convert.ToInt16(tagValue));
        }
        
        /// <summary>
        /// 变量类型代码=52，复位设备（MES）DBX0.6
        /// 通知PLC复位设备
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_ResetDevice(string opName, bool tagValue)
        {
            string cmd = "PartLoadFinish";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }
        
        /// <summary>
        ///托盘状态(22)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_PalletInfo(string opName, int tagValue)
        {
            string cmd = "PalletInfo";
            Write_TagValueByTagTypeName(cmd, opName, Convert.ToInt16(tagValue));
        }
        /// <summary>
        /// 返修标志(93)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_RepairStatusMES(string opName, int tagValue)
        {
            string cmd = "RepairStatusMES";
            Write_TagValueByTagTypeName(cmd, opName, Convert.ToInt16(tagValue));
        }
        
        /// <summary>
        /// MES通知PLC修改托盘状态成功
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_UpdatePalletOK(string opName, bool tagValue)
        {
            string cmd = "UpdatePalletOK";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }

        #region  MES交互区写值

        /// <summary>
        /// 工作完成(7)（MES）
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_MesWorkFinish(string opName, bool tagValue)
        {
            int tagTypeID = 7;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }
        /// <summary>
        /// 合格方式放行(51)（MES）
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_ConfirmOkGo(string opName, bool tagValue)
        {
            int tagTypeID = 51;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }

        /// <summary>
        /// 允许拧紧(66)（MES）
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_FixAllow(string opName, bool tagValue)
        {
            int tagTypeID = 66;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }

        /// <summary>
        /// 不合格放行(8)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_FalseGO(string opName, bool tagValue)
        {
            int tagTypeID = 8;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }

        /// <summary>
        /// 拧紧序号(77)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_ProcessCode(string opName, int tagValue)
        {
            int tagTypeID = 77;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }


        /// <summary>
        /// 套筒号(88)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_BoltCode(string opName, int tagValue)
        {
            int tagTypeID = 88;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }
        /// <summary>
        /// MES保存完数据(必须MIS)(X102.2)(20)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_MesReadOver(string opName, bool tagValue)
        {
            int tagTypeID = 20;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }
        /// <summary>
        /// 工作完成(7)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_MaterialVerifyOver(string opName, bool tagValue)
        {
            string cmd = "MaterialVerifyOver";
            Write_TagValueByTagTypeName(cmd, opName, tagValue);
        }
        public static void Write_TagTypeID(int tagTypeID, string opName, object tagValue)
        {
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }

        /// <summary>
        /// 变量类型代码=73，机型代码。DBB404~405	机型标志
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_EngineTypeID_MES(string opName, int tagValue)
        {
            int tagTypeID = 73;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }

        public static void Write_OpNameIsOk(string opName,int qualityMask)
        {
            
            // -99 是复位
            if (qualityMask == -99)
            {
                Write_TagValueByTagTypeID(51, opName, false);
                Write_TagValueByTagTypeID(8, opName, false);
            }
            else if(qualityMask == 1)
            {
                // 51 合格放行
                Write_TagValueByTagTypeID(51, opName, true);
            }
            else
            {
                // 8  不合格放行
                Write_TagValueByTagTypeID(8, opName, true);
            }
        }

        /// <summary>
        /// 变量类型代码=74，零件编号。DBB406~445	发动机编号
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_EngineID_MES(string opName, string tagValue)
        {
            int tagTypeID = 74;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }
        /// <summary>
        /// 变量类型代码=75，零件机型。DBB446~485	发动机机型
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_EngineType_MES(string opName, string tagValue)
        {
            int tagTypeID = 75;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }

        /// <summary>
        /// 订单号(string)(116)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        public static void Write_OrderForm_MES(string opName, string tagValue)
        {
            int tagTypeID = 116;
            Write_TagValueByTagTypeID(tagTypeID, opName, tagValue);
        }

        #endregion


    }
}
