From d7bd339bc5ab9d1dcb576fcafcf10f9142e04256 Mon Sep 17 00:00:00 2001 From: Marcel Nijenhof Date: Tue, 12 Feb 2019 21:15:38 +0100 Subject: [PATCH] lmwsip.py uit mkkdl-report als zelfstandige library --- lmwsip.py | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 lmwsip.py diff --git a/lmwsip.py b/lmwsip.py new file mode 100644 index 0000000..3f3902e --- /dev/null +++ b/lmwsip.py @@ -0,0 +1,199 @@ +"""Module to support the LmwSip class + +See: LmwSip""" + +import socket +import ssl +import time +import re + +class LmwSip: + """Class to connect to the LMW Standard Interface prototcol (sip) + +This class iplement connection to the Rijkswaterstaat Meetnet +Water (LMW) with the Standard Interface Protocol using the +Rijkswaterstaat Meetnet Infrastructuur (RMI). + +https://waterberichtgeving.rws.nl/water-en-weer/metingen + +Support for: + ti + cmd(wn, vw, as) + +""" + + def __init__(self, user, password, host="sip-lmw.rws.nl", port=443, + meetnet="LMW", ssl = True, check_ssl = True): + """LmwSip(user, password, [host], [port], [meetnet], [ssl], [check_ssl]) + +user: Lmw user name +password: Lmw password +host(optional): Default sip-lmw.rws.nl +port(optional): Default 443 +meetnet(optional): Default LMW +ssl(optional): Default true +check_ssl(optional): true + +Opens the connection and logs in +""" + self.user = user + self.password = password + self.host = host + self.port = port + self.meetnet = meetnet + self.ssl = ssl + self.check_ssl = check_ssl + self.connect() + self.login() + + def lasttime(self, parameter): + # + # Find the last valid 10 minute window. + # The measurement of 12:00 is avaiable at 12:05:30. + # Before 12:05:30 we should use 11:50:00. + # + # At 12:05:29 we substract 15:29 from the time! + # + # Also note that we use GMT. So we should add one hour + # because we use GMT +1 (MET, UTC-1) + # + if (parameter.find("10") != -1): + now=time.time() + dt = now%600 + if (dt < 330): + now = 3000 + now - dt + else: + now = 3600 + now - dt + else: + # + # e.g. H1 use 30 seconds to calculate the time. + # + dt = now%600 + if (dt < 30): + now = 3540 + now - dt + else: + now = 3600 + now - dt + time_of_day=time.strftime("%H:%M", time.gmtime(now)) + return { "day": time.strftime("%d-%m-%Y", time.gmtime(now)), + "time_of_day": time.strftime("%H:%M", time.gmtime(now)) } + + def connect(self): + """connect() + +connects to lmw with tcp using the values of the object creation. +""" + self._context = ssl.create_default_context() + self._context.check_hostname = self.check_ssl + self._tcp = socket.create_connection((self.host, self.port)) + self._ssl = self._context.wrap_socket(self._tcp, + server_hostname=self.host) + + def login(self): + """login() + +Login lmw using the object creation user, password. +Raises a LmwLoginFailure exception on failure +""" + li="LI " + self.user + "," + self.password + "\r" + self._ssl.sendall(li.encode('ascii')) + d = self._ssl.recv() + if (d.decode('ascii')[0] != '!'): + raise LmwLoginFailure(self.user + ":" + d.decode('ascii')) + + def ti(self): + """ti() + +Request the time from lmw and returns the string. + +Raises a LmwCmdWarn of failure +""" + ti="TI " + self.meetnet + "\r" + self._ssl.sendall(ti.encode('ascii')) + d = self._ssl.recv() + if (d.decode('ascii')[0] != '!'): + raise LmwCmdWarn("TI " + self.meetnet +":" + d.decode('ascii')) + return (d.decode('ascii')[2:-1]) + + def cmd(self, process, location, parameter, time_delta, day, + time_of_day, cmd_type="DATA"): + """cmd(process, location, parameter, time_delta, day, time_of_day) + +Send a cmd to LMW and returns the lmw string + + process: + location: + parameter: + time_delta: