diff --git a/README.md b/README.md index dc4ce48..792a8d0 100644 --- a/README.md +++ b/README.md @@ -34,16 +34,41 @@ Otherwise the connection fails. ### Library +#### Use send (low level) + ``` python from lmwsip import LmwSip -lmwsip = LmwSip(ssl=True, host="sip-lmw.ad.rws.nl", port=443) -lmwsip.send("LI user,pass\r") -print("< [%s]" % (lmwsip.recv().strip('\r'))) -lmwsip.send("TI LMW\r") -print("< [%s]" % (lmwsip.recv().strip('\r'))) -lmwsip.send("LO\r") -print("< [%s]" % (lmwsip.recv().strip('\r'))) +sip = LmwSip(ssl=True, host="sip-lmw.rws.nl", port=443) +sip.send("LI user,pass\r") +print("< [%s]" % (sip.recv().strip('\r'))) +sip.send("TI LMW\r") +print("< [%s]" % (sip.recv().strip('\r'))) +sip.send("LO\r") +print("< [%s]" % (sip.recv().strip('\r'))) +``` + +#### Use value + +``` python +from lmwsip import LmwSip +sip = LmwSip("USER", "PASS") +print(sip.ti()) +print(sip.value("WN", "HOEK", "H10")) +sip.logout() +``` + +#### Use timeseries +``` python +from lmwsip import LmwSip +from datetime import datetime, timedelta +from pprint import pprint + +end = datetime.now() +start = end - timedelta(hours=1) + +sip = LmwSip("USER", "PASS") +pprint(sip.timeSerie("WN", "HOEK", "H10", start, end).ts) ``` ### siprun diff --git a/dumpserie b/dumpserie new file mode 100755 index 0000000..74ab4c0 --- /dev/null +++ b/dumpserie @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +from lmwsip import LmwSip +from datetime import datetime, timedelta +from pprint import pprint + +def printLocPar(sip, proces, loc, par, start, end): + try: + r = sip.timeSerie(proces, loc, par, start, end) + except Exception as e: + print(e) + print("\n%s %s %s:\n" % (proces, loc, par)) + pprint(r.ts) + +def main(): + end = datetime.now() + timedelta(minutes=10) + start = end - timedelta(hours=1) + + try: + sip = LmwSip("", "") + except Exception as e: + print(e) + + printLocPar(sip, "WN", "HOEK", "H10", start, end) + printLocPar(sip, "WN", "LEG1", "Czz10", start, end) + printLocPar(sip, "VW", "HOEK", "H10V", start, end) + printLocPar(sip, "AS", "HOEK", "H10A", start, end) + +if __name__ == "__main__": + main() diff --git a/lmwsip.py b/lmwsip.py index 73c8262..c44802e 100644 --- a/lmwsip.py +++ b/lmwsip.py @@ -8,6 +8,8 @@ import select import time import re import logging +from datetime import datetime, timedelta +from dateutil import tz class LmwSip: """Class to connect to the LMW Standard Interface prototcol (sip) @@ -272,6 +274,77 @@ Returns a single string value or None # return(value) + def _lmwdelta_(self, window): + h = 24*window.days + window.seconds // 3600 + m = (window.seconds % 3600)//60 + return("+%02i:%02i" % (h, m)) + + def timeSerie(self, process, location, parameter, + startTime, endTime, cmd_type="DATB"): + """timeSerie(process, location, parameter, startTime, endTime, cmd_type="DATA") + +Parameters: + process: + location: + parameter: + startTime: Start time (datetime) + endTime: End time (datetime) + cmd_type: [DATA|DATB] + +startTime is rounded up to the next measurement time. +So 12:00:00.000001 --> 12:00:10.00.0 + +The times should have correct timezone information. Otherwise local timezone +is assumed. Timezones are converted to 'GMT+1' for the sip commands. + +Example: + lmw.data_string("WN", "HOEK", "H10", ...) + +Returns a LmwtimeSerie object + +Errors: + startTime > endTime + endTime - startTime > 24 hour + now - startTime < 30 days +""" + startTime = startTime.astimezone(tz.gettz('GMT+1')) + endTime = endTime.astimezone(tz.gettz('GMT+1')) + + startTime += timedelta(microseconds=1000000-startTime.microsecond) + startTime += timedelta(seconds=60-startTime.second) + if (parameter.find("10") != -1): + if startTime.minute % 10 != 0: + delta = timedelta(minutes=10) + startTime += timedelta(minutes=(10-startTime.minute%10)) + else: + delta = timedelta(minutes=1) + + if startTime > endTime: + raise sipTimeSeriesError(startTime, endTime, + "starttime > endtime") + if datetime.now(tz=tz.gettz('GMT+1')) - startTime > timedelta(days=30): + raise sipTimeSeriesError(startTime, endTime, + "now - starttime > 30 days") + if process == "VW": + cmd_type="DATA" + + res = lmwTimeSerie(startTime, delta, "") + + while startTime < endTime: + if endTime - startTime > timedelta(days=1): + window = timedelta(days=1) - delta + else: + window = endTime-startTime + values = self.cmd(process, location, parameter, + self._lmwdelta_(window), + startTime.strftime("%d-%m-%Y"), + startTime.strftime("%H:%M"), + cmd_type) + res.addvalues(startTime, values) + startTime += window + delta + + return(res) + def logout(self): """logout() @@ -280,6 +353,63 @@ Logs of self.send("LO\r") self.closesocket() +class lmwTimeSerie: + """Class for lmw results. + +The result are in lmwTimeSerie.ts as array + + [ , [, [] [-t