예제 #1
0
        void ethernet_J11D_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network up!");
            Debug.Print("IP Address: " + ethernet_J11D.NetworkSettings.IPAddress.ToString());

            // Configure TimeService settings.
            TimeServiceSettings settings = new TimeServiceSettings();
            settings.ForceSyncAtWakeUp = true;
            settings.RefreshTime = 1800;    // in seconds.

            IPAddress[] address = Dns.GetHostEntry("ntp.nasa.gov").AddressList;
            if (address != null && address.Length > 0)
                settings.PrimaryServer = address[0].GetAddressBytes();

            address = Dns.GetHostEntry("pool.ntp.org").AddressList;
            if (address != null && address.Length > 0)
                settings.AlternateServer = address[0].GetAddressBytes();

            TimeService.Settings = settings;
            TimeService.Start();

            WebServer.StartLocalServer(ethernet_J11D.NetworkSettings.IPAddress.ToString(), 80);
            GetReading = WebServer.SetupWebEvent("getreading");
            GetReading.WebEventReceived += new WebEvent.ReceivedWebEventHandler(GetReading_WebEventReceived);

            GetLog = WebServer.SetupWebEvent("getlog");
            GetLog.WebEventReceived += new WebEvent.ReceivedWebEventHandler(GetLog_WebEventReceived);

            ClearLog = WebServer.SetupWebEvent("clearlog");
            ClearLog.WebEventReceived += new WebEvent.ReceivedWebEventHandler(ClearLog_WebEventReceived);

            temperatureHumidity.RequestMeasurement();
        }
예제 #2
0
        void syncTimer()
        {
            Microsoft.SPOT.Time.TimeServiceSettings timeSettings = new Microsoft.SPOT.Time.TimeServiceSettings()
            {
                ForceSyncAtWakeUp = true,
                Tolerance         = 1000
            };

            Thread.Sleep(8000);
            //TimeService.Start();
            while (true)
            {
                try
                {
                    if (ethernetJ11D.IsNetworkUp && !waiting)
                    {
                        IPAddress[] address = System.Net.Dns.GetHostEntry("time.google.com").AddressList;
                        timeSettings.PrimaryServer = address[0].GetAddressBytes();
                        TimeService.Settings       = timeSettings;
                        TimeService.SetTimeZoneOffset(120);
                        //TimeService.UpdateNow(1000);
                        Debug.Print("About to start time sync\n");
                        TimeService.UpdateNow(address[0].GetAddressBytes(), 1000);
                        Debug.Print("Time sync finished.\n");
                        timeSyncfinished = true;
                    }
                }
                catch (System.Net.Sockets.SocketException e)
                {
                    Debug.Print("Exception catched.\n");
                }

                //if (TimeService.LastSyncStatus.Flags.Equals(TimeServiceStatus.TimeServiceStatusFlags.SyncSucceeded))
                //{
                //    isInternet = true;
                //}
                //else
                //{
                //    isInternet = false;
                //}
                Debug.Print("Sync thread finished there.\n");
                if (ethernetJ11D.IsNetworkUp && !waiting && isInternet)
                {
                    Thread.Sleep(3600000);//sync every 6 hours if there is internet
                }
                else
                {
                    Thread.Sleep(1000);//try again after 1s if no internet
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Configure the time sync service
        /// </summary>
        /// <see cref="http://tf.nist.gov/tf-cgi/servers.cgi "/>
        public void ConfigureTimeService()
        {
            // Set an abitary time, this needs to be updated.
            TimeService.SetUtcTime(128752416000000000);

            var settings = new TimeServiceSettings();
            settings.PrimaryServer = new byte[] { 10, 0, 0, 49 }; // Domain Controller.
            //settings.PrimaryServer = new byte[] { 64, 90, 182, 55 }; // "nist1-ny.ustiming.org "
            settings.AlternateServer = new byte[] { 64, 113, 32, 5 }; // "nist.netservicesgroup.com "
            settings.AutoDayLightSavings = true;
            settings.RefreshTime = 3600; // every hour.
            settings.Tolerance = 10;

            TimeService.Settings = settings;

            TimeService.SystemTimeChanged += TimeService_SystemTimeChanged;
            TimeService.TimeSyncFailed += TimeService_TimeSyncFailed;
        }
        public MFTestResults TimeServiceConfigTest0()
        {
            MFTestResults result = MFTestResults.Pass;
            try
            {
                byte[] primaryServer = new byte[] { 157, 54, 86, 20 };
                byte[] alternateServer = new byte[] { 10, 192, 53, 107 };
                TimeServiceSettings settings = new TimeServiceSettings();
                settings.PrimaryServer = primaryServer;
                settings.AlternateServer = alternateServer;
                settings.Tolerance = 100;
                settings.RefreshTime = 60;

                /// Save the settings.
                TimeService.Settings = settings;

                /// Validate we have what we saved.
                settings = TimeService.Settings;
                if (settings.Tolerance != 100) throw new ArgumentException("Tolerance value unexpected.");
                if (settings.RefreshTime != 60) throw new ArgumentException("RefreshTime value unexpected.");
                int i = 0;
                for (i = 0; i < 4; i++)
                {
                    if (primaryServer[i] != settings.PrimaryServer[i]) throw new ArgumentException("PrimaryServer value unexpected.");
                }

                for (i = 0; i < 4; i++)
                {
                    if (alternateServer[i] != settings.AlternateServer[i]) throw new ArgumentException("AlternateServer value unexpected.");
                }
            }
            catch (Exception ex)
            {
                Log.Exception("Unexpected exception", ex);
                result = MFTestResults.Fail;
            }

            return result;
        }
        public MFTestResults TimeServiceLastSyncStatusTest0()
        {
            MFTestResults result = MFTestResults.Pass;

            try
            {
                DateTime now = DateTime.Now;

                /// EBS bug returns value in local time instead of UTC.
                /// Remove following line once they fixed the bug.
                now = now.AddHours(12);

                TimeService.SetUtcTime(119600064000000000); /// 1/1/1980.
                /// 
                byte[] primaryServer = new byte[] { 157, 54, 86, 20 };
                byte[] alternateServer = new byte[] { 10, 192, 53, 107 };
                TimeServiceSettings settings = new TimeServiceSettings();
                settings.PrimaryServer = primaryServer;
                settings.AlternateServer = alternateServer;
                settings.Tolerance = 100;
                settings.RefreshTime = 60;

                /// Save the settings.
                TimeService.Settings = settings;
                TimeService.Start(); /// This should fire a sync event right away.

                Thread.Sleep(10 * 1000); /// Sleep for some time, we should have time synced by now.
                                         
                TimeServiceStatus status = TimeService.LastSyncStatus;

                if (status.CurrentTimeUTC.Year != DateTime.UtcNow.Year)
                {
                    throw new ArgumentException("Time not synced correctly.");
                }
            }
            catch (Exception ex)
            {
                Log.Exception("Unexpected exception", ex);
                result = MFTestResults.Fail;
            }

            return result;
        }
        public MFTestResults TimeServiceStartTest0()
        {
            MFTestResults result = MFTestResults.Pass;
            SystemTimeChangedEventHandler handler = new SystemTimeChangedEventHandler(TimeService_SystemTimeChanged);
            try
            {
                timerEvent.Reset();

                TimeService.SystemTimeChanged += handler;

                byte[] primaryServer = new byte[] { 157, 54, 86, 20 };
                byte[] alternateServer = new byte[] { 10, 192, 53, 107 };
                TimeServiceSettings settings = new TimeServiceSettings();
                settings.PrimaryServer = primaryServer;
                settings.AlternateServer = alternateServer;
                settings.Tolerance = 100;
                settings.RefreshTime = 60;

                /// Save the settings.
                TimeService.Settings = settings;
                TimeService.Start(); /// This should fire a sync event right away.

                if (!timerEvent.WaitOne(60 * 1000, false))
                {
                    result = MFTestResults.Fail;
                }                
            }
            catch (Exception ex)
            {
                Log.Exception("Unexpected exception", ex);
                result = MFTestResults.Fail;
            }

            return result;
        }
예제 #7
0
        private void OnButtonUp(object sender, RoutedEventArgs evt)
        {
            ButtonEventArgs e = (ButtonEventArgs)evt;

            const int timeZoneOffsetInMinutes = -8 * 60;

            switch (e.Button)
            {
                case Microsoft.SPOT.Hardware.Button.VK_UP:
                    // Reset the time to an arbitrary value.
                    TimeService.SetUtcTime(128752416000000000);
                    TimeService.SetTimeZoneOffset(timeZoneOffsetInMinutes);
                    break;

                case Microsoft.SPOT.Hardware.Button.VK_SELECT:
                    // Perform a one time sync with the time server.
                    TimeServiceStatus status = TimeService.UpdateNow(TimeServerIPAddress, 10);
                    TimeService.SetTimeZoneOffset(timeZoneOffsetInMinutes);
                    break;

                case Microsoft.SPOT.Hardware.Button.VK_DOWN:
                    // Start a scheduled periodic sync.
                    TimeServiceSettings settings = new TimeServiceSettings();

                    // Sync every 10 seconds.
                    settings.PrimaryServer = TimeServerIPAddress;
                    settings.RefreshTime = 10;

                    TimeService.Settings = settings;

                    TimeService.Start();
                    TimeService.SetTimeZoneOffset(timeZoneOffsetInMinutes);
                    break;
            }
        }
예제 #8
0
파일: NTP.cs 프로젝트: Zaixu/Runup
        /// <summary>
        /// Setup NTP Hosts and settings
        /// </summary>
        private void Setup()
        {
            //Get ip of primary host
                ipHostPrimary = Dns.GetHostEntry(primaryHost);
                primaryAddress = ipHostPrimary.AddressList[0];
                //Get ip of secondary host
                ipHostSecondary = Dns.GetHostEntry(secondaryHost);
                secondaryAddress = ipHostSecondary.AddressList[0];

                //Create TimeService settings with primary host and secondary host, refresh rate.
                TimeServiceSettings settings = new TimeServiceSettings();
                settings.PrimaryServer = primaryAddress.GetAddressBytes();
                settings.AlternateServer = secondaryAddress.GetAddressBytes();
                settings.AutoDayLightSavings = true; // Does nothing
                settings.ForceSyncAtWakeUp = true;
                settings.RefreshTime = 3600;
                //Set settings
                TimeService.Settings = settings;
        }
예제 #9
0
        private void GetInternetTime()
        {
            try
            {
                var NTPTime = new TimeServiceSettings();
                NTPTime.AutoDayLightSavings = true;
                NTPTime.ForceSyncAtWakeUp = true;
                NTPTime.RefreshTime = 3600;
                //Thread.Sleep(1500);
                NTPTime.PrimaryServer = Dns.GetHostEntry("2.ca.pool.ntp.org").AddressList[0].GetAddressBytes();
                NTPTime.AlternateServer = Dns.GetHostEntry("time.nist.gov").AddressList[0].GetAddressBytes();
                //Thread.Sleep(1500);
                TimeService.Settings = NTPTime;
                TimeService.SetTimeZoneOffset(LocalTimeZone); // MST Time zone : GMT-7
                TimeService.SystemTimeChanged += OnSystemTimeChanged;
                TimeService.TimeSyncFailed += OnTimeSyncFailed;
                TimeService.Start();
                //Thread.Sleep(500);
                TimeService.UpdateNow(0);
                //Thread.Sleep(9000);
                //Debug.Print("It is : " + DateTime.Now);

                var time = DateTime.Now;

                Utility.SetLocalTime(time);
                TimeService.Stop();
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }
예제 #10
0
        private void TimeServiceSetup()
        {
            // open time service for SNTP synchronization
            TimeServiceSettings settings = new TimeServiceSettings();
            settings.RefreshTime = 600; // every 600 seconds (10 minutes)
            settings.ForceSyncAtWakeUp = true;

            TimeService.SystemTimeChanged += TimeService_SystemTimeChanged;
            TimeService.TimeSyncFailed += TimeService_TimeSyncFailed;
            TimeService.SetTimeZoneOffset(60);

            IPHostEntry hostEntry = Dns.GetHostEntry("time.nist.gov");
            IPAddress[] address = hostEntry.AddressList;
            if (address != null)
                settings.PrimaryServer = address[0].GetAddressBytes();

            hostEntry = Dns.GetHostEntry("time.windows.com");
            address = hostEntry.AddressList;
            if (address != null)
                settings.AlternateServer = address[0].GetAddressBytes();

            TimeService.Settings = settings;
            TimeService.Start();
        }
예제 #11
0
        public MFTestResults TimeServiceLastSyncStatusTest0()
        {
            MFTestResults result = MFTestResults.Pass;

            try
            {
                DateTime now = DateTime.Now;

                /// EBS bug returns value in local time instead of UTC.
                /// Remove following line once they fixed the bug.
                now = now.AddHours(12);

                TimeService.SetUtcTime(119600064000000000); /// 1/1/1980.
                /// 
                byte[] primaryServer = new byte[] { 192, 43, 244, 18 };
                byte[] alternateServer = new byte[] { 129, 6, 15, 28 };
                TimeServiceSettings settings = new TimeServiceSettings();
                settings.PrimaryServer = primaryServer;
                settings.AlternateServer = alternateServer;
                settings.Tolerance = 100;
                settings.RefreshTime = 60;
                TimeServiceStatus status = null;

                timerEvent.Reset();

                TimeService.SystemTimeChanged += new SystemTimeChangedEventHandler( TimeService_SystemTimeChanged );

                /// Save the settings.
                TimeService.Settings = settings;
                TimeService.Start(); /// This should fire a sync event right away.

                timerEvent.WaitOne( 5000, false );
                /// 
                for (int i = 0; i < 10; i++)
                {
                    status = TimeService.LastSyncStatus;

                    if (status.Flags == TimeServiceStatus.TimeServiceStatusFlags.SyncSucceeded)
                    {
                        break;
                    }

                    timerEvent.WaitOne( 1000, false );
                }

                if (status.CurrentTimeUTC.Year != DateTime.UtcNow.Year)
                {
                    throw new ArgumentException("Time not synced correctly.");
                }
            }
            catch (Exception ex)
            {
                Log.Exception("Unexpected exception", ex);
                result = MFTestResults.Fail;
            }
            finally
            {
                TimeService.SystemTimeChanged -= new SystemTimeChangedEventHandler( TimeService_SystemTimeChanged );

                TimeService.Stop();
            }

            return result;
        }
예제 #12
0
        public MFTestResults TimeServiceStartTest1()
        {
            MFTestResults result = MFTestResults.Pass;
            
            try
            {
                DateTime now = DateTime.Now;

                /// EBS bug returns value in local time instead of UTC.
                /// Remove following line once they fixed the bug.
                now = now.AddHours(12);

                TimeService.SetUtcTime(119600064000000000); /// 1/1/1980.
                /// 
                byte[] primaryServer = new byte[] { 192, 43, 244, 18 };
                byte[] alternateServer = new byte[] { 129, 6, 15, 28 };
                TimeServiceSettings settings = new TimeServiceSettings();
                settings.PrimaryServer = primaryServer;
                settings.AlternateServer = alternateServer;
                settings.Tolerance = 100;
                settings.RefreshTime = 60;                

                /// Save the settings.
                TimeService.Settings = settings;
                TimeService.Start(); /// This should fire a sync event right away.

                Thread.Sleep(5 * 1000); /// Sleep for some time, we should have time synced by now.
                                         
                if (now.Year == 1980)
                {
                    throw new ArgumentException("Time not synced correctly.");
                }
            }
            catch (Exception ex)
            {
                Log.Exception("Unexpected exception", ex);
                result = MFTestResults.Fail;
            }
            finally
            {
                TimeService.Stop();
            }

            return result;
        }
예제 #13
0
        /// <summary>
        /// Manual update of system time value from a given server. This can be called orthogonally along with
        /// scheduled time service.
        /// </summary>
        /// <param name="serverAddress"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        public static TimeServiceStatus UpdateNow(byte[] serverAddress, uint tolerance)
        {
            uint serverIP = TimeServiceSettings.FromIPFragments(serverAddress);

            return(Update(serverIP, tolerance));
        }
예제 #14
0
        private void OnButtonUp(object sender, RoutedEventArgs evt)
        {
            ButtonEventArgs e = (ButtonEventArgs)evt;
            
            if (e.Button == Microsoft.SPOT.Hardware.Button.VK_UP)
            {
                // reset the time to an arbitrary value
                TimeService.SetUtcTime(128752416000000000);
                TimeService.SetTimeZoneOffset(-420);
            }
            else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_SELECT)
            {
                // Perform a one time sync with the time server
                TimeServiceStatus status = TimeService.UpdateNow( TimeServerIPAddress, 10);

                TimeService.SetTimeZoneOffset(-420); // time origin
            }
            else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_DOWN)
            {
                // start a scheduled periodic sync
                TimeServiceSettings settings = new TimeServiceSettings();

                settings.PrimaryServer = TimeServerIPAddress;
                settings.RefreshTime = 10; // sycn every 10 seconds

                TimeService.Settings = settings;

                TimeService.Start();
                TimeService.SetTimeZoneOffset(-420);  // time origin 
            }
        }
예제 #15
0
        public MFTestResults TimeServiceStartTest0()
        {
            MFTestResults result = MFTestResults.Pass;
            SystemTimeChangedEventHandler handler = new SystemTimeChangedEventHandler(TimeService_SystemTimeChanged);
            try
            {
                timerEvent.Reset();

                TimeService.SystemTimeChanged += handler;

                TimeServiceSettings settings = new TimeServiceSettings();
                settings.PrimaryServer = GetTimeServiceAddress().GetAddressBytes();
                settings.Tolerance = 100;
                settings.RefreshTime = 60;

                /// Save the settings.
                TimeService.Settings = settings;
                TimeService.Start(); /// This should fire a sync event right away.

                if (!timerEvent.WaitOne(60 * 1000, false))
                {
                    result = MFTestResults.Fail;
                }
            }
            catch (Exception ex)
            {
                Log.Exception("Unexpected exception", ex);
                result = MFTestResults.Fail;
            }
            finally
            {
                TimeService.SystemTimeChanged -= handler;
                TimeService.Stop();
            }

            return result;
        }