SServerManager.cs 18.7 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
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace OnlineStore.DeviceLibrary
{
    public class SServerManager
    {
        #region 存储机构使用的API
        private static string api_communication = "service/store/communication";    //流水线状态通信接口 
        public static string GetPostApi(string host)
        {
            if (host == "")
            {
                host = ConfigAppSettings.GetValue(Setting_Init.http_server);
            }
            if (!host.StartsWith("http://"))
            {
                host = "http://" + host;
            }
            if (!host.EndsWith("/"))
            {
                host = host + "/";
            }
            return host + api_communication;
        }

        internal static string GetShelfIDByLoc(int rfidLoc, List<string> shelfPosList)
        {
            string shelfId = "";
            if (rfidLoc.Equals(-1))
            {
                shelfId = shelfPosList[0];
            }
            if (rfidLoc >= 1 && rfidLoc <= 8 && rfidLoc <= shelfPosList.Count)
            {
                shelfId = shelfPosList[rfidLoc - 1];
            }
            return shelfId;
        }
        #endregion
        //http://localhost/myproject/service/store/emptyPosForPutin
        private static string Addr_PosForPutin = "/service/store/robotBox/emptyPosForPutin";    

        private static string serverAddr = ConfigAppSettings.GetValue(Setting_Init.http_server);
        private static string GetAddr(string addr, Dictionary<string, string> paramsMap)
        {

            if (serverAddr.EndsWith("/"))
            {
                serverAddr = serverAddr.Substring(0, serverAddr.Length - 1);
            }
            string path = serverAddr + 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;
        }
        //## 100 循环获取库位。服务器超时:循环获取库位
        private static string spiltStr = "##";
       
        public static string ProcessCodeList(List<string> codeList)
        {
            string codeStr = "";
            List<string> list = new List<string>();
            foreach (string str in codeList)
            {
                if (list.Contains(str.Trim()) || String.IsNullOrEmpty(str.Trim()))
                {
                    continue;
                }
                codeStr = codeStr + str.Trim() + spiltStr;
            }
            return codeStr;
        }
        /// <summary>
        /// 获取库位号
        /// </summary>
        /// <param name="deviceName"></param>
        /// <param name="codeList"></param>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <param name="rfid"></param>
        /// <param name="lastPosId"></param>
        /// <returns></returns>
        public static GetPosResult GetPosId(string deviceName, List<string> codeList, int height, int width, string rfid, string lastPosId)
        {
            GetPosResult result = new GetPosResult();
            try
            {
                string codeStr = "";
                List<string> list = new List<string>();
                foreach (string str in codeList)
                {
                    if (list.Contains(str.Trim()) || String.IsNullOrEmpty(str.Trim()))
                    {
                        continue;
                    }
                    list.Add(str.Trim());
                    string code = "=" + width + "x" + height + "=" + str.Trim();
                    codeStr = codeStr + code + spiltStr;
                }
                codeStr = CodeManager.ReplaceCode(codeStr);

                //判断是否不连接服务器
                if (string.IsNullOrEmpty(serverAddr))
                {
                    LogUtil.error(deviceName + "GetPosId[" + codeStr + "][" + height + "][" + width + "]:未找到服务器地址 ");
                    result.Msg = "未找到服务器地址";
                    result.Param = InOutPosInfo.NewNgPos(codeStr, "", height, width, "");
                    return result;
                }

                //http://localhost/myproject/service/store/emptyPosForPutin
                // 参数:cids: 多个 cid
                //code: 条码内容
                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                paramMap.Add("cids", StoreManager.Config.CID);
                paramMap.Add("code", codeStr);
                //paramMap.Add(ParamDefine.rfid, rfid);
                //paramMap.Add(ParamDefine.lastPosId, lastPosId);

                string server = GetAddr(Addr_PosForPutin, paramMap);
                DateTime startTime = DateTime.Now;
                LogUtil.info(deviceName + "【" + rfid + "】【 " + codeStr + "】【"+ lastPosId + "】 ,获取入库库位:");

                string resultStr = HttpHelper.Post(server, "", Encoding.UTF8, 10000, out result.IsTimeOut);

                LogUtil.info(deviceName + "CodeReceived " + FormUtil.GetSpanStr(DateTime.Now - startTime) + " 【" + server + "】【" + resultStr + "】");
                if (result.IsTimeOut)
                {
                    return result;
                }
                //{"result":"0","msg":"","pos":"11#AC1_18_4_28","barcode":"R506072019102200414","cid":"line-ac-11"}
                ForPutinData serverResult = JsonHelper.DeserializeJsonToObject<ForPutinData>(resultStr);

                if (serverResult == null)
                {
                    result.Msg = deviceName + "  【" + codeStr + "】结果:没有收到服务器反馈 ";
                    //cancelPutInTask(deviceName, codeStr);
                    result.Param = InOutPosInfo.NewNgPos(codeStr, "", height, width, "没有收到服务器反馈");
                    result.Param.rfid = rfid;
                    result.Param.IsNG = true;
                    return result;
                }
                else if (serverResult.result.Equals(98))
                {
                    result.Result = serverResult.result;
                    result.Msg = "";
                    bool cutReel = serverResult.cutReel.ToLower().Equals("true");
                    bool urgentReel = serverResult.urgentReel.ToLower().Equals("true");
                    bool smallReel = serverResult.smallReel.ToLower().Equals("true");
                    int rfidloc = FormUtil.GetIntValue(serverResult.rfidLoc);
                    int pH = FormUtil.GetIntValue(serverResult.plateH);
                    int pW = FormUtil.GetIntValue(serverResult.plateW);
                    result.Param = new InOutPosInfo(serverResult.barcode, serverResult.pos, pW, pH, urgentReel, cutReel, smallReel, serverResult.rfid, rfidloc);

                    LogUtil.error(deviceName + "  【" + codeStr + "】结果入库NG:已有出库信息: " + result.Param.ToStr() + "  ");

                    result.Msg = "已有出库信息["+ serverResult.barcode + "][" + serverResult.pos + "] ";                   
                    result.Param = InOutPosInfo.NewNgPos(codeStr, "", height, width, "已有出库信息[" + serverResult.barcode + "][" + serverResult.pos + "]");
                    result.Param.rfid = rfid;
                    result.Param.IsNG = true;
                    return result; 
                }
                else if (serverResult.result.Equals(105))
                {
                    result.Result = serverResult.result;
                    //result.Msg = deviceName + "  【" + codeStr + "】结果:" + serverResult.msg;
                    result.Msg = "查找空库位失败";
                    result.Param = InOutPosInfo.NewNgPos(codeStr, "", height, width, serverResult.msg);
                    return result;
                }
                else if (serverResult.result.Equals(0))
                {
                    result.Result = serverResult.result;
                    //result.Msg = deviceName + "  【" + codeStr + "】结果:" + serverResult.msg;
                    result.Msg = serverResult.msg;
                    result.Param = new InOutPosInfo(codeStr,serverResult.pos,width,height);
                    result.Param.Cid = serverResult.cid;
                    return result;
                }
                else if(serverResult.result.Equals(99).Equals(false)&& serverResult.result.Equals(100).Equals(false))
                {
                    result.Result = serverResult.result;
                    //result.Msg = deviceName + "  【" + codeStr + "】结果:" + serverResult.msg;
                    result.Msg = serverResult.msg;
                    result.Param = InOutPosInfo.NewNgPos(codeStr, "", height, width, serverResult.msg);
                    return result;
                }
                result.Result = serverResult.result;
            }
            catch (Exception ex)
            {
                LogUtil.error(deviceName + " ", ex);
            }
            return result;
        }
        private static string Addr_getRobotTask = "/service/store/robotBox/getRobotTask";
        public static string RobotTaskInfo = "";
        /// <summary>
        /// 机器人定时获取出库任务
        /// </summary>
        /// <returns></returns>
        public static TaskData GetRobotTask()
        {
            string msg = "";
            TaskData serverResult=null;
            try
            {
                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                string server = GetAddr(Addr_getRobotTask, paramMap);
                DateTime startTime = DateTime.Now;
                string resultStr = HttpHelper.Post(server, "");
                // 返回: {"code":0,"data":{"type":"2","posId":"004-07-200","plateH":"1","barcode":"RI12313","cid":"4","plateW":"1"},
                // "msg":"ok","msgKey":"smfcore.ok","okResult":true,"params":[]}
                serverResult = JsonHelper.DeserializeJsonToObject<TaskData>(resultStr);
                RobotTaskInfo = $"【{server}】【{resultStr}】";
                if (serverResult == null)
                {
                    msg = "GetRobotTask 没有收到服务器反馈";
                }
                else if (serverResult.code.Equals(0).Equals(false))
                {
                    // code: 0为正常,其他为异常, msg: 消息, data: 为空
                    msg = "GetRobotTask" + " :" + "[" + serverResult.code + "]" + serverResult.msg;
                }
                if (!msg.Equals(""))
                {
                    LogUtil.error(msg);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("GetRobotTask", ex);
            }
            return serverResult;
        }
        private static string Addr_updateLocInfo = "/service/store/robotBox/updateLocInfo";
        
        /// <summary>
        /// 更新任务状态
        /// </summary>
        /// <param name="barcode">料盘条码</param>
        /// <param name="status">任务状态</param>
        /// <param name="locInfo">位置信息</param>
        /// <returns></returns>
        public static string UpdateTrayLoc( string barcode, string status, string locInfo="")
        {
            string msg = "";
            try
            {
                if (String.IsNullOrEmpty(barcode))
                {
                    return msg;
                }
                if (string.IsNullOrEmpty(serverAddr))
                {
                    LogUtil.error("UpdateTrayLoc   [" + barcode + "]  [" + status + "]  :未找到服务器地址");
                    return msg;
                }

                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                paramMap.Add("barcode", barcode);//barcode = 料盘的条码
                paramMap.Add("status", status);   // status = 状态信息, 移栽 = MOVING, 流水线 = INLINE, 皮带线 = INBELT
                paramMap.Add("locInfo", locInfo);  // locInfo = 位置信息,移栽时为移栽编号,流水线时为托盘号,皮带线时为皮带线编号,机器人时为机器人编号
                string server = GetAddr(Addr_updateLocInfo, paramMap);
                DateTime startTime = DateTime.Now;
                string resultStr = HttpHelper.Post(server, "");
                LogUtil.info("UpdateTrayLoc   " + FormUtil.GetSpanStr(DateTime.Now - startTime) + " 【" + server + "】【" + resultStr + "】");
                // 返回: { "code": 0, "msg":"ok", "data":""}
                ServerData serverResult = JsonHelper.DeserializeJsonToObject<ServerData>(resultStr);
                if (serverResult == null)
                {
                    msg = "UpdateTrayLoc【 " + barcode + "】【" + status + "】【" + locInfo + "】没有收到服务器反馈";
                }
                else if (serverResult.code.Equals(0).Equals(false))
                {
                    // code: 0为正常,其他为异常, msg: 消息, data: 为空
                    msg = " UpdateTrayLoc【 " + barcode + "】【" + status + "】【" + locInfo + "】  :" + "[" + serverResult.code + "]" + serverResult.msg;
                }
                if (!msg.Equals(""))
                {
                    LogUtil.error(msg);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("UpdateTrayLoc ", ex);
            }
            return msg;
        }


        //14.异常看板

        //   > 地址: 
        //>>/ rest / api / qisda / device / updateDeviceAlarmMsg


        //    >
        //    > 参数:
        //>> deviceAlarmList : 异常列表Json字符串  `[{"name":"移栽5", "msgKey":"line.move5.timeOut", "msgValue":"运动超时"},{"name":"移栽4", "msgKey":"line.move4.timeOut", "msgValue":"误差过大"}]`
        //>>>name :  异常位置名称
        //>>>msgKey :  异常信息唯一标识
        //>>>msgValue :  异常信息
        //>
        //>   返回: 
        //>>` {"code":0,"msg":"ok","data":""}`
        //>>
        //>> - code: 0为正常,其他为异常, 
        //          >> - msg:消息, 
        //          >> - data:
        //private static string Addr_updateDeviceAlarmMsg = "/rest/api/qisda/device/updateDeviceAlarmMsg";
        //public static string updateDeviceAlarmMsg(List<AlarmMsg> msgList)
        //{
        //    string msg = "";
        //    try
        //    {
        //        if (string.IsNullOrEmpty(serverAddr))
        //        { 
        //            return msg;
        //        }
        //        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, "",5000);
        //        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;
        //}
    }
    /// <summary>
    /// 任务状态
    /// </summary>
    public class TaskStatus
    {
        /// <summary>
        /// 机器人把料盘拿走之后,更新任务状态为:INROBOT
        /// </summary>
        public const string INROBOT = "INROBOT";
        /// <summary>
        /// 机器人把料放入料仓门口后,更新任务状态为:BOXDOOR
        /// </summary>
        public const string BOXDOOR = "BOXDOOR";
        /// <summary>
        /// 机器人把出库料盘放到出口后,更新任务状态为:FINISHED
        /// </summary>
        public const string FINISHED = "FINISHED";
    }
    public class AlarmMsg
    {

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

        public AlarmMsg(string n ,string key,string value)
        {
            this.name = n;
            this.msgKey = key;
            this.msgValue = value;
        }
    }
    public class GetPosResult
    {
        /// <summary>
        /// 消息字符串
        /// </summary>
        public string Msg="";
        /// <summary>
        /// 0=获取库位成功,98=此盘为出库盘。99:暂时不能入库,需要重新获取库位。
        /// 100:服务器需要更新,需要重新获取库位
        /// 105:查找空库位失败,NG
        /// </summary>
        public int Result;
        /// <summary>
        /// 获取超时,需要重新获取库位
        /// </summary>
        public bool IsTimeOut = false;

        public InOutPosInfo Param = null;
    }
    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; }
    }
    public class TaskData
    {
        //{"code":0,"data":{"type":"2","posId":"004-07-200","plateH":"1","barcode":"RI12313","cid":"4","plateW":"1"},
        //"msg":"ok","msgKey":"smfcore.ok","okResult":true,"params":[]}
        public int code { get; set; }
        public Dictionary<string, string> data { get; set; }
        public string msg { get; set; }
        public string msgKey { get; set; }
        public bool okResult { get; set; }
    }
    public class CancelData
    {
        //{"code":0,"msg":"ok","data":"7"} 
        public int code { get; set; }

        public string msg { get; set; }

        public object data { get; set; }
    }

    public class ServerData
    {
        //{"code":0,"msg":"ok","data":"7"} 
        public int code { get; set; }

        public string msg { get; set; }

        public string data { get; set; } 
    }
}