Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help clarify web / mobile API
#1
Hi, I've been using SIP for years but recently having some problems with reliability. This is likely not SIP's fault, I have semi-regular power outs and network outages. I have some alerts set up to let me know if SIP is unavailable for a long time, but it turns out they're not enough. I'd like to add an alert if for whatever reason the sprinklers haven't been active in several days.

I thought of doing this by checking the sip log / history for latest run date. The documentation around the API and the fields it returns is ... not entirely clear to me.

I found info here: https://nosack.com/sipforum/showthread.php?tid=156
cross-referencing that with https://github.com/Dan-in-CA/SIP/blob/ma...erence.txt
I thought I could use the /jc command's lrun section. 
"last run, used to display log line on home page (list [station index, program number, duration, end time])"

While the first two numbers change, I'm always getting the same value for the last two:
  "lrun": [
    0,
    3,
    1200,
    0
  ]

this is the result shortly after one of the stations was open for 20 minutes in the middle of the afternoon.

The date and time are set correctly on the machine where SIP runs.

I also tried the /jl command. I'm getting an empty array, despite the fact that I enabled logs, see the latest run in the web UI and the /wl command downloads a CSV which includes the latest run.

Am I doing something wrong, or can you help me understand the right way to programmatically pull the latest run?

I'm running version: 4.1.53 (2022-12-04).
Reply
#2
If you are running SIP under Python 3 you might find it easier to install the sip_email plugin.
It can be configured to send you an email when SIP has restarted and the message includes the most recent log entries to help determine if a scheduled irrigation program was interrupted or skipped.

Dan
<p><br></p>
Reply
#3
Thanks, but I didn't find that sufficient for my use case. I'd really prefer there was a way to programmatically get the log from SIP.

I didn't want to mess with your code, don't want to complicate the process of upgrading to a new version of SIP. So I wrote a little app which runs alongside and exposes the log via scraping SIP's webpage. In case this is helpful to anyone else...

Code:
import requests
from bs4 import BeautifulSoup
from datetime import datetime
from flask import Flask
import json

SIP_SERVER = "http://10.0.0.128"

def get_log_from_SIP():
    resp = requests.get(SIP_SERVER + "/vl")
    soup = BeautifulSoup(resp.text, features="html.parser")
    return soup.find(id="log")

def get_days_since_last_SIP_run():
    try:
        log = get_log_from_SIP()
        sip_last_run = log.table.tbody.td.text
    except:
        return -1

    return (datetime.now() - datetime.fromisoformat(sip_last_run)).days


app = Flask(__name__)

@app.route('/log')
def log():
    try:
        log = get_log_from_SIP()
        headers = log.table.thead.tr.find_all("th")
        keys = [h.string.replace(' ', '_') for h in headers]
        rows = log.table.tbody.find_all("tr")
    except:
        return "[]"

    result = []
    for row in rows:
        cols = row.find_all("td")
        result.append({keys[i]:cols[i].string for i in range(len(cols))})
    return json.dumps(result)

@app.route('/lastrun')
def last_run():
    return json.dumps(get_days_since_last_SIP_run())
Reply
#4
Why /vl and not /wl ? Isn't that easier to parse?

--Gerard
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)