HttpServer.cs 12.6 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 = "/updateDeviceAlarmMsg";
       
        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 + "】");

                RfidData data = JsonHelper.DeserializeJsonToObject<RfidData>(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;
        }
        private static string GetAddr(string addr, Dictionary<string, string> paramsMap)
        {
            string server = ConfigAppSettings.GetValue(Setting_Init.httpAddr);
            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;
        }
        private static string httpAddr = ConfigAppSettings.GetValue(Setting_Init.httpAddr);


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

            string api = "putShelfFinished";
            DateTime startTime = DateTime.Now;
            try
            {
                string url = httpAddr + api + "?barcode=" + barcode + "&rfid=" + rfid + "&rfidLoc=" + rfidLoc + "&robotIndex=" + robotIndex;
                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;

                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 AllTaskInfo GetATaskInfo(string rfid = "", string barcode = "", string rfidLoc = "", string robotIndex = "")
        {
            AllTaskInfo task = new AllTaskInfo();
            string api = "putShelfFinished";

            try
            {
                string url = httpAddr + api + "?barcode=" + barcode + "&rfid=" + rfid + "&rfidLoc=" + rfidLoc + "&robotIndex=" + robotIndex; 
                string logName = "http :URL:" + url + " , "; 
                string json = HttpHelper.Post(url, "", 10000);
                if (string.IsNullOrWhiteSpace(json)) return task;

                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.info(logName + ": " + 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(logName + ": data=null");
                    return task;
                }
                if (dict.TryGetValue("bigTask", out value))
                    int.TryParse(value.ToString(), out task.bigTask);
                if (dict.TryGetValue("smallTask", out value))
                    int.TryParse(value.ToString(), out task.smallTask);
             
                if (task.bigTask > 0 || task.smallTask > 0)
                {
                    LogUtil.debug(logName + " Response:" + json);
                }
                else
                {
                    LogUtil.debug(logName + " Response:" + json);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("http error  : " + ex.ToString());
            }
         
            return task;
        }
        /// <summary>
        /// 获取定位机构上的料盘信息
        /// </summary>
        /// <param name="robot">ABB机器人索引号</param>
        /// <param name="currRFID">当前工位的RFID</param> 
        public static TrayInfo GetLocation(int robot, string currRFID, out string msg)
        {
            TrayInfo tray = new TrayInfo();
            string api = "getLocation";
            msg = "";
            try
            {
                string url = httpAddr + api + "?robotIndex=" + robot + "&rfid=" + currRFID;

                string logName = "http :URL:" + url + " , ";
                DateTime startTime = DateTime.Now;
                string json = HttpHelper.Post(url, "", 10000);
                LogUtil.debug(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 + robot);
                    }
                    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("w", out value))
                {
                    switch (value)
                    {
                        case "7": tray.getP = "p1"; break;
                        case "11": tray.getP = "p2"; break;
                        case "13": tray.getP = "p3"; break;
                        case "15": tray.getP = "p4"; break;
                    }
                }
                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
    {
        public int robotNum = 0;
        public string getP = "";
        public int shelfP = 0;
        public string rfid = "";
        public string realRFID = "";
        public string barcode = "";
        public string usedRfidList = "";
        public string ToStr()
        {
            return "[" + robotNum + "]  [" + barcode + "]  [" + getP + "]  [" + 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 RfidData
    {
        //{"code":0,"msg":"ok","data":"7"} 
        public int code { get; set; }

        public string msg { get; set; }

        public Dictionary<string, string> data { get; set; }
    }
}