using LLAirtightApi;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SLZ_4
{
    /// <summary>
    /// MES入站信息
    /// </summary>
    public class MesIn
    {
        public string name { get; set; }        //接口名称
        public string time { get; set; }        //时间
        public string operatorName { get; set; }    //操作员
        public int channel { get; set; }        //通道号
        public string sn { get; set; }        //条码
    }
    /// <summary>
    /// MES出站信息
    /// </summary>
    public class MesOut
    {
        public string name { get; set; }        //接口名称
        public string time { get; set; }        //时间
        public string operatorName { get; set; }    //操作员
        public int channel { get; set; }        //通道号
        public string result { get; set; }      //测试结果 OK NG
        public string sn { get; set; }        //条码

        public float diffPressure { get; set; }      //压差
        public float pressure { get; set; }          //压力值
        public float leakage { get; set; }           //泄漏量

        public float inflationTime { get; set; }    //充气时间
        public float balanceTime { get; set; }       //平衡时间
        public float testTime1 { get; set; }          //检测时间1
        public float testTime2 { get; set; }         //检测时间2  解除输出2时间(28)
        public float exhaustTime { get; set; }       //排气时间

        public float bypassTime { get; set; }        //旁路时间
        public float delayTime1 { get; set; }        //输出1延时
        public float delayTime2 { get; set; }        //输出2延时
        public float bigLeakBalanceTime { get; set; }//大漏平衡时间
        public float micLeakBalanceTime { get; set; }//微漏平衡时间

        public string pressureUnit { get; set; }        //压力单位
        public string leakageUnit { get; set; }         //泄漏量单位
        public float pressureUpperLimit { get; set; }   //充气压力上限
        public float pressureLowerLimit { get; set; }   //充气压力下限
        public float balanceUpperLimit { get; set; }    //平衡压差上限 大漏压差
        public float balanceLowerLimit { get; set; }     //平衡压差下限

        public float leakageUpperLimit { get; set; }     //泄漏量上限 微漏上限
        public float leakageLowerLimit { get; set; }     //泄漏量下限 微漏下限

        //public float fEquivalentVolume { get; set; }     //等效容积
    }

    public class MesReply
    {
        public int code;        //返回code
        public string message;  //返回信息
    }
    
    public class MesObj
    {
        public MesIn mesIn;              //MES入站信息
        public MesOut mesOut;            //MES出站信息
        public MesReply mesReply;        //MES返回
        public MesObj()
        {
            mesIn = new MesIn();
            mesOut = new MesOut();
            mesReply = new MesReply();
        }

        public string MesCheckIn(string code, int iCH, string url)
        {
            mesIn.name = "checkSN";
            mesIn.time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            mesIn.operatorName = FormMain.form.strOperator;
            mesIn.channel = iCH + 1;
            mesIn.sn = code;

            string body = JsonConvert.SerializeObject(mesIn);

            Log log = new Log();
            log.MES_Logmsg(DateTime.Now.ToString() + "   入站 CH-" + (iCH + 1).ToString() + ": " + body);

            string strResult = HTTPCom.Post(body, url, null);
            log.MES_Logmsg(DateTime.Now.ToString() + "   入站 CH-" + (iCH + 1).ToString() + "  MES返回:" + strResult);

            if (strResult.Contains("code") && strResult.Contains("message"))
            {
                mesReply = JsonConvert.DeserializeObject<MesReply>(strResult);
            }
            else
            {
                mesReply.code = 0;
            }

            return strResult;
        }
        public string MesCheckOut(ref TEST_CHANNEL testInfo, ref CONFIG_PARAM configParam, string code, int iCH, string url)
        {
            mesOut.name = "updateMes";
            mesOut.time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            mesOut.operatorName = FormMain.form.strOperator;
            mesOut.channel = iCH + 1;
            mesOut.sn = code;
            mesOut.result = FormMain.form.strResult[testInfo.ResultFlag]; 

            mesOut.diffPressure = testInfo.fTestDiffPressure;      //压差
            mesOut.pressure = testInfo.fTestPressure;          //压力值
            mesOut.leakage = testInfo.fLeakAmount;          //泄漏量

            mesOut.inflationTime = configParam.inflationTime;    //充气时间
            mesOut.balanceTime = configParam.balanceTime;       //平衡时间
            mesOut.testTime1 = configParam.testTime1;         //检测时间1
            mesOut.testTime2 = configParam.testTime2;        //检测时间2  解除输出2时间(28)
            mesOut.exhaustTime = configParam.exhaustTime;       //排气时间

            mesOut.bypassTime = configParam.bypassTime;        //旁路时间
            mesOut.delayTime1 = configParam.delayTime1;        //输出1延时
            mesOut.delayTime2 = configParam.delayTime2;        //输出2延时
            mesOut.bigLeakBalanceTime = configParam.bigLeakBalanceTime;//大漏平衡时间
            mesOut.micLeakBalanceTime = configParam.micLeakBalanceTime;//微漏平衡时间

            mesOut.pressureUnit = FormMain.form.strListPressureUnit[configParam.pressureUnit];        //压力单位
            mesOut.leakageUnit = FormMain.form.strListLeakageUnit[configParam.leakageUnit];         //泄漏量单位
            mesOut.pressureUpperLimit = configParam.pressureUpperLimit;   //充气压力上限
            mesOut.pressureLowerLimit = configParam.pressureLowerLimit;   //充气压力下限
            mesOut.balanceUpperLimit = configParam.balanceUpperLimit;    //平衡压差上限 大漏压差
            mesOut.balanceLowerLimit = configParam.balanceLowerLimit;     //平衡压差下限

            mesOut.leakageUpperLimit = configParam.leakageUpperLimit;     //泄漏量上限 微漏上限
            mesOut.leakageLowerLimit = configParam.leakageLowerLimit;     //泄漏量下限 微漏下限

            string body = JsonConvert.SerializeObject(mesOut);

            Log log = new Log();
            log.MES_Logmsg(DateTime.Now.ToString() + "  出站 CH-" + (iCH + 1).ToString()+ ": " + body);

            string strResult = HTTPCom.Post(body, url, null);
            log.MES_Logmsg(DateTime.Now.ToString() + "  出站 CH-" + (iCH + 1).ToString() + "  MES返回:" + strResult);
            mesReply = JsonConvert.DeserializeObject<MesReply>(strResult);
            return strResult;
        }
    }
}
