HttpServer.cs 15.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
using OnlineStore.Common; 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace DeviceLibrary
{
    public class HttpServer
    {

        public HttpServer()
        {
        }
        private static string Addr_updateDeviceAlarmMsg = "/rest/api/device/shelf/updateDeviceAlarmMsg"; 
        private static string Addr_ShelfFinish = "/rest/api/shelf/putShelfFinished";
        private static string Addr_getLocation = "/rest/api/shelf/getLocation";
        private static string Addr_getShelfEmptySlot = "/rest/api/shelf/getShelfEmptySlot";
        private static string Addr_clearRfid = "/rest/api/shelf/clearRfid";
        /// <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;
                }

                ResultData result = JsonHelper.DeserializeJsonToObject<ResultData>(json);
                if (result.code > 0||result.data==null)
                {
                    LogUtil.error("getShelfEmptySlot,rfids[" + rfids + "]结果:code=" + result.code + ",msg=" + result.msg);
                }
                else
                {
                    foreach(string key in result.data.Keys)
                    {
                        int v =Convert.ToInt32( result.data[key].ToString());
                        map.Add(key, v);
                    } 
                    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 =  Setting_Init.Device_Server_Address;
            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 getData(Dictionary<string, Object> data, string key)
        {
            if (data.ContainsKey(key))
            {
                return data[key].ToString();
            }
            return "";
        }
        private static  int getIntData(Dictionary<string, Object> data, string key)
        {
            try
            {
                return Int32.Parse(getData(data, key));
            }
            catch (Exception ex)
            {

            }
            return -1;
        }
        /// <summary>
        /// 放料到料架完成通知
        /// </summary>
        /// <param name="rfid"></param>
        /// <param name="barcode"></param>
        /// <param name="rfidLoc"></param>
        /// <param name="robotIndex"></param>
        /// <returns></returns>
        public static ShelfTaskInfo ShelfFinish(string rfid, string barcode = "", string rfidLoc = "" )
        {
            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 resultStr = HttpHelper.Post(url, "", 10000);
                if (barcode != "")
                {
                    LogUtil.info("http :URL:" + url + " :Response:" + resultStr + " 耗时[" + FormUtil.GetSpanStr(DateTime.Now - startTime) + "]");
                }
                else
                {
                    LogUtil.debug("http :URL:" + url + " :Response:" + resultStr);
                }
                ResultData data = JsonHelper.DeserializeJsonToObject<ResultData>(resultStr);

                if (data == null || data.code != 0)
                { 
                    return null;
                }
                else
                {
                    task.bigEmpty = getIntData(data.data, "bigEmpty");
                    task.smallEmpty = getIntData(data.data, "smallEmpty");
                    task.usedRfidList = getData(data.data, "usedRfidList"); 
                }
               
            }
            catch (Exception ex)
            {
                LogUtil.error("http error  : " + ex.ToString());
            }
            return task;
        }

        /// <summary>
        /// 根据条码获取位置
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="currRFID"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        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 resultStr = HttpHelper.Post(url, "", 5000);
                LogUtil.info(logName + ": Response:" + resultStr + ",耗时【" + FormUtil.GetSpanStr(DateTime.Now - startTime) + "】");

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

                if (data == null || data.code != 0)
                {
                    tray = TrayInfo.newNgTray(barcode, data.msg);

                    if (data.data != null)
                    {
                        int h = getIntData(data.data, "h");
                        int w = getIntData(data.data, "w");
                        string nBarcode = getData(data.data, "barcode");
                        tray.h = h;
                        tray.w = w;
                        tray.barcode = nBarcode;
                    }

                    return tray;
                }
                string dataStr = JsonHelper.SerializeObject(data.data);
                tray = JsonHelper.DeserializeJsonToObject<TrayInfo>(dataStr);
                if (tray == null || tray.rfid == "" || tray.rfidLoc == 0)
                {
                    //msg = "获取位置失败";
                    if (tray == null)
                    {
                        tray = TrayInfo.newNgTray(barcode, "获取位置失败");
                    }
                    tray.ngReel = true;
                    tray.ngMsg = "获取位置失败";
                    return tray;
                }
            }
            catch (Exception ex)
            {
                msg = "http error:" + ex.ToString();
                LogUtil.error("http error  : " + ex.ToString());
            }
            //tray.robotNum = robot;
            return tray;
        }
         
        public static string ClearRfid(string rfid, string orderNo)
        {  
            DateTime startTime = DateTime.Now;
            try
            {
                string api = Addr_clearRfid;
                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                paramMap.Add("orderNo", orderNo);
                paramMap.Add("rfid", rfid);
                string url = GetAddr(api, paramMap);
                LogUtil.debug("http :URL:" + url);

                string resultStr = HttpHelper.Get(url);

                LogUtil.info("http :URL:" + url + " :Response:" + resultStr + " 耗时[" + FormUtil.GetSpanStr(DateTime.Now - startTime) + "]");
                return resultStr;
            }
            catch (Exception ex)
            {
                LogUtil.error("http error  : " + ex.ToString());
            }
            return "";
        }
    }
    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 rfidLoc = 0;
         
        /// <summary>
        /// 虚拟料架号
        /// </summary>
        public string rfid = "";
        /// <summary>
        /// 真实料架号
        /// </summary>
        public string realRfid = "";
        /// <summary>
        /// 条码信息
        /// </summary>
        public string barcode = "";
        /// <summary>
        /// 已使用的真实料架号
        /// </summary>
        public string usedRfidList = "";
        /// <summary>
        /// 工单号,工单号为空表示紧急出料
        /// </summary>
        public string orderNo = "";

        public string getShelfLoc(string name)
        {
            string reLoc = name + "_B1_" + rfidLoc;
            if (rfidLoc <= 13)
            {
                reLoc = name + "_A1_" + rfidLoc;
            }
            else if (rfidLoc <= 26)
            { 
                reLoc = name + "_A2_" + (rfidLoc - 13);
            }
            return reLoc;
        }

        public int w = 7;
        public int h = 8;

        public bool ngReel = false;

        public string ngMsg = "";

        public string ToStr()
        {
            if (ngReel)
            { 
                return "NGReel: [" + barcode + "]["+w+"X"+h+"] [" + ngMsg + "] [" + updateTime.ToLongTimeString() + "]";
            }
            else
            {
                return " [" + barcode + "] [" + w + "X" + h + "] [" + rfidLoc + "]  [" + rfid + "]  [" + realRfid + "]  [" + usedRfidList + "] [" + updateTime.ToLongTimeString() + "]";
            }
        }
        public DateTime updateTime = DateTime.Now;

        public TrayInfo()
        {

        }
        public static TrayInfo newNgTray(string barcode, string ngMsg)
        {
            TrayInfo tray = new TrayInfo();
            tray.ngMsg = ngMsg;
            tray.ngReel = true;
            tray.barcode = barcode;
            return tray;
        }

    }

    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; }
    //}
   
}