The modifications are now up and running, and I took the opportunity 
(uneventfully!) to reinstall weewx to run under Python3, since all my other 
scripts use this Python version. The service code is below. It is modeled 
on 'p q's code from the hackaday article, with a number of modifications. 
The only change required for Python3 was a minor modification to the Try 
Except syntax. Most of the modifications to the Loop packet service were to 
reduce the load on the system for something that (for the Vantage Pro 2 
driver) gets called once every couple of seconds or so. The service only 
actually reads the external data every minute or so, returning saved values 
for the intervening calls.

import syslog
import weewx
import time
import os

from weewx.wxengine import StdService

class ArchTHService(StdService):
    def __init__(self, engine, config_dict):
        super(ArchTHService, self).__init__(engine, config_dict)
        d = config_dict.get('ArchTHService', {})
        self.filename = d.get('filename', '/var/tmp/THNow.txt')
        syslog.syslog(syslog.LOG_INFO, "ArchTH: using %s" % self.filename)
        self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_file)

    def read_file(self, event):
# save the console values in 'extra' slots
        event.record['extraTemp1'] = event.record['inTemp']
        event.record['extraHumid1'] = event.record['inHumidity']
        try:
            #skip it if it's stale. Console values will be used instead
            if time.time() - os.path.getmtime("/var/tmp/THNow.txt") < 600: #10 
minutes
                with open(self.filename) as f:
                    line = f.read() # contains temp & humidity, 
comma-separated
                values=line.split(',')
                syslog.syslog(syslog.LOG_DEBUG, "ArchTH: found value of %s" 
% line)
                event.record['inTemp'] = float(values[0])
                event.record['inHumidity'] = float(values[1])
        except Exception as e:
            syslog.syslog(syslog.LOG_ERR, "ArchTH: cannot interpret value: 
%s" % e)

class LoopTHService(StdService):
    loopcount= 0
    lastT = 0
    lastH =0

    def __init__(self, engine, config_dict):
        super(LoopTHService, self).__init__(engine, config_dict)
        d = config_dict.get('LoopTHService', {})
        self.filename = d.get('filename', '/var/tmp/THNow.txt')
        syslog.syslog(syslog.LOG_INFO, "LoopTH: using %s" % self.filename)
        self.bind(weewx.NEW_LOOP_PACKET, self.read_file)

    def read_file(self, event):
        if self.loopcount == 0:
            try:
                #skip it if it's stale. Vantage Console values will be used 
instead
                if time.time() - os.path.getmtime("/var/tmp/THNow.txt") < 
300: #5 minutes
                    with open(self.filename) as f:
                        line = f.read() # contains temp & humidity, 
comma-separated
                    values=line.split(',')
                    syslog.syslog(syslog.LOG_DEBUG, "LoopTH: found value of 
%s" % line)
                    event.packet['inTemp'] = float(values[0])
                    event.packet['inHumidity'] = float(values[1])
                    self.lastT=float(values[0])
                    self.lastH=float(values[1])
                    self.loopcount += 1
            except Exception as e:
                syslog.syslog(syslog.LOG_ERR, "LoopTH: cannot interpret 
value: %s" % e)  
        else:
            self.loopcount += 1
            if self.loopcount >= 30:
                self.loopcount = 0
            event.packet['inTemp'] = self.lastT
            event.packet['inHumidity'] = self.lastH
 


-- 
You received this message because you are subscribed to the Google Groups 
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/weewx-user/d0387564-49db-4e8f-a5ef-ee2685149ff8o%40googlegroups.com.

Reply via email to