using bizFacade;
using MesServerWork;
using System;
using System.Threading;
//using SystemFramework;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 工件到位与离开时，清空MES自身信号
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void WorkStart(object  e)
        {
            try
            {
                DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);
                if (tagValue > 0)
                {
                    BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.WorkStartIn, DateTime.Now.ToString());
                    BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.WorkStart, tagValue.ToString());
                }
                else
                {
                    BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.WorkStartOut, DateTime.Now.ToString());
                    Thread.Sleep(500);
                    MIS_BASE.Write_MesReadOver(OpName, false);
                    MIS_BASE.Write_ConfirmOkGo(OpName, false);
                    MIS_BASE.Write_FixAllow(OpName, false);

                    MIS_BASE.Write_ConfirmOkGo(OpName, false);
                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, false);
                    //工作完成
                    MIS_BASE.Write_MaterialVerifyOver(OpName, false);
                    //补拧工件  写入地址  在工件离开后清空
                    MIS_BASE.WritePLC(77, OpName, 0);
                    MIS_BASE.WritePLC(88, OpName, 0);
                    // 是否返修点
                    MIS_BASE.WritePLC(33, OpName, false);
                    MIS_BASE.WritePLC(34, OpName, false);
                    MIS_BASE.WritePLC(35, OpName, false);
                    if (OpName == "J07MA070")
                    {
                        MIS_BASE.WritePLC(124, OpName, 0);
                        MIS_BASE.WritePLC(112, OpName, 0);
                    }
                    // 清除程序号那些BYTE点
                    for (int i = 1;i <= 40; i++)
                    {
                        MIS_BASE.WritePLC(127+i, OpName, 0);
                    }
                    if(OpName == "J07MA080")
                    {
                        MIS_BASE.WritePLC(168, OpName, 0);
                        MIS_BASE.WritePLC(53, OpName, 0);
                    }
                    if (OpName == "J07ET030")
                    {
                        MIS_BASE.WritePLC(99, OpName, 0);
                        MIS_BASE.WritePLC(53, OpName, false);
                        MIS_BASE.WritePLC(114, OpName, 0);
                    }
                    if(OpName == "J07MA140")
                    {
                        MIS_BASE.WritePLC(35, OpName, 0);
                    }
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function02:WorkStart");
            }
        }

    }
}
