Merge branch 'master' of https://marceln.org/git/Werk/lmwsip
This commit is contained in:
39
README.md
39
README.md
@@ -34,16 +34,41 @@ Otherwise the connection fails.
|
|||||||
|
|
||||||
### Library
|
### Library
|
||||||
|
|
||||||
|
#### Use send (low level)
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
from lmwsip import LmwSip
|
from lmwsip import LmwSip
|
||||||
|
|
||||||
lmwsip = LmwSip(ssl=True, host="sip-lmw.ad.rws.nl", port=443)
|
sip = LmwSip(ssl=True, host="sip-lmw.rws.nl", port=443)
|
||||||
lmwsip.send("LI user,pass\r")
|
sip.send("LI user,pass\r")
|
||||||
print("< [%s]" % (lmwsip.recv().strip('\r')))
|
print("< [%s]" % (sip.recv().strip('\r')))
|
||||||
lmwsip.send("TI LMW\r")
|
sip.send("TI LMW\r")
|
||||||
print("< [%s]" % (lmwsip.recv().strip('\r')))
|
print("< [%s]" % (sip.recv().strip('\r')))
|
||||||
lmwsip.send("LO\r")
|
sip.send("LO\r")
|
||||||
print("< [%s]" % (lmwsip.recv().strip('\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
|
### siprun
|
||||||
|
30
dumpserie
Executable file
30
dumpserie
Executable file
@@ -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("<user>", "<pass>")
|
||||||
|
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()
|
136
lmwsip.py
136
lmwsip.py
@@ -8,6 +8,8 @@ import select
|
|||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from dateutil import tz
|
||||||
|
|
||||||
class LmwSip:
|
class LmwSip:
|
||||||
"""Class to connect to the LMW Standard Interface prototcol (sip)
|
"""Class to connect to the LMW Standard Interface prototcol (sip)
|
||||||
@@ -272,6 +274,77 @@ Returns a single string value or None
|
|||||||
#
|
#
|
||||||
return(value)
|
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: <WN|VW|AS>
|
||||||
|
location: <lmw location (e.g. HOEK)>
|
||||||
|
parameter: <lmw parameter (e.g. H10)>
|
||||||
|
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):
|
def logout(self):
|
||||||
"""logout()
|
"""logout()
|
||||||
|
|
||||||
@@ -280,6 +353,63 @@ Logs of
|
|||||||
self.send("LO\r")
|
self.send("LO\r")
|
||||||
self.closesocket()
|
self.closesocket()
|
||||||
|
|
||||||
|
class lmwTimeSerie:
|
||||||
|
"""Class for lmw results.
|
||||||
|
|
||||||
|
The result are in lmwTimeSerie.ts as array
|
||||||
|
|
||||||
|
[ <time1>, [<value1 a, value1 b, ...], kwaliteit1, additionele kwaliteit1],
|
||||||
|
[ <time2>, [<value2 a, value2 b, ...], kwaliteit2, additionele kwaliteit2],
|
||||||
|
...
|
||||||
|
|
||||||
|
Note:
|
||||||
|
* For most measurements there is only one value (e.g H10).
|
||||||
|
* Additionale kwaliteit is optional and may contain None.
|
||||||
|
* Result times in UTC
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, start, delta, values=""):
|
||||||
|
"""lmwTimeSerie(start, delta, values)
|
||||||
|
|
||||||
|
Create a lmwTimeSerie object with:
|
||||||
|
start: Start time
|
||||||
|
delta: Period of the measurements
|
||||||
|
values: lmw result string
|
||||||
|
"""
|
||||||
|
self.ts = []
|
||||||
|
self.delta = delta
|
||||||
|
if values != "":
|
||||||
|
self.addvalues(start, values)
|
||||||
|
|
||||||
|
def addvalues(self, start, values):
|
||||||
|
"""addvalues(start, delta, values)
|
||||||
|
|
||||||
|
Add values to a timeserie
|
||||||
|
start: Start time
|
||||||
|
delta: Period of the measurements
|
||||||
|
values: lmw result string
|
||||||
|
|
||||||
|
"""
|
||||||
|
start = start.astimezone(tz.gettz('UTC'))
|
||||||
|
for e in values.split(";"):
|
||||||
|
v = e.split("/")
|
||||||
|
v[0] = v[0].split(",")
|
||||||
|
if len(v) == 2:
|
||||||
|
v.append(None)
|
||||||
|
self.ts.append([start, v[0], v[1], v[2]])
|
||||||
|
start += self.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):
|
class LmwSipConnectError(Exception):
|
||||||
"""Connection exceptions for LmwSip"""
|
"""Connection exceptions for LmwSip"""
|
||||||
@@ -298,13 +428,13 @@ class LmwLoginFailure(Exception):
|
|||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return("Login with user %s failed: %s", self.user, self.message)
|
return("Login with user %s failed: %s" % (self.user, self.message))
|
||||||
|
|
||||||
class LmwCmdWarn(Warning):
|
class LmwCmdWarn(Warning):
|
||||||
"""Exception fro LmwSip on a cmd"""
|
"""Exception fro LmwSip on a cmd"""
|
||||||
def __init__(self, cmd, message):
|
def __init__(self, cmd, message):
|
||||||
self.cmd = cmd
|
self.cmd = cmd.replace('\r', '')
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return("Cmd %s failed: %s", self.cmd, self.message)
|
return("Cmd %s failed: %s" %(self.cmd, self.message))
|
||||||
|
78
siprun
78
siprun
@@ -2,66 +2,39 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
import getopt
|
import getopt
|
||||||
|
import argparse
|
||||||
from lmwsip import LmwSip
|
from lmwsip import LmwSip
|
||||||
|
|
||||||
def usage():
|
|
||||||
print("siprun [-H] [-s] [-d <date>] [-t <time>] [-h <host>] [-p <port>] [<files>]")
|
|
||||||
print("\t-H: Show usage")
|
|
||||||
print("\t-s: SSL connection")
|
|
||||||
print("\t-d <date>: Date replacement string (2019-02-14)")
|
|
||||||
print("\t-t <time>: Time replacement string (17:00)")
|
|
||||||
print("\t-h <host>: Connect to host")
|
|
||||||
print("\t-p <port>: Connect to port")
|
|
||||||
print("\t-<files>: LMW commando files")
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
lastTime=LmwSip.lasttime(None, "H10")
|
||||||
|
parser = argparse.ArgumentParser(description="Run a sip file.")
|
||||||
|
parser.add_argument("-u", "--unencrypted", action="store_true",
|
||||||
|
help="Run a sip connection without ssl")
|
||||||
|
parser.add_argument("-H", "--host", action='store',
|
||||||
|
default="sip-lmw.rws.nl",
|
||||||
|
help="Host to connect to")
|
||||||
|
parser.add_argument("-p", "--port", action='store', type=int, default=443,
|
||||||
|
help="Port to connect to")
|
||||||
|
parser.add_argument("-d", "--date", action='store',
|
||||||
|
default=lastTime["day"],
|
||||||
|
help="Date replacement string [DD-MM-YYYY]")
|
||||||
|
parser.add_argument("-t", "--time", action='store',
|
||||||
|
default=lastTime["time_of_day"],
|
||||||
|
help="Time replacement string [HH:MM]")
|
||||||
|
parser.add_argument("files", type=argparse.FileType('r'), nargs="+",
|
||||||
|
help="Sip files to run")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], "sh:p:d:t:", ["help", "output="])
|
lmwsip = LmwSip(host=args.host, port=args.port,
|
||||||
except getopt.GetoptError as err:
|
ssl=not args.unencrypted)
|
||||||
# print help information and exit:
|
|
||||||
print(err) # will print something like "option -a not recognized"
|
|
||||||
usage()
|
|
||||||
sys.exit(2)
|
|
||||||
ssl=False
|
|
||||||
time=None
|
|
||||||
date=None
|
|
||||||
host=None
|
|
||||||
port=None
|
|
||||||
for o, a in opts:
|
|
||||||
if o == "-H":
|
|
||||||
usage()
|
|
||||||
sys.exit(0)
|
|
||||||
elif o == "-s":
|
|
||||||
ssl=True
|
|
||||||
elif o == "-d":
|
|
||||||
date=a
|
|
||||||
elif o == "-t":
|
|
||||||
time=a
|
|
||||||
elif o == "-h":
|
|
||||||
host=a
|
|
||||||
elif o == "-p":
|
|
||||||
port=a
|
|
||||||
if (host==None or port==None):
|
|
||||||
print("Set host and port")
|
|
||||||
usage()
|
|
||||||
sys.exit(3)
|
|
||||||
try:
|
|
||||||
lmwsip = LmwSip(ssl=ssl, host=host, port=port)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Connect to lmw failed: %s" % e)
|
print("Connect to lmw failed: %s" % e)
|
||||||
exit(1)
|
exit(1)
|
||||||
if (date == None or time == None):
|
for f in args.files:
|
||||||
# We assume a 10 minut interval so we use H10
|
|
||||||
r = lmwsip.lasttime("H10")
|
|
||||||
if (date == None):
|
|
||||||
date = r["day"]
|
|
||||||
if (time == None):
|
|
||||||
time = r["time_of_day"]
|
|
||||||
for cmdfile in args:
|
|
||||||
with open(cmdfile, "r") as f:
|
|
||||||
for cmd in f:
|
for cmd in f:
|
||||||
cmd = cmd.replace('{DATE}', date)
|
cmd = cmd.replace('{DATE}', args.date)
|
||||||
cmd = cmd.replace('{TIME}', time)
|
cmd = cmd.replace('{TIME}', args.time)
|
||||||
cmd = cmd.replace('\n', '\r')
|
cmd = cmd.replace('\n', '\r')
|
||||||
print("> [%s]" % (cmd.strip('\r')))
|
print("> [%s]" % (cmd.strip('\r')))
|
||||||
try:
|
try:
|
||||||
@@ -69,7 +42,6 @@ def main():
|
|||||||
print("< [%s]" % (lmwsip.recv().strip('\r')))
|
print("< [%s]" % (lmwsip.recv().strip('\r')))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
f.close()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Reference in New Issue
Block a user