From 3985fdaa04458bb58bc0283cef110e6ceaffb28b Mon Sep 17 00:00:00 2001 From: Denny Page Date: Fri, 25 Jul 2014 03:22:50 +0000 Subject: [PATCH] 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. --- bin/wee_config_vantage | 2 +- bin/wee_config_ws23xx | 8 +++----- bin/weewx/abstractstation.py | 2 +- bin/weewx/drivers/cc3000.py | 10 +++++----- bin/weewx/drivers/fousb.py | 4 ++-- bin/weewx/drivers/vantage.py | 28 +++++++++++++++------------- bin/weewx/drivers/ws23xx.py | 4 ++-- bin/weewx/drivers/ws28xx.py | 2 +- bin/weewx/wxengine.py | 7 ++++--- docs/changes.txt | 4 ++++ 10 files changed, 38 insertions(+), 33 deletions(-) diff --git a/bin/wee_config_vantage b/bin/wee_config_vantage index ca2ab343..61b27f78 100755 --- a/bin/wee_config_vantage +++ b/bin/wee_config_vantage @@ -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) diff --git a/bin/wee_config_ws23xx b/bin/wee_config_ws23xx index 9fbc65f7..7a3cd748 100755 --- a/bin/wee_config_ws23xx +++ b/bin/wee_config_ws23xx @@ -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 diff --git a/bin/weewx/abstractstation.py b/bin/weewx/abstractstation.py index a54e6555..4bfffbcf 100644 --- a/bin/weewx/abstractstation.py +++ b/bin/weewx/abstractstation.py @@ -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): diff --git a/bin/weewx/drivers/cc3000.py b/bin/weewx/drivers/cc3000.py index 31c130d6..2c620da0 100644 --- a/bin/weewx/drivers/cc3000.py +++ b/bin/weewx/drivers/cc3000.py @@ -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: diff --git a/bin/weewx/drivers/fousb.py b/bin/weewx/drivers/fousb.py index 4b24f04a..9d2e4540 100644 --- a/bin/weewx/drivers/fousb.py +++ b/bin/weewx/drivers/fousb.py @@ -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.""" diff --git a/bin/weewx/drivers/vantage.py b/bin/weewx/drivers/vantage.py index f7f36d0e..90788385 100644 --- a/bin/weewx/drivers/vantage.py +++ b/bin/weewx/drivers/vantage.py @@ -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(" 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: diff --git a/docs/changes.txt b/docs/changes.txt index 0eac312a..841acf0b 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -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