ConfigOperation.cs 9.9 KB
using Common;
using DeviceLib.Model.AGV;
using DeviceLib.Model.AGV.MiR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace DeviceLib.DB.Config
{
    public class ConfigOperation
    {
        #region 机器人
        public static List<Robot> GetRobots()
        {
            List<Robot> list = new List<Robot>();
            using (ConfigContext context = new ConfigContext())
            {
                list = context.Robots.ToList();
                foreach (var agv in list)
                {
                    agv.io_modules = new List<IOModule>();
                    string[] ios = agv.io_module_ids?.Split('#');
                    if (ios != null)
                        foreach (string ios2 in ios)
                        {
                            int.TryParse(ios2, out int id);
                            IOModule iOModule = context.IOModules.Where(s => s.id == id).FirstOrDefault();
                            if (iOModule != null)
                                agv.io_modules.Add(iOModule);
                        }
                    agv.fleet = context.Fleets.Where(s => s.id == agv.fleet_id).FirstOrDefault();
                    agv.workshop = context.Workshops.Where(s => s.id == agv.workshop_id).FirstOrDefault();
                    agv.mission_group = context.MissionGroups.Where(x => x.id.Equals(agv.group_id)).FirstOrDefault();
                    agv.load_infos = context.LoadInfos.Where(s => s.robot_id == agv.id).ToList();
                    int robotType = agv.type;
                    RobotType robotType1 = context.RobotTypes.Where(s => s.id == robotType).FirstOrDefault();
                    if (robotType1 != null)
                    {
                        switch (robotType1.name.ToLower())
                        {
                            case "mir":
                                agv.operation = new MiROperation(agv);
                                break;
                        }
                    }
                    else
                    {
                        agv.operation = new MiROperation(agv);
                    }
                }
            }
            return list;
        }
        public static bool SetRobot(Robot robot)
        {
            if (robot != null)
            {
                if (Monitor.TryEnter(robot, 3000))
                {
                    try
                    {
                        using (ConfigContext context = new ConfigContext())
                        {
                            var find = context.Robots.FirstOrDefault(x => x.id == robot.id);
                            if (find != null)
                            {
                                find.auto = robot.auto;
                                find.use_fleet = robot.use_fleet;
                                find.is_debug = robot.is_debug;
                            }
                            foreach (var item in robot.load_infos)
                            {
                                var loadInfo = context.LoadInfos.FirstOrDefault(s => s.id == item.id);
                                if (loadInfo != null)
                                {
                                    loadInfo.shelf_type = item.shelf_type;
                                    loadInfo.shelf_id = item.shelf_id;
                                }
                            }
                            context.SaveChangesAsync();
                            return true;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogUtil.Error($"SetRobot {robot?.name}",ex);
                    }
                    finally { Monitor.Exit(robot); }

                }

            }
            return false;
        }
        #endregion

        #region 节点
        public static List<ClientNode> GetNodes()
        {
            List<ClientNode> list;
            using (ConfigContext context = new ConfigContext())
            {
                list = context.Nodes.ToList();
                foreach (var item in list)
                {
                    if (string.IsNullOrEmpty(item.ip)) item.online = true;
                    item.workshop = context.Workshops.Where(s => s.id.Equals(item.workshop_id)).FirstOrDefault();
                }
            }
            return list;
        }
        public static bool SetNode(ClientNode clientNode)
        {
            using (ConfigContext context = new ConfigContext())
            {
                var find = context.Nodes.Where(s => s.id == clientNode.id).FirstOrDefault();
                if (find != null)
                {
                    find.opened = clientNode.opened;
                    find.online = clientNode.online;
                    find.status = clientNode.status;
                    find.description = clientNode.description;
                    find.level = clientNode.level;
                    find.shelf_id = clientNode.shelf_id;
                    find.shielded = clientNode.shielded;
                    context.SaveChangesAsync();
                }
            }
            return false;
        }
        public static bool AddNode(ClientNode clientNode)
        {
            using (ConfigContext context = new ConfigContext())
            {
                var find = context.Nodes.Where(s => s.id == clientNode.id).FirstOrDefault();
                if (find == null)
                {
                    context.Nodes.Add(clientNode);
                    context.SaveChangesAsync();
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region 任务
        public static List<Mission> Get_Missions()
        {
            List<Mission> list;
            using (ConfigContext context = new ConfigContext())
            {
                list = context.Missions.ToList();
                foreach (var item in list)
                {
                    item.type = context.MissionTypes.Where(s => s.id.Equals(item.type_id)).FirstOrDefault();
                    item.group = context.MissionGroups.Where(s => s.id.Equals(item.group_id)).FirstOrDefault();
                }
            }
            return list;
        }
        #endregion

        #region 订单
        public static List<Order> Get_Orders()
        {
            List<Order> list;
            using (ConfigContext context = new ConfigContext())
            {
                list = context.Orders.ToList();
            }
            return list;
        }
        public static bool Add_Order(Order order)
        {
            using (ConfigContext context = new ConfigContext())
            {
                var find = context.Orders.Where(s => s.id.Equals(order.id)).FirstOrDefault();
                if (find == null)
                {
                    context.Orders.Add(order);
                    context.SaveChangesAsync();
                    return true;
                }
            }
            return false;
        }
        public static bool Del_Order(Order order)
        {
            using (ConfigContext context = new ConfigContext())
            {
                var find = context.Orders.Where(s => s.id.Equals(order.id)).FirstOrDefault();
                if (find != null)
                {
                    context.Orders.Remove(find);
                    context.SaveChangesAsync();
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 删除任务编号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Del_Order(string id)
        {
            using (ConfigContext context = new ConfigContext())
            {
                var find = context.Orders.Where(s => s.id.Equals(id)).FirstOrDefault();
                if (find != null)
                {
                    context.Orders.Remove(find);
                    context.SaveChangesAsync();
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 删除任务类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Del_Order(int typeId)
        {
            using (ConfigContext context = new ConfigContext())
            {
                var find = context.Orders.Where(s => s.type.Equals(typeId)).FirstOrDefault();
                if (find != null)
                {
                    context.Orders.Remove(find);
                    context.SaveChangesAsync();
                    return true;
                }
            }
            return false;
        }
        public static bool SetOrder(Order order)
        {
            using (ConfigContext context = new ConfigContext())
            {
                var find = context.Orders.Where(s => s.id.Equals(order.id)).FirstOrDefault();
                if (find != null)
                {
                    find.state = order.state;
                    find.robot_id = order.robot_id;
                    context.SaveChangesAsync();
                    return true;
                }

            }
            return false;
        }
        public static OrderState GetOrderState(int id)
        {
            using (ConfigContext context = new ConfigContext())
            {
                return context.OrderStates.Where(s => s.id.Equals(id)).FirstOrDefault();
            }
        }
        public static OrderType GetOrderType(int id)
        {
            using (ConfigContext context = new ConfigContext())
            {
                return context.OrderTypes.Where(s => s.id.Equals(id)).FirstOrDefault();
            }
        }
        public static List<OrderType> GetAllOrderTypes()
        {
            using (ConfigContext context = new ConfigContext())
            {
                return context.OrderTypes.ToList();
            }
        }
        #endregion
    }
}