using MesServerWork;
using MisDataFunDll;
using System;
using System.Collections.Generic;
using System.Threading;

namespace MesServerFunctions
{
    public partial class FunctionMES
    {
        public void FunWork(string recData)
        {
            List<string> dataArray = MisDataFun.GetCmdStringList(recData);
            string source = dataArray[0].ToString();
            string command = dataArray[1].ToString();
            string opName = dataArray[2].ToString();
            string value = dataArray[3].ToString();

            var e = new DeviceDriver_BasicData.CustomeEvetnArgs();
            switch (source)
            {
                case "WEB":
                    e.OpName = opName;
                    e.TagValue = command + "|" + value;
                    fun_WebSubmit(e);
                    break;
                case "PLC":
                    e = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceDriver_BasicData.CustomeEvetnArgs>(value);
                    e.TagValue = NormalizeTagValue(e.TagValue);
                    RoutePlcSignal(e, opName);
                    break;
            }
        }

        private string NormalizeTagValue(object tagValue)
        {
            string value = Convert.ToString(tagValue);
            switch (value.ToLower())
            {
                case "false":
                    return "0";
                case "true":
                    return "1";
                default:
                    return value;
            }
        }

        private void RoutePlcSignal(DeviceDriver_BasicData.CustomeEvetnArgs e, string opName)
        {
            switch (e.TagTypeCodeID.ToString())
            {
                // PLC 工件到位
                case "2":
                    fun_WorkStart(e);
                    break;
                // PLC 允许保存质量数据
                case "19":
                    fun_MesRead(e);
                    break;
                // PLC 申请上线
                case "43":
                    PartLoad(e);
                    break;
                // PLC 传递机型
                case "44":
                    fun_EngineTypeGetOverPLC(e);
                    break;
                // PLC/MES 系统健康心跳
                case "14":
                    fun_HeartBeatMIS(e);
                    break;
                // MES 设备报警复位
                case "61":
                    MIS_BASE.Write_MesErrorType(opName, 0);
                    break;
            }
        }

        /// <summary>
        /// 传递机型
        /// </summary>
        public void fun_EngineTypeGetOverPLC(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(EngineTypeGetOver_PLC, e);
        }

        /// <summary>
        /// 保存完成 19
        /// </summary>
        public void fun_MesRead(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(MesRead, e);
        }

        /// <summary>
        /// 工件到位离开，清空 MES 信号
        /// </summary>
        public void fun_WorkStart(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(WorkStart, e);
        }

        /// <summary>
        /// 处理 WEB/PAD 页面传递过来的数据
        /// </summary>
        public void fun_WebSubmit(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(WebSubmit, e);
        }

        /// <summary>
        /// MES 系统健康心跳监测
        /// </summary>
        public void fun_HeartBeatMIS(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(HeartBeatMES, e);
        }

        /// <summary>
        /// PLC 系统健康心跳监测
        /// </summary>
        public void fun_HeartBeatPLC(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(HeartBeatPLC, e);
        }
    }
}
