SServerManager.cs 17.2 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class SServerManager
    {
        //http://localhost/myproject/service/store/emptyPosForPutin
        private static string Addr_PosForPutin = "/service/store/emptyPosForPutin";
        /// <summary>
        /// 1  皮带线扫码后调用,用于获取尺寸后升起气缸
        ///  地址: /rest/api/qisda/device/getSize
        /// </summary>
        private static string Addr_getSize = "/rest/api/qisda/device/getSize";

        /// <summary>
        /// 2  料盘流转位置信息更新
        ///     地址: /rest/api/qisda/device/updateLocInfo
        /// </summary>
        private static string Addr_updateLocInfo = "/rest/api/qisda/device/updateLocInfo";
        /// <summary>
        /// 3  放入料架(A,B,C,D)后调用,根据返回值决定当前料架是否放满,以及后续是否还有任务
        //        地址: /rest/api/qisda/device/putShelfFinished
        /// </summary>
        private static string Addr_putShelfFinished = "/rest/api/qisda/device/putShelfFinished";


        private 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;
        }
        //##
        private static string spiltStr = "##";
        public static string CodeReceived(string deviceName, int trayNum, List<string> codeList, int height, int width)
        {
            string msg = "";
            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 = "=1+0x0-" + width + "x" + height + "=" + str.Trim();
                    string code = "=" + width + "x" + height + "=" + str.Trim();
                    codeStr = codeStr + code + spiltStr;
                }
                if (String.IsNullOrEmpty(codeStr))
                {
                    return msg = deviceName + "未扫到条码";
                }
                //if (LineManager.Line.runStatus.Equals(LineRunStatus.Wait))
                //{
                //    return msg = deviceName + "托盘【" + trayNum + "】 收到条码<< " + codeStr + ",暂未开启,不需要发送服务器";
                //}

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

                string server = GetAddr(Addr_PosForPutin, paramMap);
                //string server = GetAddr(Addr_PosForPutin) + "?cids=" + LineServer.GetAllCID() + "&code=%3D" + codeStr;
                LogUtil.info(deviceName + "托盘【" + trayNum + "】 条码<< " + codeStr + ",获取入库PosID:" + server);

                string resultStr = HttpHelper.Post(server, "");
                LogUtil.info("CodeReceived  【" + server + "】【" + resultStr + "】");
                //{"result":"0","msg":"","pos":"11#AC1_18_4_28","barcode":"R506072019102200414","cid":"line-ac-11"}
                LineOperation serverResult = JsonHelper.DeserializeJsonToObject<LineOperation>(resultStr);
                if (serverResult == null)
                {
                    return msg = deviceName + " 条码【" + codeStr + "】没有收到服务器反馈";
                }
                else if ((!string.IsNullOrEmpty(serverResult.msg)) || serverResult.result.Equals(0).Equals(false))
                {
                    return msg = deviceName + " 条码【" + codeStr + "】  :" + serverResult.msg;
                }

                if (!serverResult.pos.Equals(""))
                {
                    string posId = serverResult.pos;
                    int plateW = width;
                    int plateH = height;
                    string[] posArray = posId.Split('#');
                    if (!(posArray.Length == 2))
                    {
                        return msg = deviceName + " 入库库位格式错误:条码【" + codeStr + "】库位【" + posId + "】";
                    }
                    int storeId = int.Parse(posArray[0]);
                    string wareNum = serverResult.barcode;
                    //根据库位号查找移栽
                    MoveEquip moveEquip = LineManager.Line.MoveEquipMap[storeId];
                    //  判断PosID是否已经在入库或者在排队列表中,如果已经存在,加入列表失败     
                    InOutParam param = new InOutParam(trayNum, wareNum, posId, plateH, plateW);
                    if (LineManager.Line.IsReviceInPosId(moveEquip, posId))
                    {
                        LineManager.Line.SetWarnMsg("入库库位重复: " + param.ToStr() + " ,入库失败!");
                        moveEquip.SetWarnMsg("入库库位重复: " + param.ToStr() + " ,入库失败!");
                        return msg = ("收到服务器入库命令  " + "入库库位重复: " + param.ToStr() + " ,入库失败!");
                    }
                    LogUtil.info("收到入库命令: " + param.ToStr() + " ,更新盘空满信息,托盘号【" + trayNum + "】,有料," + ReelType.InStore + "");

                    TrayManager.UpdateTrayInfo(trayNum, true, ReelType.InStore,new InOutParam(trayNum, wareNum, posId, plateH, plateW,false));

                    //TODO:判断BOX是否处于可以入库状态,如果调试或急停中,需要返回给服务器;
                    if (LineServer.BoxCanInStore(moveEquip.DeviceID))
                    {
                        LineServer.CheckInStorePos(storeId, param);
                    }
                    lock (moveEquip.waitInListLock)
                    {
                        //如果当前正在出入库中,需要记录下来,等待空闲时执行
                        LogUtil.info(moveEquip.Name + " 入库命令: "  + param.ToStr()  + "加入等待列表中!");
                        moveEquip.waitInStoreList.Add(param);
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(deviceName + " " + ex.ToString());
            }
            return "";
        } 
        public static string GetTraySize(string deviceName, int robotIndex, List<string> codeList, out int outSize)
        {
            outSize = 0;
            string msg = "";
            try
            {
                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;
                }
                if (String.IsNullOrEmpty(codeStr))
                {
                    return msg = deviceName + "未扫到条码";
                } 
                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                paramMap.Add("robotIndex", robotIndex.ToString());// 参数: robotIndex = 机器人编号,IP为51的机器人为1, 52的机器人为2, 53的机器人为3
                paramMap.Add("barcode", codeStr);//  barcode = 扫到的条码

                string server = GetAddr(Addr_getSize, paramMap);
                string resultStr = HttpHelper.Post(server, "");

                LogUtil.info("GetTraySize 【" + server + "】【" + resultStr + "】");
                //返回: { "code": 0, "msg":"ok", data: 7} 
                ServerData serverResult = JsonHelper.DeserializeJsonToObject<ServerData>(resultStr);
                if (serverResult == null)
                {
                    return msg = deviceName + " 【" + robotIndex + "】 条码【 " + codeStr + "】没有收到服务器反馈";
                }
                else if ( serverResult.code.Equals(0).Equals(false))
                {
                    //   code: 0为正常,其他为异常, 
                    // msg:消息, 
                    return msg = deviceName + " 【" + robotIndex + "】 条码【 " + codeStr + "】返回:" + "[" + serverResult.code + "]" + serverResult.msg;
                }

                if (!serverResult.data.Equals(""))
                {
                    //   data:料盘直径,= 7时升起气缸
                    outSize = Convert.ToInt32(serverResult.data);
                    LogUtil.info(deviceName + "【" + robotIndex + "】 条码【 " + codeStr + "】,获得尺寸:" + outSize);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(deviceName + " " + ex.ToString());
            }
            return "";
        }


        public static string UpdateTrayLoc(string deviceName, string barcode, string status, int locInfo)
        {
            string msg = "";
            try
            {
                if (String.IsNullOrEmpty(barcode))
                {
                    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.ToString());  // locInfo = 位置信息,移栽时为移栽编号,流水线时为托盘号,皮带线时为皮带线编号,机器人时为机器人编号
                string server = GetAddr(Addr_updateLocInfo, paramMap);
                string resultStr = HttpHelper.Post(server, "");
                LogUtil.info("UpdateTrayLoc  【" + server + "】【" + resultStr + "】");
                // 返回: { "code": 0, "msg":"ok", "data":""}
                ServerData serverResult = JsonHelper.DeserializeJsonToObject<ServerData>(resultStr);
                if (serverResult == null)
                {
                      msg = deviceName + "UpdateTrayLoc【 " + barcode + "】【" + status + "】【" + locInfo + "】没有收到服务器反馈";
                }
                else if (serverResult.code.Equals(0).Equals(false))
                {
                    // code: 0为正常,其他为异常, msg: 消息, data: 为空
                      msg = deviceName + " UpdateTrayLoc【 " + barcode + "】【" + status + "】【" + locInfo + "】  :" + "[" + serverResult.code + "]" + serverResult.msg;
                }
                if (!msg.Equals(""))
                {
                    LogUtil.error(msg);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(deviceName + " " + ex.ToString());
            }
            return msg;
        }


        public static string PutShelfFinished(string deviceName, string barcode, string rfid, string rfidPosId, out ShelfData shelfData)
        {
            string msg = "";
            shelfData = null;
            try
            {
                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                paramMap.Add("barcode", barcode);      // 参数: barcode=料盘的条码
                paramMap.Add("rfid", rfid);                     // rfid = 料架的RFID信息
                paramMap.Add("rfidLoc", rfidPosId);     // rfidLoc=料架的架位信息 
                // paramMap.Add("robotIndex", "0");          // robotIndex = 机器人编号(非机器人放置时不传此参数), IP为51的机器人为1, 52的机器人为2, 53的机器人为3
                string server = GetAddr(Addr_putShelfFinished, paramMap);
                string resultStr = HttpHelper.Post(server, "");
                LogUtil.info("PutShelfFinished  【" + server + "】【" + resultStr + "】");
                // 返回: {"code": 0, "msg":"ok", "data":{"rfid":"xxx","smallEmpty":0,"bigEmpty":0, "packageEmpty":0,"cutPackageTask":0,"packageTask":10,"cutTask":10, "smallTask":5, "bigTask":5}
                ServerData serverResult = JsonHelper.DeserializeJsonToObject<ServerData>(resultStr);
                if (serverResult == null)
                {
                    return msg = deviceName + "PutShelfFinished【 " + barcode + "】【" + rfid + "】【" + rfidPosId + "】没有收到服务器反馈";
                }
                else if (serverResult.code.Equals(0).Equals(false))
                {
                    return msg = deviceName + " PutShelfFinished【 " + barcode + "】【" + rfid + "】【" + rfidPosId + "】  :" + serverResult.msg;
                }
                if (String.IsNullOrEmpty(serverResult.data).Equals(false))
                {
                    shelfData = JsonHelper.DeserializeJsonToObject<ShelfData>(serverResult.data);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(deviceName + " " + ex.ToString());
            }
            return "";
        }       
    }
    public class LocStatus
    {
        /// <summary>
        /// 料盘位置:移栽 = MOVING
        /// </summary>
        public static string MOVING = "MOVING";
        /// <summary>
        /// 料盘位置:流水线 = INLINE
        /// </summary>
        public static string INLINE = "INLINE";
        /// <summary>
        /// 料盘位置:皮带线 = INBELT
        /// </summary>
        public static string INBELT = "INBELT";
    }
    public class ServerData
    {
        //{"code":0,"msg":"ok","data":"7"}
    //返回: {"code": 0, "msg":"ok", "data":{"rfid":"xxx","smallEmpty":0,"bigEmpty":0, "packageEmpty":0,"cutPackageTask":0,"packageTask":10,"cutTask":10, "smallTask":5, "bigTask":5}
    public int code { get; set; }

        public string msg { get; set; }

        public string data { get; set; }
    }

    public class ShelfData 
    {
        /// <summary>
        /// rfid: 当前料架的RFID
        /// </summary>
        public string rfid { get; set; }
        /// <summary>
        /// packageEmpty: 当前料架还可放置的包装料的数量(C料架和A料架有值, 其他料架为0)
        /// </summary>
        public int packageEmpty { get; set; }
        /// <summary>
        /// smallEmpty: 当前料架还可放置的小料盘(7x8)的数量(D料架, 其他料架为0)
        /// </summary>
        public int smallEmpty { get; set; }
        /// <summary>
        /// bigEmpty:当前料架还可放置的大料盘的数量(C料架, 其他料架为0)
        /// </summary>
        public int bigEmpty { get; set; }
        /// <summary>
        /// cutPackageTask:还有多少盘分盘的包装料任务(放到A料架上, 转运到分盘区)
        /// </summary>
        public int cutPackageTask { get; set; }
        /// <summary>
        /// packageTask:还有多少盘包装料任务(放到A料架上, 并转运到包装线, 最终到C料架)
        /// </summary>
        public int packageTask { get; set; }
        /// <summary>
        /// cutTask: 还有多少盘分盘料任务(放置到料串B上, 转运到分盘区)
        /// </summary>
        public int cutTask { get; set; }
        /// <summary>
        /// smallTask: 还有多少盘小料任务(放置到双层线的D料架上)
        /// </summary>
        public int smallTask { get; set; }
        /// <summary>
        /// bigTask: 还有多少盘大料任务(放置到C料架上)
        /// </summary>
        public int bigTask { get; set; }

        //         rfid: 当前料架的RFID
        //         packageEmpty: 当前料架还可放置的包装料的数量(C料架和A料架有值, 其他料架为0)
        //         smallEmpty: 当前料架还可放置的小料盘(7x8)的数量(D料架, 其他料架为0)
        //         bigEmpty:当前料架还可放置的大料盘的数量(C料架, 其他料架为0)
        //         cutPackageTask:还有多少盘分盘的包装料任务(放到A料架上, 转运到分盘区)
        //         packageTask:还有多少盘包装料任务(放到A料架上, 并转运到包装线, 最终到C料架)
        //         cutTask: 还有多少盘分盘料任务(放置到料串B上, 转运到分盘区)
        //         smallTask: 还有多少盘小料任务(放置到双层线的D料架上)
        //         bigTask: 还有多少盘大料任务(放置到C料架上)
    }
}