clean-up re-format
This commit is contained in:
parent
8b8b952ce1
commit
e4f384c2b7
@ -39,18 +39,13 @@ def console():
|
|||||||
|
|
||||||
# create tcp or serial connection depending on args
|
# create tcp or serial connection depending on args
|
||||||
if args.host and args.port:
|
if args.host and args.port:
|
||||||
create_connection = partial(
|
create_connection = partial(create_tcp_dsmr_reader,
|
||||||
create_tcp_dsmr_reader,
|
args.host, args.port, args.version,
|
||||||
args.host,
|
print_callback, loop=loop)
|
||||||
args.port,
|
|
||||||
args.version,
|
|
||||||
print_callback,
|
|
||||||
loop=loop,
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
create_connection = partial(
|
create_connection = partial(create_dsmr_reader,
|
||||||
create_dsmr_reader, args.device, args.version, print_callback, loop=loop
|
args.device, args.version,
|
||||||
)
|
print_callback, loop=loop)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# connect and keep connected until interrupted by ctrl-c
|
# connect and keep connected until interrupted by ctrl-c
|
||||||
|
@ -10,49 +10,40 @@ from dsmr_parser import telegram_specifications
|
|||||||
from dsmr_parser.clients.telegram_buffer import TelegramBuffer
|
from dsmr_parser.clients.telegram_buffer import TelegramBuffer
|
||||||
from dsmr_parser.exceptions import ParseError, InvalidChecksumError
|
from dsmr_parser.exceptions import ParseError, InvalidChecksumError
|
||||||
from dsmr_parser.parsers import TelegramParser
|
from dsmr_parser.parsers import TelegramParser
|
||||||
from dsmr_parser.clients.settings import (
|
from dsmr_parser.clients.settings import SERIAL_SETTINGS_V2_2, \
|
||||||
SERIAL_SETTINGS_V2_2,
|
SERIAL_SETTINGS_V4, SERIAL_SETTINGS_V5
|
||||||
SERIAL_SETTINGS_V4,
|
|
||||||
SERIAL_SETTINGS_V5,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def create_dsmr_protocol(dsmr_version, telegram_callback, loop=None, **kwargs):
|
def create_dsmr_protocol(dsmr_version, telegram_callback, loop=None, **kwargs):
|
||||||
"""Creates a DSMR asyncio protocol."""
|
"""Creates a DSMR asyncio protocol."""
|
||||||
|
|
||||||
if dsmr_version == "2.2":
|
if dsmr_version == '2.2':
|
||||||
specification = telegram_specifications.V2_2
|
specification = telegram_specifications.V2_2
|
||||||
serial_settings = SERIAL_SETTINGS_V2_2
|
serial_settings = SERIAL_SETTINGS_V2_2
|
||||||
elif dsmr_version == "4":
|
elif dsmr_version == '4':
|
||||||
specification = telegram_specifications.V4
|
specification = telegram_specifications.V4
|
||||||
serial_settings = SERIAL_SETTINGS_V4
|
serial_settings = SERIAL_SETTINGS_V4
|
||||||
elif dsmr_version == "5":
|
elif dsmr_version == '5':
|
||||||
specification = telegram_specifications.V5
|
specification = telegram_specifications.V5
|
||||||
serial_settings = SERIAL_SETTINGS_V5
|
serial_settings = SERIAL_SETTINGS_V5
|
||||||
elif dsmr_version == "5B":
|
elif dsmr_version == '5B':
|
||||||
specification = telegram_specifications.BELGIUM_FLUVIUS
|
specification = telegram_specifications.BELGIUM_FLUVIUS
|
||||||
serial_settings = SERIAL_SETTINGS_V5
|
serial_settings = SERIAL_SETTINGS_V5
|
||||||
elif dsmr_version == "5L":
|
elif dsmr_version == '5L':
|
||||||
specification = telegram_specifications.LUXEMBOURG_SMARTY
|
specification = telegram_specifications.LUXEMBOURG_SMARTY
|
||||||
serial_settings = SERIAL_SETTINGS_V5
|
serial_settings = SERIAL_SETTINGS_V5
|
||||||
elif dsmr_version == "5S":
|
elif dsmr_version == '5S':
|
||||||
specification = telegram_specifications.SWEDEN
|
specification = telegram_specifications.SWEDEN
|
||||||
serial_settings = SERIAL_SETTINGS_V5
|
serial_settings = SERIAL_SETTINGS_V5
|
||||||
elif dsmr_version == "Q3D":
|
elif dsmr_version == 'Q3D':
|
||||||
specification = telegram_specifications.Q3D
|
specification = telegram_specifications.Q3D
|
||||||
serial_settings = SERIAL_SETTINGS_V5
|
serial_settings = SERIAL_SETTINGS_V5
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError("No telegram parser found for version: %s",
|
||||||
"No telegram parser found for version: %s", dsmr_version
|
dsmr_version)
|
||||||
)
|
|
||||||
|
|
||||||
protocol = partial(
|
protocol = partial(DSMRProtocol, loop, TelegramParser(specification),
|
||||||
DSMRProtocol,
|
telegram_callback=telegram_callback, **kwargs)
|
||||||
loop,
|
|
||||||
TelegramParser(specification),
|
|
||||||
telegram_callback=telegram_callback,
|
|
||||||
**kwargs
|
|
||||||
)
|
|
||||||
|
|
||||||
return protocol, serial_settings
|
return protocol, serial_settings
|
||||||
|
|
||||||
@ -60,26 +51,22 @@ def create_dsmr_protocol(dsmr_version, telegram_callback, loop=None, **kwargs):
|
|||||||
def create_dsmr_reader(port, dsmr_version, telegram_callback, loop=None):
|
def create_dsmr_reader(port, dsmr_version, telegram_callback, loop=None):
|
||||||
"""Creates a DSMR asyncio protocol coroutine using serial port."""
|
"""Creates a DSMR asyncio protocol coroutine using serial port."""
|
||||||
protocol, serial_settings = create_dsmr_protocol(
|
protocol, serial_settings = create_dsmr_protocol(
|
||||||
dsmr_version, telegram_callback, loop=None
|
dsmr_version, telegram_callback, loop=None)
|
||||||
)
|
serial_settings['url'] = port
|
||||||
serial_settings["url"] = port
|
|
||||||
|
|
||||||
conn = create_serial_connection(loop, protocol, **serial_settings)
|
conn = create_serial_connection(loop, protocol, **serial_settings)
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
|
|
||||||
def create_tcp_dsmr_reader(
|
def create_tcp_dsmr_reader(host, port, dsmr_version,
|
||||||
host, port, dsmr_version, telegram_callback, loop=None, keep_alive_interval=None
|
telegram_callback, loop=None,
|
||||||
):
|
keep_alive_interval=None):
|
||||||
"""Creates a DSMR asyncio protocol coroutine using TCP connection."""
|
"""Creates a DSMR asyncio protocol coroutine using TCP connection."""
|
||||||
if not loop:
|
if not loop:
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
protocol, _ = create_dsmr_protocol(
|
protocol, _ = create_dsmr_protocol(
|
||||||
dsmr_version,
|
dsmr_version, telegram_callback, loop=loop,
|
||||||
telegram_callback,
|
keep_alive_interval=keep_alive_interval)
|
||||||
loop=loop,
|
|
||||||
keep_alive_interval=keep_alive_interval,
|
|
||||||
)
|
|
||||||
conn = loop.create_connection(protocol, host, port)
|
conn = loop.create_connection(protocol, host, port)
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
@ -90,9 +77,8 @@ class DSMRProtocol(asyncio.Protocol):
|
|||||||
transport = None
|
transport = None
|
||||||
telegram_callback = None
|
telegram_callback = None
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, loop, telegram_parser,
|
||||||
self, loop, telegram_parser, telegram_callback=None, keep_alive_interval=None
|
telegram_callback=None, keep_alive_interval=None):
|
||||||
):
|
|
||||||
"""Initialize class."""
|
"""Initialize class."""
|
||||||
self.loop = loop
|
self.loop = loop
|
||||||
self.log = logging.getLogger(__name__)
|
self.log = logging.getLogger(__name__)
|
||||||
@ -109,16 +95,16 @@ class DSMRProtocol(asyncio.Protocol):
|
|||||||
def connection_made(self, transport):
|
def connection_made(self, transport):
|
||||||
"""Just logging for now."""
|
"""Just logging for now."""
|
||||||
self.transport = transport
|
self.transport = transport
|
||||||
self.log.debug("connected")
|
self.log.debug('connected')
|
||||||
self._active = False
|
self._active = False
|
||||||
if self.loop and self._keep_alive_interval:
|
if self.loop and self._keep_alive_interval:
|
||||||
self.loop.call_later(self._keep_alive_interval, self.keep_alive)
|
self.loop.call_later(self._keep_alive_interval, self.keep_alive)
|
||||||
|
|
||||||
def data_received(self, data):
|
def data_received(self, data):
|
||||||
"""Add incoming data to buffer."""
|
"""Add incoming data to buffer."""
|
||||||
data = data.decode("ascii", errors="ignore")
|
data = data.decode('ascii', errors='ignore')
|
||||||
self._active = True
|
self._active = True
|
||||||
self.log.debug("received data: %s", data)
|
self.log.debug('received data: %s', data)
|
||||||
self.telegram_buffer.append(data)
|
self.telegram_buffer.append(data)
|
||||||
|
|
||||||
for telegram in self.telegram_buffer.get_all():
|
for telegram in self.telegram_buffer.get_all():
|
||||||
@ -126,26 +112,26 @@ class DSMRProtocol(asyncio.Protocol):
|
|||||||
|
|
||||||
def keep_alive(self):
|
def keep_alive(self):
|
||||||
if self._active:
|
if self._active:
|
||||||
self.log.debug("keep-alive checked")
|
self.log.debug('keep-alive checked')
|
||||||
self._active = False
|
self._active = False
|
||||||
if self.loop:
|
if self.loop:
|
||||||
self.loop.call_later(self._keep_alive_interval, self.keep_alive)
|
self.loop.call_later(self._keep_alive_interval, self.keep_alive)
|
||||||
else:
|
else:
|
||||||
self.log.warning("keep-alive check failed")
|
self.log.warning('keep-alive check failed')
|
||||||
if self.transport:
|
if self.transport:
|
||||||
self.transport.close()
|
self.transport.close()
|
||||||
|
|
||||||
def connection_lost(self, exc):
|
def connection_lost(self, exc):
|
||||||
"""Stop when connection is lost."""
|
"""Stop when connection is lost."""
|
||||||
if exc:
|
if exc:
|
||||||
self.log.exception("disconnected due to exception", exc_info=exc)
|
self.log.exception('disconnected due to exception', exc_info=exc)
|
||||||
else:
|
else:
|
||||||
self.log.info("disconnected because of close/abort.")
|
self.log.info('disconnected because of close/abort.')
|
||||||
self._closed.set()
|
self._closed.set()
|
||||||
|
|
||||||
def handle_telegram(self, telegram):
|
def handle_telegram(self, telegram):
|
||||||
"""Send off parsed telegram to handling callback."""
|
"""Send off parsed telegram to handling callback."""
|
||||||
self.log.debug("got telegram: %s", telegram)
|
self.log.debug('got telegram: %s', telegram)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
parsed_telegram = self.telegram_parser.parse(telegram)
|
parsed_telegram = self.telegram_parser.parse(telegram)
|
||||||
|
Loading…
Reference in New Issue
Block a user