ServerConn.cs 6.2 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    public class ServerConn
    {
        static string inputCounterDataByXRayMachine_URL;
        static string DetermineReelStorageLocation_URL;
        static ServerConn()
        {
            inputCounterDataByXRayMachine_URL = ConfigAppSettings.GetValue("inputCounterDataByXRayMachine");
            DetermineReelStorageLocation_URL = ConfigAppSettings.GetValue("DetermineReelStorageLocation");
        }
        public static CountResult inputCounterDataByXRayMachine(string TwoDBarcode, int qty)
        {
            var wc = new MyWebClient(15000);
            //if (string.IsNullOrEmpty(wc.Headers["Content-Type"]))
            //    wc.Headers.Add("Content-Type", "application/json;charset=UTF-8");

            wc.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";
            wc.Encoding = Encoding.UTF8;

            var data = new Wiston_Request();
            data.userId = "Q14050052";
            data.language = 1;
            data.requestTime = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
            data.data.Add("TwoDBarcode", TwoDBarcode);
            data.data.Add("Qty", qty);
            data.data.Add("Counter", ConfigHelper.Config.Get("upload_Counter"));

            string json = JsonHelper.SerializeObject(data);
            string result = "";
            int retry = 0;
        retry:
            try
            {
                result = wc.UploadString(ConfigHelper.Config.Get("inputCounterDataByXRayMachine"), "POST", json);
                return JsonHelper.DeserializeJsonToObject<CountResult>(result);
            }
            catch (WebException ex)   // 先捕获 HTTP 级异常
            {
                string respBody = null;
                if (ex.Response != null)
                {
                    using (var sr = new StreamReader(ex.Response.GetResponseStream()))
                        respBody = sr.ReadToEnd();
                }

                LogUtil.info($"inputCounterDataByXRayMachine url:{ConfigHelper.Config.Get("inputCounterDataByXRayMachine")} retry:{retry}, " +
                             $"status:{(int?)((HttpWebResponse)ex.Response)?.StatusCode}, " +
                             $"responseBody:{respBody}, " +
                             $"exception:{ex}");
                retry++;
                if (retry < 3) goto retry;
                return null;
            }
            catch (Exception e)
            {
                LogUtil.info($"inputCounterDataByXRayMachine retry:{retry}, {e.ToString()}");

                retry++;
                if (retry < 3)
                    goto retry;
                return null;
            }
            finally
            {
                LogUtil.info($"inputCounterDataByXRayMachine request:{json} response:{result}");
            }
        }

        public static ReelLocation DetermineReelStorageLocation(string TwoDBarcode)
        {
            var wc = new MyWebClient(15000);
            //if (string.IsNullOrEmpty(wc.Headers["Content-Type"]))
            //    wc.Headers.Add("Content-Type", "application/json;charset=UTF-8");

            wc.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";
            wc.Headers.Add("Accept", "application/json");
            wc.Encoding = Encoding.UTF8;

            var data = new Wiston_Request();
            data.userId = "Q14050052";
            data.language = 0;
            data.requestTime = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
            //data.requestTime = 0;
            data.data.Add("str2DBarcode", TwoDBarcode);
            data.data.Add("labelPrinter", ConfigHelper.Config.Get("upload_labelPrinter"));


            string json = JsonHelper.SerializeObject(data);
            string result = "";
            int retry = 0;
        retry:
            try
            {
                result = wc.UploadString(ConfigHelper.Config.Get("DetermineReelStorageLocation"), "POST", json);
                return JsonHelper.DeserializeJsonToObject<ReelLocation>(result);
            }
            catch (WebException ex)   // 先捕获 HTTP 级异常
            {
                string respBody = null;
                if (ex.Response != null)
                {
                    using (var sr = new StreamReader(ex.Response.GetResponseStream()))
                        respBody = sr.ReadToEnd();
                }

                LogUtil.info($"DetermineReelStorageLocation url:{ConfigHelper.Config.Get("DetermineReelStorageLocation")} retry:{retry}, " +
                             $"status:{(int?)((HttpWebResponse)ex.Response)?.StatusCode}, " +
                             $"responseBody:{respBody}, " +
                             $"exception:{ex}");
                retry++;
                if (retry < 3) goto retry;
                return null;
            }
            catch (Exception e)
            {
                LogUtil.info($"DetermineReelStorageLocation retry:{retry}, {e.ToString()}");

                retry++;
                if (retry < 3)
                    goto retry;
                return null;
            }
            finally
            {
                LogUtil.info($"DetermineReelStorageLocation request:{json} response:{result}");
            }
        }
    }

    class Wiston_Request
    {
        public string userId = "";
        public int language = 0;
        public string requestTime = "";
        public Dictionary<string, object> data = new Dictionary<string, object>();
    }
    class Wiston_Request2
    {
        public string userId = "";
        public int language = 0;
        public string requestTime = "";
        public Dictionary<string, object> data = new Dictionary<string, object>();
    }
    public class CountResult
    {
        public bool result;
        public string message;
        public string type;
        public long responseTime;
        public object data;
    }
    public class ReelLocation {
        public string isVacuum;
        public string isTower;
        public string isVTTower;
        public string message;
    }
}