StructIO für export erweitert

Export und Importfunktionen für ersetzte IOs hinzugefügt
This commit is contained in:
2019-06-10 22:50:29 +02:00
parent 9d78d62b31
commit a13ba75bee
2 changed files with 72 additions and 1 deletions

View File

@@ -950,7 +950,7 @@ class StructIO(IOBase):
""" """
__slots__ = "__frm", "_parentio_address", "_parentio_defaultvalue", \ __slots__ = "__frm", "_parentio_address", "_parentio_defaultvalue", \
"_parentio_length" "_parentio_length", "_parentio_name"
def __init__(self, parentio, name, frm, **kwargs): def __init__(self, parentio, name, frm, **kwargs):
"""Erstellt einen IO mit struct-Formatierung. """Erstellt einen IO mit struct-Formatierung.
@@ -975,6 +975,9 @@ class StructIO(IOBase):
raise ValueError("byteorder must be 'little' or 'big'") raise ValueError("byteorder must be 'little' or 'big'")
bofrm = "<" if byteorder == "little" else ">" bofrm = "<" if byteorder == "little" else ">"
# Namen des parent fuer export merken
self._parentio_name = parentio._name
if frm == "?": if frm == "?":
bitaddress = kwargs.get("bit", 0) bitaddress = kwargs.get("bit", 0)
max_bits = parentio._length * 8 max_bits = parentio._length * 8
@@ -995,6 +998,9 @@ class StructIO(IOBase):
else: else:
bitaddress = "" bitaddress = ""
bitlength = struct.calcsize(bofrm + frm) * 8 bitlength = struct.calcsize(bofrm + frm) * 8
self._parentio_address = None
self._parentio_defaultvalue = None
self._parentio_length = None
# [name,default,anzbits,adressbyte,export,adressid,bmk,bitaddress] # [name,default,anzbits,adressbyte,export,adressid,bmk,bitaddress]
valuelist = [ valuelist = [

View File

@@ -5,6 +5,7 @@ __copyright__ = "Copyright (C) 2018 Sven Sager"
__license__ = "LGPLv3" __license__ = "LGPLv3"
import warnings import warnings
from configparser import ConfigParser
from json import load as jload from json import load as jload
from multiprocessing import cpu_count from multiprocessing import cpu_count
from os import access, F_OK, R_OK from os import access, F_OK, R_OK
@@ -519,6 +520,36 @@ class RevPiModIO(object):
if not self._monitoring: if not self._monitoring:
self.writeprocimg(dev) self.writeprocimg(dev)
def export_replaced_ios(self, filename):
"""Exportiert ersetzte IOs dieser Instanz.
Exportiert alle ersetzten IOs, welche mit .replace_io(...) angelegt
wurden. Die Datei kann z.B. fuer RevPiPyLoad verwndet werden um Daten
in den neuen Formaten per MQTT zu uebertragen oder mit RevPiPyControl
anzusehen.
@param filename Dateiname fuer Exportdatei"""
acheck(str, filename=filename)
cp = ConfigParser()
for io in self.io:
if isinstance(io, StructIO):
cp.add_section(io.name)
cp[io.name]["parentio_name"] = io._parentio_name
cp[io.name]["frm"] = io.frm
cp[io.name]["bmk"] = io.bmk
cp[io.name]["bitaddress"] = str(io._bitaddress)
cp[io.name]["byteorder"] = io._byteorder
cp[io.name]["defaultvalue"] = str(io.defaultvalue)
try:
with open(filename, "w") as fh:
cp.write(fh)
except Exception as e:
raise RuntimeError(
"could not write export file '{0}' | {1}"
"".format(filename, e)
)
def get_jconfigrsc(self): def get_jconfigrsc(self):
"""Laedt die piCtory Konfiguration und erstellt ein <class 'dict'>. """Laedt die piCtory Konfiguration und erstellt ein <class 'dict'>.
@return <class 'dict'> der piCtory Konfiguration""" @return <class 'dict'> der piCtory Konfiguration"""
@@ -584,6 +615,39 @@ class RevPiModIO(object):
signal(SIGINT, self.__evt_exit) signal(SIGINT, self.__evt_exit)
signal(SIGTERM, self.__evt_exit) signal(SIGTERM, self.__evt_exit)
def import_replaced_ios(self, filename):
"""Importiert ersetzte IOs in diese Instanz.
Importiert ersetzte IOs, welche vorher mit .export_replaced_ios(...)
in eine Datei exportiert worden sind. Diese IOs werden in dieser
Instanz wieder hergestellt.
@param filename Dateiname der Exportdatei"""
acheck(str, filename=filename)
cp = ConfigParser()
try:
with open(filename, "r") as fh:
cp.read_file(fh)
except Exception as e:
raise RuntimeError(
"could not read export file '{0}' | {1}"
"".format(filename, e)
)
for io in cp:
if io == "DEFAULT":
continue
self.io[cp[io].get("parentio_name")].replace_io(
io,
frm=cp[io].get("frm"),
bmk=cp[io].get("bmk"),
bit=cp[io].getint("bitaddress"),
byteorder=cp[io].get("byteorder", "little"),
defaultvalue=cp[io].getint("defaultvalue")
)
def mainloop(self, blocking=True, no_warn=False): def mainloop(self, blocking=True, no_warn=False):
"""Startet den Mainloop mit Eventueberwachung. """Startet den Mainloop mit Eventueberwachung.
@@ -977,5 +1041,6 @@ from . import device as devicemodule
from . import helper as helpermodule from . import helper as helpermodule
from . import summary as summarymodule from . import summary as summarymodule
from .io import IOList from .io import IOList
from .io import StructIO
from .netio import RevPiNetIODriver, RevPiNetIO from .netio import RevPiNetIODriver, RevPiNetIO