using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.CustomResource.UserControls.MessageShow; using BPASmartClient.MorkCL.HelpClass; using System.Reflection.Metadata; using System.Windows.Markup; namespace BPASmartClient.MorkCL.ViewModel { public class DebugViewModel : NotifyBase { private ushort heatGear; private ushort agitaFrequency; public DebugViewModel() { #region 炒锅1#命令实例 WriteAgitaParamCommand = new(async () => { //if (!GetDeviceIsIdle(EDeviceType.炒锅1)) //{ // return; //} if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅1, "LW1", (ushort)(AgitaFrequency * 100)), "WriteUshort"); await SendWriteRequest("LB10", EDeviceType.炒锅1); ShowNotify($"写入搅拌参数成功。"); }); StartAgitateCommand = new(() => { if (!GetDeviceIsIdle(EDeviceType.炒锅1)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅1, "LB0", true), "WriteBool"); ShowNotify($"写入搅拌启动成功。"); }); StopAgitateCommand = new(() => { //if (!GetDeviceIsIdle(EDeviceType.炒锅1)) //{ // return; //} if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅1, "LB0", false), "WriteBool"); ShowNotify($"写入搅拌停止成功。"); }); WriteHeatParamCommand = new(async () => { //if (!GetDeviceIsIdle(EDeviceType.炒锅1)) //{ // return; //} if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅1, "LW0", HeatGear), "WriteUshort"); await SendWriteRequest("LB9", EDeviceType.炒锅1); ShowNotify($"写入加热参数成功。"); }); StartHeatCommand = new(() => { if (!GetDeviceIsIdle(EDeviceType.炒锅1)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅1, "LB3", true), "WriteBool"); ShowNotify($"写入加热启动成功。"); }); StopHeatCommand = new(() => { //if (!GetDeviceIsIdle(EDeviceType.炒锅1)) //{ // return; //} if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅1, "LB3", false), "WriteBool"); ShowNotify($"写入加热停止成功。"); }); ChannelCuttingCommand = new(async () => { //var channelIndex = ChannelSelect; //var weight = Convert.ToUInt16(Weight); //byte[] LWOffset = new byte[3] { 3, 4, 5 }; //byte[] LBOffset = new byte[3] { 6, 7, 8 }; //ActionManage.GetInstance.Send("WriteUshort", new WriteModel(EDeviceType.炒锅1, $"LW{LWOffset[channelIndex]}", weight)); //await SendWriteRequest($"LB{LBOffset[channelIndex]}"); if (!GetDeviceIsIdle(EDeviceType.炒锅1)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } object[] para = new object[3]; para[0] = EDeviceType.炒锅1; para[1] = ChannelSelect + 1; para[2] = Convert.ToUInt16(Weight); await Task.Run(() => ActionManage.GetInstance.Send(para, "DeviceCuttingControl")); ShowNotify($"写入通道下料控制成功。"); }); GoFryLocCommand = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅1)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } var loc = StirFryLocSelect + 17; await SendWriteRequest($"LB{loc}", EDeviceType.炒锅1); ShowNotify($"写入去炒制位成功。"); }); GoHomeCommand = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅1)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB12", EDeviceType.炒锅1); ShowNotify($"写入回原点成功。"); }); GoSeasoningFeddLocCommand = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅1)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB16", EDeviceType.炒锅1); ShowNotify($"写入去调料投料位成功。"); }); OutDiningStartCommand = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅1)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB13", EDeviceType.炒锅1); ShowNotify($"写入出餐启动成功。"); }); FryPanCleanCommand = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅1)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB14", EDeviceType.炒锅1); ShowNotify($"写入炒锅清洗成功。"); }); SetAutoModeCommand = new(() => { ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅1, "LB20", true), "WriteBool"); ShowNotify($"设置设备自动模式成功。"); }); SetManualModeCommand = new(() => { if (!GetDeviceIsIdle(EDeviceType.炒锅1)) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅1, "LB20", false), "WriteBool"); ShowNotify($"设置设备手动模式成功。"); }); InitalDeviceCommand = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅1)) { return; } //复位所有变量。 if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ResetFryPanVariable(EDeviceType.炒锅1); await SendWriteRequest("LB21", EDeviceType.炒锅1); ShowNotify($"设置设备初始化成功。"); }); StopDeviceCommand = new(async () => { //if (!GetDeviceIsIdle(EDeviceType.炒锅1)) //{ // return; //} if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB22", EDeviceType.炒锅1); ShowNotify($"设置设备停止成功。"); }); //炒锅那边复位变量没有使用。 GoCleanLocCommand = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅1)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB23", EDeviceType.炒锅1); ShowNotify($"设置设备去洗锅位成功。"); }); #endregion #region 炒锅2#命令实例 WriteAgitaParamCommand2 = new(async () => { //if (!GetDeviceIsIdle(EDeviceType.炒锅2)) //{ // return; //} if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅2, "LW1", (ushort)(AgitaFrequency * 100)), "WriteUshort"); await SendWriteRequest("LB10", EDeviceType.炒锅2); ShowNotify($"写入搅拌参数成功。"); }); StartAgitateCommand2 = new(() => { if (!GetDeviceIsIdle(EDeviceType.炒锅2)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅2, "LB0", true), "WriteBool"); ShowNotify($"写入搅拌启动成功。"); }); StopAgitateCommand2 = new(() => { //if (!GetDeviceIsIdle(EDeviceType.炒锅2)) //{ // return; //} if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅2, "LB0", false), "WriteBool"); ShowNotify($"写入搅拌停止成功。"); }); WriteHeatParamCommand2 = new(async () => { //if (!GetDeviceIsIdle(EDeviceType.炒锅2)) //{ // return; //} if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅2, "LW0", HeatGear), "WriteUshort"); await SendWriteRequest("LB9", EDeviceType.炒锅2); ShowNotify($"写入加热参数成功。"); }); StartHeatCommand2 = new(() => { if (!GetDeviceIsIdle(EDeviceType.炒锅2)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅2, "LB3", true), "WriteBool"); ShowNotify($"写入加热启动成功。"); }); StopHeatCommand2 = new(() => { //if (!GetDeviceIsIdle(EDeviceType.炒锅2)) //{ // return; //} if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅2, "LB3", false), "WriteBool"); ShowNotify($"写入加热停止成功。"); }); ChannelCuttingCommand2 = new(async () => { //var channelIndex = ChannelSelect; //var weight = Convert.ToUInt16(Weight); //byte[] LWOffset = new byte[3] { 3, 4, 5 }; //byte[] LBOffset = new byte[3] { 6, 7, 8 }; //ActionManage.GetInstance.Send("WriteUshort", new WriteModel(EDeviceType.炒锅2, $"LW{LWOffset[channelIndex]}", weight)); //await SendWriteRequest($"LB{LBOffset[channelIndex]}"); if (!GetDeviceIsIdle(EDeviceType.炒锅2)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } object[] para = new object[3]; para[0] = EDeviceType.炒锅2; para[1] = ChannelSelect + 1; para[2] = Convert.ToUInt16(Weight); await Task.Run(() => ActionManage.GetInstance.Send(para, "DeviceCuttingControl")); ShowNotify($"写入通道下料控制成功。"); }); GoFryLocCommand2 = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅2)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } var loc = StirFryLocSelect + 17; await SendWriteRequest($"LB{loc}", EDeviceType.炒锅2); ShowNotify($"写入去炒制位成功。"); }); GoHomeCommand2 = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅2)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB12", EDeviceType.炒锅2); ShowNotify($"写入回原点成功。"); }); GoSeasoningFeddLocCommand2 = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅2)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB16", EDeviceType.炒锅2); ShowNotify($"写入去调料投料位成功。"); }); OutDiningStartCommand2 = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅2)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB13", EDeviceType.炒锅2); ShowNotify($"写入出餐启动成功。"); }); FryPanCleanCommand2 = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅2)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB14", EDeviceType.炒锅2); ShowNotify($"写入炒锅清洗成功。"); }); SetAutoModeCommand2 = new(() => { ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅2, "LB20", true), "WriteBool"); ShowNotify($"设置设备自动模式成功。"); }); SetManualModeCommand2 = new(() => { if (!GetDeviceIsIdle(EDeviceType.炒锅2)) { return; } ActionManage.GetInstance.Send(new WriteModel(EDeviceType.炒锅2, "LB20", false), "WriteBool"); ShowNotify($"设置设备手动模式成功。"); }); InitalDeviceCommand2 = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅2)) { return; } //复位所有变量。 if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } ResetFryPanVariable(EDeviceType.炒锅2); await SendWriteRequest("LB21", EDeviceType.炒锅2); ShowNotify($"设置设备初始化成功。"); }); StopDeviceCommand2 = new(async () => { //if (!GetDeviceIsIdle(EDeviceType.炒锅2)) //{ // return; //} if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB22", EDeviceType.炒锅2); ShowNotify($"设置设备停止成功。"); }); //炒锅那边复位变量没有使用。 GoCleanLocCommand2 = new(async () => { if (!GetDeviceIsIdle(EDeviceType.炒锅2)) { return; } if (!MessageNotify.GetInstance.ShowDialog("确认是否进行手动操作?")) { return; } await SendWriteRequest("LB23", EDeviceType.炒锅2); ShowNotify($"设置设备去洗锅位成功。"); }); #endregion CancelFryPanTaskCommand = new((string device) => { ActionManage.GetInstance.Send("CancelFryPanTask", device); ShowNotify($"[{device}#]取消当前任务成功"); }); ClearFryPanTaskCommand_1 = new(() => { }); ClearFryPanTaskCommand_2 = new(() => { }); InitRobotVarCommand = new(() => { Task.Run(() => { ActionManage.GetInstance.Send("InitRobotAllVar"); }); }); RobotGoCleanLocCommand = new(() => { if (!MessageNotify.GetInstance.ShowDialog("确认是否将机械手移动到清洗位?")) { return; } ActionManage.GetInstance.Send("RobotGoCleanLoc"); }); CleanFinishCommand = new(() => { if (!MessageNotify.GetInstance.ShowDialog("请确认机械手清洗完成且机器人周围安全,否则可能会造成碰撞事故。")) { return; } ActionManage.GetInstance.Send("RobotCleanFinish"); }); } #region 私有方法 private async Task SendWriteRequest(string address, EDeviceType device) { ActionManage.GetInstance.Send(new WriteModel(device, address, false), "WriteBool"); await Task.Delay(400); ActionManage.GetInstance.Send(new WriteModel(device, address, true), "WriteBool"); await Task.Delay(400); ActionManage.GetInstance.Send(new WriteModel(device, address, false), "WriteBool"); } private void ShowNotify(string info) { NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, Application.Current.MainWindow, "提示", info); } private bool GetDeviceIsIdle(EDeviceType device) { bool frypanisIdle = false; ActionManage.GetInstance.Send(device, "GetDeviceIsIdle").OnSuccess(S => { frypanisIdle = true; }).OnFailure(E => { NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "失败", "当前炒锅正在炒菜,稍后再试。"); }); //if (ActionManage.GetInstance.SendResult("GetDeviceIsIdle", device) is bool frypanisIdle) //{ // if (frypanisIdle) // { // return true; // } // else // { // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "失败", "当前炒锅正在炒菜,稍后再试。"); // } //} return frypanisIdle; } /// /// 复位指定炒锅的所有变量。 /// /// private void ResetFryPanVariable(EDeviceType deviceType) { ActionManage.GetInstance.Send(new WriteModel(deviceType, "LB0", false), "WriteBool"); ActionManage.GetInstance.Send(new WriteModel(deviceType, "LB3", false), "WriteBool"); for (int i = 6; i <= 19; i++) { ActionManage.GetInstance.Send(new WriteModel(deviceType, "LB" + i, false), "WriteBool"); } for (int i = 0; i <= 5; i++) { ActionManage.GetInstance.Send(new WriteModel(deviceType, "LW" + i, 0), "WriteUshort"); } } #endregion 私有方法 private EDeviceType fryingPanSelect = EDeviceType.炒锅1; /// 炒锅选择。 public EDeviceType FryingPanSelect { get { return fryingPanSelect; } set { fryingPanSelect = value; OnPropertyChanged(); } } public ObservableCollection DeviceTypes { get; set; } = new ObservableCollection() { EDeviceType.炒锅1, EDeviceType.炒锅2 }; #region 属性_1#炒锅 /// 搅拌频率 public ushort AgitaFrequency { get => agitaFrequency; set { if (value >= 50) agitaFrequency = 50; else if (value <= 1) agitaFrequency = 1; else agitaFrequency = value; OnPropertyChanged(); } } /// 加热档位 public ushort HeatGear { get => heatGear; set { if (value >= 8) heatGear = 8; else if (value <= 1) heatGear = 1; else heatGear = value; OnPropertyChanged(); } } private float weight; /// 下料重量 public float Weight { get { return weight; } set { weight = value; } } private int channelSelect; /// 下料通道选择。 public int ChannelSelect { get { return channelSelect; } set { channelSelect = value; OnPropertyChanged(); } } private int stirFryLocSelect; /// 炒制位选择。 public int StirFryLocSelect { get { return stirFryLocSelect; } set { stirFryLocSelect = value; OnPropertyChanged(); } } #endregion 属性 #region 属性_2#炒锅 private ushort heatGear2; private ushort agitaFrequency2; /// 搅拌频率 public ushort AgitaFrequency2 { get => agitaFrequency2; set { if (value >= 50) agitaFrequency2 = 50; else if (value <= 1) agitaFrequency2 = 1; else agitaFrequency2 = value; OnPropertyChanged(); } } /// 加热档位 public ushort HeatGear2 { get => heatGear2; set { if (value >= 8) heatGear2 = 8; else if (value <= 1) heatGear2 = 1; else heatGear2 = value; OnPropertyChanged(); } } private float weight2; /// 下料重量 public float Weight2 { get { return weight2; } set { weight2 = value; } } private int channelSelect2; /// 下料通道选择。 public int ChannelSelect2 { get { return channelSelect2; } set { channelSelect2 = value; OnPropertyChanged(); } } private int stirFryLocSelect2; /// 炒制位选择。 public int StirFryLocSelect2 { get { return stirFryLocSelect2; } set { stirFryLocSelect2 = value; OnPropertyChanged(); } } #endregion #region 命令 /// /// 取消指定炒锅当前任务。 /// public BPARelayCommand CancelFryPanTaskCommand { get; set; } /// /// 清空1#炒锅所有任务。 /// public BPARelayCommand ClearFryPanTaskCommand_1 { get; set; } /// /// 清空2#炒锅所有任务。 /// public BPARelayCommand ClearFryPanTaskCommand_2 { get; set; } /// /// 复位机器人所有由上位机下发的变量。 /// public BPARelayCommand InitRobotVarCommand { get; set; } public BPARelayCommand RobotGoCleanLocCommand { get; set; } public BPARelayCommand CleanFinishCommand { get; set; } #endregion #region 命令_1#炒锅 /// 写入搅拌参数。 public BPARelayCommand WriteAgitaParamCommand { get; set; } /// 开始搅拌。 public BPARelayCommand StartAgitateCommand { get; set; } /// 停止搅拌。 public BPARelayCommand StopAgitateCommand { get; set; } /// 写入加热参数。 public BPARelayCommand WriteHeatParamCommand { get; set; } /// 开始加热。 public BPARelayCommand StartHeatCommand { get; set; } /// 停止加热。 public BPARelayCommand StopHeatCommand { get; set; } /// 通道下料启动。 public BPARelayCommand ChannelCuttingCommand { get; set; } /// 去炒制位。 public BPARelayCommand GoFryLocCommand { get; set; } /// 回原点/投料位置。 public BPARelayCommand GoHomeCommand { get; set; } /// 去调料投料位。 public BPARelayCommand GoSeasoningFeddLocCommand { get; set; } /// 出餐启动。 public BPARelayCommand OutDiningStartCommand { get; set; } /// 炒锅清洗。 public BPARelayCommand FryPanCleanCommand { get; set; } /// /// 设为自动模式。 /// public BPARelayCommand SetAutoModeCommand { get; set; } /// /// 设为手动模式。 /// public BPARelayCommand SetManualModeCommand { get; set; } /// /// 初始化设备。 /// public BPARelayCommand InitalDeviceCommand { get; set; } /// /// 设备停止。 /// public BPARelayCommand StopDeviceCommand { get; set; } /// /// 设备去洗锅位置。 /// public BPARelayCommand GoCleanLocCommand { get; set; } #endregion 命令 #region 命令_2#炒锅 /// 写入搅拌参数。 public BPARelayCommand WriteAgitaParamCommand2 { get; set; } /// 开始搅拌。 public BPARelayCommand StartAgitateCommand2 { get; set; } /// 停止搅拌。 public BPARelayCommand StopAgitateCommand2 { get; set; } /// 写入加热参数。 public BPARelayCommand WriteHeatParamCommand2 { get; set; } /// 开始加热。 public BPARelayCommand StartHeatCommand2 { get; set; } /// 停止加热。 public BPARelayCommand StopHeatCommand2 { get; set; } /// 通道下料启动。 public BPARelayCommand ChannelCuttingCommand2 { get; set; } /// 去炒制位。 public BPARelayCommand GoFryLocCommand2 { get; set; } /// 回原点/投料位置。 public BPARelayCommand GoHomeCommand2 { get; set; } /// 去调料投料位。 public BPARelayCommand GoSeasoningFeddLocCommand2 { get; set; } /// 出餐启动。 public BPARelayCommand OutDiningStartCommand2 { get; set; } /// 炒锅清洗。 public BPARelayCommand FryPanCleanCommand2 { get; set; } /// /// 设为自动模式。 /// public BPARelayCommand SetAutoModeCommand2 { get; set; } /// /// 设为手动模式。 /// public BPARelayCommand SetManualModeCommand2 { get; set; } /// /// 初始化设备。 /// public BPARelayCommand InitalDeviceCommand2 { get; set; } /// /// 设备停止。 /// public BPARelayCommand StopDeviceCommand2 { get; set; } /// /// 设备去洗锅位置。 /// public BPARelayCommand GoCleanLocCommand2 { get; set; } #endregion 命令 } }