예제 #1
0
        private INetwork SetupEthernet()
        {
            try
            {
                _characterDisplay.Clear();
                _characterDisplay.SetCursorPosition(0, 0);
                _characterDisplay.Print("Getting IP address...");

                //NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
                //NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;

                // Try mIP? http://mip.codeplex.com/
                _ethernet.Open();
                _ethernet.EnableDhcp();
                _ethernet.EnableDynamicDns();
                while (_ethernet.IPAddress == "0.0.0.0")
                {
                    Debug.Print("Waiting for DHCP");
                    Thread.Sleep(250);
                }
                return(new NetworkWrapper(_ethernet));
            }
            catch (Exception ex)
            {
                Debug.Print("Could not set up Ethernet - " + ex);
                throw;
            }
        }
예제 #2
0
        internal static void Initialize()
        {
            ethernet_Modul = new EthernetENC28J60(SPI.SPI_module.SPI2, G120.P1_10, G120.P2_11, G120.P1_9);
            ethernet_Modul.Open();
            ethernet_Modul.EnableDhcp();
            ethernet_Modul.EnableDynamicDns();

            NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
            NetworkChange.NetworkAddressChanged      += NetworkChange_NetworkAddressChanged;

            Debug.Print("Ethernet initialized.");
        }
예제 #3
0
        public VolumioRestApiPlayer(Cpu.Pin chipSelect, Cpu.Pin externalInterrupt, Cpu.Pin reset)
        {
            commands = new QueueThreadWorker(ProcessCommand, "httpRequestsThread", ThreadPriority.Lowest);

            var netif = new EthernetENC28J60(SPI.SPI_module.SPI2, chipSelect, externalInterrupt /*, reset*/);

            netif.Open();
            ////netif.EnableDhcp();
            ////netif.EnableDynamicDns();
            netif.EnableStaticIP("169.254.194.93", "255.255.0.0", "0.0.0.0");
            int attemps = 0;

            while (netif.IPAddress != "169.254.194.93" && attemps++ < 4)
            {
                System.Threading.Thread.Sleep(250);
            }
            Inited = true;

            CheckStatusThread.Start();
        }
예제 #4
0
        private static void SetupNetwork()
        {
            const string suntimeUpdateFormat     = "{0} - {1}: {2}";
            const string suntimeUpdateTypeThread = "Thread";
            const string suntimeUpdateTypeInit   = "Init";
            const string suntimeUpdateSunrise    = "Sunrise";
            const string suntimeUpdateSunset     = "Sunset";
            int          currentDay = DateTime.Now.DayOfYear;

            Trace.TraceInformation("Setting up networking...");

            ethernet = new EthernetENC28J60(SPI.SPI_module.SPI2, Pin.P1_10, Pin.P2_11, Pin.P1_9, 4000);

            ethernet.NetworkAddressChanged +=
                (o, e) =>
            {
                if (ethernet.NetworkInterface.IPAddress == "0.0.0.0")
                {
                    return;
                }

                Trace.TraceInformation("IP Address: {0}", ethernet.NetworkInterface.IPAddress);

                time.EnsureTime(true);

                time.GetSunTimes(out sunrise, out sunset);
                Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeInit, suntimeUpdateSunrise, sunrise.ToString());
                Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeInit, suntimeUpdateSunset, sunset.ToString());

                //new System.Threading.Timer(
                //    (state) =>
                //    {
                //        time.GetSunTimes(out sunrise, out sunset);
                //        Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeThread, suntimeUpdateSunrise, sunrise.ToString());
                //        Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeThread, suntimeUpdateSunset, sunset.ToString());
                //    },
                //    null,
                //    DateTime.Today.AddHours(24) - DateTime.Now.AddMinutes(-5),
                //    new TimeSpan(24, 0, 0));

                try
                {
                    Trace.TraceInformation("Sunrise/set updater...");
                    new Thread(
                        () =>
                    {
                        while (true)
                        {
                            // 1 min? 5 mins? 1 hour?
                            Thread.Sleep(60000);

                            if (DateTime.Now.DayOfYear != currentDay)
                            {
                                currentDay = DateTime.Now.DayOfYear;

                                time.GetSunTimes(out sunrise, out sunset);
                                Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeThread, suntimeUpdateSunrise, sunrise.ToString());
                                Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeThread, suntimeUpdateSunset, sunset.ToString());
                            }
                        }
                    }).Start();
                }
                catch (Exception ex)
                {
                    Trace.TraceInformation("Sunrise/set updater exception: {0}", ex.Message);
                }
            };

            ethernet.CableConnectivityChanged +=
                (o, e) =>
            {
                Trace.TraceInformation("Network cable is {0}", e.IsConnected ? "connected" : "disconnected");
            };

            if (!ethernet.IsOpen)
            {
                ethernet.Open();
            }

            if (settings.Network.IPType == Constants.NetworkIPType.DHCP)
            {
                if (!ethernet.NetworkInterface.IsDhcpEnabled)
                {
                    ethernet.NetworkInterface.EnableDhcp();
                }
                else
                {
                    ethernet.NetworkInterface.RenewDhcpLease();
                }
            }
            else
            {
                var ip      = settings.Network.IPAddress;
                var subnet  = settings.Network.Subnet;
                var gateway = settings.Network.Gateway;

                ethernet.NetworkInterface.EnableStaticIP(ip, subnet, gateway);
                ethernet.NetworkInterface.EnableStaticDns(new string[] { gateway });
            }

            NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet);
        }