AGVManager.cs 17.5 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
using DeviceLib.WebApi.Schemas;
using DeviceLibrary.AGVService.Schemas;
using DL.StandardRobot;
using OnlineStore;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static DL.StandardRobot.StandardRobot;

namespace DeviceLibrary
{
    public class AGVManager
    {
        #region 斯坦得AGV PLC方式
        static StandardRobot robot = new StandardRobot();
        public static void Connect(string ip)
        {
            robot.IP = ip;
            robot.Connect();
        }
        public static void Close()
        {
            robot.Close();
        }
        public static MissionState GetMissionState()
        {
            return robot.GetMissionState();
        }
        public static void Register(MissionStateChangedEventHandler changedEventHandler)
        {
            robot.MissionSateChanged += changedEventHandler;
        }
        public static void UnRegister(MissionStateChangedEventHandler changedEventHandler)
        {
            robot.MissionSateChanged -= changedEventHandler;
        }
        public static int CurMissionId = 0;
        public static void AddMission(int id)
        {
            CurMissionId = id;
            robot.AddMission(id);
        }
        public static bool IsConnected()
        {
            return false;
        }
        #endregion
        static string std_httpserver = ConfigHelper.Config.Get("std_httpserver");
        static string api_createTask = "/nepconSmf/createTask";
        static List<TaskState> taskStates = new List<TaskState>();
        /// <summary>
        /// MIMO折叠门是否开启
        /// </summary>
        public static bool StringDoorIsOpen = false;
        /// <summary>
        /// 更新AGV任务状态
        /// </summary>
        /// <param name="task_code"></param>
        /// <param name="task_status"></param>
        public static void UpdateTaskStatus(string task_code, string task_status)
        {
            if (Monitor.TryEnter(taskStates, 300))
            {
                try
                {
                    //var find = taskStates.Find(s => s.task_code.Equals(task_code));
                    //if (find != null)
                    //{
                    //    find.task_status = task_status;
                    //}
                    //else
                    //{
                    //    taskStates.Add(new TaskState() { task_code = task_code, task_status = task_status });
                    //}
                    if (taskStates.Count > 0)
                    {
                        var find = taskStates.Find(s => s.task_code.Equals(task_code));
                        if (find != null)
                        {
                            find.task_status = task_status;
                        }
                        else
                        {
                            taskStates.Clear();
                            taskStates.Add(new TaskState() { task_code = task_code, task_status = task_status });
                        }
                    }
                    else
                    {
                        taskStates.Add(new TaskState() { task_code = task_code, task_status = task_status });
                    }
                    sendAgvLog("AGV任务" + task_code + "状态更新" + task_status);
                }
                catch (Exception ex)
                {
                    LogUtil.error("UpdateTaskStatus error:" + task_code + "," + task_status, ex);
                }
                finally
                {
                    Monitor.Exit(taskStates);
                }
            }
        }

        /// <summary>
        /// 处理AGV请求的操作
        /// </summary>
        /// <param name="deviceCheckDto"></param>
        /// <param name="rtnData"></param>
        public static void HandleDeviceOperate(DeviceCheckDto deviceCheckDto, out RtnData rtnData)
        {
            rtnData = new RtnData();

            switch (deviceCheckDto.task_type)
            {
                case STDTaskType.NS200_LOAD_B1://NS200入料口上料任务
                    if (DeviceCheckDto.LOAD_REQ.Equals(deviceCheckDto.operate))//上料请求
                    {
                        if (!RobotManage.isRunning)
                        {
                            rtnData.code = -1;
                            rtnData.msg = crc.GetString("Res0079", "机器尚未启动不能呼叫Agv");
                        }
                        else
                        {
                            RobotManage.mainMachine.RightMoveInfo.NewMove(MoveStep.R40_InShelf);//等待agv到位后执行,启动进入
                        }
                    }
                    else if (DeviceCheckDto.LOAD_DONE.Equals(deviceCheckDto.operate))//上料完成
                    {
                        if (RobotManage.mainMachine.RightMoveInfo.MoveStep >= MoveStep.R40_InShelf)
                        {
                            rtnData.code = -1;
                            rtnData.msg = "入料口正在进料";
                        }
                    }
                    else
                    {
                        rtnData.code = -1;
                        rtnData.msg = "任务类型" + deviceCheckDto.task_type + "不允许使用该操作" + deviceCheckDto.operate;
                    }
                    break;
                case STDTaskType.NS200_UNLOAD_B1://NS200入料口下料任务
                    if (DeviceCheckDto.UNLOAD_REQ.Equals(deviceCheckDto.operate))//下料请求
                    {
                        if (!RobotManage.isRunning)
                        {
                            rtnData.code = -1;
                            rtnData.msg = crc.GetString("Res0079", "机器尚未启动不能呼叫Agv");
                        }
                        else
                        {
                            RobotManage.mainMachine.RightMoveInfo.NewMove(MoveStep.R30_OutShelf);//等待agv到位后执行,启动料串离开 
                        }
                    }
                    else if (DeviceCheckDto.UNLOAD_DONE.Equals(deviceCheckDto.operate))//下料完成
                    {
                        if (RobotManage.mainMachine.RightMoveInfo.MoveStep >= MoveStep.R30_OutShelf)
                        {
                            rtnData.code = -1;
                            rtnData.msg = "入料口正在出料";
                        }
                    }
                    else
                    {
                        rtnData.code = -1;
                        rtnData.msg = "任务类型" + deviceCheckDto.task_type + "不允许使用该操作" + deviceCheckDto.operate;
                    }
                    break;
                case STDTaskType.NS200_LOAD_B2://NS200出料口上料任务
                    if (DeviceCheckDto.LOAD_REQ.Equals(deviceCheckDto.operate))//上料请求
                    {
                        if (!RobotManage.isRunning)
                        {
                            rtnData.code = -1;
                            rtnData.msg = crc.GetString("Res0079", "机器尚未启动不能呼叫Agv");
                        }
                        else
                        {
                            RobotManage.mainMachine.LeftMoveInfo.NewMove(MoveStep.L60_InShelf);//等待agv到位后执行,启动进入
                        }
                    }
                    else if (DeviceCheckDto.LOAD_DONE.Equals(deviceCheckDto.operate))//上料完成
                    {
                        if (RobotManage.mainMachine.LeftMoveInfo.MoveStep >= MoveStep.L60_InShelf)
                        {
                            rtnData.code = -1;
                            rtnData.msg = "出料口正在入料";
                        }
                    }
                    else
                    {
                        rtnData.code = -1;
                        rtnData.msg = "任务类型" + deviceCheckDto.task_type + "不允许使用该操作" + deviceCheckDto.operate;
                    }
                    break;
                case STDTaskType.NS200_UNLOAD_B2://NS200出料口下料任务
                    if (DeviceCheckDto.UNLOAD_REQ.Equals(deviceCheckDto.operate))//下料请求
                    {
                        if (!RobotManage.isRunning)
                        {
                            rtnData.code = -1;
                            rtnData.msg = crc.GetString("Res0079", "机器尚未启动不能呼叫Agv");
                        }
                        else
                        {
                            RobotManage.mainMachine.LeftMoveInfo.NewMove(MoveStep.L50_OutShelf);//等待agv到位后执行,启动料串离开 
                        }
                    }
                    else if (DeviceCheckDto.UNLOAD_DONE.Equals(deviceCheckDto.operate))//下料完成
                    {
                        if (RobotManage.mainMachine.LeftMoveInfo.MoveStep >= MoveStep.L50_OutShelf)
                        {
                            rtnData.code = -1;
                            rtnData.msg = "出料口正在出料";
                        }
                    }
                    else
                    {
                        rtnData.code = -1;
                        rtnData.msg = "任务类型" + deviceCheckDto.task_type + "不允许使用该操作" + deviceCheckDto.operate;
                    }
                    break;
                case STDTaskType.MIMO_LOAD://MIMO上料任务
                    if (DeviceCheckDto.LOAD_REQ.Equals(deviceCheckDto.operate))//上料请求
                    {
                        if (!StringDoorIsOpen)
                        {
                            rtnData.code = -1;
                            rtnData.msg = "折叠门开门信号未亮,不允许上料";
                        }
                        else
                        {
                            //链条正转
                            sendOpMimoCmd(deviceCheckDto.operate, StringLine(true));
                        }
                    }
                    else if (DeviceCheckDto.OPEN_DOOR.Equals(deviceCheckDto.operate))//开门请求
                    {
                        if (!StringDoorIsOpen)//开门
                        {
                            sendOpMimoCmd(deviceCheckDto.operate, StringDoor(true));
                        }
                    }
                    else if (DeviceCheckDto.CLOSE_DOOR.Equals(deviceCheckDto.operate))//关门请求
                    {
                        sendOpMimoCmd(deviceCheckDto.operate, StringDoor(false));
                    }
                    else if (DeviceCheckDto.LOAD_DONE.Equals(deviceCheckDto.operate))//上料完成
                    {

                    }
                    else
                    {
                        rtnData.code = -1;
                        rtnData.msg = "任务类型" + deviceCheckDto.task_type + "不允许使用该操作" + deviceCheckDto.operate;
                    }
                    break;

                case STDTaskType.MIMO_UNLOAD:////MIMO下料任务
                    if (DeviceCheckDto.UNLOAD_REQ.Equals(deviceCheckDto.operate))//下料请求
                    {
                        if (!StringDoorIsOpen)
                        {
                            rtnData.code = -1;
                            rtnData.msg = "折叠门开门信号未亮,不允许下料";
                        }
                        else
                        {
                            //链条反转
                            sendOpMimoCmd(deviceCheckDto.operate, StringBackLine(true));
                        }
                    }
                    else if (DeviceCheckDto.OPEN_DOOR.Equals(deviceCheckDto.operate))//开门请求
                    {
                        if (!StringDoorIsOpen)//开门
                        {
                            sendOpMimoCmd(deviceCheckDto.operate, StringDoor(true));
                        }
                    }
                    else if (DeviceCheckDto.CLOSE_DOOR.Equals(deviceCheckDto.operate))//关门请求
                    {
                        sendOpMimoCmd(deviceCheckDto.operate, StringDoor(false));
                    }
                    else if (DeviceCheckDto.UNLOAD_DONE.Equals(deviceCheckDto.operate))//下料完成
                    {

                    }
                    else
                    {
                        rtnData.code = -1;
                        rtnData.msg = "任务类型" + deviceCheckDto.task_type + "不允许使用该操作" + deviceCheckDto.operate;
                    }
                    break;
            }
            sendAgvLog($"AGV在设备{deviceCheckDto.device_code}的操作{deviceCheckDto.operate}");
        }
        static Dictionary<string, string> StringDoor(bool open)
        {
            //Y14,Y15 折叠门 open/close
            if (open)
            {
                return new Dictionary<string, string> { { "openLock", "doit" } };
            }
            else
            {
                return new Dictionary<string, string> { { "closeLock", "doit" } };
            }
        }
        static Dictionary<string, string> StringLine(bool run)
        {
            if (run)
            {
                return new Dictionary<string, string> { { "Y08", "open" } };
            }
            else
            {
                return new Dictionary<string, string> { { "Y08", "close" } };
            }
        }
        static Dictionary<string, string> StringBackLine(bool run)
        {
            if (run)
            {
                return new Dictionary<string, string> { { "Y09", "open" } };
            }
            else
            {
                return new Dictionary<string, string> { { "Y09", "close" } };
            }
        }
        /// <summary>
        /// 处理AGV任务状态更新
        /// </summary>
        /// <param name="notificationDto"></param>
        /// <param name="rtnData"></param>
        public static void HandleTaskNotification(NotificationDto notificationDto, out RtnData rtnData)
        {
            rtnData = new RtnData();
            UpdateTaskStatus(notificationDto.task_code, notificationDto.status);
        }
        static void sendAgvLog(string msg)
        {
            Task.Factory.StartNew(() =>
            {
                while (!RobotManage.mainMachine.ServerCM.SendAgvLog(msg))
                {
                    Thread.Sleep(500);
                }
            });
        }
        static void sendOpMimoCmd(string opMsg, Dictionary<string, string> op)
        {
            Task.Factory.StartNew(() =>
            {
                while (!RobotManage.mainMachine.ServerCM.OpMIMO(opMsg, op))
                {
                    Thread.Sleep(500);
                }
            });
        }
        /// <summary>
        /// 创建AGV运输任务
        /// </summary>
        /// <param name="task_type"></param>
        /// <returns></returns>
        public static bool CreateAGVTask(string task_type)
        {
            TaskDto taskDto = new TaskDto() { task_type = task_type, task_code = $"{task_type}_{DateTime.Now.ToString("MMddHHmmss")}" };
            try
            {
                string data = JsonHelper.SerializeObject(taskDto);
                string result = HttpHelper.Post(std_httpserver + api_createTask, data, 700);
                LogUtil.info($"CreateAGVTask :[{data}][{result}]");
                sendAgvLog("Create task:" + task_type + ";FMS response:" + result);
                if (string.IsNullOrEmpty(result))
                {
                    return false;
                }
                RtnData rtnData = JsonHelper.DeserializeJsonToObject<RtnData>(result);
                if (rtnData != null)
                {
                    if (rtnData.code == 0)
                    {
                        UpdateTaskStatus(taskDto.task_code, taskDto.task_type);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error($"CreateAGVTask Error:{taskDto.task_code}", ex);
            }
            return false;
        }
    }

    public class TaskDto
    {
        /// <summary>
        /// 任务编码
        /// </summary>
        public string task_code { get; set; }
        /// <summary>
        /// 任务类型
        /// </summary>
        public string task_type { get; set; }
    }
    /// <summary>
    /// 斯坦得AGV可执行的任务类型
    /// </summary>
    public class STDTaskType
    {
        /// <summary>
        /// NS200 入料口上料任务
        /// </summary>
        public const string NS200_LOAD_B1 = "NS200_LOAD_B1";
        /// <summary>
        /// NS200 入料口下料任务
        /// </summary>
        public const string NS200_UNLOAD_B1 = "NS200_UNLOAD_B1";
        /// <summary>
        /// NS200 出料口上料任务
        /// </summary>
        public const string NS200_LOAD_B2 = "NS200_LOAD_B2";
        /// <summary>
        /// NS200 出料口下料任务
        /// </summary>
        public const string NS200_UNLOAD_B2 = "NS200_UNLOAD_B2";
        /// <summary>
        /// MIMO上料任务
        /// </summary>
        public const string MIMO_LOAD = "MIMO_LOAD";
        /// <summary>
        /// MIMO下料任务
        /// </summary>
        public const string MIMO_UNLOAD = "MIMO_UNLOAD";
    }
    public class TaskState
    {
        public string task_code { get; set; }
        public bool updated { get; set; } = false;
        public string task_status { get; set; } = "CREATED";
    }
}