using CatLib;
using Highway.Assemble.common;
using Highway.Assemble.Common;
using Highway.Assemble.EquipClient;
using log4net;

using Newtonsoft.Json.Linq;
using RRQMSocket;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml;
using TouchSocket.Core;
using TouchSocket.Rpc;
using TouchSocket.Sockets;
using TouchSocket.WebApi;

namespace HighWayAssemble.Protocol
{
    public class MesHttpClient
    {
        public static WebApiClient _MeshttpClient;

        public MesHttpClient()
        {

        }
        public async void init()
        {
            try
            {
                _MeshttpClient = new WebApiClient();
                await _MeshttpClient.SetupAsync(new TouchSocketConfig()
                     .SetRemoteIPHost("172.16.0.106:9880")
                     //.SetRemoteIPHost("127.0.0.1:9880")
                     .ConfigurePlugins(a =>
                     {
                         
                     }));
                await _MeshttpClient.ConnectAsync();
            }
            catch (Exception ex)
            {
                FrmDisplayView.LogInfo.Error("ERROR: " + ex.Message);
            }
        }

        public ApiResponse<MesParaData> autoread(MesParaData mesParaData)
        {
            try
            {
                var requestForPost = new WebApiRequest();
                requestForPost.Method = HttpMethodType.Post;
                requestForPost.Body = new MesParaData() { EQUID = mesParaData.EQUID, EPCID = mesParaData.EPCID, READSTATUS = mesParaData.READSTATUS, ERRORMSG = mesParaData.ERRORMSG, READTIME = mesParaData.READTIME };
                FrmDisplayView.LogInfo.Info(JsonSerializer.Serialize(requestForPost.Body));
                JToken responseValue = _MeshttpClient.InvokeT<JToken>("/autoread", null, requestForPost);
                return MsgUtil.JTokenToEntity<ApiResponse<MesParaData>>(responseValue);

            }
            catch (Exception ex)
            {
                FrmDisplayView.LogInfo.Error("ERROR: " + ex.Message);
                return null;
            }
        }
    }
}
