GrooveComputerVM.cs 4.76 KB
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HHECS.BllModel;
using HHECS.RobotTool.Dto;
using HHECS.RobotTool.Dto.WeldMonitor;
using HHECS.RobotTool.Service;
using HHECS.RobotTool.Utils;
using HHECS.RobotTool.View;
using MessageBox = HandyControl.Controls.MessageBox;

namespace HHECS.RobotTool.ViewModel
{
    internal partial class GrooveComputerVM : ObservableObject
    {
        [ObservableProperty]
        private RobotInputParameter inputParameter = new RobotInputParameter();

        [ObservableProperty]
        private RobotOutputParameter outputParameter = new RobotOutputParameter();

        /// <summary>
        /// 监控地址
        /// </summary>
        /// <remarks>http://172.16.29.87:6002/index.html#/Home?key={currentIdentifier}&w=770&h=520&font=12</remarks>
        [ObservableProperty]
        private string monitorSource = string.Empty;

        [ObservableProperty]
        private List<WeldMonitorDataGrid> weldMonitorDataGrids = new List<WeldMonitorDataGrid>();

        public GrooveComputerPage Owner = null!;

        /// <summary>
        /// 唯一标识
        /// </summary>
        private readonly Guid currentIdentifier;

        private readonly HttpService _httpService;

        public GrooveComputerVM(HttpService httpService)
        {
            currentIdentifier = Guid.NewGuid();
            MonitorSource = $"http://172.16.29.87:6002/index.html#/Home?key={currentIdentifier}&w=770&h=520&font=10";
            _httpService = httpService;
            Reset();
        }

        [RelayCommand]
        private void Computer()
        {
            var result = GrooveComputerTool.GetComputedData(InputParameter);
            if (!result.Success)
            {
                MessageBox.Error($"数据计算出现异常:{result.Msg}");
                return;
            }
            var data = result.Data;
            OutputParameter = data;
            WeldMonitorDataGrids = data.LayerModels.SelectMany(layer => layer.ChannelModels.Select(channel => new WeldMonitorDataGrid
            {
                Layer = layer.CurrentLayer,
                LayerHeight = layer.LayerHeight,
                SwingFrequency = layer.WeldSwingFrequency,
                LeftAndRightDwellTime = layer.LeftAndRightDwellTime,
                Channel = channel.CurrentChannel,
                StartingPoint = channel.StartingPoint,
                EndingPoint = channel.EndingPoint,
                WeldSpeed = Math.Round(channel.WeldSpeed, 3),
                Amplitude = channel.WeldAmplitude,
            })).ToList();
            Task.Run(() =>
            {
                var reloadResult = ReloadMonitor(data);
                if (!reloadResult.Success)
                {
                    MessageBox.Error($"监控页面刷新失败:{reloadResult.Msg}");
                }
            });
        }

        [RelayCommand]
        private void Reset()
        {
            InputParameter = new RobotInputParameter
            {
                GrooveWidth = 37,//坡口宽度BC
                GrooveDepth = 30,//坡口深度H
                WeldLength = 1000,//焊缝长度AD
                WeldingWire = 1.2f,//焊丝直径1.2mm D
                OneWeldHeight = 4.5f,//一次焊接的高度4.5mm
                WeldHeight = 0,//盖面后所需焊缝余高2mm
                WeldWidth = 0,//盖面后单边所需增加余宽1.5mm
                WireFeedingSpeed = 154,//送丝速度固定154mm/s
                OneLayerWireFeedingSpeed = 124,//一层打底速度
            };
            OutputParameter = new RobotOutputParameter();
            WeldMonitorDataGrids = new List<WeldMonitorDataGrid>();
            Task.Run(() =>
            {
                ReloadMonitor(new RobotOutputParameter());
            });
        }

        private BllResult ReloadMonitor(RobotOutputParameter data)
        {
            try
            {
                var monitor = new WeldMonitorDto
                {
                    Key = currentIdentifier,
                    Height = data.LayerModels.Sum(x => x.LayerHeight),
                    Data = data.LayerModels.Select(l => new LayerItem
                    {
                        Layer = l.CurrentLayer,
                        Height = l.LayerHeight,
                        Done = l.ChannelModels.Select(c => new DoneItem
                        {
                            State = 0,
                            Position = $"Y:{Math.Round(c.StartingPoint.Y, 1)},Z:{Math.Round(c.StartingPoint.Z, 1)}"
                        }).ToList()
                    }).ToList(),
                };
                return _httpService.SetWeldMonitorData(monitor);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }
    }
}