Exemple #1
0
        /// <summary>
        ///
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// </remarks>
        private async void SetDeviceConfigButton_Click(object sender, RoutedEventArgs e)
        {
            // disable button
            (sender as Button).IsEnabled = false;

            await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
            {
                try
                {
                    // Create cancellation token source
                    CancellationTokenSource cts = new CancellationTokenSource();

                    var device = (DataContext as MainViewModel).AvailableDevices[DeviceGrid.SelectedIndex];

                    // get device info
                    var deviceConfig = device.DebugEngine.GetDeviceConfiguration(cts.Token);

                    // change device configuration using the global configuration class
                    //deviceConfig.NetworkConfiguraton.MacAddress = new byte[] { 0, 0x80, 0xe1, 0x01, 0x35, 0x56 };
                    //deviceConfig.NetworkConfiguraton.StartupAddressMode = DeviceConfiguration.AddressMode.DHCP;

                    // update new network configuration
                    DeviceConfiguration.NetworkConfigurationProperties newDeviceNetworkConfiguration = new DeviceConfiguration.NetworkConfigurationProperties
                    {
                        MacAddress = new byte[] { 0, 0x80, 0xe1, 0x01, 0x35, 0x56 },
                        InterfaceType = nanoFramework.Tools.Debugger.NetworkInterfaceType.Ethernet,
                        StartupAddressMode = AddressMode.DHCP,

                        IPv4DNSAddress1 = IPAddress.Parse("192.168.1.254"),
                    };

                    // write device configuration to device
                    var returnValue = device.DebugEngine.UpdateDeviceConfiguration(newDeviceNetworkConfiguration, 0);

                    // add new wireless 802.11 configuration
                    DeviceConfiguration.Wireless80211ConfigurationProperties newWireless80211Configuration = new DeviceConfiguration.Wireless80211ConfigurationProperties()
                    {
                        Id = 44,
                        Ssid = "Nice_Ssid",
                        Password = "******",
                    };

                    // write wireless configuration to device
                    returnValue = device.DebugEngine.UpdateDeviceConfiguration(newWireless80211Configuration, 0);

                    Debug.WriteLine("");
                    Debug.WriteLine("");
                    Debug.WriteLine($"device config update result: {returnValue}");
                    Debug.WriteLine("");
                    Debug.WriteLine("");
                }
                catch (Exception ex)
                {
                }
            }));

            // enable button
            (sender as Button).IsEnabled = true;
        }
        public static string ToStringForOutput(this DeviceConfiguration.NetworkConfigurationProperties networkConfiguration)
        {
            StringBuilder output = new StringBuilder();

            if (networkConfiguration.StartupAddressMode != AddressMode.Invalid)
            {
                output.AppendLine("IPv4 configuration");
                output.AppendLine("------------------------------------");
                output.AppendLine($"address: {networkConfiguration.IPv4Address.ToString()}");
                output.AppendLine($"subnet mask: {networkConfiguration.IPv4NetMask.ToString()}");
                output.AppendLine($"gateway: {networkConfiguration.IPv4GatewayAddress.ToString()}");
                output.AppendLine($"DNS server 1: {networkConfiguration.IPv4DNSAddress1.ToString()}");
                output.AppendLine($"DNS server 2: {networkConfiguration.IPv4DNSAddress2.ToString()}");

                output.AppendLine("");
                output.AppendLine("IPv6 configuration");
                output.AppendLine("------------------------------------");
                output.AppendLine($"address: {networkConfiguration.IPv6Address.ToString()}");
                output.AppendLine($"subnet mask: {networkConfiguration.IPv6NetMask.ToString()}");
                output.AppendLine($"gateway: {networkConfiguration.IPv6GatewayAddress.ToString()}");
                output.AppendLine($"DNS server 1: {networkConfiguration.IPv6DNSAddress1.ToString()}");
                output.AppendLine($"DNS server 2: {networkConfiguration.IPv6DNSAddress2.ToString()}");

                output.AppendLine("");
                output.Append("IP configuration: ");

                switch (networkConfiguration.StartupAddressMode)
                {
                case AddressMode.Static:
                    output.AppendLine("IP configuration: Static");
                    break;

                case AddressMode.DHCP:
                    output.AppendLine("IP configuration: DHCP");
                    break;

                case AddressMode.AutoIP:
                    output.AppendLine("IP configuration: auto IP");
                    break;
                }

                return(output.ToString());
            }
            else
            {
                return("IP configuration is invalid");
            }
        }