For setTime, move retrieval of system time from the engine into the setTime method itself. This greatly improves the accuracy of time synchronization with the station.

This commit is contained in:
Denny Page
2014-07-25 03:22:50 +00:00
parent 870497d690
commit 3985fdaa04
10 changed files with 38 additions and 33 deletions

View File

@@ -330,7 +330,7 @@ def set_rain_year_start(station, rain_year_start):
def set_time(station):
print "Setting time on console..."
station.setTime(time.time())
station.setTime()
newtime_ts = station.getTime()
print "Current console time is %s" % weeutil.weeutil.timestamp_to_string(newtime_ts)

View File

@@ -132,15 +132,13 @@ def setclock(station, prompt):
v = station.getTime()
vstr = weeutil.weeutil.timestamp_to_string(v)
print "Station clock is", vstr
now = int(time.time() + 0.5)
nstr = weeutil.weeutil.timestamp_to_string(now)
if prompt:
ans = raw_input("Set station clock to %s (y/n)? " % nstr)
ans = raw_input("Set station clock (y/n)? ")
else:
print "Setting station clock to %s" % nstr
print "Setting station clock"
ans = 'y'
if ans == 'y' :
station.setTime(now)
station.setTime()
v = station.getTime()
vstr = weeutil.weeutil.timestamp_to_string(v)
print "Station clock is now", vstr

View File

@@ -32,7 +32,7 @@ class AbstractStation(object):
def getTime(self):
raise NotImplementedError("Method 'getTime' not implemented")
def setTime(self, newtime_ts):
def setTime(self):
raise NotImplementedError("Method 'setTime' not implemented")
def closePort(self):

View File

@@ -169,9 +169,9 @@ class CC3000(weewx.abstractstation.AbstractStation):
v = station.get_time()
return _to_ts(v)
def setTime(self, ts):
def setTime(self):
with Station(self.port) as station:
station.set_time(ts)
station.set_time()
def get_current(self):
with Station(self.port) as station:
@@ -508,8 +508,8 @@ class Station(object):
data = self.command("TIME=?")
return data
def set_time(self, ts):
tstr = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(ts))
def set_time(self):
tstr = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(time.time()))
logdbg("set time to %s (%s)" % (tstr, ts))
s = "TIME=%s" % tstr
data = self.command(s)
@@ -617,7 +617,7 @@ if __name__ == '__main__':
if options.gettime:
print s.get_time()
if options.settime:
s.set_time(time.time())
s.set_time()
if options.getint:
print s.get_interval()
if options.setint:

View File

@@ -802,8 +802,8 @@ class FineOffsetUSB(weewx.abstractstation.AbstractStation):
# def getTime(self):
# return self.get_clock()
# def setTime(self, ts):
# self.set_clock(ts)
# def setTime(self):
# self.set_clock()
def genLoopPackets(self):
"""Generator function that continuously returns decoded packets."""

View File

@@ -637,24 +637,26 @@ class Vantage(weewx.abstractstation.AbstractStation):
syslog.syslog(syslog.LOG_ERR, "vantage: Max retries exceeded while getting time")
raise weewx.RetriesExceeded("While getting console time")
def setTime(self, newtime_ts):
"""Set the clock on the Davis Vantage console
def setTime(self):
"""Set the clock on the Davis Vantage console"""
newtime_ts: The time the internal clock should be set to in unix epoch time."""
# Unfortunately, this algorithm takes a little while to execute, so the clock
# usually ends up a few hundred milliseconds slow
newtime_tt = time.localtime(int(newtime_ts + 0.5))
# The Davis expects the time in reversed order, and the year is since 1900
_buffer = struct.pack("<bbbbbb", newtime_tt[5], newtime_tt[4], newtime_tt[3], newtime_tt[2],
newtime_tt[1], newtime_tt[0] - 1900)
for unused_count in xrange(self.max_tries) :
try :
# Wake the console and begin the settime command
self.port.wakeup_console(max_tries=self.max_tries, wait_before_retry=self.wait_before_retry)
self.port.send_data('SETTIME\n')
self.port.send_data_with_crc16(_buffer, max_tries=self.max_tries)
# Unfortunately, clock resolution is only 1 second, and transmission takes a
# little while to complete, so round up the clock up. 0.5 for clock resolution
# and 0.25 for transmission delay
newtime_tt = time.localtime(int(time.time() + 0.75))
# The Davis expects the time in reversed order, and the year is since 1900
_buffer = struct.pack("<bbbbbb", newtime_tt[5], newtime_tt[4], newtime_tt[3], newtime_tt[2],
newtime_tt[1], newtime_tt[0] - 1900)
# Complete the settime command
self.port.send_data_with_crc16(_buffer, max_tries=1)
syslog.syslog(syslog.LOG_NOTICE,
"vantage: Clock set to %s" % weeutil.weeutil.timestamp_to_string(time.mktime(newtime_tt)))
return

View File

@@ -422,9 +422,9 @@ class WS23xx(weewx.abstractstation.AbstractStation):
# with Station(self.port) as s:
# return s.get_time()
# def setTime(self, ts):
# def setTime(self):
# with Station(self.port) as s:
# s.set_time(ts)
# s.set_time()
def getArchiveInterval(self):
with Station(self.port) as s:

View File

@@ -1258,7 +1258,7 @@ class WS28xx(weewx.abstractstation.AbstractStation):
# return getHistoryInterval(cfg['history_interval']) * 60
# FIXME: implement set/get time
# def setTime(self, ts):
# def setTime(self):
# pass
# def getTime(self):
# pass

View File

@@ -679,12 +679,13 @@ class StdTimeSynch(StdService):
try:
console_time = self.engine.console.getTime()
if console_time is None: return
diff = console_time - now_ts
# getTime can take a long time to run, so we use the curent system time
diff = console_time - time.time()
syslog.syslog(syslog.LOG_INFO,
"wxengine: Clock error is %.2f seconds (positive is fast)" % diff)
if abs(now_ts - console_time) > self.max_drift:
if abs(diff) > self.max_drift:
try:
self.engine.console.setTime(now_ts)
self.engine.console.setTime()
except NotImplementedError:
syslog.syslog(syslog.LOG_DEBUG, "wxengine: Station does not support setting the time")
except NotImplementedError:

View File

@@ -14,6 +14,10 @@ Changed lux-to-W/m^2 conversion factor in the fine offset driver.
Added rain rate calculation to Ultimeter driver.
Changed setTime to retrieve system time directly rather than using a value
passed by the engine. This greatly improves the accuracy of StdTimeSync,
particularly in network based implementations.
2.6.4 06/16/14