StoreServerManager.cs 5.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 StoreServerManager
    {
        private static string spiltStr = "%23%23";
        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()))
                    {
                        continue;
                    }
                    list.Add(str.Trim());
                    //string code = "=1+0x0-" + width + "x" + height + "=" + str.Trim();
                    string code = width + "x" + height + "%3D" + 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: 条码内容

                string server = ConfigAppSettings.GetValue(Setting_Init.http_server) + "?cids=" + LineServer.GetAllCID() + "&code=%3D" + codeStr;
                LogUtil.info(deviceName + "托盘【" + trayNum + "】 条码<< " + codeStr + ",获取入库PosID:" + server);
                 
                string resultStr = HttpHelper.Post(server, "");
                LogUtil.info("HttpHelper.Post结果:"+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, wareNum, posId, plateH, plateW);

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