HttpServer.cs 19.9 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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
using OnlineStore.Common;
using RestSharp;
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
    {
        //  private Asa.File.Log log = DoubleLine.Common.log;
        public delegate void StatusEmptyEvent(string[] s);
        public static event StatusEmptyEvent StatusEmpty;

        public HttpServer()
        {
        }
        private static string Addr_CurSO = "/currentOutLine";
        /// <summary>
        /// 查询当前出料工单信息
        /// </summary>
        /// <returns></returns>
        public static bool FindCurSO(out WOData woData)
        {
            woData = new WOData();
            try
            {
                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                //paramMap.Add("deviceAlarmList", msgListStr);
                string server = GetAddr(Addr_CurSO, paramMap);
                DateTime startTime = DateTime.Now;
                string resultStr = HttpHelper.Get(server);
                LogUtil.debug("当前正在出工单料信息【" + server + "】【" + resultStr + "】");

                WorkOrder workOrder = JsonHelper.DeserializeJsonToObject<WorkOrder>(resultStr);

                if (workOrder == null)
                {
                    return false;
                }
                else
                {
                    if (workOrder.data == null)
                    {
                        return false;
                    }
                    else
                    {
                        if (workOrder.data != null)
                        {
                            woData = workOrder.data;
                            return true;
                        }

                    }

                }

            }
            catch (Exception ex)
            {
                LogUtil.error("FindCurSO", ex);
                return false;
            }
            return false;
        }
        private static string Addr_updateDeviceAlarmMsg = "/updateDeviceAlarmMsg";
        /// <summary>
        /// 异常看板
        /// </summary>
        /// <param name="msgList"></param>
        /// <returns></returns>
        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 = "")
        {
            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);

                //       var client = new RestClient(url) { Timeout = -1 };
                //     var request = new RestRequest(Method.GET);
                //   IRestResponse response = client.Execute(request); 
                //  string json = response.Content;  
                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("packageEmpty", out value))
                //    int.TryParse(value.ToString(), out task.packageEmpty);
                task.packageEmpty = 0;
                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;
                // LogUtil.info("http :URL:" + url);

                string logName = "http :URL:" + url + " , ";
                //var client = new RestClient(url) { Timeout = -1 };
                //var request = new RestRequest(Method.GET);
                //IRestResponse response = client.Execute(request);
                //string json = response.Content;
                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 (dict.TryGetValue("packageTask", out value))
                //    int.TryParse(value.ToString(), out task.packageTask);
                task.packageTask = 0;
                StatusEmpty?.Invoke(new string[] { task.smallTask.ToString(), task.bigTask.ToString(), task.packageTask.ToString() });
                if (task.bigTask > 0 || task.smallTask > 0 || task.packageTask > 0)
                {
                    LogUtil.debug(logName + " Response:" + json);
                }
                else
                {
                    LogUtil.debug (logName + " Response:" + json);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("http error  : " + ex.ToString());
            }
            //  task.bigTask = 1;
       //    task.smallTask = 1;
            return task;
        }
        /// <summary>
        /// 获取定位机构上的料盘信息
        /// </summary>
        /// <param name="robot">ABB机器人索引号</param>
        /// <param name="currRFID">当前工位的RFID</param> 
        public static TrayInfo GetDiskInfo(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;
        }

        /// <summary>
        /// 包装料机器人获取取出和放置位置
        /// </summary>
        public static ResultBean GetPackInfo(string bigRfid, string packageRfid)
        {
            string api = "getPackageLocation";

            try
            {
                string url = httpAddr + api + "?bigRfid=" + bigRfid + "&packageRfid=" + packageRfid;
                LogUtil.debug("http :URL:" + url);

                var client = new RestClient(url) { Timeout = -1 };
                var request = new RestRequest(Method.GET);
                IRestResponse response = client.Execute(request);
                string json = response.Content;
                LogUtil.debug("http :Response:" + json);
                if (string.IsNullOrWhiteSpace(json)) return null;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                ResultBean resultBean = serializer.Deserialize<ResultBean>(json);
                return resultBean;
            }
            catch (Exception ex)
            {
                LogUtil.error("http error  : " + ex.ToString());
            }
            return null;
        }
        public class ResultBean
        {
            public ResultBean()
            {

            }
            public int code;
            public string msg;
            public PackageRobotTaskBean data;
        }
        public class PackageRobotTaskBean
        {
            public PackageRobotTaskBean()
            {

            }
            public string barcode = "";
            public int emptyInBig = 0;
            public int reelInPackage = 0;
            public int packageTask = 0;
            public string packageRfid = "";
            public int packageLoc = 0;
            public int bigLoc = 0;
            public string bigRfid = "";
            public string realBigRfid = "";
        }

        /// <summary>
        /// 包料料机器人放置包装料完成
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="packageRfid"></param>
        /// <param name="packageLoc"></param>
        /// <param name="bigRfid"></param>
        /// <param name="bigLoc"></param>
        public static void PackFinish(string barcode, string packageRfid, int packageLoc, string bigRfid, int bigLoc)
        {
            string api = "putPackageFinished";

            try
            {
                string url = httpAddr + api + "?barcode=" + barcode + "&packageRfid=" + packageRfid + "&packageLoc=" + packageLoc + "&bigRfid=" + bigRfid + "&bigLoc=" + bigLoc;
                LogUtil.info("http :URL:" + url);

                var client = new RestClient(url) { Timeout = -1 };
                var request = new RestRequest(Method.GET);
                IRestResponse response = client.Execute(request);
                string json = response.Content;
                LogUtil.info("http :Response:" + json);
                if (string.IsNullOrWhiteSpace(json)) return;

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


    }
    /// <summary>
    /// 当前正在出的工单信息
    /// </summary>
    public class WorkOrder
    {
        //{"code":0,"msg":"ok","data":{"hSerial":"32622","line":null,"so":"2506287"}}
        //{"code":0,"msg":"ok","data":{"hSerial":"32651","line":"D2","so":"2508551"}}
        public int code { get; set; }
        public string msg { get; set; }
        public WOData data { get; set; }

    }
    public class WOData
    {
        public string hSerial { get; set; } = "";
        public string line { get; set; } = "";
        public string so { get; set; } = "";

        public string ToTxt()
        {
            return string.Format("[hSerial={0},line={1},so={2}]", hSerial, line, so);
        }
    }
    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 && packageEmpty != -1)
            {
                return true;
            }
            return false;
        }

        public string ToStr()
        {
            return "  " + rfid + "剩余位置:  小料=" + smallEmpty + ",大料=" + bigEmpty + ",包装料=" + packageEmpty + ",已使用料架=" + usedRfidList + " ";
        }
    }
    public class AllTaskInfo
    {
        public int smallTask = -1;
        public int bigTask = -1;
        public int packageTask = -1;
        public string ToStr()
        {
            return " 剩余任务: 小料=" + smallTask + ",大料=" + bigTask + ",包装料=" + packageTask + " ";
        }
        public bool IsValid()
        {
            if (smallTask != -1 && bigTask != -1 && packageTask != -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; }
    }
}