HttpServer.cs 14.4 KB
using OnlineStore.Common; 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace OnlineStore.DeviceLibrary
{
    public class HttpServer
    {

        public HttpServer()
        {
        }
        private static string Addr_updateDeviceAlarmMsg = "/rest/api/qisda/device/updateDeviceAlarmMsg"; 
        private static string Addr_ShelfFinish = "/rest/api/qisda/device/putShelfFinished";
        private static string Addr_getLocation = "/rest/api/qisda/device/getLocation";
        private static string Addr_getShelfEmptySlot = "/rest/api/dcs/device/getShelfEmptySlot";
        /// <summary>
        ///   rest/api/dcs/device/getShelfEmptySlot
        ///   获取当前任务数及料架的剩余空位
        ///   参数:rfids 料架rfid列表,逗号分割,未发送rfids只返回当前剩余任务数量 
        /// </summary>
        /// <param name="rfids"></param>
        /// <returns></returns>
        public static Dictionary<string, int> getShelfEmptySlot(string rfids)
        {

            Dictionary<string, int> map = new Dictionary<string, int>();
            DateTime startTime = DateTime.Now;
            try
            {
                string api = Addr_getShelfEmptySlot;
                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                paramMap.Add("rfids", rfids);
                string url = GetAddr(api, paramMap);
                LogUtil.debug("http :URL:" + url);

                string json = HttpHelper.Post(url, "", 2000);
                if (rfids != "")
                {
                    LogUtil.debug("http :URL:" + url + " :Response:" + json + " 耗时[" + FormUtil.GetSpanStr(DateTime.Now - startTime) + "]");
                }
                else
                {
                    LogUtil.debug("http :URL:" + url + " :Response:" + json);
                }
                if (string.IsNullOrWhiteSpace(json))
                {
                    return map;
                }

                RFIDResultData result = JsonHelper.DeserializeJsonToObject<RFIDResultData>(json);
                if (result.code > 0||result.data==null)
                {
                    LogUtil.error("getShelfEmptySlot,rfids[" + rfids + "]结果:code=" + result.code + ",msg=" + result.msg);
                }
                else
                {
                    map = result.data;
                    return map;
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("http getShelfEmptySlot error  : " + ex.ToString());
            }
            return map;
        }


        //private static string Addr_putShelfFinished = "/rest/api/qisda/device/putShelfFinished";
        //        取消出库任务地址:   /cancelOutTask      //参数: barcode
        //private static string Addr_cancelPutInTask = "/rest/api/qisda/device/cancelOutTask";
        public static string updateDeviceAlarmMsg(List<AlarmMsg> msgList)
        {
            string msg = "";
            try
            {
                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                string msgListStr = JsonHelper.SerializeObject(msgList);
                paramMap.Add("deviceAlarmList", msgListStr);
                string server = GetAddr(Addr_updateDeviceAlarmMsg, paramMap);
                DateTime startTime = DateTime.Now;
                string resultStr = HttpHelper.Post(server, "", 2000);
                LogUtil.debug("updateDeviceAlarmMsg    " + FormUtil.GetSpanStr(DateTime.Now - startTime) + "  【" + server + "】【" + resultStr + "】");

                ResultData data = JsonHelper.DeserializeJsonToObject<ResultData>(resultStr);

                if (data == null)
                {
                    return msg = " updateDeviceAlarmMsg 没有收到服务器反馈";
                }
                else if (data.code.Equals(0).Equals(false))
                {
                    return msg = " updateDeviceAlarmMsg   【" + server + "】【" + resultStr + "】" + data.msg;
                }
                return "";
            }
            catch (Exception ex)
            {
                LogUtil.error("  updateDeviceAlarmMsg Error:  " + ex.ToString());
            }
            return msg;
        } 
        public static string GetAddr(string addr, Dictionary<string, string> paramsMap)
        {
            string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
            if (server.EndsWith("/"))
            {
                server = server.Substring(0, server.Length - 1);
            }
            string path = server + addr.Trim() + "?";
            foreach (string paramName in paramsMap.Keys)
            {
                string par = System.Web.HttpUtility.UrlEncode(paramsMap[paramName], System.Text.Encoding.UTF8);
                path += paramName + "=" + par + "&";
            }
            path = path.Substring(0, path.Length - 1);
            return path;
        }
     


        public static ShelfTaskInfo ShelfFinish(string rfid, string barcode = "", string rfidLoc = "", string robotIndex = "1")
        {
            ShelfTaskInfo task = new ShelfTaskInfo();
            task.rfid = rfid;

            DateTime startTime = DateTime.Now;
            try
            {
                string api = Addr_ShelfFinish;
                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                paramMap.Add("barcode", barcode);
                paramMap.Add("rfid", rfid);
                paramMap.Add("rfidLoc", rfidLoc);
                paramMap.Add("robotIndex", robotIndex);
                //string url = httpAddr + api + "?barcode=" + barcode + "&rfid=" + rfid + "&rfidLoc=" + rfidLoc + "&robotIndex=" + robotIndex;
                string url = GetAddr(api, paramMap);
                LogUtil.debug("http :URL:" + url);
                 
                string json = HttpHelper.Post(url, "", 10000);
                if (barcode != "")
                {
                    LogUtil.info("http :URL:" + url + " :Response:" + json + " 耗时[" + FormUtil.GetSpanStr(DateTime.Now - startTime) + "]");
                }
                else
                {
                    LogUtil.debug("http :URL:" + url + " :Response:" + json);
                }
                if (string.IsNullOrWhiteSpace(json)) return task;
                //行 2234: [2021 - 04 - 07 15:09:31,412][9]INFO - http :URL: 
                //http://192.168.100.14/myproject/rest/api/qisda/device/putShelfFinished?barcode=640253A*34005600000309*QG00006*5000*23C4&rfid=F103&rfidLoc=8&robotIndex=1 :
                //Response:{"code":0,"msg":"ok","data":{"smallTask":"0","cutPackageTask":"0","packageTask":"0","bigTask":"0","smallEmpty":"0","bigEmpty":"5","packageEmpty":"0","rfid":"F103","usedRfidList":"F106,F105,F103","barcode":"640253A*34005600000309*QG00006*5000*23C4","cutTask":"0"}} 耗时[00:00:00.1]

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> obj = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (!obj.TryGetValue("code", out object value)) return task;
                if (value.ToString() != "0")
                {
                    if (obj.TryGetValue("msg", out value))
                        LogUtil.error("http" + api + ": " + value.ToString());
                    return task;
                }
                if (!obj.TryGetValue("data", out value)) return task;
                Dictionary<string, object> dict = (Dictionary<string, object>)value;
                if (dict == null)
                {
                    LogUtil.info("http" + api + ": data=null");
                    return task;
                }

                if (dict.TryGetValue("bigEmpty", out value))
                    int.TryParse(value.ToString(), out task.bigEmpty);
                if (dict.TryGetValue("smallEmpty", out value))
                    int.TryParse(value.ToString(), out task.smallEmpty); 

                if (dict.TryGetValue("usedRfidList", out value))
                    task.usedRfidList = value.ToString();
            }
            catch (Exception ex)
            {
                LogUtil.error("http error  : " + ex.ToString());
            }
            return task;
        }

         
        public static TrayInfo GetLocation(string barcode, string currRFID, out string msg)
        {
            //getLocation这个接口传入barcode和rfid列表会分配料架
            TrayInfo tray = new TrayInfo();           
            msg = "";
            try
            {
                //string api = "getLocation" + "?barcode=" + barcode + "&rfid=" + currRFID;
                //string url = httpAddr + api;
                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                paramMap.Add("barcode", barcode);
                paramMap.Add("rfid", currRFID); 
              
                string url = GetAddr(Addr_getLocation, paramMap);
                string logName = "http :URL:" + url;
                DateTime startTime = DateTime.Now;
                string json = HttpHelper.Post(url, "", 10000);
                LogUtil.info(logName + ": Response:" + json + ",耗时【" + FormUtil.GetSpanStr(DateTime.Now - startTime) + "】");
                if (string.IsNullOrWhiteSpace(json))
                {
                    return tray;
                }
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> obj = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (!obj.TryGetValue("code", out object value)) return tray;
                if (value.ToString() != "0")
                {
                    if (obj.TryGetValue("msg", out value))
                    {
                        msg = value.ToString();
                        LogUtil.error(logName + ": " + value.ToString(), 500  );
                    }
                    return tray;
                }
                if (!obj.TryGetValue("data", out value)) return tray;
                Dictionary<string, object> dict = (Dictionary<string, object>)value;
                if (dict == null)
                {
                    LogUtil.info(logName + ": data=null");
                    return tray;
                }
                 
                if (dict.TryGetValue("rfidLoc", out value))
                    int.TryParse(value.ToString(), out tray.shelfP);
                if (dict.TryGetValue("rfid", out value))
                    tray.rfid = value.ToString();
                if (dict.TryGetValue("realRfid", out value))
                    tray.realRFID = value.ToString();
                if (dict.TryGetValue("barcode", out value))
                    tray.barcode = value.ToString();

                if (dict.TryGetValue("usedRfidList", out value))
                    tray.usedRfidList = value.ToString();
            }
            catch (Exception ex)
            {
                LogUtil.error("http error  : " + ex.ToString());
            }
            //tray.robotNum = robot;
            return tray;
        }
   
         
    }
    public class TrayInfo
    {
        //Response:{"code":0,"msg":"ok","data":{"w":"7","realRfid":"","h":"8","rfid":"1-2F","usedRfidList":"F102","rfidLoc":"11","barcode":"985022*35030377*0822*3000*08220350"}}
        /// <summary>
        /// 料架位置,1-31
        /// </summary>
        public int shelfP = 0;
        /// <summary>
        /// 虚拟料架号
        /// </summary>
        public string rfid = "";
        /// <summary>
        /// 真实料架号
        /// </summary>
        public string realRFID = "";
        /// <summary>
        /// 条码信息
        /// </summary>
        public string barcode = "";
        /// <summary>
        /// 已使用的真实料架号
        /// </summary>
        public string usedRfidList = "";
        public string ToStr()
        {
            return " [" + barcode + "] [" + shelfP + "]  [" + rfid + "]  [" + realRFID + "]  [" + usedRfidList + "] [" + updateTime.ToLongTimeString() + "]";
        }
        public DateTime updateTime = DateTime.Now;

    }

    public class ShelfTaskInfo
    {
        public string rfid = "";
        public int bigEmpty = -1;
        public int smallEmpty = -1;
        //public int packageEmpty = -1; 
        public string usedRfidList = "";
        public bool IsValid()
        {
            if (bigEmpty != -1 && smallEmpty != -1)
            {
                return true;
            }
            return false;
        }

        public string ToStr()
        {
            return "  " + rfid + "剩余位置:  小料=" + smallEmpty + ",大料=" + bigEmpty + ",已使用料架=" + usedRfidList + " ";
        }
    }
    public class AllTaskInfo
    {
        public int smallTask = -1;
        public int bigTask = -1;
        //public int packageTask = -1;
        public string ToStr()
        {
            return " 剩余任务: 小料=" + smallTask + ",大料=" + bigTask + " ";
        }
        public bool IsValid()
        {
            if (smallTask != -1 && bigTask != -1)
            {
                return true;
            }
            return false;
        }
    }

    public class AlarmMsg
    {

        //>>>name :  异常位置名称
        public string name = "";
        //>>>msgKey :  异常信息唯一标识
        public string msgKey = "";
        //>>>msgValue :  异常信息
        public string msgValue = "";

        /// <summary>
        /// 异常信息
        /// </summary>
        /// <param name="name">异常位置名称</param>
        /// <param name="key">异常信息唯一标识</param>
        /// <param name="value">异常信息</param>
        public AlarmMsg(string name, string key, string value)
        {
            this.name = name;
            this.msgKey = key;
            this.msgValue = value;
        }
    }
    public class ResultData
    { 
        public int code { get; set; }

        public string msg { get; set; }

        public object data { get; set; }
    }
    public class RFIDResultData
    {
        public int code { get; set; }

        public string msg { get; set; }

        public Dictionary<string,int> data { get; set; }
    }
    //public class ResultData
    //{
    //    //{"code":0,"msg":"ok","data":"7"} 
    //    public int code { get; set; }

    //    public string msg { get; set; }

    //    public object data { get; set; }
    //}
}