The inline diff works for me.  I'm not a diehard python coder.  I'll like 
to have this touched up a bit.  While this works, it might not be ideal.  I 
don't like the hard coded library path needed for BSD. 

* What I'd like is to have the OS installed as a weewx.conf option.  Then 
the station.py code could get the OS version and use the correct library 
path and not have keep testing which OS is being used.
     
* Make setup.py find the correct OS and insert the correct code again, 
keeping station.py from checking on every execution.
     
* Failing 1 or 2, perhaps the order should be Linux, Freebsd, MacOS?   This 
keeps is close to the current configuration.
     
* I think it might be simple to have Linux pull data from /proc as opposed 
to using the same code as FreeBSD and executing based on the OS detected.  
But I think option 1 and 2 would still be nice.  Just install the correct 
code in a stub in station.py.
     
* I only tested this on FreeBSD.  I'm not sure all the 
necessary/recommended error recovery is in place.  I think the existing 
code will return NA if one of the three matches isn't found.

# diff --context station.py.orig station.py.new
*** station.py.orig    Tue Dec  6 10:23:34 2016
--- station.py.new    Sun Dec 11 12:20:08 2016
***************
*** 8,13 ****
--- 8,14 ----
  
  import weeutil.weeutil
  import weewx.units
+ import os, sys, datetime
  
  # For MacOS:
  try:
***************
*** 100,121 ****
      @property
      def os_uptime(self):
          """Lazy evaluation of the server uptime."""
-         # Get the OS uptime. Because this is highly operating system 
dependent, several
-         # different strategies may have to be tried:
          os_uptime_secs = None
!         try:
              # For Linux:
!             os_uptime_secs = float(open("/proc/uptime").read().split()[0])
!         except (IOError, KeyError):
!             try:
                  # For MacOs:
!                 os_uptime_secs = CACurrentMediaTime()
!             except NameError:
!                 pass
! 
!         return weewx.units.ValueHelper(value_t=(os_uptime_secs, "second", 
"group_deltatime"),
!                                        formatter=self.formatter,
!                                        converter=self.converter)
  
      def __getattr__(self, name):
          # This is to get around bugs in the Python version of Cheetah's 
namemapper:
--- 101,146 ----
      @property
      def os_uptime(self):
          """Lazy evaluation of the server uptime."""
          os_uptime_secs = None
!     try:
!             import ctypes
!             class timespec(ctypes.Structure):
!                  _fields_ = [
!                 ('tv_sec', ctypes.c_long), ('tv_nsec', ctypes.c_long)
!                  ]
! 
!             if sys.platform.startswith('freebsd'):
!                 CLOCK_MONOTONIC = 4# see < time.h >
!                 dylib = 'libc.so.7'
!             #else :
!             #    sys.platform.startswith('linux')
!             #    CLOCK_MONOTONIC = 1# see < linux / time.h >
!             #    dylib = 'librt.so.1'
! 
!             syslib = ctypes.CDLL(dylib, use_errno = True)
!             clock_gettime = syslib.clock_gettime
!             clock_gettime.argtypes = [ctypes.c_int, 
ctypes.POINTER(timespec)]
!             t = timespec()
! 
!             if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0:
!                 errno_ = ctypes.get_errno()
!              raise OSError(errno_, os.strerror(errno_))
!             os_uptime_secs = t.tv_sec + t.tv_nsec * 1e-9
!     
!     except ImportError:
!             try:
              # For Linux:
!                 os_uptime_secs = 
float(open("/proc/uptime").read().split()[0])
!             except (IOError, KeyError):
!                 try:
                  # For MacOs:
!                  os_uptime_secs = CACurrentMediaTime()
!                 except NameError:
!                  pass
! 
!         return weewx.units.ValueHelper(value_t=(os_uptime_secs, "second", 
"group_deltatime"), 
!                                         formatter=self.formatter,
!                                         converter=self.converter)
  
      def __getattr__(self, name):
          # This is to get around bugs in the Python version of Cheetah's 
namemapper:

Reply via email to