BoxBean_Shelf.cs 21.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
using Asa;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public partial class BoxBean
    { 
         
        #region 与服务器通信定时器

        private string CodeMsg = "";
        private bool isInProcess = false;
        private DateTime lastConTime = DateTime.Now;
        public void server_connect_timer_Tick(object sender, EventArgs e)
        {
            if (isInProcess)
            {
                TimeSpan span = DateTime.Now - lastConTime;
                if (span.TotalSeconds < 60)
                {
                    return;
                }
            }
            isInProcess = true;
            lastConTime = DateTime.Now;
            try
            {
                //HumitureController.QueryData();
                humBean.HumidityProcess(this);
                if (StoreManager.IsConnectServer)
                {

                    SendLineStatus();

                }

                if (OutDoorReelType.Equals(1))
                {
                    IOMove(IO_Type.DoorLed_Red, IO_VALUE.LOW, true);
                    if (IOValue(IO_Type.DoorLed_Green).Equals(IO_VALUE.LOW))
                    { 
                        IOMove(IO_Type.DoorLed_Green, IO_VALUE.HIGH);
                    }
                    else
                    { 
                        IOMove(IO_Type.DoorLed_Green, IO_VALUE.LOW);
                    }
                }
                else if (OutDoorReelType.Equals(2))
                {
                    IOMove(IO_Type.DoorLed_Green, IO_VALUE.LOW, true);
                    if (IOValue(IO_Type.DoorLed_Red).Equals(IO_VALUE.LOW))
                    {
                        IOMove(IO_Type.DoorLed_Red, IO_VALUE.HIGH);
                    }
                    else
                    {
                        IOMove(IO_Type.DoorLed_Red, IO_VALUE.LOW);
                    }
                }
                else
                {
                    IOMove(IO_Type.DoorLed_Green, IO_VALUE.LOW, true);
                    IOMove(IO_Type.DoorLed_Red, IO_VALUE.LOW, true);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "定时SendLineStatus出错:" + ex.ToString());
            }
            finally
            {
                isInProcess = false;
            }
        }

        /// <summary>
        /// 获取整个料仓的状态
        /// </summary> 
        public Operation getLineBoxStatus()
        {
            //构建发送给服务器的对象
            Operation lineOperation = new Operation();
            lineOperation.msg = "";
            lineOperation.alarmList = new List<AlarmInfo>();
            lineOperation.cid = CID;
            lineOperation.seq = ConfigAppSettings.nextSeq();
            lineOperation.status = 1;
            if (WarnMsg != "")
            {
                lineOperation.status = (int)StoreStatus.Warning;
                lineOperation.msg = WarnMsg;
            }
            lineOperation.status = (int)StoreStatus.StoreOnline;

            BoxStatus boxStatus = new BoxStatus();
            boxStatus.boxId = 1;

            boxStatus.msg = WarnMsg;
            lineOperation.msg = WarnMsg;

            if (WarnMsg.Equals(""))
            {
                boxStatus.msg = CodeMsg;
                lineOperation.msg = CodeMsg;
            }
            CodeMsg = "";
            //WarnMsg = ""; 
            //状态
            boxStatus.status = (int)storeStatus;
            if (IsDebug)
            {
                boxStatus.status = (int)StoreStatus.Debugging;
            }
            else if (storeStatus.Equals(StoreStatus.OutStoreBoxEnd) || storeStatus.Equals(StoreStatus.InStoreEnd))
            {
                boxStatus.data.Add(ParamDefine.posId, lastPosId);
            }
            else if (!lastPosId.Equals(""))
            {
                boxStatus.data.Add(ParamDefine.posId, lastPosId);
                boxStatus.status = (int)lastPosIdStatus;
                if (lastPosId != "")
                {
                    LogUtil.info("给服务器发送出入库完成消息:" + Name + ",status【" + lastPosIdStatus + "】posId【" + lastPosId + "】");
                }
                lastPosId = "";
            }

            //温湿度
            //ASTemperateParam param = HumitureServer.GetTemperateParam(Config.Temperate_Serveraddress);
            HumitureParam param = humBean.LastData;
            if (param != null)
            {
                boxStatus.humidity = param.Humidity.ToString();
                boxStatus.temperature = param.Temperate.ToString();
            }
            lineOperation.boxStatus.Add(1, boxStatus);

            if (!alarmType.Equals(AlarmType.None))
            {
                lineOperation.alarmList.Add(alarmInfo);
            }
            return lineOperation;
        }

        public void SendLineStatus()
        {
            DateTime time = DateTime.Now;
            //构建发送给服务器的对象
            Operation lineOperation = getLineBoxStatus();
            //如果还没湿度范围,先获取
            if (humBean.NeedGetTem())
            {
                lineOperation.op = 5;
                LogUtil.error(Name + "没有湿度预警范围,需要从服务器获取,发送OP=" + lineOperation.op, ID + 105);
            }
            string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
            Operation resultOperation = HttpHelper.Post(StoreManager.GetPostApi(server), lineOperation, false);

            //发送状态信息到服务器 
            if (resultOperation == null || (resultOperation.op <= 0))
            {
                //判断服务端是否返回出库操作
                return;
            }
            if (resultOperation.op.Equals(1))
            {
                //  ReviceInStoreProcess("", resultOperation);
            }
            else if (resultOperation.op.Equals(2))
            {
                ReviceOutStoreProcess(resultOperation);
            }
            else if (resultOperation.op.Equals(5))
            {
                humBean.ProcessHumidityCMD(resultOperation);
            }
            else
            {
                LogUtil.error("收到服务器命令:op=" + resultOperation.op + ",未找到对应处理");
            }
            TimeSpan span = DateTime.Now - time;
            if (span.TotalMilliseconds > 10)
            {
                LogUtil.info(Name + "执行TimerProcess 共处理了【" + span.TotalMilliseconds + "】毫秒");
            }
        }


        private void ReviceOutStoreProcess(Operation resultOperation)
        {
            DateTime time = DateTime.Now;
            Dictionary<string, string> data = resultOperation.data;
            if (data != null && data.ContainsKey(ParamDefine.posId)
                      && data.ContainsKey(ParamDefine.plateH) && data.ContainsKey(ParamDefine.plateW))
            {
                char splitChar = '|';
                string[] posIdArray = data[ParamDefine.posId].Split(splitChar);
                string[] plateWArray = data[ParamDefine.plateW].Split(splitChar);
                string[] plateHArray = data[ParamDefine.plateH].Split(splitChar);

                bool urgentReel = FormUtil.GetBoolData(data, ParamDefine.urgentReel);
                bool cutReel = FormUtil.GetBoolData(data, ParamDefine.cutReel);
                bool smallReel = FormUtil.GetBoolData(data, ParamDefine.smallReel);
                string rfid = data.ContainsKey(ParamDefine.rfid) ? data[ParamDefine.rfid] : "";
                int rfidLoc = FormUtil.GetIntData(data, ParamDefine.rfidLoc);
                string barcode = data.ContainsKey(ParamDefine.barcode) ? data[ParamDefine.barcode] : "";
                string realRfid = data.ContainsKey(ParamDefine.realRfid) ? data[ParamDefine.realRfid] : "";
                int taskCount = FormUtil.GetIntData(data, ParamDefine.taskCount, 0);

                //urgentReel: true 表示紧急料,需要出到料串上
                //cutReel: true 表示分盘料,需要出到料串上
                //smallReel: true  小料(7x8),放置到小料架上
                //rfid: 分配的料架RFID
                //rfidLoc: 料架的架位,值为 - 1时,可以自由分配皮带线, 小料时,架位为1 - 46优先走1 / 2号皮带线,47 - 92优先走3 / 4号皮带线, 70,71,72时只能分配到3 / 4号皮带线; 大料时,架位1 - 6优先走1 / 2号皮带线, 7 - 12优先走3 / 4号皮带线

                string dataStr = JsonHelper.SerializeObject(data);
                LogUtil.info("收到服务器出库消息:【" + dataStr + "】");

                int index = -1;
                foreach (string posId in posIdArray)
                {
                    index++;
                    string plateW = plateWArray[index];
                    string plateH = plateHArray[index]; 
                    InOutParam inoutParam = new InOutParam(barcode, posId,  plateW, plateH);

                    //根据发送的posId获取位置列表
                    ACBoxPosition position = CSVPositionReader<ACBoxPosition>.GetPositon(posId);
                    if (position == null)
                    {
                        //出入库没有找到服务器发送的库位,需要打印日志方便查询原因
                        WarnMsg = Name + "未找到库位:【" + inoutParam.ToStr() + "】";
                        LogUtil.error(WarnMsg);
                        continue;
                    }
                    try
                    {
                        //判断是否接收过此库位的出库信息
                        if (MoveInfo.MoveType.Equals(MoveType.OutStore) && MoveInfo.MoveParam.PosID.Equals(posId))
                        {
                            LogUtil.error(Name + " 出库命令【" + inoutParam.ToStr() + "】重复,正在【" + posId + "】出库中");
                            continue;
                        }

                        //判断排队列表中是否已存在
                        List<InOutParam> reviceList = new List<InOutParam>(waitOutStoreList);
                        reviceList = (from m in reviceList where m.PosID.Equals(posId) select m).ToList<InOutParam>();
                        if (reviceList.Count > 0)
                        {
                            LogUtil.error(Name + " 出库命令【" + inoutParam.ToStr() + "】重复,排队列表中已存在【" + reviceList[0].ToStr() + "】");
                            continue;
                        }

                    }
                    catch (Exception ex)
                    {
                        LogUtil.error(Name + "验证出库【" + inoutParam.ToStr() + "】是否重复出错:" + ex.ToString());
                    }
                    StartExecuctOut(inoutParam);
                }

                TimeSpan span = DateTime.Now - time;
                if (span.TotalMilliseconds > 10)
                {
                    LogInfo(Name + "执行 ReviceOutStoreProcess 共处理了【" + span.TotalMilliseconds + "】毫秒");
                }
            }
        }
        #endregion


        #region 扫码获取库位 

        /// <summary>
        /// 数据接收
        /// </summary>
        /// <param name="message"></param>
        private bool GetPosIdByCode()
        {
          //  try
          //  {
             
          //      //string message = StoreManager.ProcessCode(LastWidth, LastHeight, LastScanCodes);
          //      string message = MoveInfo.shelfLockDatas[MoveInfo.currShelfIndex].barcode;
          //      if (message.Equals("") || string.IsNullOrEmpty(message))
          //      {
          //          CodeMsg = "未扫到二维码";
          //          LogInfo("未扫到二维码");
          //          return false;
          //      }

          //      if (storeRunStatus.Equals(StoreRunStatus.Wait))
          //      {
          //          LogInfo("二维码【 " + message + "】,设备未启动,不需要发送服务器");
          //          return false;
          //      }
          //      // CodeMsg = "收到二维码【 " + message + "】,发送给服务器获取入库PosID";
          //      LogUtil.info(Name + "二维码【 " + message + "】,发送给服务器获取入库PosID");
          //      //发送扫码内容到服务器进行入库操作
          //      Operation operation = getLineBoxStatus();
          //      operation.op = 1;
          ////      operation.data = new Dictionary<string, string>() { { "code", message }, { "boxId", 1.ToString() }, { ParamDefine.rfid, CurrShelfID }};
          //      string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
          //      Operation resultOperation = HttpHelper.Post(StoreManager.GetPostApi(server), operation, false);
          //      if (resultOperation == null)
          //      {
          //          CodeMsg = "二维码【" + message + "】没有收到服务器反馈";
          //          LogInfo("二维码【 " + message + "】没有收到服务器反馈!");
          //          return false;
          //      }
          //      else if (!string.IsNullOrEmpty(resultOperation.msg))
          //      {
          //          //如果有提示消息,直接显示提示
          //          LogInfo("二维码【 " + message + "】  :" + resultOperation.msg);
          //          return false;
          //      }


          //      if (resultOperation.op.Equals(1))
          //      {
          //          Dictionary<string, string> data = resultOperation.data;
          //          if (data != null && data.ContainsKey(ParamDefine.posId) && data.ContainsKey(ParamDefine.plateH) && data.ContainsKey(ParamDefine.plateW))
          //          {
          //              //服务器返回时有:posId库位编号,plateW:料盘宽度,plateH:料盘高度,
          //              //postId格式BoxId#位置
          //              string posId = data[ParamDefine.posId];
          //              string plateW = data[ParamDefine.plateW];
          //              string plateH = data[ParamDefine.plateH];
          //              int storeId = InOutParam.GetPosStoreId(posId);
          //              //根据发送的posId获取位置列表
          //              ACBoxPosition position = CSVPositionReader<ACBoxPosition>.GetPositon(posId);
          //              if (position == null)
          //              {   //出入库没有找到服务器发送的库位,需要打印日志方便查询原因
          //                  WarnMsg = "入库未找到库位:二维码【" + message + "】库位【" + posId + "】 ";
          //                  LogUtil.error(Name + "收到服务器入库命令:入库未找到库位:二维码【" + message + "】库位【" + posId + "】");
          //                  return false;
          //              }
          //              LogInfo(" 收到服务器入库命令:库位号【" + posId + "】二维码【" + message + "】设置入库参数 , 开始入库!");
          //              int p3 = Config.CompAxis_P3;
          //            //  MoveInfo.MoveParam.UpdatePosId(message, posId, plateW, plateH, ComTargetPosition, p3);
          //              return true;
          //          }
          //      }
          //      else if (resultOperation.op.Equals(2))
          //      {
          //          ReviceOutStoreProcess(resultOperation);
          //      }
          //      else if (resultOperation.op.Equals(5))
          //      {
          //          humBean.ProcessHumidityCMD(resultOperation);
          //      }
          //      else
          //      {
          //          LogUtil.error("收到服务器命令:op=" + resultOperation.op + ",未找到对应处理");
          //      }
          //  }
          //  catch (Exception ex)
          //  {
          //      LogUtil.error(Name + "GetPosIdByCode" + ex.StackTrace);
          //  }
            return false;
        }

        /// <summary>
        /// 扫码结果类型
        /// </summary>
        enum CodeResultType
        {
            NoCode,
            DeviceNotOpen,
            NotResponseFromServer,
            WarnMsg,
            NotFoundPos,
            Error,
            Success
        }
        /// <summary>
        /// 扫码枪数据接收
        /// </summary>
        /// <param name="codeResultType">扫码结果</param>
        /// <returns></returns>
        private bool GetPosIdByCode (int LastWidth,int LastHeight,List<string> LastScanCodes, int rfidLoc, out CodeResultType codeResultType)
        {
            string CurrShelfID = "";
            try
            {
                if (LastWidth <= 0)
                {
                    LastWidth = 7;
                }
                string message = StoreManager.ProcessCode(LastWidth, LastHeight, LastScanCodes);
                if (message.Equals("") || string.IsNullOrEmpty(message))
                {
                    CodeMsg = "未扫到二维码";
                    LogInfo("未扫到二维码");
                    codeResultType = CodeResultType.NoCode;
                    return false;
                }

                if (storeRunStatus.Equals(StoreRunStatus.Wait))
                {
                    LogInfo("二维码【 " + message + "】,设备未启动,不需要发送服务器");
                    codeResultType = CodeResultType.DeviceNotOpen;
                    return false;
                }
                // CodeMsg = "收到二维码【 " + message + "】,发送给服务器获取入库PosID";
                LogUtil.info(Name + "二维码【 " + message + "】,发送给服务器获取入库PosID");

                //查询该料盘所在料仓以及仓位
                bool checkRelationship = StoreManager.GetPosForPutIn(Name, Config, message, CurrShelfID, rfidLoc);

                if (!checkRelationship)
                {
                    codeResultType = CodeResultType.NotFoundPos;
                    return false;
                }

                //发送扫码内容到服务器进行入库操作
                Operation operation = getLineBoxStatus();
                operation.op = 1;
                operation.data = new Dictionary<string, string>() { { "code", message }, { "boxId", 1.ToString() }, { ParamDefine.rfid, CurrShelfID } };
                string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
                Operation resultOperation = HttpHelper.Post(StoreManager.GetPostApi(server), operation, false);
                if (resultOperation == null)
                {
                    CodeMsg = "二维码【" + message + "】没有收到服务器反馈";
                    LogInfo("二维码【 " + message + "】没有收到服务器反馈!");
                    codeResultType = CodeResultType.NotResponseFromServer;
                    return false;
                }
                else if (!string.IsNullOrEmpty(resultOperation.msg))
                {
                    //如果有提示消息,直接显示提示
                    LogInfo("二维码【 " + message + "】  :" + resultOperation.msg);
                    codeResultType = CodeResultType.WarnMsg;
                    return false;
                }


                if (resultOperation.op.Equals(1))
                {
                    Dictionary<string, string> data = resultOperation.data;
                    if (data != null && data.ContainsKey(ParamDefine.posId) && data.ContainsKey(ParamDefine.plateH) && data.ContainsKey(ParamDefine.plateW))
                    {
                        //服务器返回时有:posId库位编号,plateW:料盘宽度,plateH:料盘高度,
                        //postId格式BoxId#位置
                        string posId = data[ParamDefine.posId];
                        string plateW = data[ParamDefine.plateW];
                        string plateH = data[ParamDefine.plateH];
                        int storeId = InOutParam.GetPosStoreId(posId);
                        //根据发送的posId获取位置列表
                        ACBoxPosition position = CSVPositionReader<ACBoxPosition>.GetPositon(posId);
                        if (position == null)
                        {   //出入库没有找到服务器发送的库位,需要打印日志方便查询原因
                            WarnMsg = "入库未找到库位:二维码【" + message + "】库位【" + posId + "】 ";
                            LogUtil.error(Name + "收到服务器入库命令:入库未找到库位:二维码【" + message + "】库位【" + posId + "】");
                            codeResultType = CodeResultType.NotFoundPos;
                            return false;
                        }
                        LogInfo(" 收到服务器入库命令:库位号【" + posId + "】二维码【" + message + "】设置入库参数 , 开始入库!");
                        int p3 = Config.CompAxis_P3;
                        //MoveInfo.MoveParam.UpdatePosId(message, posId, plateW, plateH, ComTargetPosition, p3);
                        codeResultType = CodeResultType.Success;
                        return true;
                    }
                }
                else if (resultOperation.op.Equals(2))
                {
                    ReviceOutStoreProcess(resultOperation);
                }
                else if (resultOperation.op.Equals(5))
                {
                    humBean.ProcessHumidityCMD(resultOperation);
                }
                else
                {
                    LogUtil.error("收到服务器命令:op=" + resultOperation.op + ",未找到对应处理");
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "GetPosIdByCode" + ex.StackTrace);
            }
            codeResultType = CodeResultType.Error;
            return false;
        }

        #endregion

    }
}