Calling compiled executable right before rsync runs

48 views
Skip to first unread message

Cat22

unread,
Apr 23, 2023, 2:56:56 PM4/23/23
to weewx-user
I have a C program (Linux) that gets the forecast and inserts it index.php, it needs to run right before the rsync report. The trouble is i cant get it patched into rsyncupload.py properly
Here is what i have, the log statement never makes an entry into /var/log/weewx.log even tho debug=1 in the weew.conf file (this is weewx 4.10.2). It would be even better if i could create a user module that would run before rsync, but i tried and tried and couldn't get it right. I've played with subprocess.call and subprocess.run also. It needs to wait till forecast is done.
if __name__ == '__main__':
    import configobj

    import weewx
    import weeutil.logger

    weewx.debug = 1

    weeutil.logger.setup('rsyncupload', {})

    log.info("rsyncupload: executing forecast")
    os.system('"/usr/local/bin/weewx/skins/ss/forecast"')

matthew wall

unread,
Apr 24, 2023, 7:53:34 AM4/24/23
to noreply-spamdigest via weewx-user


> On 23 Apr 2023, at 14:56, Cat22 <erben...@gmail.com> wrote:
>
> I have a C program (Linux) that gets the forecast and inserts it index.php, it needs to run right before the rsync report. The trouble is i cant get it patched into rsyncupload.py properly

it sounds like you tried to modify the rsyncupload.py that ships with weewx, but the code you posted later looks nothing like rsyncupload.py

if you decide to modify rsyncupload.py, then you should insert your forecast invocation in the 'run' method of the 'RsyncUpload' class.

however, you might want to write a service or generator, that way your changes would not have to be re-applied whenever you update weewx.

in the service approach, your code would be executed based on where you specify it in the 'Services' section of the 'Engine' stanza in your weewx config. that is kind of tricky - it is not clear to me whether you generator your index.php using weewx, or with the c program, or something else. and without that information, i cannot tell you when to invoke the service.

in the generator approach, your code would be executed based on where you specify it in the 'Generators' section of the skin config (or, by inheritance/overrides, the skin config in your weewx config).

the service code would look something like this (in the file user/fc.py):

```
import os
import weewx
from weewx.engine import StdService

class ForecastGrabberService(StdService):
def new_archive_record(self, event):
os.system("/usr/local/bin/weewx/skins/ss/forecast")
```

then add it to the service list in your weewx config.

the generator code would look something like this (in the file user/fc.py):

```
import os

class ForecastGrabberGenerator(object):
def run(self):
os.system("/usr/local/bin/weewx/skins/ss/forecast")
```

then add it to the generator list in your skin config:

```
[Generators]
generator_list = weewx.cheetahgenerator.CheetahGenerator, ..., weewx.reportengine.CopyGenerator, user.fc.ForecastGrabberGenerator
```

that way the pages would be generated and copied to the local report area as usual, then your forecast-thingy would do its thing. then after that the rsync would happen, based on whatever you defined in the StdReport section of your weewx config.

please see the customization guide for examples of how to specify parameters and do proper logging.

finally, you need to test your code to ensure that you handle the callout to the c program properly. do that by defining a 'main' within your code so that you can invoke it independently of the weewx engine - you want to run with weewx plumbing, but not with the weewx engine controlling everything.

these assume that you use 'system' to invoke the c code. subprocess would give you better control and access to stdout/stderr.

you want it to block until the c code finishes, but you might want to build in a timeout in case the c code hangs. and you almost certainly want to capture the stdout/stderr from the c code, and handle any return status properly so that you can log it in weewx. that is an exercise left for a longer email.

m
Reply all
Reply to author
Forward
0 new messages