RobotManager.cs 6.4 KB
using Common;
using DeviceLib.DB.Config;
using DeviceLib.Model.AGV;
using System;
using System.Collections.Generic;

namespace DeviceLib.BLL
{
    public class RobotManager
    {
        static List<Robot> robots;
        static int lowBattery = 30;
        /// <summary>
        /// 加载配置
        /// </summary>
        public static void Load()
        {
            //ConfigOperation.GetRobots();
            robots = ConfigOperation.GetRobots() ?? new List<Robot>();
            LogUtil.Info("机器人配置加载成功!");
        }
        /// <summary>
        /// 获取所有机器人
        /// </summary>
        /// <returns></returns>
        public static List<Robot> GetAllRobots()
        {
            return robots;
        }
        public static Robot GetRobot(int id)
        {
            return robots.Find(s => s.id == id);
        }
        public static Robot GetRobot(string ip)
        {
            return robots.Find(s => s.ip == ip);
        }
        /// <summary>
        /// 查询机充电桩上是否有机器人
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns>true:有机器人</returns>
        public static bool CheckRobotInChargePile(int nodeId)
        {
            Robot robot = robots.Find(s => s.job != null && (s.job is ChargeJob && s.job.JobParam.CurDstNode.Id.Equals(nodeId)));
            return robot != null;
        }
        /// <summary>
        /// 查询机待机点上是否有机器人
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns>true:有机器人</returns>
        public static bool CheckRobotInStandbyPile(int nodeId)
        {
            Robot robot = robots.Find(s => s.job != null && s.job is StandbyJob && s.job.JobParam.CurDstNode.Id.Equals(nodeId));
            return robot != null;
        }
        /// <summary>
        /// 检查是否有机器人在当前节点
        /// </summary>
        /// <param name="nodename"></param>
        /// <returns></returns>
        public static bool RobotIsInNode(string nodename)
        {
            return robots.Find(s=>s?.mission_state?.node_name.Equals(nodename) ??false)!=null;
        }
        /// <summary>
        /// 获取可执行任务的机器人
        /// </summary>
        /// <param name="workshop_id">车间编号:默认0,表示不区分车间</param>
        /// <returns></returns>
        public static Robot GetReadyRobot(int workshop_id = 0)
        {
            var finds = robots.FindAll(s => s.status.battery_percentage > lowBattery);
            finds.FindAll(s => s.state.Equals(RobotState.待机中) || s.state.Equals(RobotState.充电中));
            if (workshop_id > 0)
                return finds.Find(s => s.workshop_id == workshop_id);
            else if (finds.Count > 0)
                return finds[0];
            else
                return null;
        }
        static Robot getRobot(int id)
        {
            return robots.Find(s => s.id.Equals(id));
        }
        public static void SetRobot(Robot robot)
        {
            ConfigOperation.SetRobot(robot);
        }       
        public static void SetDebug(int id, bool debug)
        {
            Robot robot = getRobot(id);
            if (robot != null)
            {
                robot.is_debug = debug;
                SetRobot(robot);
                LogUtil.Info($"设置【{robot.name}】调试模式={debug}");
            }
        }
        public static void SetLoadInfo(int robotId, int pos, ShelfType type, string shelf_id = "")
        {
            Robot robot = getRobot(robotId);
            if (robot != null)
            {
                LoadInfo loadInfo = robot.load_infos.Find(s => s.pos == pos);
                if (loadInfo != null)
                {
                    loadInfo.shelf_type = type;
                    loadInfo.shelf_id = shelf_id;
                    SetRobot(robot);
                    LogUtil.Info($"设置【{robot.name}】负载状态【{loadInfo}】");
                }
                else
                {
                    LogUtil.Error($"设置【{robot.name}】负载状态失败【shelf_type={type.ToString()},shelf_id={shelf_id}】,因未找到pos={pos}");
                }
            }
        }

        public static void SetAuto(int id, bool auto)
        {
            Robot robot = getRobot(id);
            if (robot != null)
            {
                robot.auto = auto;
                SetRobot(robot);
                LogUtil.Info($"设置【{robot.name}】自动模式={auto}");
            }

        }
        public static void UploadState(Robot robot)
        {
            try
            {
                AgvStatus agv=  new AgvStatus()
                {
                    name = robot.name,
                    elec = robot?.status?.battery_percentage.ToString("f2"),
                    type= robot.id
                 };
                if(robot.job!=null)
                {
                    if(robot.job is StandbyJob)
                    {
                        agv.loc = "STANDBY";
                    }
                    else if(robot.job is ChargeJob)
                    {
                        agv.loc = "CHARGE";
                    }
                    else if(robot.job.JobParam.LineNode!=null)
                    {
                        agv.loc = robot.job.JobParam.LineNode.name;
                    }
                    else
                    {
                        agv.loc = "";
                    }
                }
                else
                {
                    agv.loc = "STANDBY";
                }
                HttpManager.UpdateAgvStatus(new List<AgvStatus> {agv });
            }
            catch(Exception ex)
            {
                LogUtil.Error($"{robot.name} 上报状态失败",ex);
            }
        }
        static MissionState getMissionState(int id)
        {
            Robot robot = getRobot(id);
            if (robot != null)
            {
                return robot.mission_state;
            }
            return null;
        }
        static Job getJob(int id)
        {
            Robot robot = getRobot(id);
            if (robot != null)
            {
                return robot.job;
            }
            return null;
        }
        public static void ClearBoxBind(Robot robot,string rfid)
        {
            HttpManager.ClearBoxBind(rfid,robot.name);
        }
    }
}