using Newtonsoft.Json;
using SLC1_N;

//using NPOI.SS.Formula.Functions;
//using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace C_Windows_1
{
    class MES
    {
        static string ip;
        static string port;

        public MES(string IP, string PORT)
        {
            ip = IP;
            port = PORT;
        }

        /// <summary>
        /// 接口调用
        /// </summary>
        public static string HTTPPost(string requstUrl, string requestParams)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requstUrl);

            //Post请求方式
            request.Method = "POST";

            // 内容类型
            request.ContentType = "application/json";
            byte[] data = Encoding.UTF8.GetBytes(requestParams);// JsonStringToObject

            request.ContentLength = data.Length;

            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }

            // 获得响应流
            HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            StreamReader myreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            // 获得数据

            string responseText = myreader.ReadToEnd();
            response.Close();
            myreader.Close();
            return responseText;

        }


        /// <summary>
        /// mes接口  
        /// </summary>
        public string UploadData(MES_IN_Data request)
        {
            string logtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string requstUrl = string.Format("http://{0}:{1}/XX/XXX/XXXX", ip, port);
            string requestParams = JsonConvert.SerializeObject(request);

            mxlLog.Instance.MESDebug($"UploadData [IN]Url:{requstUrl}");
            mxlLog.Instance.MESDebug($"UploadData [IN]data:{requestParams}");

            string responseText = HTTPPost(requstUrl, requestParams);
            mxlLog.Instance.MESDebug($"UploadData [IN]return:{responseText}");

            return responseText;
        }

        public string UploadData(MES_OUT_Data request)
        {
            string logtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string requstUrl = string.Format("http://{0}:{1}/XX/XXX/XXXX", ip, port);
            string requestParams = JsonConvert.SerializeObject(request);

            mxlLog.Instance.MESDebug($"UploadData [OUT]Url:{requstUrl}");
            mxlLog.Instance.MESDebug($"UploadData [OUT]data:{requestParams}");

            string responseText = HTTPPost(requstUrl, requestParams);
            mxlLog.Instance.MESDebug($"UploadData [OUT]return:{responseText}");

            return responseText;
        }

        public class MES_IN_Data
        {
            public string time { get; set; }
            public string code { get; set; }
            public string fulltime { get; set; }
            public string balantime { get; set; }
            public string testtime { get; set; }
            public string exhausttime { get; set; }
        }

        public class MES_OUT_Data
        {
            public string time { get; set; }
            public string code { get; set; }
            public string result { get; set; }
            public string TestPressure { get; set; }
            public string LeakValue { get; set; }
        }
    }
}
