示例#1
0
        /// <summary>
        /// 获取第一个空车位。
        /// </summary>
        /// <param name="cfgChannelId">巷道的主键。</param>
        /// <returns>第一个空车位。</returns>
        public int?GetFirstEmptyPosition(int cfgChannelId)
        {
            using (GeelyPtlEntities dbContext = new GeelyPtlEntities())
            {
                CFG_ChannelCurrentCart cfgChannelCurrentCart = dbContext.CFG_ChannelCurrentCarts
                                                               .Where(ccc => ccc.CFG_ChannelId == cfgChannelId && ccc.CFG_CartId == null)
                                                               .OrderBy(ccc => ccc.Position)
                                                               .FirstOrDefault();

                return(cfgChannelCurrentCart == null ? null : (int?)cfgChannelCurrentCart.Position);
            }
        }
示例#2
0
        /// <summary>
        /// 发送拣料区配送任务
        /// </summary>
        /// <returns></returns>
        private bool SendPickAreaDistributeTask()
        {
            bool isSuccess = false;

            try
            {
                using (GeelyPtlEntities dbContext = new GeelyPtlEntities())
                {
                    #region 注释
                    ////获取物料超市已满4辆车的工位
                    //List<string> NoDistributeWorkStationCode = new List<string>();
                    //Hashtable htWorkStation = new Hashtable();

                    //List<CFG_Cart> cfgCarts = dbContext.CFG_Carts.Where(t => (t.CartStatus == CartStatus.ArrivedAtBufferArea
                    //    || t.CartStatus == CartStatus.NeedToWorkStation
                    //    || t.CartStatus == CartStatus.WaitingToWorkStation)
                    //    && t.FND_Tasks.FirstOrDefault(f => f.FindingStatus == FindingStatus.New
                    //        || f.FindingStatus == FindingStatus.NeedDisplay
                    //        || f.FindingStatus == FindingStatus.Displaying) != null).ToList();
                    //foreach (CFG_Cart cfgCart in cfgCarts)
                    //{
                    //    CFG_CartCurrentMaterial firstNotEmptyCfgCartCurrentMaterial = cfgCart.CFG_CartCurrentMaterials
                    //                                                                  .FirstOrDefault(ccm => ccm.AST_CartTaskItemId != null);
                    //    if (firstNotEmptyCfgCartCurrentMaterial != null)
                    //    {
                    //        string sWorkStationCode = firstNotEmptyCfgCartCurrentMaterial.CFG_WorkStation.Code;
                    //        if (!htWorkStation.Contains(sWorkStationCode))
                    //        {
                    //            htWorkStation.Add(sWorkStationCode, 1);
                    //        }
                    //        else
                    //        {
                    //            htWorkStation[sWorkStationCode] = Convert.ToInt32(htWorkStation[sWorkStationCode]) + 1;
                    //            if (Convert.ToInt32(htWorkStation[sWorkStationCode]) > 2)
                    //            {
                    //                NoDistributeWorkStationCode.Add(sWorkStationCode);
                    //            }
                    //        }
                    //    }
                    //}

                    //获取未响应的配送任务
                    //List<DST_DistributeTask> distributeTasks = dbContext.DST_DistributeTasks.
                    //        Where(t => !t.isResponse &&
                    //            (t.DistributeReqTypes == DistributeReqTypes.PickAreaDistribute
                    //            || t.DistributeReqTypes == DistributeReqTypes.NullCartAreaDistribute)
                    //            && !NoDistributeWorkStationCode.Contains(t.endPosition)
                    //            && t.sendErrorCount < 5).
                    //        OrderBy(t => t.reqTime).ToList();
                    #endregion

                    List <DST_DistributeTask> distributeTasks = dbContext.DST_DistributeTasks.
                                                                Where(t => !t.isResponse &&
                                                                      (t.DistributeReqTypes == DistributeReqTypes.PickAreaDistribute ||
                                                                       t.DistributeReqTypes == DistributeReqTypes.NullCartAreaDistribute ||
                                                                       t.DistributeReqTypes == DistributeReqTypes.PointToPointDistribute) &&
                                                                      t.sendErrorCount < 5).
                                                                OrderBy(t => t.reqTime).ToList();

                    if (distributeTasks.Count == 0)
                    {
                        return(false);
                    }

                    //发送信息
                    string sendInfo = "";
                    //AGV服务地址
                    string sURL = ptlToAgvServiceUrl + "/genAgvSchedulingTask";
                    //HTTP响应结果
                    string    result     = "";
                    ArrayList arrChannel = new ArrayList();

                    foreach (DST_DistributeTask distributeTask in distributeTasks)
                    {
                        if (distributeTask.DistributeReqTypes == DistributeReqTypes.PickAreaDistribute) //拣料区配送
                        {
                            //查询巷道对应一侧的物料超市停靠的车辆信息
                            string[] arrPosition = distributeTask.startPosition.Replace("H", "").Replace("P", ",").Split(',');
                            int      nChannelId  = Convert.ToInt32(arrPosition[0]); //巷道
                            int      nPosition   = Convert.ToInt32(arrPosition[1]); //车位

                            if (arrChannel.Contains(nChannelId))
                            {
                                continue;
                            }
                            arrChannel.Add(nChannelId);

                            int nMaxPosition = 0;
                            int nMinPosition = 0;
                            if (nPosition <= 2)
                            {
                                nMinPosition = 1;
                                nMaxPosition = 2;
                            }
                            else
                            {
                                nMinPosition = 3;
                                nMaxPosition = 4;
                            }
                            string sWorkStationCode = distributeTask.endPosition;

                            CFG_CartCurrentMaterial firstCurDisCartCurrentMaterial = dbContext.CFG_CartCurrentMaterials.FirstOrDefault(t => t.CFG_Cart.Code.Equals(distributeTask.podCode) && t.AST_CartTaskItemId != null);
                            if (firstCurDisCartCurrentMaterial != null)
                            {
                                //查询拣料区正在往物料超市配送的料架的批次跟需配送的巷道的料架的批次是否一致
                                List <string>             arrPodCodes         = new List <string>();
                                List <DST_DistributeTask> pickDistributeTasks = dbContext.DST_DistributeTasks.Where(t => t.DistributeReqTypes == DistributeReqTypes.PickAreaDistribute &&
                                                                                                                    t.isResponse && t.sendErrorCount < 5 && t.arriveTime == null &&
                                                                                                                    !t.reqCode.Equals(distributeTask.reqCode) &&
                                                                                                                    t.endPosition.Equals(sWorkStationCode)).ToList();

                                int nPickStartPosition = 0;
                                foreach (DST_DistributeTask pickDistributeTask in pickDistributeTasks)
                                {
                                    nPickStartPosition = Convert.ToInt32(pickDistributeTask.startPosition.Split('P')[1]);
                                    if (nPickStartPosition >= nMinPosition && nPickStartPosition <= nMaxPosition)
                                    {
                                        arrPodCodes.Add(pickDistributeTask.podCode);
                                    }
                                }

                                if (dbContext.CFG_CartCurrentMaterials.Any(t => arrPodCodes.Contains(t.CFG_Cart.Code) && t.AST_CartTaskItemId != null && t.BatchCode != firstCurDisCartCurrentMaterial.BatchCode))
                                {
                                    continue;
                                }
                            }
                            List <MarketZone> marketZones = dbContext.MarketZones.Where(t => t.AreaId.Equals(sWorkStationCode) &&
                                                                                        t.CFG_CartId != null &&
                                                                                        t.Position >= nMinPosition && t.Position <= nMaxPosition).ToList();

                            //string sWorkStationCode = distributeTask.endPosition;
                            //List<MarketZone> marketZones = dbContext.MarketZones.Where(t => t.AreaId.Equals(sWorkStationCode)
                            //    && t.CFG_CartId != null).ToList();
                            if (marketZones.Count > 0)
                            {
                                //物料超市对应一侧已停满
                                if (marketZones.Count >= 2)
                                {
                                    continue;
                                }

                                //物料超市停靠的车+正在配送的车>=2时,拣料区对应工位相应一侧不再配送
                                string sql = string.Format(@"select count(*) from DST_DistributeTask a
inner join DST_DistributeTaskResult b on a.reqCode=b.reqCode
where a.DistributeReqTypes=3 and a.isResponse=1 and a.sendErrorCount<5 and a.arriveTime is null
and b.data not in(select taskCode from DST_DistributeArriveTask where method='OutFromBin')
and ((a.startPosition='{0}'+'L' and {1}<=2) or (a.startPosition='{0}'+'R' and {1}>2))", sWorkStationCode, nPosition);
                                int    nNoCompleteDistributeCount = dbContext.Database.SqlQuery <int>(sql).FirstOrDefault();

                                if (marketZones.Count + nNoCompleteDistributeCount >= 2)
                                {
                                    continue;
                                }

                                //查询需配送的巷道的料架和物料超市的料架批次是否一致
                                if (firstCurDisCartCurrentMaterial != null)
                                {
                                    int nFirstMarketCartId = Convert.ToInt32(marketZones[0].CFG_CartId);
                                    CFG_CartCurrentMaterial firstDockCartCurrentMaterial = dbContext.CFG_CartCurrentMaterials.FirstOrDefault(t => t.CFG_CartId == nFirstMarketCartId && t.AST_CartTaskItemId != null);
                                    if (firstDockCartCurrentMaterial != null)
                                    {
                                        if (!firstCurDisCartCurrentMaterial.BatchCode.Equals(firstDockCartCurrentMaterial.BatchCode))
                                        {
                                            continue;
                                        }
                                    }
                                }

                                //if (marketZones.Count >= 4)
                                //{
                                //    continue;
                                //}

                                //CFG_CartCurrentMaterial firstCurDisCartCurrentMaterial = dbContext.CFG_CartCurrentMaterials.FirstOrDefault(t => t.CFG_Cart.Code.Equals(distributeTask.podCode) && t.AST_CartTaskItemId != null);
                                //if (firstCurDisCartCurrentMaterial != null)
                                //{
                                //    List<int?> ListCartId = new List<int?>();
                                //    foreach (MarketZone marketZone in marketZones)
                                //    {
                                //        ListCartId.Add(marketZone.CFG_CartId);
                                //    }

                                //    CFG_CartCurrentMaterial firstDockCartCurrentMaterial = dbContext.CFG_CartCurrentMaterials.FirstOrDefault(t => ListCartId.Contains(t.CFG_CartId)
                                //        && t.AST_CartTaskItemId != null
                                //        && !t.BatchCode.Equals(firstCurDisCartCurrentMaterial.BatchCode));
                                //    if (firstDockCartCurrentMaterial != null)
                                //    {
                                //        continue;
                                //    }
                                //}
                            }
                        }
                        else if (distributeTask.DistributeReqTypes == DistributeReqTypes.NullCartAreaDistribute) //空料架缓冲区配送
                        {
                            string[] arrPosition = distributeTask.endPosition.Replace("H", "").Replace("P", ",").Split(',');
                            int      nChannelId  = Convert.ToInt32(arrPosition[0]); //巷道
                            int      nPosition   = Convert.ToInt32(arrPosition[1]); //车位

                            //如果对应巷道车位上停靠了料架,则先不补空料架
                            CFG_ChannelCurrentCart cfgChannelCurrentCart = dbContext.CFG_ChannelCurrentCarts.FirstOrDefault(ccc => ccc.CFG_ChannelId == nChannelId && ccc.Position == nPosition && ccc.CFG_CartId != null);
                            if (cfgChannelCurrentCart != null)
                            {
                                continue;
                            }
                        }

                        //绑定配送任务
                        DST_DistributeTaskDto distributeTaskDto = new DST_DistributeTaskDto();
                        distributeTaskDto.reqCode          = distributeTask.reqCode;
                        distributeTaskDto.reqTime          = distributeTask.reqTime.ToString("yyyy-MM-dd HH:mm:ss");
                        distributeTaskDto.clientCode       = distributeTask.clientCode;
                        distributeTaskDto.tokenCode        = distributeTask.tokenCode;
                        distributeTaskDto.taskTyp          = distributeTask.taskTyp;
                        distributeTaskDto.userCallCode     = distributeTask.userCallCode;
                        distributeTaskDto.userCallCodePath = new List <string>();
                        distributeTaskDto.userCallCodePath.AddRange(new string[] { distributeTask.startPosition, distributeTask.endPosition });
                        distributeTaskDto.podCode   = distributeTask.podCode;
                        distributeTaskDto.robotCode = distributeTask.robotCode;
                        distributeTaskDto.taskCode  = distributeTask.taskCode;
                        distributeTaskDto.data      = distributeTask.data;

                        //发送信息
                        sendInfo = JsonConvert.SerializeObject(distributeTaskDto);
                        //发送HTTP请求,并返回响应结果
                        result = HttpService.HttpPost(sURL, sendInfo);

                        if (!string.IsNullOrEmpty(result))
                        {
                            //实例化HTTP响应结果
                            DST_DistributeTaskResultDto distributeTaskResultDto = JsonConvert.DeserializeObject <DST_DistributeTaskResultDto>(result);
                            if (distributeTaskResultDto != null)
                            {
                                //新增配送任务结果
                                DST_DistributeTaskResult distributeTaskResult = new DST_DistributeTaskResult();
                                distributeTaskResult.code        = distributeTaskResultDto.code;
                                distributeTaskResult.message     = distributeTaskResultDto.message;
                                distributeTaskResult.reqCode     = distributeTaskResultDto.reqCode;
                                distributeTaskResult.data        = distributeTaskResultDto.data;
                                distributeTaskResult.receiveTime = DateTime.Now;

                                dbContext.DST_DistributeTaskResults.Add(distributeTaskResult);

                                //更新配送任务响应值
                                if (distributeTaskResultDto.code.Equals("0"))
                                {
                                    distributeTask.isResponse = true;

                                    if (distributeTask.DistributeReqTypes == DistributeReqTypes.PickAreaDistribute) //拣料区配送
                                    {
                                        //解绑巷道停靠
                                        CFG_ChannelCurrentCart cfgChannelCurrentCart = dbContext.CFG_ChannelCurrentCarts.FirstOrDefault(ccc => ccc.CFG_Cart.Code.Equals(distributeTask.podCode));
                                        if (cfgChannelCurrentCart != null)
                                        {
                                            cfgChannelCurrentCart.CFG_CartId = null;
                                            cfgChannelCurrentCart.DockedTime = null;
                                        }
                                    }
                                }
                                else
                                {
                                    distributeTask.sendErrorCount = distributeTask.sendErrorCount + 1;
                                }
                            }
                        }
                    }

                    //更新数据库
                    isSuccess = dbContext.SaveChanges() > 0 ? true : false;
                }
            }
            catch (Exception)
            {
                isSuccess = false;
            }
            return(isSuccess);
        }
示例#3
0
        /// <summary>
        /// 停靠小车到分拣口。
        /// </summary>
        /// <param name="cfgChannelId">分拣口的主键。</param>
        /// <param name="position">车位。</param>
        /// <param name="cfgCartId">待停靠小车的主键。</param>
        /// <exception cref="System.ArgumentException">position 车位上的小车还未解除绑定。</exception>
        public void Dock(int cfgChannelId, int position, int cfgCartId)
        {
            using (GeelyPtlEntities dbContext = new GeelyPtlEntities())
            {
                CFG_Channel cfgChannel = dbContext.CFG_Channels
                                         .First(c => c.Id == cfgChannelId);
                CFG_Cart cfgCart = dbContext.CFG_Carts
                                   .First(c => c.Id == cfgCartId);
                CFG_ChannelCurrentCart cfgChannelCurrentCart = dbContext.CFG_ChannelCurrentCarts
                                                               .FirstOrDefault(ccc => ccc.CFG_ChannelId == cfgChannelId && ccc.Position == position);
                if (cfgCart.CartStatus != CartStatus.Free)
                {
                    throw new ArgumentException("小车 " + cfgCart.Code + " 未释放:" + cfgCart.CartStatus + "。");
                }
                if (!cfgCart.OnLine)
                {
                    throw new ArgumentException("小车 " + cfgCart.Code + " 不在线。");
                }
                foreach (CFG_CartPtlDevice cfgCartPtlDevice in cfgCart.CFG_CartPtlDevices)
                {
                    if (!cfgCartPtlDevice.OnLine)
                    {
                        throw new ArgumentException("小车上的 " + cfgCartPtlDevice.DeviceAddress + " 号标签不在线。");
                    }
                }
                if (cfgChannelCurrentCart == null)
                {
                    throw new ArgumentException("车位 " + position + " 不存在。", "position");
                }
                if (cfgChannelCurrentCart.CFG_CartId != null && cfgChannelCurrentCart.CFG_CartId != cfgCartId)
                {
                    throw new ArgumentException("车位 " + position + " 上的小车还未解除绑定。", "position");
                }

                if (cfgChannelCurrentCart.CFG_CartId == null)
                {
                    CFG_ChannelCurrentCart dockedCfgChannelCurrentCart = dbContext.CFG_ChannelCurrentCarts
                                                                         .FirstOrDefault(ccc => ccc.CFG_CartId == cfgCartId);
                    if (dockedCfgChannelCurrentCart != null)
                    {
                        throw new ArgumentException("小车 " + dockedCfgChannelCurrentCart.CFG_Cart.Code + " 已停靠在 " + dockedCfgChannelCurrentCart.CFG_Channel.Name + " 车位 " + dockedCfgChannelCurrentCart.Position + "。", "cfgCartId");
                    }

                    //停靠即开始播种
                    cfgCart.CartStatus = CartStatus.WaitingAssorting;

                    cfgChannelCurrentCart.CFG_CartId = cfgCart.Id;
                    cfgChannelCurrentCart.DockedTime = DateTime.Now;

                    //清空小车上的物料
                    List <CFG_CartCurrentMaterial> cfgCartCurrentMaterials = cfgCart.CFG_CartCurrentMaterials
                                                                             .ToList();
                    foreach (CFG_CartCurrentMaterial cfgCartCurrentMaterial in cfgCartCurrentMaterials)
                    {
                        cfgCartCurrentMaterial.AST_CartTaskItemId = null;
                        cfgCartCurrentMaterial.ProjectCode        = null;
                        cfgCartCurrentMaterial.WbsId              = null;
                        cfgCartCurrentMaterial.ProjectStep        = null;
                        cfgCartCurrentMaterial.CFG_WorkStationId  = null;
                        cfgCartCurrentMaterial.BatchCode          = null;
                        cfgCartCurrentMaterial.CFG_ChannelId      = null;
                        cfgCartCurrentMaterial.CFG_PalletId       = null;
                        cfgCartCurrentMaterial.BoxCode            = null;
                        cfgCartCurrentMaterial.FromPalletPosition = null;
                        cfgCartCurrentMaterial.MaterialCode       = null;
                        cfgCartCurrentMaterial.MaterialName       = null;
                        cfgCartCurrentMaterial.MaterialBarcode    = null;
                        cfgCartCurrentMaterial.Quantity           = null;
                        cfgCartCurrentMaterial.AssortedTime       = null;
                        cfgCartCurrentMaterial.CFG_EmployeeId     = null;
                        if (cfgCartCurrentMaterial.Usability != CartPositionUsability.DisableByOffLineDevice)
                        {
                            cfgCartCurrentMaterial.Usability = CartPositionUsability.Enable;
                        }
                    }

                    dbContext.SaveChanges();

                    //设备控制
                    CartPtl cartPtl          = CartPtlHost.Instance.GetCartPtl(cfgCart.Id);
                    Ptl900U ptl900UPublisher = cartPtl.GetPtl900UPublisher();

                    Display900UItem publisherDisplay900UItem = new Display900UItem();
                    publisherDisplay900UItem.Name        = "停靠成功";
                    publisherDisplay900UItem.Description = cfgChannel.Name;
                    publisherDisplay900UItem.Count       = (ushort)position;
                    publisherDisplay900UItem.Unit        = "位";

                    ptl900UPublisher.Clear(true);
                    ptl900UPublisher.Lock();
                    ptl900UPublisher.Display(publisherDisplay900UItem, LightColor.Off);

                    Logger.Log(this.GetType().Name, DateTime.Now.ToString("HH:mm:ss") + "停靠 " + cfgCart.Name + " 到 " + cfgChannel.Name + " 车位 " + position + Environment.NewLine);
                }
            }
        }
示例#4
0
        /// <summary>
        /// 从分拣口解除小车绑定。
        /// </summary>
        /// <param name="cfgChannelId">分拣口的主键。</param>
        /// <param name="cfgCartId">待移出小车的主键。</param>
        /// <exception cref="System.ArgumentException">position 车位上的小车还未作业完成。</exception>
        public void UnDock(int cfgChannelId, int cfgCartId)
        {
            using (GeelyPtlEntities dbContext = new GeelyPtlEntities())
            {
                CFG_ChannelCurrentCart cfgChannelCurrentCart = dbContext.CFG_ChannelCurrentCarts
                                                               .FirstOrDefault(ccc => ccc.CFG_ChannelId == cfgChannelId && ccc.CFG_CartId == cfgCartId);
                if (cfgChannelCurrentCart != null && cfgChannelCurrentCart.CFG_CartId != null)
                {
                    CFG_Cart cfgCart = cfgChannelCurrentCart.CFG_Cart;
                    if (cfgCart.CartStatus == CartStatus.Assorting)
                    {
                        throw new ArgumentException("车位 " + cfgChannelCurrentCart.Position + " 上的小车 " + cfgCart.Code + " 还未作业完成。", "position");
                    }

                    //移出
                    cfgChannelCurrentCart.CFG_CartId = null;
                    cfgChannelCurrentCart.DockedTime = null;

                    //准备基础数据
                    AST_PalletTask  astPalletTask  = null;
                    CFG_WorkStation cfgWorkStation = null;
                    List <CFG_CartCurrentMaterial> cfgCartCurrentMaterials = cfgCart.CFG_CartCurrentMaterials
                                                                             .ToList();
                    CFG_CartCurrentMaterial firstNotEmptyCfgCartCurrentMaterial = cfgCartCurrentMaterials
                                                                                  .FirstOrDefault(ccm => ccm.AST_CartTaskItemId != null);
                    if (firstNotEmptyCfgCartCurrentMaterial != null)
                    {
                        AST_CartTaskItem astCartTask = firstNotEmptyCfgCartCurrentMaterial.AST_CartTaskItem;
                        astPalletTask  = astCartTask.AST_PalletTaskItem.AST_PalletTask;
                        cfgWorkStation = astCartTask.AST_PalletTaskItem.CFG_WorkStation;
                    }

                    dbContext.SaveChanges();

                    //设备控制
                    CartPtl cartPtl          = CartPtlHost.Instance.GetCartPtl(cfgCart.Id);
                    Ptl900U ptl900UPublisher = cartPtl.GetPtl900UPublisher();
                    Ptl900U ptl900ULight     = cartPtl.GetPtl900ULight();

                    ptl900UPublisher.Clear(true);
                    ptl900UPublisher.Unlock();

                    if (astPalletTask != null)
                    {
                        Display900UItem publisherDisplay900UItem = new Display900UItem();
                        publisherDisplay900UItem.Name        = "已分拣完成";
                        publisherDisplay900UItem.Description = string.Format(CultureInfo.InvariantCulture, @"项目:{0}
阶段:{1}
工位:{2}", astPalletTask.ProjectCode, astPalletTask.ProjectStep, cfgWorkStation.Code);
                        publisherDisplay900UItem.Count       = (ushort)cfgCartCurrentMaterials
                                                               .Where(ccm => ccm.Quantity != null)
                                                               .Select(ccm => ccm.Quantity.Value)
                                                               .Sum();
                        publisherDisplay900UItem.Unit = "个";

                        ptl900UPublisher.Lock();
                        ptl900UPublisher.Display(publisherDisplay900UItem, LightColor.Off);
                    }

                    foreach (CFG_CartCurrentMaterial cfgCartCurrentMaterial in cfgCartCurrentMaterials)
                    {
                        Ptl900U ptl900U = cartPtl.GetPtl900UByPosition(cfgCartCurrentMaterial.Position);

                        ptl900U.Clear(true);
                        ptl900U.Unlock();
                    }

                    ptl900ULight.Clear();
                }
            }
        }
示例#5
0
        /// <summary>
        /// 确保基础数据已经初始化。
        /// </summary>
        public static void EnsureInitialized()
        {
            using (GeelyPtlEntities dbContext = new GeelyPtlEntities())
            {
                if (!dbContext.CFG_Employees.Any())
                {
                    //操作员
                    CFG_Employee cfgEmployee = new CFG_Employee();
                    cfgEmployee.Code      = "Administrator";
                    cfgEmployee.Name      = "管理员";
                    cfgEmployee.LoginName = "admin";
                    cfgEmployee.Password  = string.Empty;
                    cfgEmployee.IsEnabled = true;

                    dbContext.CFG_Employees.Add(cfgEmployee);

                    //巷道
                    //转台上的标签,托盘有 5 个库位,但只在单侧使用 3 个标签
                    //7 个巷道共用 1 个 4 口 XGate,按现场走线,总线 1 对应 1、2 巷道,总线 2 对应 3 巷道,总线 3 对应 4、5 巷道,总线 4 对应 6、7 巷道
                    //标签地址分别为:1、2、3,4、5、6,51、52、53,101、102、103,104、105、106,151、152、153,154、155、156。
                    //托盘库位定义为:1、2 在远端需旋转,3、4、5 在近段
                    string channelXGateIP = "10.34.36.17";
                    Dictionary <int, byte[]> channelPtl900UAddressesByChannelNumber = new Dictionary <int, byte[]>();
                    channelPtl900UAddressesByChannelNumber.Add(1, new byte[] { 3, 1, 1, 2, 3 });
                    channelPtl900UAddressesByChannelNumber.Add(2, new byte[] { 6, 4, 4, 5, 6 });
                    channelPtl900UAddressesByChannelNumber.Add(3, new byte[] { 53, 51, 51, 52, 53 });
                    channelPtl900UAddressesByChannelNumber.Add(4, new byte[] { 103, 101, 101, 102, 103 });
                    channelPtl900UAddressesByChannelNumber.Add(5, new byte[] { 106, 104, 104, 105, 106 });
                    channelPtl900UAddressesByChannelNumber.Add(6, new byte[] { 153, 151, 151, 152, 153 });
                    channelPtl900UAddressesByChannelNumber.Add(7, new byte[] { 156, 154, 154, 155, 156 });

                    for (int channelNumber = 1; channelNumber <= channelPtl900UAddressesByChannelNumber.Count; channelNumber++)
                    {
                        CFG_Channel cfgChannel = new CFG_Channel();
                        cfgChannel.Code = string.Format(CultureInfo.InvariantCulture, "{0}", channelNumber);
                        cfgChannel.Name = string.Format(CultureInfo.InvariantCulture, "巷道 {0}", channelNumber);

                        dbContext.CFG_Channels.Add(cfgChannel);

                        //转台上的标签
                        byte[] deviceAddresses = channelPtl900UAddressesByChannelNumber[channelNumber];
                        for (int position = 1; position <= 5; position++)
                        {
                            byte deviceAddress = deviceAddresses[position - 1];
                            byte busIndex      = (byte)(deviceAddress / 50);

                            CFG_ChannelPtlDevice cfgChannelPtlDevice = new CFG_ChannelPtlDevice();
                            cfgChannelPtlDevice.CFG_Channel    = cfgChannel;
                            cfgChannelPtlDevice.Position       = position;
                            cfgChannelPtlDevice.XGateIP        = channelXGateIP;
                            cfgChannelPtlDevice.RS485BusIndex  = busIndex;
                            cfgChannelPtlDevice.Ptl900UAddress = deviceAddress;

                            dbContext.CFG_ChannelPtlDevices.Add(cfgChannelPtlDevice);
                        }

                        //巷道边的 4 个车位,多加两个备用车位
                        for (int position = 1; position <= 6; position++)
                        {
                            CFG_ChannelCurrentCart cfgChannelCurrentCart = new CFG_ChannelCurrentCart();
                            cfgChannelCurrentCart.CFG_Channel = cfgChannel;
                            cfgChannelCurrentCart.Position    = position;

                            dbContext.CFG_ChannelCurrentCarts.Add(cfgChannelCurrentCart);
                        }

                        //巷道上的 1 个托盘
                        CFG_ChannelCurrentPallet cfgChannelCurrentPallet = new CFG_ChannelCurrentPallet();
                        cfgChannelCurrentPallet.CFG_Channel = cfgChannel;

                        dbContext.CFG_ChannelCurrentPallets.Add(cfgChannelCurrentPallet);
                    }

                    //小车
                    for (int cartNumber = 1; cartNumber <= 100; cartNumber++)
                    {
                        CFG_Cart cfgCart = new CFG_Cart();
                        cfgCart.Code       = string.Format(CultureInfo.InvariantCulture, "{0:000000}", 100000 + cartNumber);
                        cfgCart.Name       = string.Format(CultureInfo.InvariantCulture, "料车 {0}", cartNumber);
                        cfgCart.Rfid1      = "AABBCCDDEEFF";
                        cfgCart.XGateIP    = "192.168.0.10";
                        cfgCart.CartStatus = CartStatus.Free;

                        dbContext.CFG_Carts.Add(cfgCart);

                        //小车上的 8 个库位
                        for (int position = 1; position <= 8; position++)
                        {
                            CFG_CartCurrentMaterial cfgCartCurrentMaterial = new CFG_CartCurrentMaterial();
                            cfgCartCurrentMaterial.CFG_Cart  = cfgCart;
                            cfgCartCurrentMaterial.Position  = position;
                            cfgCartCurrentMaterial.Usability = CartPositionUsability.Enable;

                            dbContext.CFG_CartCurrentMaterials.Add(cfgCartCurrentMaterial);
                        }

                        //小车上的 10 个标签
                        for (byte deviceAddress = 1; deviceAddress <= 10; deviceAddress++)
                        {
                            CFG_CartPtlDevice cfgCartPtlDevice = new CFG_CartPtlDevice();
                            cfgCartPtlDevice.CFG_Cart      = cfgCart;
                            cfgCartPtlDevice.DeviceAddress = deviceAddress;

                            dbContext.CFG_CartPtlDevices.Add(cfgCartPtlDevice);
                        }
                    }

                    //dbContext.SaveChanges();
                }

                ////线边工位的8个车位
                //List<CFG_WorkStation> cfgWorkStations = dbContext.CFG_WorkStations.ToList();
                //foreach (CFG_WorkStation cfgWorkStation in cfgWorkStations)
                //{
                //    if (cfgWorkStation.CFG_WorkStationCurrentCarts != null && cfgWorkStation.CFG_WorkStationCurrentCarts.Count > 0)
                //    {
                //        continue;
                //    }

                //    for (int position = 1; position <= 8; position++)
                //    {
                //        CFG_WorkStationCurrentCart cfgWorkStationCurrentCart = new CFG_WorkStationCurrentCart();
                //        cfgWorkStationCurrentCart.CFG_WorkStation = cfgWorkStation;
                //        cfgWorkStationCurrentCart.Position = position;

                //        dbContext.CFG_WorkStationCurrentCarts.Add(cfgWorkStationCurrentCart);
                //    }
                //}

                //AGV开关信息
                if (!dbContext.DST_AgvSwitchs.Any())
                {
                    DST_AgvSwitch dstAgvSwitch = new DST_AgvSwitch();
                    dstAgvSwitch.isOpen        = false;
                    dstAgvSwitch.lastCloseTime = DateTime.Now;

                    dbContext.DST_AgvSwitchs.Add(dstAgvSwitch);
                }

                dbContext.SaveChanges();
            }
        }