コード例 #1
0
ファイル: NtpService.cs プロジェクト: porrey/iWindow
		/// <summary>
		/// Get the current date and time from an NTP server(s) and update
		/// the DS1307 RTC (Real Time Clock).
		/// </summary>
		/// <returns></returns>
		private async Task UpdateClock()
		{
			// ***
			// *** Make sure I2C is available. This will
			// *** allow the code to be run on a normal PC
			// *** without error.
			// ***
			if (ApiInformation.IsTypePresent(typeof(I2cDevice).FullName))
			{
				// ***
				// *** Create the clint object and set a timeout value.
				// ***
				NtpClient ntp = new NtpClient();
				ntp.Timeout = TimeSpan.FromSeconds(15);

				// ***
				// *** Make the call to the NTP server(s)
				// ***
				DateTime? dateTimeValue = await ntp.GetAsync(this.ApplicationSettngs.NtpServers);

				// ***
				// *** Set the date and time on the RTC
				// ***
				if (dateTimeValue.HasValue)
				{
					Ds1307 rtc = new Ds1307();
					await rtc.SetAsync(dateTimeValue.Value);
				}
			}
		}
コード例 #2
0
ファイル: MainPage.xaml.cs プロジェクト: h82258652/iot
		private async Task TestClock()
		{
			// ***
			// *** Clock
			// ***
			Ds1307 dtc = new Ds1307();
			await dtc.InitializeAsync();

			// ***
			// *** Get the date and time from the clock
			// ***
			DateTimeOffset dt = await dtc.GetAsync();

			// ***
			// *** Create an NTP client and get the date and time
			// ***
			NtpClient ntp = new NtpClient();
			DateTimeOffset? ndt = await ntp.GetAsync("0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org");

			// ***
			// *** Update the clock if we have a result from the servers
			// ***
			if (ndt.HasValue)
			{
				await dtc.SetAsync(ndt.Value);
			}
		}