using RobotClientWpf.Utilities; using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace RobotClientWpf.Views { /// /// Interaction logic for ConfigView.xaml /// public partial class ConfigView : UserControl { private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger(); private ChallengeFactory? challenge; private MainWindow? mainWindow; public ConfigView() { InitializeComponent(); this.EnableRobotConfigurationOptions(false); } public void InitializeChildView(ChallengeFactory challenge, MainWindow parent) { this.challenge = challenge; this.mainWindow = parent; this.challenge.RobotMobile.LineSensor.NewCalibrationDataArrived += LineSensor_NewCalibrationDataArrived; } private void btnApplyConfiguration_Click(object sender, RoutedEventArgs e) { List splitFlapTextboxes = new List() { tbSf1, tbSf2, tbSf3, tbSf4 }; int setupIdCounter = 0; // counting setup ID from 0 foreach (var textbox in splitFlapTextboxes) { this.GetHwIdFromTbAndSendMqtt(textbox, setupIdCounter++); UIAccessHelpers.SetTextboxText(textbox, ""); } } private void GetHwIdFromTbAndSendMqtt(TextBox tb, int id) { if(challenge != null) { string text = UIAccessHelpers.GetTextboxText(tb); // if nothing is in the textbox if (String.IsNullOrEmpty(text)) { return; } int hwId; if(int.TryParse(text, out hwId) == false) { log.Warn($"Failed to parse HardwareID={text} given for Splitflap id={id}"); return; } this.challenge.RobotStationary.SplitFlap.ConfigureSplitflap(id, hwId); } else { log.Error("challenge object is null"); } } private void btnStartCalibration_Click(object sender, RoutedEventArgs e) { this.challenge?.RobotMobile.LineSensor.StartCalibration(true); } private void btnEndCalibration_Click(object sender, RoutedEventArgs e) { this.challenge?.RobotMobile.LineSensor.StartCalibration(false); this.challenge?.RobotMobile.LineSensor.GetCalibrationData(); } private void btnGetCalibrationData_Click(object sender, RoutedEventArgs e) { this.challenge?.RobotMobile.LineSensor.GetCalibrationData(); } private void LineSensor_NewCalibrationDataArrived(object? sender, RobotLib.Movement.LineSensorCalibrationDataEventArgs e) { UIAccessHelpers.SetTextboxText(this.tbCalibrationData, e.CalibrationValues); UIAccessHelpers.SetTextboxText(this.tbCalibrationState, e.State); } private void btnInitSplitflaps_Click(object sender, RoutedEventArgs e) { this.challenge?.RobotStationary.SplitFlap.InitializeAllSplitflaps(); } private void tbRobotConfiguratorHostname_TextChanged(object sender, TextChangedEventArgs e) { this.EnableRobotConfigurationOptions(String.IsNullOrEmpty(UIAccessHelpers.GetTextboxText(this.tbRobotConfiguratorHostname)) == false); } private void EnableRobotConfigurationOptions(bool enabled) { if (btnSetBrokerIp != null) UIAccessHelpers.SetButtonState(btnSetBrokerIp, enabled); if (btnReboot != null) UIAccessHelpers.SetButtonState(btnReboot, enabled); if (btnSetModeStationary != null) UIAccessHelpers.SetButtonState(btnSetModeStationary, enabled); if (btnSetModeMobile != null) UIAccessHelpers.SetButtonState(btnSetModeMobile, enabled); } private void btnClearRobotConfHostname_Click(object sender, RoutedEventArgs e) { UIAccessHelpers.SetTextboxText(tbRobotConfiguratorHostname, ""); } private async void btnSetBrokerIp_Click(object sender, RoutedEventArgs e) { if (this.mainWindow == null) return; UIAccessHelpers.SetButtonState(btnSetBrokerIp, false); var brokerIp = UIAccessHelpers.GetTextboxText(this.mainWindow.tbIp); var robotHostname = UIAccessHelpers.GetTextboxText(this.tbRobotConfiguratorHostname); await Task.Run(() => RobotLib.IndependentRobotConfigurator.Adopt(robotHostname, brokerIp)); UIAccessHelpers.SetButtonState(btnSetBrokerIp, true); } private async void btnReboot_Click(object sender, RoutedEventArgs e) { UIAccessHelpers.SetButtonState(btnReboot, false); var robotHostname = UIAccessHelpers.GetTextboxText(this.tbRobotConfiguratorHostname); await Task.Run(() => RobotLib.IndependentRobotConfigurator.RebootEsp32(robotHostname)); UIAccessHelpers.SetButtonState(btnReboot, true); } private async void btnSetModeStationary_Click(object sender, RoutedEventArgs e) { UIAccessHelpers.SetButtonState(btnSetModeStationary, false); var robotHostname = UIAccessHelpers.GetTextboxText(this.tbRobotConfiguratorHostname); await Task.Run(() => RobotLib.IndependentRobotConfigurator.SetRobotMode(robotHostname, "s")); UIAccessHelpers.SetButtonState(btnSetModeStationary, true); } private async void btnSetModeMobile_Click(object sender, RoutedEventArgs e) { UIAccessHelpers.SetButtonState(btnSetModeMobile, false); var robotHostname = UIAccessHelpers.GetTextboxText(this.tbRobotConfiguratorHostname); await Task.Run(() => RobotLib.IndependentRobotConfigurator.SetRobotMode(robotHostname, "m")); UIAccessHelpers.SetButtonState(btnSetModeMobile, true); } } }