python 3 and 2.7 weewx4 and 3.9

62 views
Skip to first unread message

vigilance wx

unread,
Nov 8, 2020, 7:34:01 AM11/8/20
to weewx-user
HI
I have a service running under weewx 3,9 its working fine connected to a Davis vantage. On a second pi i have installed weewx  4.2 with view to upgrading the 3.9 pi to the latest weewx version
the service i have on the weewx3.9 will not run under python 3

import syslog
import weewx
import os
import csv
from weewx.wxengine import StdService
class PlanetService(StdService):
    def __init__(self, engine, config_dict):
        super(PlanetService, self).__init__(engine, config_dict)
        d = config_dict.get('PlanetService', {})
        self.filename = d.get('filename', '/home/pi/cc/allplanets.csv')
        syslog.syslog(syslog.LOG_INFO, "planet: using %s" % self.filename)
        self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_file)

    def read_file(self, event):
        try:
            with open(self.filename) as f:
              f.seek(-10, os.SEEK_END)
              line = f.readlines()[-1]
              value = line.split(',')

            syslog.syslog(syslog.LOG_DEBUG, "allplanets: found value of %s" % value)
            event.record['plutoAzi'] = float(value[3])
        except Exception as e:
            syslog.syslog(syslog.LOG_ERR, "allplanets: cannot read value: %s" % e)

the above runs under weewx 3.9 but it will not run under weewx 4.2

i have tried to modify if but i am getting the error in the log file
"/weewxd: allplanets: cannot read value: read"

import syslog
import weewx
import os
import csv

from weewx.wxengine import StdService

class PlanetService(StdService):
    def __init__(self, engine, config_dict):
        super(PlanetService, self).__init__(engine, config_dict)
        d = config_dict.get('PlanetService', {})
        self.filename = d.get('filename', '/home/pi/cc/allplanets.csv')
        syslog.syslog(syslog.LOG_INFO, "planet: using %s" % self.filename)
        self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_file)

    def read_file(self, event):
        try:
            with open(self.filename , 'ab') as  f:

              f.seek(-10, 2)
              line = f.readlines()[-1]
              value = line.split(',')

            syslog.syslog(syslog.LOG_DEBUG, "allplanets: found value of %s" % value)
            event.record['extraTemp1'] = float(value[1])
        except Exception as e:
            syslog.syslog(syslog.LOG_ERR, "allplanets: cannot read value: %s" % e)

could any one offer advice as to why it will not run? is it the seek function?

thanks for any advice

Graham Eddy

unread,
Nov 8, 2020, 8:10:20 AM11/8/20
to weewx...@googlegroups.com
i don’t know if readlines reads from current filepos or if it rewinds first, but if the former then your readlines will return no lines and the seek is harmful, or if the latter the seek is redundant → either way get rid of it, then see if you have a read problem

--
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 weewx-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/52382be8-9031-4d4a-85da-9353aecf40f8n%40googlegroups.com.

Tom Keffer

unread,
Nov 8, 2020, 8:49:15 AM11/8/20
to weewx-user
Pretty simple. You're opening the file with mode 'ab', which means for writing, appending to the file, and in binary mode. Then you try to read it. Instead, open it in read mode:

with open(self.filename, 'rb') as f:

Incidentally, this is a good example of a 'try' clause being too broad. You're catching everything, and not giving yourself enough information to diagnose the problem. Instead, try something like

try:
  ...
except FileNotFoundError as e:
  syslog.syslog(syslog.LOG_INFO, "File not found: %s" % e)
except ValueError as e:
  syslog.syslog(syslog.LOG_INFO, "Could not convert string to value: %s" % e)

etc.

Or, even better, use multiple 'try' clauses.

Finally, WeeWX V4 no longer uses syslog. Instead, it uses the module logging. See the wiki article WeeWX V4 and logging.

-tk

--

vigilance wx

unread,
Nov 8, 2020, 9:29:55 AM11/8/20
to weewx-user
thank you
up and running now
Reply all
Reply to author
Forward
0 new messages