using System;
using MesServerWork;
using System.Threading;
using MisDataSaveDate;
using System.Collections;
//using SystemFramework;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 处理WEB页面发送过来的请求（m_Command + "|" + m_Value;）
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="Content"></param>
        private void WebSubmit(object e)
        {
            try
            {
                DeviceDriver_BasicData.CustomeEvetnArgs msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                string OpName=Convert.ToString(msgEvent.OpName);
                string Content = Convert.ToString(msgEvent.TagValue);

                string[] dataArray = Content.Split('|');
                if (dataArray.Length != 2)
                    return;
                //消息类型
                string m_Command = dataArray[0].ToString();
                //消息参数
                string m_Value = dataArray[1].ToString();
                //消息参数数量
                string[] m_Value_array = m_Value.Split('&');
                //消息参数长度
                int m_Value_length = m_Value_array.Length;

                //工件信息
                string engineID = "";
                if (MIS_BASE.hashtable_EngineGetOver_EngineTypeID == null)
                {
                    MIS_BASE.InitHashtable_MesServerCode();
                }
                if (MIS_BASE.hashtable_EngineGetOver_EngineTypeID == null)
                {
                    return;
                }
                if (MIS_BASE.hashtable_EngineGetOver_EngineID.ContainsKey(OpName))
                {
                    engineID = MIS_BASE.hashtable_EngineGetOver_EngineID[OpName].ToString();
                }

                DeviceDriver_BasicData.CustomeEvetnArgs _e = new DeviceDriver_BasicData.CustomeEvetnArgs();
                _e.OpName = OpName;
                _e.TagValue = m_Value;

                //根据请求的命令处理逻辑
                switch (m_Command)
                {
                    case "Barcode":
                        if (m_Value_length > 1) return;
                        fun_Barcode(_e);
                        break;
                    case "OnLineState"://在线0、离线1、屏蔽2
                        try
                        {
                            if (m_Value_length > 1) return;
                            int a = Convert.ToInt32(m_Value);
                            if (a <= 2)
                            {
                                //BaseDataSystemMES.IsOnLine_Update(OpName, m_Value.ToString());
                                MIS_BASE.Write_OnLineState(OpName, Convert.ToInt32(m_Value));
                                if (a == 2) SendMessage.AsyncSendMessage_Notice(OpName, "屏蔽MES！");
                                if (a == 0) SendMessage.AsyncSendMessage_Notice(OpName, "启用MES！");
                            }
                        }
                        catch
                        {

                        }
                        break;
                    case "Reset"://复位，将MES所有的信息都复位了
                        SetMoby(OpName);//将MES所有信号清空
                        break;
                    case "FixAllow"://重新拧紧
                        MIS_BASE.Write_FixAllow(OpName, false);
                        MIS_BASE.Write_FixAllow(OpName, true);
                        break;
                    case "IsAutoGetEngineID"://自动手动申请编号（线首）
                        if (m_Value_length > 1) return;
                        MIS_BASE.Write_IsAutoGetEngineID(OpName, Convert.ToInt32(m_Value));
                        break;
                    case "PartPalletGoOver"://空托盘放行
                         int Is_PartPalletGoOver;
                        MIS_BASE.Read_PartPalletGoOver(OpName, out Is_PartPalletGoOver);
                        if (Is_PartPalletGoOver == 1)
                        {
                            MIS_BASE.Write_PartPalletGoOver(OpName, 1);
                        }
                        else
                        {
                            MIS_BASE.Write_PartPalletGoOver(OpName,0);
                        }
                        break;
                    case "SetMoby"://清空托盘（线首）
                        SetMoby(OpName);//将MES所有信号清空
                        //MIS_BASE.Write_PartPalletCode_PLC(OpName, 1);
                        break;
                    case "RepairUplineMES"://返修上线
                        MIS_BASE.Write_RepairUpline_MES(OpName, true);
                        break;
                    case "CancelRepair"://将返修件置为正常件

                        break;
                    case "PartPalletCode"://申请写入托盘号
                        //MIS_BASE.Write_PartPalletCode_PLC(OpName, 1);
                        break;
                    case "WritePartPalletCode"://写入托盘号
                        if (m_Value_length > 1) return;
                        MIS_BASE.Write_PalletCode(OpName, Convert.ToInt32(m_Value));
                        break;
                    case "Normal"://启用正常拧紧工位
                        MIS_BASE.Write_Normal(OpName, 1);
                        MIS_BASE.Write_UnNormal(OpName, 0);
                        break;
                    case "UnNormal"://启用备用拧紧工位
                        MIS_BASE.Write_Normal(OpName, 0);
                        MIS_BASE.Write_UnNormal(OpName, 1);
                        break;
                    case "ResetDevice"://复位设备
                        MIS_BASE.Write_ResetDevice(OpName, true);
                        System.Threading.Thread.Sleep(1000);
                        MIS_BASE.Write_ResetDevice(OpName, false);
                        break;
                    case "IsNeedRepaire"://返修上线(返修上线点)
                        if (Convert.ToInt32(m_Value) == 1)
                        {
                            //修改托盘状态
                            MIS_BASE.Write_PalletInfo(OpName, 3);
                            //通知修改成功
                            MIS_BASE.Write_UpdatePalletOK(OpName, true);
                            IsNeedRepaire_OK(OpName);

                        }
                        else if (Convert.ToInt32(m_Value) == 0)
                        {
                            //通知修改成功
                            MIS_BASE.Write_UpdatePalletOK(OpName, true);
                            //直接给出工作完成
                            MIS_BASE.Write_MaterialVerifyOver(OpName, true);
                        }
                        else
                        { }

                        break;
                    case "ContinueMatch":
                        //BaseDataSystemMES.QualityVerifyStatus_Hash_Write(OpName, true);
                        break;
                    //初始化网页信号
                    case "IniSingalToWeb":
                        InitializeSingalToWeb(OpName);
                        break;
                    case "Reset_Engine"://返修上线(返修上线点)
                        SetMoby(OpName);//将MES所有信号清空
                        //fun_EngineTypeGetOverPLC(OpName, 0);
                        //fun_EngineTypeGetOverPLC(OpName, 1);
                        break;

                    case "twoWorkSkip"://重新工作工件跳过工作(重新工作工件)
                        //工作完成
                        EngineTypeGetOver_PLC_Work_Skip(OpName);
                        break;
                    case "twoWork"://重新工作工件重新工作(重新工作工件)
                        //允许工作
                        EngineTypeGetOver_PLC_Work_two(OpName);
                        break;
                    case "SelectOrderForm"://分线接受前端模拟传递机型
                        _e.TagValueUp = "6";
                        EngineTypeGetOver_PLC(_e);
                        break;
                    case "outLineOpNameWorkOver"://分线接受前端模拟工作完成
                        _e.TagValueUp = "6";
                        _e.TagValue = 1;
                        MesRead(_e);
                        break;
                    default: break;
                }
            }
            catch(Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function02:WebSubmit");
            }
        }
        private void SetMoby(string OpName)
        {
            try
            {
                //清空Moby时，需要将MES信号清空
                MIS_BASE.Write_ClearMesMoby(OpName);

                //BaseDataSystemMES.MesServer_Process_EngineTypeOver(OpName);

                SendMessageFun.SendMessage("5123", "1111", OpName);


                MIS_BASE.Write_ResetDevice(OpName, true);
                System.Threading.Thread.Sleep(1000);
                MIS_BASE.Write_ResetDevice(OpName, false); 
            }
            catch
            {

            }
        }
        /// <summary>
        /// 网页重现登录，恢复关闭前的页面状态
        /// </summary>
        /// <param name="Opname">工位号</param>
        private void InitializeSingalToWeb(string OpName)
        {
            InitOpPosion(OpName);

            ////3显示操作指导图片 PicShow_String_FirstOpName
            //SendMessage.PicShow_FirstOpName(OpOpNamename);
            ////1（直接在界面通过选择机型确认工序内容）
            //SendMessage.ProcessList_FirstOpName(OpName);
            //// MOBY数据
            //SendMessageFun.SendMessage("5", "1", OpName);
            //// 物料信息 物料信息
            //SendMessage.MaterialList(OpName);

            SendMessageFun.SendMessage("315", "111", OpName);

            // 新增查询PLC直通状态 网页显示
            //MIS_BASE.Read_TagTypeID(63, Opname, out S_plcStatus);
            InitAlarm(OpName);
        }

        //分线发送工件到位信号函数
        private void sendmessage(string OpName, int tagValue, string Command)
        {
            string SendMessage;
            SendMessage = "MES|" + Command + "|" + OpName + "|" + tagValue;
            SendMessageFun.AsyncSendMessage(SendMessage);

        }
        private void InitAlarm(string Opname)
        {
            FuntionMesRead.Alarm_InitAlarm_Send(Opname, out Hashtable ht);
            foreach (DictionaryEntry kv in ht)
            {
                var TagID = kv.Key.ToString();
                var TagValue = kv.Value.ToString();
                FuntionMesRead.Send_AlarmMsg(TagID, TagValue);
            }

        }

        private void InitOpPosion(string Opname)
        {
            int S_WorkCheckOver = Convert.ToInt32(MIS_BASE.ReadPLC(30, Opname));
            string S_WorkCheckOver_Msg = "MES|checkWorkOverIsShow|" + Opname + "|" + S_WorkCheckOver;
            SendMessage.AsyncSendMessage(S_WorkCheckOver_Msg);

            bool S_WorkStart;//工件到位 DBX0.1
            MIS_BASE.Read_WorkStart(Opname, out S_WorkStart);
            if (S_WorkStart == true)
            {
                SendMessageFun.SendInfomation("WorkStart", Convert.ToString(Convert.ToInt32(S_WorkStart)), Opname);
            }
            //----------------------------------------------------
            bool S_PartLoad;//申请上线
            MIS_BASE.Read_PartLoad(Opname, out S_PartLoad);
            if (S_PartLoad == true)
            {
                SendMessageFun.SendInfomation("PartLoad", Convert.ToString(Convert.ToInt32(S_PartLoad)), Opname);
            }
            //----------------------------------------------------
            bool S_EngineGetOver;//传递机型 DBX0.4
            MIS_BASE.Read_EngineGetOver_PLC(Opname, out S_EngineGetOver);
            if (S_EngineGetOver == true)
            {
                SendMessageFun.SendInfomation("EngineTypeGetOverPLC", Convert.ToString(Convert.ToInt32(S_EngineGetOver)), Opname);

                //5 显示订单、机型信息 线首工位用
                SendMessage.ShowMoby_FirstOpName(Opname);
                //界面显示物料信息
                SendMessage.MaterialList_FirstOpName(Opname);
            }
            //----------------------------------------------------
            bool S_FixAllow;//允许工作 DBX3.1
            MIS_BASE.Read_FixAllow(Opname, out S_FixAllow);
            if (S_FixAllow == true)
            {
                SendMessageFun.SendInfomation("FixAllow", Convert.ToString(Convert.ToInt32(S_FixAllow)), Opname);
            }
            //----------------------------------------------------
            bool S_MesRead;//允许保存 DBX3.1
            MIS_BASE.Read_QualityData_Read_CF(Opname, out S_MesRead);
            if (S_MesRead == true)
            {
                SendMessageFun.SendInfomation("MesRead", Convert.ToString(Convert.ToInt32(S_MesRead)), Opname);
            }
            //----------------------------------------------------
            bool S_ReadOver;//保存完成 
            MIS_BASE.Read_MesReadOver(Opname, out S_ReadOver);
            if (S_ReadOver == true)
            {
                SendMessageFun.SendInfomation("MesReadOver", Convert.ToString(Convert.ToInt32(S_ReadOver)), Opname);
            }
            //----------------------------------------------------
            bool S_MaterialVerifyOver;//工作完成 DBX2.3
            MIS_BASE.Read_MaterialVerifyOver(Opname, out S_MaterialVerifyOver);
            if (S_MaterialVerifyOver == true)
            {
                SendMessageFun.SendInfomation("MaterialVerifyOver", Convert.ToString(Convert.ToInt32(S_MaterialVerifyOver)), Opname);
            }
            //----------------------------------------------------
            int S_QualityMask;//合格标志 DBX588
            MIS_BASE.Read_QualityMask(Opname, out S_QualityMask);
            if (S_QualityMask == 1)
            {
                SendMessageFun.SendInfomation("QualityMask", Convert.ToString(Convert.ToInt32(S_QualityMask)), Opname);
            }
            //----------------------------------------------------
            string S_plcStatus = "false";//DBX501.5 PLC直通，1 强制放行 0 正常循环
            if (S_plcStatus.ToLower() == "false")
            {
                SendMessageFun.SendInfomation("PlcStatus", "0", Opname);
            }
            else
            {
                SendMessageFun.SendInfomation("PlcStatus", "1", Opname);
            }
        }
    }
}
