Esempio n. 1
0
 private void BtnTestConnection_Click(object sender, RoutedEventArgs e)
 {
     if (!CheckIP())
     {
         return;
     }
     if (MyUtils.IpReachable(txtIp.Text))
     {
         MessageBox.Show("Sucess!\n\nDevice is reachable.");
     }
     else
     {
         MessageBox.Show("Device could not be pinged!");
     }
 }
        public async void AddDeviceDialogAsync()
        {
            string devicename = "";
            string ip         = "";
            int    port       = 0;
            int    lines      = 32;
            int    smoothing  = 0;

            var x = new MetroDialogSettings();

            x.AffirmativeButtonText = "Next";
            x.NegativeButtonText    = "Cancel";
            devicename = await this.ShowInputAsync("New Device", "How should the device be called?", x);

            if (String.IsNullOrEmpty(devicename))
            {
                return;
            }

            var exist = MyUtils.UdpDevices.Find(o => o.DeviceName == devicename);

            if (exist != null)
            {
                await this.ShowMessageAsync("Error!", "A device with that name already exists!");

                return;
            }

            x.DefaultText = "192.168.0.";
            ip            = await this.ShowInputAsync("New Device", "Whats the IP-Address of your device?", x);

            while (!MyUtils.ValidateIp(ip))
            {
                await this.ShowMessageAsync("Error!", "The IP-Address that was entered is invalid!");

                ip = await this.ShowInputAsync("New Device", "Whats the IP-Address of your device?", x);

                if (String.IsNullOrEmpty(ip))
                {
                    return;
                }
            }
            var controller = await this.ShowProgressAsync("Please wait...", "Trying to reach the device");

            controller.SetIndeterminate();
            Thread.Sleep(500);
            bool success = MyUtils.IpReachable(ip);
            await controller.CloseAsync();

            if (!success)
            {
                x.AffirmativeButtonText = "Yes";
                var cont = await this.ShowMessageAsync("The device could not be reached!", "Continue anyway?", MessageDialogStyle.AffirmativeAndNegative, x);

                if (cont == MessageDialogResult.Negative)
                {
                    return;
                }
            }

            string porttext = "";

            x.DefaultText = "4210";
            porttext      = await this.ShowInputAsync("New Device", "On what UDP-Port should the data be sent?", x);

            if (!int.TryParse(porttext, out port))
            {
                port = -1;
            }
            while (port <= 0)
            {
                porttext = await this.ShowInputAsync("Invalid Number!", "On what UDP-Port should the data be sent?", x);

                if (String.IsNullOrEmpty(porttext))
                {
                    return;
                }
                if (!int.TryParse(porttext, out port))
                {
                    port = -1;
                }
            }
            x.DefaultText = "32";
            string linestext = "";

            if (!success)
            {
                linestext = await this.ShowInputAsync("New Device", "How many lines of data should be sent to the device?", x);

                if (!int.TryParse(linestext, out lines))
                {
                    lines = -1;
                }
                while ((lines <= 0 || lines > 1023))
                {
                    linestext = await this.ShowInputAsync("Invalid Number!", "How many lines of data should be sent to the device?", x);

                    if (String.IsNullOrEmpty(linestext))
                    {
                        return;
                    }
                    if (!int.TryParse(linestext, out lines))
                    {
                        lines = -1;
                    }
                }
            }


            x.DefaultText = "2";
            string smtext = "";

            smtext = await this.ShowInputAsync("New Device", "How much should the spectrum be smoothed?", x);

            if (!int.TryParse(smtext, out smoothing))
            {
                smoothing = -1;
            }
            while ((smoothing <= 0 || smoothing > 100))
            {
                smtext = await this.ShowInputAsync("Invalid Number!", "How much should the spectrum be smoothed?", x);

                if (String.IsNullOrEmpty(smtext))
                {
                    return;
                }
                if (!int.TryParse(smtext, out smoothing))
                {
                    smoothing = -1;
                }
            }

            x.AffirmativeButtonText = "Add Device";
            x.NegativeButtonText    = "Cancel Device Creation";
            var res = await this.ShowMessageAsync("Confirm", "Name: " + devicename + "\nIP-Address: " + ip + "\nLines: " + lines, MessageDialogStyle.AffirmativeAndNegative, x);

            if (res == MessageDialogResult.Negative)
            {
                return;
            }

            try
            {
                UdpDevice newDev = new UdpDevice(devicename, ip, port, lines, smoothing);
                newDev.Smooth = true;
                MyUtils.UdpDevices.Add(newDev);
                Save();
                RefreshDeviceList();
                await this.ShowMessageAsync("Success!", "Device created successfully!");
            }
            catch
            {
                await this.ShowInputAsync("Error", "Something went wrong while creating the new device!");
            }
        }