diff --git a/lmwsip.py b/lmwsip.py index 73c8262..9fc7bfc 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,71 @@ Returns a single string value or None # return(value) + def _lmwdelta_(self, startTime, endTime): + d = endTime - startTime + h = 24*d.days + d.seconds // 3600 + m = (d.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 endTime - startTime > timedelta(days=1): + raise sipTimeSeriesError(startTime, endTime, + "endtime - starttime > 24 hour") + 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" + + values = self.cmd(process, location, parameter, + self._lmwdelta_(startTime, endTime), + startTime.strftime("%d-%m-%Y"), + startTime.strftime("%H:%M"), + cmd_type) + return(lmwTimeSerie(startTime, delta, values)) + def logout(self): """logout() @@ -280,6 +347,28 @@ Logs of self.send("LO\r") self.closesocket() +class lmwTimeSerie: + def __init__(self, start, delta, values): + self.ts = [] + self.addvalues(start, delta, values) + + def addvalues(self, start, delta, values): + for e in values.split(";"): + a = [ start ] + a.append(e.split("/")) + self.ts.append(a) + start += delta + +class sipTimeSeriesError(Exception): + """Parameter errors for timeSeries""" + def __init__(self, startTime, endTime, message): + self.startTime = startTime + self.endTime = endTime + self.message = message + + def __str__(self): + return("%s\n starttime: %s\n end time: %s" % + (self.message, self.startTime, self.endTime)) class LmwSipConnectError(Exception): """Connection exceptions for LmwSip"""