fixed #45
This commit is contained in:
parent
b6e26feb0f
commit
eb2615b623
77
NTP.py
77
NTP.py
|
@ -1,38 +1,39 @@
|
||||||
import utime as time
|
import utime as time
|
||||||
import socket
|
import socket
|
||||||
import ustruct as struct
|
import ustruct as struct
|
||||||
import machine
|
import machine
|
||||||
|
|
||||||
class NTP():
|
class NTP():
|
||||||
def __init__(self, Logger):
|
def __init__(self, Logger):
|
||||||
self.Logger = Logger
|
self.Logger = Logger
|
||||||
|
|
||||||
def SetRTCTimeFromNTP(self, host, gmt_offset, auto_summertime):
|
def SetRTCTimeFromNTP(self, host, gmt_offset, auto_summertime):
|
||||||
self.Logger.LogMessage("getting date and time from NTP " + host)
|
self.Logger.LogMessage("getting date and time from NTP " + host)
|
||||||
NTP_DELTA = 2208988800
|
NTP_DELTA = 2208988800
|
||||||
NTP_QUERY = bytearray(48)
|
NTP_QUERY = bytearray(48)
|
||||||
NTP_QUERY[0] = 0x1B
|
NTP_QUERY[0] = 0x1B
|
||||||
addr = socket.getaddrinfo(host, 123)[0][-1]
|
addr = socket.getaddrinfo(host, 123)[0][-1]
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
try:
|
try:
|
||||||
s.settimeout(5)
|
s.settimeout(5)
|
||||||
res = s.sendto(NTP_QUERY, addr)
|
res = s.sendto(NTP_QUERY, addr)
|
||||||
msg = s.recv(48)
|
msg = s.recv(48)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
self.Logger.LogMessage("Error: " + str(err))
|
self.Logger.LogMessage("Error: " + str(err))
|
||||||
return False
|
return False
|
||||||
finally:
|
finally:
|
||||||
s.close()
|
s.close()
|
||||||
ntp_time = struct.unpack("!I", msg[40:44])[0] + (3600 * gmt_offset) #gmt offset from config
|
ntp_time = struct.unpack("!I", msg[40:44])[0] + (3600 * gmt_offset) #gmt offset from config
|
||||||
tm = time.gmtime(ntp_time - NTP_DELTA)
|
tm = time.gmtime(ntp_time - NTP_DELTA)
|
||||||
if (auto_summertime):
|
if (auto_summertime):
|
||||||
self.Logger.LogMessage("performing auto_summertime adjustment")
|
self.Logger.LogMessage("performing auto_summertime adjustment")
|
||||||
#Time of March change for the current year
|
#Time of March change for the current year
|
||||||
t1 = time.mktime((tm[0],3,(31-(int(5*tm[0]/4+4))%7),1,0,0,0,0))
|
t1 = time.mktime((tm[0],3,(31-(int(5*tm[0]/4+4))%7),1,0,0,0,0))
|
||||||
#Time of October change for the current year
|
#Time of October change for the current year
|
||||||
t2 = time.mktime((tm[0],10,(31-(int(5*tm[0]/4+1))%7),1,0,0,0,0))
|
t2 = time.mktime((tm[0],10,(31-(int(5*tm[0]/4+1))%7),1,0,0,0,0))
|
||||||
t = time.mktime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
|
t = time.mktime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
|
||||||
if t >= t1 and t < t2:
|
if t >= t1 and t < t2:
|
||||||
tm = time.gmtime(tm + 3600)
|
tm = time.gmtime(ntp_time - NTP_DELTA + 3600)
|
||||||
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
|
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue