The line

self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_xml)


means when a weeWX NEW_ARCHIVE_RECORD event comes in, call my member
function read_xml. It binds the event to the function. The expression
self.read_xml refers to the member function. Note that it does not actually
invoke the function, which would require a set of parenthesis afterwards,
which, yes, would have to have the right number of parameters. But, that's
not what's happening here.

See the section *Creating a new service
<http://weewx.com/docs/customizing.htm#Adding_a_service>* in the
Customizing Guide.

-tk

On Mon, Jan 15, 2018 at 7:10 AM, Tryphon Cosinus <[email protected]>
wrote:

> Hello,
>
> I try to adapt the code of the Pond service found at
> https://github.com/weewx/weewx/wiki/add-sensor to access xml data from a
> known url and record these data in my sqlite weewx database. Sure I am not
> a Python guy and I did my best to write the following code :
>
> import syslog
> import weewx
> from weewx.wxengine import StdService
> import urllib2
> import xml.etree.ElementTree as ET
>
> class ElectronService(StdService):
>         def __init__(self, engine, config_dict):
>                 try:
>                         super(ElectronService, self).__init__(engine,
> config_dict)
>                         self.dashdata_url = 'http://192.168.1.111/api/
> DashData.xml?T=0&D=0&M=1'
>                         xmlurl = urllib2.urlopen(self.dashdata_url)
>                         xmldata = xmlurl.read()
>                         tree = ET.parse(xmldata)
>                         root = tree.getroot()
>                         syslog.syslog(syslog.LOG_INFO, "TED Pro: probing
> %s" % self.dashdata_url)
>                         self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_xml)
>                 except Exception as e:
>                        syslog.syslog(syslog.LOG_ERR, "TED Pro: cannot
> reach TED url: %s" % e)
>
>         def read_xml(self, event):
>                 try:
>                        event.record['networkVoltage'] =
> float(root.find('Voltage').text)
>                        event.record['powerConsumption'] =
> float(root.find('TDY').text)
>                 except Exception as e:
>                        syslog.syslog(syslog.LOG_ERR, "TED Pro: cannot read
> value: %s" % e)
>
>
> Could you give some advises on this code ?
>
> For example I do not understand the line : self.bind(weewx.NEW_ARCHIVE_RECORD,
> self.read_xml)
>
> read_xml is defined with two parameters but in this line this function is
> not even called with parameters ...
>
> Thank you.
>
> --
> 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].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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].
For more options, visit https://groups.google.com/d/optout.

Reply via email to