UCRobotStatus.cs 23.3 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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
using Common;
using DeviceLib.BLL;
using DeviceLib.Model.AGV;
using Dolen.Forms;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace UControl.UC
{
    public partial class UCRobotStatus : UserControl
    {
        public UCRobotStatus(Robot robot)
        {
            InitializeComponent();
            ucInfoControlWarn.SetForeColor(Color.Red);
            this.robot = robot;
            timer1.Start();
        }
        public bool EditMode { get; set; } = false;
        public Robot robot;
        public int MaxBattery { get; set; } = 90;
        public int MinBattery { get; set; } = 20;
        int posLeft = 1;
        int posRight = 2;
        void SetTitle()
        {
            this.Invoke(new Action(() =>
            {
                lblTitle.Text = robot.name;
            }
            ));
        }
        void enableEdit()
        {
            EditMode = true;
        }
        /// <summary>
        /// 设置电量
        /// </summary>
        /// <param name="curBattery"></param>
        /// <param name="isInCharge"></param>
        void SetBattery(double curBattery, bool isInCharge = false)
        {
            object[] objs = Manager.SetBattery(curBattery, isInCharge, MinBattery, MaxBattery);
            try
            {
                this.Invoke(new Action(() =>
                {
                    lblBattery.Text = objs[0] as string;
                    picBattery.Image = objs[1] as Image;
                }
                ));
            }
            catch { }
        }
        /// <summary>
        /// 设置连接状态
        /// </summary>
        /// <param name="state"></param>
        void SetConnectState(bool state = false)
        {
            Image image = Manager.SetConnState(state);
            this.Invoke(new Action(() =>
            {
                picWifi.Image = image;
            }
            ));
        }
        /// <summary>
        /// 设置当前任务名称
        /// </summary>
        /// <param name="missionName"></param>
        void SetMissionName(string missionName)
        {
            this.Invoke(new Action(() =>
            {
                lblMissionName.Text = missionName;
            }
            ));
        }
        /// <summary>
        /// 设置agv当前状态
        /// </summary>
        /// <param name="curState"></param>
        void SetMissionState(string agvState, string missionState)
        {
            object[] objs = Manager.SetAGVState(agvState);
            this.Invoke(new Action(() =>
            {
                lblMissionStateStr.Text = $"【{agvState}】【{missionState}】【{robot.mission_state?.run_id ?? -1}】";
                picMissionState.Image = objs[1] as Image;
            }
            ));
        }
        void setCustomState(RobotState state)
        {
            lblCurStatus.Text = robot.state.ToString();
        }
        #region 圆角
        private void UCRobotStatus_Resize(object sender, EventArgs e)
        {
            // SetWindowRegion();
        }
        public void SetWindowRegion()
        {
            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 10);
            this.Region = new Region(FormPath);

        }
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }
        #endregion
        object locObj = new object();
        /// <summary>
        /// 信息刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!this.IsHandleCreated)
                return;
            if (Monitor.TryEnter(locObj, 300))
            {
                try
                {
                    this.Invoke(new Action(() =>
                    {
                        负载信息ToolStripMenuItem.Enabled = !robot.auto;
                        SetTitle();
                        updateRunMode();
                        updateShelfSett();
                        //机器人自定义状态
                        setCustomState(robot.state);
                        //当前运行模式
                        if (robot.auto)
                        {
                            if (robot.is_debug)
                            {
                                lblMode.Text = "调试模式";
                            }
                            else
                                lblMode.Text = "自动模式";
                        }
                        else
                        {
                            lblMode.Text = "手动模式";
                        }
                        //订单信息
                        ucInfoControlOrder.Info = $"{robot?.job?.JobParam?.OrderId ?? ""}";
                        //车间
                        lblWorkshop.Text = robot?.workshop?.description ?? "";
                        //设置当前机器人状态
                        SetMissionState(robot?.status?.state ?? "", robot?.mission_state?.state ?? "");
                        //设置机器人当前任务名称
                        SetMissionName($"【{robot?.mission_state?.mission?.alias ?? ""}】");//【{robot?.mission_state?.mission?.alias ?? ""}】【{robot?.mission_state?.mission?.name ?? ""}】
                        //设置运输任务名称
                        ucInfoControlJobName.Info = robot?.job?.JobName ?? "无运输任务";
                        //运输任务步骤
                        ucInfoControlStep.Info = robot?.job?.JobRunStep?.CurStepStr ?? "";
                        //运输步骤信息
                        ucInfoControlStepMsg.Info = robot?.job?.JobRunStep?.Msg ?? "";
                        //步骤警告信息
                        ucInfoControlWarn.Info = robot?.job?.JobRunStep?.WarnMsg ?? "";
                        //设置电量
                        SetBattery(robot?.status?.battery_percentage ?? 0.0, robot.state.Equals(RobotState.充电中));
                        //设置连接状态
                        SetConnectState(robot.status?.is_online ?? false);
                        //离目的地距离
                        lblDistance.Text = $"{robot.status?.distance_to_next_target.ToString("f1") ?? "0"}米";
                        //车上负载信息
                        setLoadInfo();
                        //位置
                        lblPos.Text = $"{robot?.place ?? ""}";
                    }
                    ));
                }
                catch (Exception ex)
                {
                    LogUtil.Error($"{robot?.name ?? ""}机器人状态更新出错", ex);
                }
                finally
                {
                    Monitor.Exit(locObj);
                }
            }
        }
        private void setLoadInfo()
        {
            if (robot.load_infos == null)
            {
                ucInfoControlShelf.Info = "null";
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in robot.load_infos)
                {
                    string pos = item.pos == posLeft ? "左" : "右";
                    sb.Append($"【{pos};{item.shelf_id};{item.shelf_type}】");
                }
                ucInfoControlShelf.Info = sb.ToString();
            }
        }
        string gouhao = "✔ ";
        private void updateRunMode()
        {
            if (robot.auto)
            {
                menuItemOpenAuto.Text = $"{gouhao}开启";
                menuItemCloseAuto.Text = $"关闭";
            }
            else
            {
                menuItemOpenAuto.Text = $"开启";
                menuItemCloseAuto.Text = $"{gouhao}关闭";
            }
            if (robot.is_debug)
            {
                menuItemOpenDebug.Text = $"{gouhao}开启";
                menuItemCloseDebug.Text = $"关闭";
            }
            else
            {
                menuItemOpenDebug.Text = $"开启";
                menuItemCloseDebug.Text = $"{gouhao}关闭";
            }
        }
        private void updateShelfSett()
        {
            if (robot.load_infos != null)
            {
                foreach (var item in robot.load_infos)
                {
                    if (item.pos == posLeft)
                    {
                        左货架号toolStripTextBox1.Text = item.shelf_id;
                        if (item.shelf_type.Equals(ShelfType.None))
                        {
                            左无货架ToolStripMenuItem.Text = $"{gouhao}无货架";
                            左有满货架ToolStripMenuItem.Text = $"有满货架";
                            左有空货架ToolStripMenuItem.Text = $"有空货架";

                        }
                        else if (item.shelf_type.Equals(ShelfType.Empty))
                        {
                            左无货架ToolStripMenuItem.Text = $"无货架";
                            左有满货架ToolStripMenuItem.Text = $"有满货架";
                            左有空货架ToolStripMenuItem.Text = $"{gouhao}有空货架";
                        }
                        else if (item.shelf_type.Equals(ShelfType.Full))
                        {
                            左无货架ToolStripMenuItem.Text = $"无货架";
                            左有满货架ToolStripMenuItem.Text = $"{gouhao}有满货架";
                            左有空货架ToolStripMenuItem.Text = $"有空货架";
                        }
                    }
                    else
                    {
                        右货架号toolStripTextBox1.Text = item.shelf_id;
                        if (item.shelf_type.Equals(ShelfType.None))
                        {
                            右无货架ToolStripMenuItem.Text = $"{gouhao}无货架";
                            右有满货架ToolStripMenuItem.Text = $"有满货架";
                            右有空货架ToolStripMenuItem.Text = $"有空货架";

                        }
                        else if (item.shelf_type.Equals(ShelfType.Empty))
                        {
                            右无货架ToolStripMenuItem.Text = $"无货架";
                            右有满货架ToolStripMenuItem.Text = $"有满货架";
                            右有空货架ToolStripMenuItem.Text = $"{gouhao}有空货架";
                        }
                        else if (item.shelf_type.Equals(ShelfType.Full))
                        {
                            右无货架ToolStripMenuItem.Text = $"无货架";
                            右有满货架ToolStripMenuItem.Text = $"{gouhao}有满货架";
                            右有空货架ToolStripMenuItem.Text = $"有空货架";
                        }
                    }
                }
            }
        }

        #region 设置
        private void menuItemOpenAuto_Click(object sender, EventArgs e)
        {
            if (checkState())
            {
                robot.auto = true;
                enableEdit();
                LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:打开自动模式");
            }

        }
        bool checkState()
        {
            try
            {
                bool OpenCheckLoadIO = AppConfigSetting.GetBoolVal(Setting_Str.OpenCheckLoadIO, false);
                if (!OpenCheckLoadIO) return true;
                if (robot.io_modules != null && robot.io_modules.Count == 2)
                {
                    if (robot.io_modules[0].status?.input_state != null
                        && robot.io_modules[0].status?.input_state.Count == 4
                        && robot.io_modules[1].status?.input_state != null
                          && robot.io_modules[1].status?.input_state.Count == 4)
                    {
                        if ((robot.io_modules[0].status?.input_state[1]??false) 
                            && (robot.io_modules[1].status?.input_state[1] ?? false))
                        {
                            MessageBox.Show("检测到左右两侧均有容器,请拿走一个!");
                            return false;
                        }
                        else if (!(robot.io_modules[0].status?.input_state[1] ?? false)
                            && !(robot.io_modules[1].status?.input_state[1] ?? false))
                        {
                            MessageBox.Show("检测到左右两侧无容器,请放一个到AGV上!");
                            return false;
                        }
                        else if ((robot.io_modules[0].status?.input_state[1] ?? false)
                            && !(robot.io_modules[1].status?.input_state[1] ?? false))//左侧有,右侧无
                        {
                            LoadInfo loadInfo = getLoadInfo(posLeft);
                            if (loadInfo != null)
                            {
                                loadInfo.shelf_type = ShelfType.Empty;
                            }
                            LoadInfo loadInfo1 = getLoadInfo(posRight);
                            if (loadInfo1 != null)
                            {
                                loadInfo1.shelf_type = ShelfType.None;
                                loadInfo1.shelf_id = "";
                            }
                            setRobot();
                            LogUtil.Info($"{robot.name}切换为自动时,AGV负载状态:左侧有空容器,右侧无容器");
                            return true;
                        }
                        else if (!(robot.io_modules[0].status?.input_state[1] ?? false)
                                && (robot.io_modules[1].status?.input_state[1] ?? false))//左侧无,右侧有
                        {
                            LoadInfo loadInfo = getLoadInfo(posLeft);
                            if (loadInfo != null)
                            {
                                loadInfo.shelf_type = ShelfType.None;
                                loadInfo.shelf_id = "";
                            }
                            LoadInfo loadInfo1 = getLoadInfo(posRight);
                            if (loadInfo1 != null)
                            {
                                loadInfo1.shelf_type = ShelfType.Empty;
                            }
                            setRobot();
                            LogUtil.Info($"{robot.name}切换为自动时,AGV负载状态:左侧无容器,右侧有空容器");
                            return true;
                        }
                    }
                    else
                    {
                        MessageBox.Show("获取车上负载状态异常!");
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.Error("检查小车负载异常", ex);
            }
            return false;
        }
        private void menuItemCloseAuto_Click(object sender, EventArgs e)
        {
            if (robot.auto)
            {
                DialogResult dialogResult = MessageBox.Show("将小车切换为手动状态将清除当前运输任务,是否切换?", "提示", MessageBoxButtons.OKCancel);
                if (dialogResult == DialogResult.OK)
                {
                    robot.auto = false;
                    if (!string.IsNullOrEmpty(robot?.job?.JobParam?.OrderId ?? ""))
                        OrderManager.DeleteOrderById(robot.job.JobParam.OrderId, out string msg, true);
                    Context.SetNodeOccupied(robot.id);
                    LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:关闭自动模式。中断运输单:{robot?.job?.JobParam?.OrderId}");
                    robot.job = null;
                    MissionManager.Init(robot);
                }
            }

            enableEdit();
        }

        private void menuItemOpenDebug_Click(object sender, EventArgs e)
        {
            robot.is_debug = true;
            enableEdit();
            LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:打开调试模式");
        }

        private void menuItemCloseDebug_Click(object sender, EventArgs e)
        {
            robot.is_debug = false;
            enableEdit();
            LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:关闭调试模式");
        }
        private void RobotStateChange_Click(object sender, EventArgs e)
        {
            if (robot.status == null) return;
            switch (robot.status.state_id)
            {
                case 4:
                    robot.operation?.Set_Ready();
                    LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:Pause->Ready");
                    break;
                case 11:
                    robot.operation?.Set_Ready();
                    LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:ManualControl->Ready");
                    break;
                case 12:
                    robot.operation?.Clear_Error();
                    LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:清除 Error");
                    break;
                default:
                    robot.operation?.Set_Pause();
                    LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:点击 Pause");
                    break;
            }
            enableEdit();
        }
        /// <summary>
        /// 任务重发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (robot.auto)
            {
                try
                {
                    robot.job?.ReSendMission(robot, true);
                    LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:任务重发");
                }
                catch (Exception ex)
                {
                    LogUtil.Error($"{robot?.name ?? ""}机器人手动操作:任务重发出错", ex);
                }
                enableEdit();
            }
        }
        #endregion
        private LoadInfo getLoadInfo(int pos)
        {
            return robot.load_infos?.Find(s => s.pos == pos) ?? null;
        }
        private void 左无货架ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadInfo loadInfo = getLoadInfo(posLeft);
            if (loadInfo != null)
            {
                loadInfo.shelf_type = ShelfType.None;
                enableEdit();
                setRobot();
                LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:设置左侧无货架");
            }

        }

        private void 左有空货架ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadInfo loadInfo = getLoadInfo(posLeft);
            if (loadInfo != null)
            {
                loadInfo.shelf_type = ShelfType.Empty;
                enableEdit();
                setRobot();
                LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:设置左侧有空货架");
            }
        }

        private void 左有满货架ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadInfo loadInfo = getLoadInfo(posLeft);
            if (loadInfo != null)
            {
                loadInfo.shelf_type = ShelfType.Full;
                enableEdit();
                setRobot();
                LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:设置左侧有满货架");
            }
        }

        private void 右无货架ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadInfo loadInfo = getLoadInfo(posRight);
            if (loadInfo != null)
            {
                loadInfo.shelf_type = ShelfType.None;
                enableEdit();
                setRobot();
                LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:设置右侧无货架");
            }
        }

        private void 右有空货架ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadInfo loadInfo = getLoadInfo(posRight);
            if (loadInfo != null)
            {
                loadInfo.shelf_type = ShelfType.Empty;
                enableEdit();
                setRobot();
                LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:设置右侧有空货架");
            }
        }

        private void 右有满货架ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadInfo loadInfo = getLoadInfo(posRight);
            if (loadInfo != null)
            {
                loadInfo.shelf_type = ShelfType.Full;
                enableEdit();
                setRobot();
                LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:设置右侧有满货架");
            }
        }

        private void 左货架号toolStripTextBox1_TextChanged(object sender, EventArgs e)
        {
            LoadInfo loadInfo = getLoadInfo(posLeft);
            if (loadInfo != null)
            {
                loadInfo.shelf_id = 左货架号toolStripTextBox1.Text;
                enableEdit();
                setRobot();
                LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:设置左侧货架号为{loadInfo.shelf_id}");
            }
        }

        private void 右货架号toolStripTextBox1_TextChanged(object sender, EventArgs e)
        {
            LoadInfo loadInfo = getLoadInfo(posRight);
            if (loadInfo != null)
            {
                loadInfo.shelf_id = 右货架号toolStripTextBox1.Text;
                enableEdit();
                setRobot();
                LogUtil.Info($"{robot?.name ?? ""}机器人手动操作:设置右侧货架号为{loadInfo.shelf_id}");
            }
        }

        void setRobot()
        {
            RobotManager.SetRobot(robot);
        }

        private void robotSetting_DropDownClosed(object sender, EventArgs e)
        {
            canSettting(false);
        }

        private void robotSetting_DropDownOpening(object sender, EventArgs e)
        {
            //FrmPwd fw = new FrmPwd(10);
            //DialogResult result = fw.ShowDialog();
            //if (!result.Equals(DialogResult.OK))
            //{
            //    LogUtil.Warn($"打开机器人{robot.name}设置时,没有正确输入密码");
            //    canSettting(false);
            //    return;
            //}
            canSettting(true);
        }
        void canSettting(bool can)
        {
            自动ToolStripMenuItem.Enabled = can;
            调试ToolStripMenuItem.Enabled = can;
        }
    }
}