Compare commits
97 Commits
PythonModu
...
b19f5d9049
Author | SHA1 | Date | |
---|---|---|---|
|
b19f5d9049 | ||
|
2052bfd2cc | ||
|
418d3e6041 | ||
|
08cd160713 | ||
|
da80bd93d9 | ||
|
61997df1f3 | ||
|
047bb783b7 | ||
|
281bbfdac4 | ||
|
bcd761653f | ||
|
f521992f58 | ||
|
230b929b4c | ||
|
a884a5295b | ||
|
3414fde39c | ||
|
da6a9c3584 | ||
|
73b15f58ca | ||
|
f8f374569d | ||
|
8d4a40389c | ||
|
6a732a33c8 | ||
|
f87e571e46 | ||
|
4f593b008b | ||
|
34fe39371b | ||
|
beadb51698 | ||
|
a7bc8a51c4 | ||
|
2f4f2eef3f | ||
|
d711b51ce5 | ||
|
f2b7723210 | ||
|
7df03add42 | ||
|
9bd1d668da | ||
|
e9a65af74d | ||
|
d7799db8fb | ||
|
3b22676048 | ||
|
0b8e763344 | ||
|
707d4fc13a | ||
|
f29d49fafa | ||
|
3219139041 | ||
|
62c8b2021e | ||
|
7091efe006 | ||
|
2ae37db7a8 | ||
|
1556832ab2 | ||
|
0b201743ba | ||
|
8e85c8ab3a | ||
|
9feac5c8eb | ||
|
aa7ae08863 | ||
|
3890abb479 | ||
|
2181afeb2a | ||
|
69dc25fcba | ||
|
b5e2dad196 | ||
|
fe564e5721 | ||
|
2b2aaa7336 | ||
|
00d9d64f50 | ||
830aebdfa4 | |||
|
08af855c45 | ||
|
23f9723d27 | ||
|
3001b434a6 | ||
|
17301b70b3 | ||
|
57f6619c80 | ||
|
5968b6ff5b | ||
|
ad1fed8340 | ||
|
42133b80bd | ||
|
13fea782d7 | ||
|
bf1996931e | ||
eccd9011a3 | |||
872f77037b | |||
|
4422dd4fe5 | ||
|
0d44e053cf | ||
|
5b296a1da7 | ||
|
81511300bf | ||
|
6dd1d36add | ||
|
640c058d98 | ||
|
a8583d3eec | ||
|
6c7a505775 | ||
|
3d66a31792 | ||
|
b79438209b | ||
|
5b45b077f6 | ||
|
5f4b5d7d7d | ||
|
6e3a21c6c4 | ||
|
2281ef9427 | ||
|
1bfa73a0f7 | ||
|
04667dc28c | ||
|
dea73c249f | ||
|
62456fd35b | ||
|
7ba9e27e14 | ||
|
dd47094b75 | ||
|
da5da044ef | ||
|
8159dc83da | ||
|
6900b9fc57 | ||
|
7aa6ffc3d4 | ||
|
03503da5a4 | ||
|
80b8ebe4ff | ||
|
7e6751f60d | ||
|
76407e2a86 | ||
|
6885b03d40 | ||
|
3a07310e86 | ||
|
8acaf98e4b | ||
|
a2b8a52d47 | ||
|
df36ae495c | ||
|
827807be73 |
161
.drone.yml
161
.drone.yml
@@ -1,13 +1,166 @@
|
|||||||
---
|
---
|
||||||
kind: pipeline
|
kind: pipeline
|
||||||
type: exec
|
type: exec
|
||||||
name: default
|
name: CentOS7_test
|
||||||
|
|
||||||
platform:
|
platform:
|
||||||
os: linux
|
os: linux
|
||||||
arch: amd64
|
arch: amd64
|
||||||
|
variant: CentOS7
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Run unit test
|
- name: Run unit test
|
||||||
commands:
|
environment:
|
||||||
- python -m unittest lmwsip
|
PYTHONPATH: .
|
||||||
|
commands:
|
||||||
|
- python3 setup.py test
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: exec
|
||||||
|
name: CentOS8_test
|
||||||
|
|
||||||
|
platform:
|
||||||
|
os: linux
|
||||||
|
arch: amd64
|
||||||
|
variant: CentOS8
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Run unit test
|
||||||
|
environment:
|
||||||
|
PYTHONPATH: .
|
||||||
|
commands:
|
||||||
|
- python3 setup.py test
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: exec
|
||||||
|
name: Ubuntu1804_test
|
||||||
|
|
||||||
|
platform:
|
||||||
|
os: linux
|
||||||
|
arch: amd64
|
||||||
|
variant: Ubuntu1804
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Run unit test
|
||||||
|
environment:
|
||||||
|
PYTHONPATH: .
|
||||||
|
commands:
|
||||||
|
- python3 setup.py test
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: exec
|
||||||
|
name: Fedora_test
|
||||||
|
|
||||||
|
platform:
|
||||||
|
os: linux
|
||||||
|
arch: amd64
|
||||||
|
variant: Fedora
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Run unit test
|
||||||
|
environment:
|
||||||
|
PYTHONPATH: .
|
||||||
|
commands:
|
||||||
|
- python3 setup.py test
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: docker_python:3.6
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Run unit test
|
||||||
|
image: python:3.6
|
||||||
|
environment:
|
||||||
|
PYTHONPATH: .
|
||||||
|
commands:
|
||||||
|
- pip install python-dateutil
|
||||||
|
- python setup.py test
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: docker_python:latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Run unit test
|
||||||
|
image: python:latest
|
||||||
|
environment:
|
||||||
|
PYTHONPATH: .
|
||||||
|
commands:
|
||||||
|
- pip install python-dateutil
|
||||||
|
- python --version
|
||||||
|
- python setup.py test
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: exec
|
||||||
|
name: Build
|
||||||
|
|
||||||
|
platform:
|
||||||
|
os: linux
|
||||||
|
arch: amd64
|
||||||
|
variant: Fedora
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Build package files
|
||||||
|
commands:
|
||||||
|
- python3 setup.py sdist bdist_wheel
|
||||||
|
|
||||||
|
- name: Versie toevoegen aan download
|
||||||
|
environment:
|
||||||
|
DOWNLOADDIR: /usr/share/nginx/html/download/python/lmwsip
|
||||||
|
commands:
|
||||||
|
- mkdir -p "$${DOWNLOADDIR}"
|
||||||
|
- cd dist
|
||||||
|
- |
|
||||||
|
for f in *
|
||||||
|
do
|
||||||
|
if [ -f "$${DOWNLOADDIR}/$${f}" ]
|
||||||
|
then
|
||||||
|
echo version error
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
cp "$${f}" "$${DOWNLOADDIR}";
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
- ls -l "$${DOWNLOADDIR}"
|
||||||
|
when:
|
||||||
|
branch:
|
||||||
|
- master
|
||||||
|
|
||||||
|
depends_on:
|
||||||
|
- CentOS7_test
|
||||||
|
- CentOS8_test
|
||||||
|
- Ubuntu1804_test
|
||||||
|
- Fedora_test
|
||||||
|
- docker_python:latest
|
||||||
|
- docker_python:3.6
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: installCheck
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: install Check
|
||||||
|
image: python:3.6
|
||||||
|
# Make sure we run the pip installed version
|
||||||
|
commands:
|
||||||
|
- rm -rf lmwsip
|
||||||
|
- pip install --extra-index-url https://marceln.org/download/python lmwsip
|
||||||
|
- python -c "import lmwsip"
|
||||||
|
- python -c "import lmwsip; print(lmwsip.__version__)"
|
||||||
|
- >
|
||||||
|
[ $(python -c "import lmwsip; print(lmwsip.__version__)") =
|
||||||
|
$(python setup.py --version) ]
|
||||||
|
- python -m unittest -v lmwsip.tests
|
||||||
|
when:
|
||||||
|
branch:
|
||||||
|
- master
|
||||||
|
|
||||||
|
depends_on:
|
||||||
|
- Build
|
||||||
|
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,8 +1,10 @@
|
|||||||
*.pyc
|
*.pyc
|
||||||
*.sip
|
|
||||||
tmp
|
tmp
|
||||||
__pycache__
|
__pycache__
|
||||||
build
|
build
|
||||||
dist
|
dist
|
||||||
lmwsip_marceln.egg-info
|
lmwsip_marceln.egg-info
|
||||||
test/__pycache__
|
test/__pycache__
|
||||||
|
lmwsip.egg-info
|
||||||
|
*.swp
|
||||||
|
.tox
|
||||||
|
21
LICENSE
21
LICENSE
@@ -1 +1,20 @@
|
|||||||
TODO
|
Copyright (c) 2021 Rijkswaterstaat
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
|
||||||
|
65
README.md
65
README.md
@@ -3,38 +3,32 @@
|
|||||||
|
|
||||||
lmwsip is a python library for the lmw sip protocol.
|
lmwsip is a python library for the lmw sip protocol.
|
||||||
|
|
||||||
## Library
|
## Package
|
||||||
|
|
||||||
The lmwsip.py library contains the class LmwSip to connect to the
|
The lmwsip package contains the class LmwSip to connect to the
|
||||||
LMW meetnet using de SIP protocol. The library contains documentation
|
[LMW](https://waterberichtgeving.rws.nl/water-en-weer/metingen/lmw-info)
|
||||||
|
meetnet using de SIP protocol. The library contains documentation
|
||||||
how to use it.
|
how to use it.
|
||||||
|
|
||||||
## Tools
|
## Installing
|
||||||
### lmwsip-test
|
|
||||||
|
|
||||||
A small test program containing some functions to test the library.
|
The package is not available on [PyPI](https://pypi.org/).
|
||||||
|
At the moment the package is hosted at https://marceln.org/download/python.
|
||||||
|
|
||||||
### siprun
|
You can install the package with pip:
|
||||||
|
```
|
||||||
A prgram to run SIP command files.
|
pip install --extra-index-url https://marceln.org/download/python lmwsip
|
||||||
See "siprun -H" for usage information.
|
```
|
||||||
|
|
||||||
#### hoek-h10.sip
|
|
||||||
|
|
||||||
A sample sip command file to request the waterlevel at hoek van holland
|
|
||||||
from the last hour
|
|
||||||
|
|
||||||
## Username password
|
|
||||||
|
|
||||||
All files contain "USER", "PASS".
|
|
||||||
These values should be replaced by real credentials.
|
|
||||||
Otherwise the connection fails.
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
### Library
|
### Username password
|
||||||
|
|
||||||
#### Use send (low level)
|
All examples contain "USER", "PASS".
|
||||||
|
These values should be replaced by real credentials.
|
||||||
|
Otherwise the connection fails.
|
||||||
|
|
||||||
|
### Use send (low level)
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
from lmwsip import LmwSip
|
from lmwsip import LmwSip
|
||||||
@@ -71,16 +65,29 @@ sip = LmwSip("USER", "PASS")
|
|||||||
pprint(sip.timeSerie("WN", "HOEK", "H10", start, end).ts)
|
pprint(sip.timeSerie("WN", "HOEK", "H10", start, end).ts)
|
||||||
```
|
```
|
||||||
|
|
||||||
### siprun
|
### lmwsip.run
|
||||||
|
|
||||||
```
|
```
|
||||||
$ ./siprun -h sip-lmw.ad.rws.nl -s -p 443 hoek-h10.sip
|
$ python -m lmwsip.run /tmp/hoek-h10.sip
|
||||||
> [LI USER,PASS]
|
> [LI USER,PASS]
|
||||||
< [! ]
|
< [! ]
|
||||||
> [TI LMW]
|
> [TI LMW]
|
||||||
< [! 05-SEP-19 08:24:43]
|
< [! 08-SEP-20 12:03:27]
|
||||||
> [WN LMW,HOEK,H10,-25:00,05-09-2019,08:10,DATA]
|
> [WN LMW,HOEK,H10,-01:00,08-09-2020,11:50,DATA]
|
||||||
< [? 0211 Waarnemingen vallen buiten opslag range]
|
< [! -17/50;-21/50;-24/50;-26/50;-27/50;-28/50;-28/50]
|
||||||
> [LO]
|
> [LO]
|
||||||
< [! ]
|
< [! ]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Unit tests
|
||||||
|
|
||||||
|
The code containts a python unittest.
|
||||||
|
|
||||||
|
This code runs a dummy sip server and runs a number of test against the dummy
|
||||||
|
server.
|
||||||
|
|
||||||
|
## Git pre commit hook
|
||||||
|
|
||||||
|
There is a pre-commit `githooks/pre-commit' with two functions:
|
||||||
|
* Updating the `__version__` in the module from setup.py
|
||||||
|
* Running the unit test code.
|
||||||
|
* Running a syntaxt test.
|
||||||
|
10
githooks/pre-commit
Executable file
10
githooks/pre-commit
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
VERSION=$(grep version setup.cfg | sed 's/.*= *//')
|
||||||
|
sed -i "s/^__version__ = .*/__version__ = '${VERSION}'/" src/lmwsip/__init__.py
|
||||||
|
git add src/lmwsip/__init__.py
|
||||||
|
|
||||||
|
tox
|
||||||
|
yamllint .drone.yml
|
@@ -1,511 +0,0 @@
|
|||||||
"""Module to support the LmwSip class
|
|
||||||
|
|
||||||
See: LmwSip"""
|
|
||||||
|
|
||||||
import socket
|
|
||||||
import ssl
|
|
||||||
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)
|
|
||||||
|
|
||||||
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=None, password=None,
|
|
||||||
host="sip-lmw.rws.nl", port=443, meetnet="LMW", ssl = True,
|
|
||||||
check_ssl = True, timeout = 10, log = None, cleartelnet = False,
|
|
||||||
reconnecttime=540):
|
|
||||||
"""LmwSip(user, password, [host], [port], [meetnet], [ssl], [check_ssl], [timeout], [log])
|
|
||||||
|
|
||||||
user(optinal): Lmw user name
|
|
||||||
password(optional): Lmw password
|
|
||||||
host(optional): Default sip-lmw.rws.nl
|
|
||||||
port(optional): Default 443
|
|
||||||
meetnet(optional): Default LMW
|
|
||||||
ssl(optional): Default true
|
|
||||||
check_ssl(optional): Default true
|
|
||||||
timeout(optional): Default 10
|
|
||||||
log(optional): Default None
|
|
||||||
cleartelnet(optional): Default False
|
|
||||||
reconnecttime(optional): Default 540
|
|
||||||
|
|
||||||
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.timeout = timeout
|
|
||||||
self.cleartelnet = cleartelnet
|
|
||||||
self.reconnecttime = reconnecttime
|
|
||||||
self._connecttime = time.time()
|
|
||||||
self._socket = None
|
|
||||||
if (log != None):
|
|
||||||
self.log = log
|
|
||||||
self.log.debug("LmwSip.init(%s, **********, %s, %s, %s, %s, %s, %s, %s, %s)" %
|
|
||||||
(user, host, port, meetnet, ssl, check_ssl, timeout, cleartelnet, reconnecttime))
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
self.log = logging.getLogger("lmwsip")
|
|
||||||
self.log.debug("LmwSip.init: Start log")
|
|
||||||
except Exception as e:
|
|
||||||
print("Logger failed: %s" % e)
|
|
||||||
if (self.host != None):
|
|
||||||
self.connect()
|
|
||||||
if (self.user != None):
|
|
||||||
self.login()
|
|
||||||
else:
|
|
||||||
self.reconnecttime = 0
|
|
||||||
|
|
||||||
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.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
self._tcp = socket.create_connection((self.host, self.port))
|
|
||||||
self._connecttime = time.time()
|
|
||||||
except Exception as e:
|
|
||||||
self.log.error("LmwSip.connect(%s, %s) failed: %s",
|
|
||||||
self.host, self.port, e)
|
|
||||||
raise LmwSipConnectError("LmwSip.connect: Socket create failed")
|
|
||||||
if (self.ssl):
|
|
||||||
try:
|
|
||||||
self._context = ssl.create_default_context()
|
|
||||||
self._context.check_hostname = self.check_ssl
|
|
||||||
self._ssl = self._context.wrap_socket(self._tcp,
|
|
||||||
server_hostname=self.host)
|
|
||||||
self._socket = self._ssl
|
|
||||||
except Exception as e:
|
|
||||||
self.log.error("LmwSip.connect setup ssl failed:\n%s", e)
|
|
||||||
raise LmwSipConnectError("LmwSip.connect: setup ssl failed")
|
|
||||||
else:
|
|
||||||
self._socket = self._tcp
|
|
||||||
self._socket.settimeout(self.timeout)
|
|
||||||
|
|
||||||
def closesocket(self):
|
|
||||||
"""Closes the socket and set the socket to None. Doesn't logout"""
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.log.debug("LmwSip.closesocket")
|
|
||||||
self._socket.close()
|
|
||||||
except Exception as e:
|
|
||||||
pass
|
|
||||||
self._socket = None
|
|
||||||
|
|
||||||
def reconnectcheck(self):
|
|
||||||
"""Checks if the connection is longer open than the reconnect time.
|
|
||||||
After this time a logout is sent and a new connection is created.
|
|
||||||
|
|
||||||
This prevents the 10 minute server timeout"""
|
|
||||||
if self.reconnecttime > 0:
|
|
||||||
ct = time.time() - self._connecttime
|
|
||||||
if ct > self.reconnecttime:
|
|
||||||
self.log.debug("LmwSip.reconnectcheck: reconnect after %i seconds" % ct)
|
|
||||||
#
|
|
||||||
# Disable check for the reconnect
|
|
||||||
#
|
|
||||||
self.reconnecttime = - self.reconnecttime
|
|
||||||
self.logout()
|
|
||||||
time.sleep(1)
|
|
||||||
self.connect()
|
|
||||||
self.login()
|
|
||||||
self.reconnecttime = - self.reconnecttime
|
|
||||||
|
|
||||||
def send(self, sipcmd):
|
|
||||||
"""send(sipcmd)
|
|
||||||
|
|
||||||
send a sip command to the server
|
|
||||||
"""
|
|
||||||
self.reconnectcheck()
|
|
||||||
if self._socket != None:
|
|
||||||
try:
|
|
||||||
logcmd = sipcmd.strip('\r')
|
|
||||||
if re.match("^LI", logcmd, re.IGNORECASE):
|
|
||||||
logcmd = re.sub(",.*", ", ******", logcmd)
|
|
||||||
self.log.debug("LmwSip.send(%s)" % logcmd)
|
|
||||||
self._socket.sendall(sipcmd.encode('ascii'))
|
|
||||||
except Exception as e:
|
|
||||||
self.log.error("LmwSip.send(%s) failed: %s" % (sipcmd, e))
|
|
||||||
self.closesocket()
|
|
||||||
raise LmwSipConnectError("LmwSip.send: Socket connection lost")
|
|
||||||
else:
|
|
||||||
self.log.warning("LmwSip.send: No connection")
|
|
||||||
|
|
||||||
def telnetheader(self, header):
|
|
||||||
a = b'\xff\xfd\x01\xff\xfd\x03\xff\xfd\x00\xff\xfc\x01\xff\xfb\x00'
|
|
||||||
self.log.debug("LmwSip.telnetheader(%s) --> %s" % (header, a))
|
|
||||||
try:
|
|
||||||
self._socket.sendall(a)
|
|
||||||
except Exception as e:
|
|
||||||
self.log.error("LmwSip.telnetheader(%s) --> %s failed: %s" % (header, a, e))
|
|
||||||
self.closesocket()
|
|
||||||
raise LmwSipConnectError("LmwSip.telnetheader: Socket connection lost")
|
|
||||||
|
|
||||||
def recv(self):
|
|
||||||
"""recv()
|
|
||||||
|
|
||||||
recieve a answer from the sip server
|
|
||||||
"""
|
|
||||||
bytebuf=b''
|
|
||||||
stringbuf=""
|
|
||||||
while (self._socket != None) and (stringbuf.find("\r") == -1):
|
|
||||||
try:
|
|
||||||
self.log.debug("LmwSip.recv: %s: Waiting for data" % self.cleartelnet);
|
|
||||||
bytebuf = self._socket.recv(4096)
|
|
||||||
self.log.debug("recv: bytebuf: %s" % bytebuf)
|
|
||||||
if self.cleartelnet:
|
|
||||||
if bytebuf[0] == 255:
|
|
||||||
bytebuf = b''
|
|
||||||
except Exception as e:
|
|
||||||
self.log.error("SipLmw.recv: socket timeout: %s", e)
|
|
||||||
self.closesocket()
|
|
||||||
raise LmwSipConnectError("LmwSip.recv: No data recieved")
|
|
||||||
try:
|
|
||||||
stringbuf += bytebuf.decode('utf-8')
|
|
||||||
self.log.debug("recv: stringbuf: %s" % stringbuf)
|
|
||||||
except Exception as e:
|
|
||||||
self.log.error("SipLmw.recv: decode error: %s", e)
|
|
||||||
self.closesocket()
|
|
||||||
raise LmwSipDecodeError("LmwSip.recv: decode error", bytebuf)
|
|
||||||
if self._socket == None:
|
|
||||||
self.log.warn("LmwSip.recv: No connection")
|
|
||||||
elif len(stringbuf) == 0:
|
|
||||||
self.log.warn("LmwSip.recv: No data")
|
|
||||||
elif stringbuf[0] != '!':
|
|
||||||
self.log.warn("LmwSip.recv: Sip error: %s" % stringbuf.strip('\r'))
|
|
||||||
else:
|
|
||||||
self.log.debug("LmwSip.recv: result: %s" % stringbuf.strip('\r'))
|
|
||||||
return(stringbuf)
|
|
||||||
|
|
||||||
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.send(li)
|
|
||||||
d = self.recv()
|
|
||||||
if (d[0] != '!'):
|
|
||||||
raise LmwLoginFailure(self.user + ":" + d)
|
|
||||||
|
|
||||||
def ti(self):
|
|
||||||
"""ti()
|
|
||||||
|
|
||||||
Request the time from lmw and returns the string.
|
|
||||||
|
|
||||||
Raises a LmwCmdWarn of failure
|
|
||||||
"""
|
|
||||||
ti="TI " + self.meetnet + "\r"
|
|
||||||
self.send(ti)
|
|
||||||
d = self.recv()
|
|
||||||
return (d[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: <WN|VW|AS>
|
|
||||||
location: <lmw location (e.g. HOEK)>
|
|
||||||
parameter: <lmw parameter (e.g. H10)>
|
|
||||||
time_delta: <Time windows (max 23:59, e.g. +01:00>
|
|
||||||
day: <Date>
|
|
||||||
time_of_day: <Time>
|
|
||||||
cmd_type: [DATA|DATB|OORS|OORB|""]
|
|
||||||
|
|
||||||
Example:
|
|
||||||
lmw.cmd("WN", "HOEK", "H10", "+01:00", "13-08-2018", "16:00")
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The LMW answer string
|
|
||||||
"""
|
|
||||||
if (process == "AS"):
|
|
||||||
data=""
|
|
||||||
else:
|
|
||||||
data="," + cmd_type
|
|
||||||
|
|
||||||
cmdstr=process + " " + self.meetnet + "," + location + "," + \
|
|
||||||
parameter + "," + time_delta + "," + day + "," + \
|
|
||||||
time_of_day + data + "\r"
|
|
||||||
|
|
||||||
self.send(cmdstr)
|
|
||||||
d = self.recv()
|
|
||||||
if (d[0] != '!'):
|
|
||||||
raise LmwCmdWarn(cmdstr, d)
|
|
||||||
return (d[2:-1])
|
|
||||||
|
|
||||||
def value(self, process, location, parameter, day = None,
|
|
||||||
time_of_day = None):
|
|
||||||
"""value(process, location, parameter, [day], [time_of_day]):
|
|
||||||
|
|
||||||
Parameters:
|
|
||||||
process: <WN|VW|AS>
|
|
||||||
location: <lmw location (e.g. HOEK)>
|
|
||||||
parameter: <lmw parameter (e.g. H10)>
|
|
||||||
day: [date = now()]
|
|
||||||
time_of_day: [time = now()]
|
|
||||||
|
|
||||||
The default returns the last value.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
lmw.data_string("WN", "HOEK", "H10")
|
|
||||||
|
|
||||||
Returns a single string value or None
|
|
||||||
"""
|
|
||||||
if (day == None or time_of_day == None):
|
|
||||||
last = self.lasttime(parameter)
|
|
||||||
if (day==None):
|
|
||||||
day=last["day"]
|
|
||||||
if (time_of_day==None):
|
|
||||||
time_of_day=last["time_of_day"]
|
|
||||||
res = self.cmd(process, location, parameter, "+00:00", day,
|
|
||||||
time_of_day, "DATA")
|
|
||||||
value=re.sub("/.*$", "", res)
|
|
||||||
if (value == "99999"):
|
|
||||||
value=""
|
|
||||||
elif (value == "-999999999"):
|
|
||||||
value=""
|
|
||||||
#
|
|
||||||
# We should check the "kwaliteit"
|
|
||||||
#
|
|
||||||
return(value)
|
|
||||||
|
|
||||||
def _lmwdelta_(self, window):
|
|
||||||
h = 24*window.days + window.seconds // 3600
|
|
||||||
m = (window.seconds % 3600)//60
|
|
||||||
return("+%02i:%02i" % (h, m))
|
|
||||||
|
|
||||||
def _roundtime_(self, time, parameter):
|
|
||||||
if time.microsecond != 0:
|
|
||||||
time += timedelta(microseconds=1000000-time.microsecond)
|
|
||||||
if time.second != 0:
|
|
||||||
time += timedelta(seconds=60-time.second)
|
|
||||||
if (parameter.find("10") != -1) and (time.minute % 10 != 0):
|
|
||||||
time += timedelta(minutes=(10-time.minute%10))
|
|
||||||
return(time)
|
|
||||||
|
|
||||||
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 = self._roundtime_(startTime.astimezone(tz.gettz('GMT+1')), parameter)
|
|
||||||
endTime = endTime.astimezone(tz.gettz('GMT+1'))
|
|
||||||
|
|
||||||
if (parameter.find("10") != -1):
|
|
||||||
delta = timedelta(minutes=10)
|
|
||||||
else:
|
|
||||||
delta = timedelta(minutes=1)
|
|
||||||
|
|
||||||
if startTime > endTime:
|
|
||||||
self.log.warn("starttime > endtime: %s > %s", startTime, endTime)
|
|
||||||
raise sipTimeSeriesError(startTime, endTime,
|
|
||||||
"starttime > endtime")
|
|
||||||
|
|
||||||
if datetime.now(tz=tz.gettz('GMT+1')) - startTime > timedelta(days=30):
|
|
||||||
self.log.warn("now() - starttime > 30 days: %s", startTime)
|
|
||||||
raise sipTimeSeriesError(startTime, endTime,
|
|
||||||
"now - starttime > 30 days")
|
|
||||||
|
|
||||||
self.log.debug("LmwSip.timeSerie: startTime: %s" % startTime)
|
|
||||||
self.log.debug("LmwSip.timeSerie: endTime: %s" % endTime)
|
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
Logs of
|
|
||||||
"""
|
|
||||||
self.send("LO\r")
|
|
||||||
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):
|
|
||||||
"""Connection exceptions for LmwSip"""
|
|
||||||
|
|
||||||
def __init__(self, message):
|
|
||||||
self.message = message
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return(self.message)
|
|
||||||
|
|
||||||
class LmwSipDecodeError(Exception):
|
|
||||||
"""Connection exceptions for LmwSip"""
|
|
||||||
|
|
||||||
def __init__(self, message, buf):
|
|
||||||
self.message = message
|
|
||||||
self.buf = buf
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return(self.message + ":" + buf)
|
|
||||||
|
|
||||||
|
|
||||||
class LmwLoginFailure(Exception):
|
|
||||||
"""Exception from LmwSip on login failure"""
|
|
||||||
|
|
||||||
def __init__(self, user, message):
|
|
||||||
self.user = user
|
|
||||||
self.message = message
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return("Login with user %s failed: %s" % (self.user, self.message))
|
|
||||||
|
|
||||||
class LmwCmdWarn(Warning):
|
|
||||||
"""Exception fro LmwSip on a cmd"""
|
|
||||||
def __init__(self, cmd, message):
|
|
||||||
self.cmd = cmd.replace('\r', '')
|
|
||||||
self.message = message
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return("Cmd %s failed: %s" %(self.cmd, self.message))
|
|
6
pyproject.toml
Normal file
6
pyproject.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = [
|
||||||
|
"setuptools>=42",
|
||||||
|
"wheel"
|
||||||
|
]
|
||||||
|
build-backend = "setuptools.build_meta"
|
24
setup.cfg
Normal file
24
setup.cfg
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
[metadata]
|
||||||
|
name = lmwsip
|
||||||
|
version = 0.9.1
|
||||||
|
author = Marcel Nijenhof
|
||||||
|
author_email = pypi@marceln.org
|
||||||
|
description = Interface for the lmw sip protocol
|
||||||
|
long_description = file: README.md
|
||||||
|
long_description_content_type = text/markdown
|
||||||
|
url = https://marceln.org/git/Werk/lmwsip
|
||||||
|
classifiers =
|
||||||
|
Programming Language :: Python :: 3
|
||||||
|
License :: OSI Approved :: MIT License
|
||||||
|
Operating System :: OS Independent
|
||||||
|
|
||||||
|
[options]
|
||||||
|
package_dir =
|
||||||
|
= src
|
||||||
|
install_requires =
|
||||||
|
python-dateutil
|
||||||
|
packages = find:
|
||||||
|
python_requires = >= 3.6
|
||||||
|
|
||||||
|
[options.packages.find]
|
||||||
|
where = src
|
24
setup.py
24
setup.py
@@ -1,22 +1,4 @@
|
|||||||
import setuptools
|
from setuptools import setup
|
||||||
|
|
||||||
with open("README.md", "r") as fh:
|
if __name__ == "__main__":
|
||||||
long_description = fh.read()
|
setup()
|
||||||
|
|
||||||
setuptools.setup(
|
|
||||||
name="lmwsip-marceln", # Replace with your own username
|
|
||||||
version="0.0.1",
|
|
||||||
author="Marcel Nijenhof",
|
|
||||||
author_email="pip@pion.xs4all.nl",
|
|
||||||
description="Interface for the lmw sip protocol",
|
|
||||||
long_description=long_description,
|
|
||||||
long_description_content_type="text/markdown",
|
|
||||||
url="https://marceln.org/git/Werk/lmwsip",
|
|
||||||
packages=setuptools.find_packages(),
|
|
||||||
classifiers=[
|
|
||||||
"Programming Language :: Python :: 3",
|
|
||||||
"License :: TODO",
|
|
||||||
"Operating System :: OS Independent",
|
|
||||||
],
|
|
||||||
python_requires='>=3.6',
|
|
||||||
)
|
|
||||||
|
1067
src/lmwsip/__init__.py
Normal file
1067
src/lmwsip/__init__.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -3,33 +3,16 @@
|
|||||||
import sys
|
import sys
|
||||||
import getopt
|
import getopt
|
||||||
import argparse
|
import argparse
|
||||||
|
import logging
|
||||||
from lmwsip import LmwSip
|
from lmwsip import LmwSip
|
||||||
|
|
||||||
def main():
|
def run(args):
|
||||||
lastTime=LmwSip.lasttime(None, "H10")
|
logging.basicConfig(level=args.debug)
|
||||||
parser = argparse.ArgumentParser(description="Run a sip file.")
|
logging.debug("lmwsip.run %s" % args)
|
||||||
parser.add_argument("-u", "--unencrypted", action="store_true",
|
|
||||||
help="Run a sip connection without ssl")
|
|
||||||
parser.add_argument("-c", "--cleartelnet", action="store_true",
|
|
||||||
help="Clear telnet protocol in tcp session")
|
|
||||||
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:
|
||||||
lmwsip = LmwSip(host=args.host, port=args.port,
|
lmwsip = LmwSip(host=args.host, port=args.port,
|
||||||
ssl=not args.unencrypted,
|
ssl=not args.unencrypted,
|
||||||
|
check_ssl=not args.acceptssl,
|
||||||
cleartelnet=args.cleartelnet)
|
cleartelnet=args.cleartelnet)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Connect to lmw failed: %s" % e)
|
print("Connect to lmw failed: %s" % e)
|
||||||
@@ -45,6 +28,37 @@ def main():
|
|||||||
print("< [%s]" % (lmwsip.recv().strip('\r')))
|
print("< [%s]" % (lmwsip.recv().strip('\r')))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
try:
|
||||||
|
lmwsip.closesocket()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def main():
|
||||||
|
lastTime=LmwSip(host=None).lasttime("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("-a", "--acceptssl", action="store_true",
|
||||||
|
help="Accept ssl certificate")
|
||||||
|
parser.add_argument("-c", "--cleartelnet", action="store_true",
|
||||||
|
help="Clear telnet protocol in tcp session")
|
||||||
|
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("-D", "--debug", action='store',
|
||||||
|
default="WARN",
|
||||||
|
help="Debug level")
|
||||||
|
parser.add_argument("files", type=argparse.FileType('r'), nargs="+",
|
||||||
|
help="Sip files to run")
|
||||||
|
run(parser.parse_args())
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
164
src/lmwsip/tests/__init__.py
Executable file
164
src/lmwsip/tests/__init__.py
Executable file
@@ -0,0 +1,164 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import io
|
||||||
|
import unittest
|
||||||
|
import lmwsip
|
||||||
|
import lmwsip.tests.stubSipServer
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from lmwsip.tests.stubSipServer import sipServer
|
||||||
|
from lmwsip.run import run
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from dateutil import tz
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
class myTestArgs():
|
||||||
|
pass
|
||||||
|
|
||||||
|
class lmwsipTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.sipserver = sipServer()
|
||||||
|
self.sip = None
|
||||||
|
self.sipserver.run()
|
||||||
|
|
||||||
|
def login(self, **args):
|
||||||
|
log = logging.basicConfig(level=logging.DEBUG)
|
||||||
|
self.sip = lmwsip.LmwSip("USER", "PASS", "localhost",
|
||||||
|
self.sipserver.port, ssl=False,
|
||||||
|
log=log, **args)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
if self.sip:
|
||||||
|
self.sip.closesocket()
|
||||||
|
self.sipserver.kill()
|
||||||
|
|
||||||
|
def test_sipobj(self):
|
||||||
|
self.login()
|
||||||
|
self.assertEqual(type(self.sip), lmwsip.LmwSip)
|
||||||
|
|
||||||
|
def test_H1(self):
|
||||||
|
self.sip = lmwsip.LmwSip(host=None)
|
||||||
|
self.assertEqual(self.sip.period('H1'), 1)
|
||||||
|
|
||||||
|
def test_H10(self):
|
||||||
|
self.sip = lmwsip.LmwSip(host=None)
|
||||||
|
self.assertEqual(self.sip.period('H10'), 10)
|
||||||
|
|
||||||
|
def test_xHm0(self):
|
||||||
|
self.sip = lmwsip.LmwSip(host=None)
|
||||||
|
self.assertEqual(self.sip.period('xHm0'), 10)
|
||||||
|
|
||||||
|
def test_Noparm(self):
|
||||||
|
self.sip = lmwsip.LmwSip(host=None)
|
||||||
|
with self.assertRaises(lmwsip.LmwParmWarn):
|
||||||
|
self.assertEqual(self.sip.period('Noparm'), None)
|
||||||
|
|
||||||
|
def test_loginfail(self):
|
||||||
|
with self.assertRaises(lmwsip.LmwLoginFailure):
|
||||||
|
self.sip = lmwsip.LmwSip("FAIL", "FAIL", "localhost",
|
||||||
|
self.sipserver.port, ssl=False)
|
||||||
|
|
||||||
|
def test_ti(self):
|
||||||
|
self.login()
|
||||||
|
self.assertEqual(type(self.sip.ti()), str)
|
||||||
|
|
||||||
|
def test_telnetti(self):
|
||||||
|
self.login(cleartelnet=True)
|
||||||
|
self.assertEqual(type(self.sip.ti()), str)
|
||||||
|
|
||||||
|
def test_cmd(self):
|
||||||
|
self.login()
|
||||||
|
self.assertEqual(type(self.sip.cmd("WN", "DUMMY", "H10", "+00:59", "2020-01-01", "00:00")), str)
|
||||||
|
|
||||||
|
def test_cmderr(self):
|
||||||
|
self.login()
|
||||||
|
with self.assertRaises(lmwsip.LmwCmdWarn):
|
||||||
|
self.assertEqual(type(self.sip.cmd("NOP", "DUMMY", "H10", "+00:59", "2020-01-01", "00:00")), str)
|
||||||
|
|
||||||
|
def test_value(self):
|
||||||
|
self.login()
|
||||||
|
self.assertEqual(type(self.sip.value("WN", "DUMMY", "H10")), str)
|
||||||
|
|
||||||
|
def test_value1min(self):
|
||||||
|
self.login()
|
||||||
|
self.assertEqual(type(self.sip.value("WN", "DUMMY", "H1")), str)
|
||||||
|
|
||||||
|
def test_valueStr(self):
|
||||||
|
self.login()
|
||||||
|
self.assertEqual(type(self.sip.valueStr("WN", "DUMMY", "H10")), str)
|
||||||
|
|
||||||
|
def test_logout(self):
|
||||||
|
self.login()
|
||||||
|
self.assertEqual(self.sip.logout(), None)
|
||||||
|
|
||||||
|
def test_lmwTimeSerie(self):
|
||||||
|
self.login()
|
||||||
|
timezone = tz.gettz('GMT+1')
|
||||||
|
res = self.sip.timeSerie("WN", "DUMMY", "H10",
|
||||||
|
datetime.now(timezone)-timedelta(minutes=60),
|
||||||
|
datetime.now(timezone))
|
||||||
|
self.assertEqual(type(res.ts), list)
|
||||||
|
self.assertEqual(len(res.ts), 6)
|
||||||
|
self.assertEqual(res.ts[1][1][0], '1')
|
||||||
|
|
||||||
|
def test_roundtime(self):
|
||||||
|
self.login()
|
||||||
|
timezone = tz.gettz('GMT+1')
|
||||||
|
t1 = datetime(2020, 1, 1, 0, 10, 0, 0, timezone)
|
||||||
|
t2 = datetime(2020, 1, 1, 0, 0, 0, 1, timezone)
|
||||||
|
self.assertEqual(self.sip._roundtime_(t1, timedelta(minutes=10)), t1)
|
||||||
|
self.assertEqual(self.sip._roundtime_(t2, timedelta(minutes=10)), t1)
|
||||||
|
|
||||||
|
def test_closerecv(self):
|
||||||
|
self.login()
|
||||||
|
self.sip.send("CLOSE")
|
||||||
|
with self.assertRaises(lmwsip.LmwSipConnectError):
|
||||||
|
self.sip.recv()
|
||||||
|
|
||||||
|
def test_closeti(self):
|
||||||
|
self.login()
|
||||||
|
self.sip.send("CLOSE")
|
||||||
|
self.assertEqual(type(self.sip.ti()), str)
|
||||||
|
|
||||||
|
def test_closecmd(self):
|
||||||
|
self.login()
|
||||||
|
self.sip.send("CLOSE")
|
||||||
|
self.assertEqual(type(self.sip.cmd("WN", "DUMMY", "H10", "+00:59", "2020-01-01", "00:00")), str)
|
||||||
|
|
||||||
|
def test_reconnect(self):
|
||||||
|
self.login(reconnecttime=1)
|
||||||
|
sleep(2)
|
||||||
|
self.assertEqual(self.sip.sendrecv("LOGOUTCOUNT"), "1\r")
|
||||||
|
|
||||||
|
def test_idlereconnect(self):
|
||||||
|
self.login(idlereconnect=1)
|
||||||
|
sleep(2)
|
||||||
|
self.assertEqual(self.sip.sendrecv("LOGOUTCOUNT"), "1\r")
|
||||||
|
|
||||||
|
def test_versionstr(self):
|
||||||
|
self.assertEqual(type(lmwsip.__version__), str)
|
||||||
|
|
||||||
|
def test_run(self):
|
||||||
|
capturedOutput = io.StringIO()
|
||||||
|
sys.stdout = capturedOutput
|
||||||
|
testSipFile = io.StringIO("LI USER,PASS\rTI LMW\rLO")
|
||||||
|
testSipFile.seek(0)
|
||||||
|
args = myTestArgs()
|
||||||
|
args.debug = "DEBUG"
|
||||||
|
args.host = "localhost"
|
||||||
|
args.port = self.sipserver.port
|
||||||
|
args.unencrypted = True
|
||||||
|
args.acceptssl = True
|
||||||
|
args.cleartelnet = False
|
||||||
|
args.time = "+00:59"
|
||||||
|
args.date = "2020-01-01"
|
||||||
|
args.files = [testSipFile]
|
||||||
|
run(args)
|
||||||
|
args.files[0].close()
|
||||||
|
self.assertEqual(capturedOutput.getvalue().find("!")>= 0, True)
|
||||||
|
self.assertEqual(capturedOutput.getvalue().find("?"), -1)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
@@ -13,12 +13,15 @@ Implements the following commands:
|
|||||||
CMD> TI LMW
|
CMD> TI LMW
|
||||||
ANS< ! 20-JAN-01 00:00:00
|
ANS< ! 20-JAN-01 00:00:00
|
||||||
|
|
||||||
CMD> WN LMW,DUMMY,D10,+HH:MM,yyyy-mm-dd,HH:MM,DATA
|
CMD> WN LMW,DUMMY,H10,+HH:MM,yyyy-mm-dd,HH:MM,DATA
|
||||||
ANS< ! 1/10,;2/10;....
|
ANS< ! 1/10,;2/10;....
|
||||||
|
|
||||||
CMD> WN LMW,DUMMY,D10,+HH:MM,yyyy-mm-dd,HH:MM,DATB
|
CMD> WN LMW,DUMMY,H10,+HH:MM,yyyy-mm-dd,HH:MM,DATB
|
||||||
ANS< ! 1/10/0;2/10/0;....
|
ANS< ! 1/10/0;2/10/0;....
|
||||||
|
|
||||||
|
CMD> WN LMW,DUMMY,H1,+HH:MM,yyyy-mm-dd,HH:MM,DATA
|
||||||
|
ANS< ! 1/10,;2/10;....
|
||||||
|
|
||||||
CMD> LO
|
CMD> LO
|
||||||
ANS< !
|
ANS< !
|
||||||
|
|
||||||
@@ -41,8 +44,9 @@ import time
|
|||||||
import random
|
import random
|
||||||
import socketserver
|
import socketserver
|
||||||
|
|
||||||
class sipProtocol(socketserver.BaseRequestHandler):
|
logoutcount=0
|
||||||
|
|
||||||
|
class sipProtocol(socketserver.BaseRequestHandler):
|
||||||
def match(self, m):
|
def match(self, m):
|
||||||
return(self.data.find(m.encode()) == 0)
|
return(self.data.find(m.encode()) == 0)
|
||||||
|
|
||||||
@@ -56,18 +60,19 @@ class sipProtocol(socketserver.BaseRequestHandler):
|
|||||||
except:
|
except:
|
||||||
self.data = None
|
self.data = None
|
||||||
|
|
||||||
def meting(self):
|
def number(self, b):
|
||||||
res = ""
|
if b[0] == b'0':
|
||||||
sep = "! "
|
return(int(b[0:1]))
|
||||||
if self.data[18:19] == b'0':
|
else:
|
||||||
h = int(self.data[19:20].decode())
|
return(int(b[0:2]))
|
||||||
else:
|
|
||||||
h = int(self.data[18:20].decode())
|
def meting(self, delta=10):
|
||||||
if self.data[21:22] == b'0':
|
res = ""
|
||||||
m = int(self.data[22:23].decode())
|
sep = "! "
|
||||||
else:
|
elem = self.data.decode().split(",")
|
||||||
m = int(self.data[21:23].decode())
|
h = self.number(elem[3][1:3])
|
||||||
aantal = 1+(60*h+m)//10
|
m = self.number(elem[3][4:6])
|
||||||
|
aantal = 1+(60*h+m)//delta
|
||||||
if self.data[-1:] == b'A':
|
if self.data[-1:] == b'A':
|
||||||
data = "%i/10"
|
data = "%i/10"
|
||||||
else:
|
else:
|
||||||
@@ -78,16 +83,24 @@ class sipProtocol(socketserver.BaseRequestHandler):
|
|||||||
self.send(res)
|
self.send(res)
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
|
global logoutcount
|
||||||
self.read()
|
self.read()
|
||||||
while self.data:
|
while self.data:
|
||||||
if self.match("LI USER,PASS"):
|
if self.match("LI USER,PASS"):
|
||||||
self.send("!")
|
self.send("!")
|
||||||
elif self.match("TI LMW"):
|
elif self.match("TI LMW"):
|
||||||
self.send("! 20-JAN-01 00:00:00")
|
self.send("! 20-JAN-01 00:00:00")
|
||||||
elif self.match("WN LMW,DUMMY,D10"):
|
elif self.match("WN LMW,DUMMY,H10,"):
|
||||||
self.meting()
|
self.meting(10)
|
||||||
|
elif self.match("WN LMW,DUMMY,H1,"):
|
||||||
|
self.meting(1)
|
||||||
|
elif self.match("LOGOUTCOUNT"):
|
||||||
|
self.send(str(logoutcount))
|
||||||
elif self.match("LO"):
|
elif self.match("LO"):
|
||||||
|
logoutcount+=1
|
||||||
self.send("!")
|
self.send("!")
|
||||||
|
elif self.match("CLOSE"):
|
||||||
|
self.request.close()
|
||||||
else:
|
else:
|
||||||
self.send("? ERROR")
|
self.send("? ERROR")
|
||||||
self.read()
|
self.read()
|
||||||
@@ -113,4 +126,6 @@ class sipServer(socketserver.TCPServer):
|
|||||||
self.server_close()
|
self.server_close()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
s = sipServer()
|
||||||
|
s.run()
|
||||||
pass
|
pass
|
@@ -1,51 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
import unittest
|
|
||||||
import lmwsip
|
|
||||||
import stubSipServer
|
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
from dateutil import tz
|
|
||||||
|
|
||||||
class lmwsipTest(unittest.TestCase):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.sipserver = stubSipServer.sipServer()
|
|
||||||
self.sipserver.run()
|
|
||||||
self.sip = lmwsip.LmwSip("USER", "PASS", "localhost",
|
|
||||||
self.sipserver.port, ssl=False)
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.sipserver.kill()
|
|
||||||
self.sip.closesocket()
|
|
||||||
|
|
||||||
def test_sipobj(self):
|
|
||||||
self.assertEqual(type(self.sip), lmwsip.LmwSip)
|
|
||||||
|
|
||||||
def test_ti(self):
|
|
||||||
self.assertEqual(type(self.sip.ti()), str)
|
|
||||||
|
|
||||||
def test_cmd(self):
|
|
||||||
self.assertEqual(type(self.sip.cmd("WN", "DUMMY", "D10", "+00:59", "2020-01-01", "00:00")), str)
|
|
||||||
|
|
||||||
def test_logout(self):
|
|
||||||
self.assertEqual(self.sip.logout(), None)
|
|
||||||
|
|
||||||
def test_lmwTimeSerie(self):
|
|
||||||
timezone = tz.gettz('GMT+1')
|
|
||||||
res = self.sip.timeSerie("WN", "DUMMY", "D10",
|
|
||||||
datetime.now(timezone)-timedelta(minutes=60),
|
|
||||||
datetime.now(timezone))
|
|
||||||
self.assertEqual(type(res.ts), list)
|
|
||||||
self.assertEqual(len(res.ts), 6)
|
|
||||||
self.assertEqual(res.ts[1][1][0], '1')
|
|
||||||
|
|
||||||
def test_roundtime(self):
|
|
||||||
timezone = tz.gettz('GMT+1')
|
|
||||||
t1 = datetime(2020, 1, 1, 0, 10, 0, 0, timezone)
|
|
||||||
t2 = datetime(2020, 1, 1, 0, 0, 0, 1, timezone)
|
|
||||||
self.assertEqual(self.sip._roundtime_(t1, "D10"), t1)
|
|
||||||
self.assertEqual(self.sip._roundtime_(t2, "D10"), t1)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
unittest.main()
|
|
Reference in New Issue
Block a user