mirror of
https://github.com/naruxde/revpimodio2.git
synced 2025-11-08 22:03:53 +01:00
VirtualDevices in alter piCtory "adap." mit 64 beginnen (Kunbus Standard)
Konstanten um INP, OUT, MEM erweitert und io.Type gelöscht Änderung der Klassen auf neue Konstanten device.__getioiter angelegt für Rückgabe der IOs ohne None bei BIT-Bytes device.__getioiter in Rückgabefunktionen eingebaut io.DeadIO mit _parentdevice = None erweitert io.IOBase.__bool__ gibt nun False bei leeren Bytes aus io.StructIO.frm gibt nun nur einzelnen Zeichen aus (Byteorder über .byteorder)
This commit is contained in:
@@ -24,7 +24,7 @@ __all__ = [
|
||||
__author__ = "Sven Sager <akira@revpimodio.org>"
|
||||
__name__ = "revpimodio2"
|
||||
__package__ = "revpimodio2"
|
||||
__version__ = "2.0.5"
|
||||
__version__ = "2.0.6"
|
||||
|
||||
# Global package values
|
||||
OFF = 0
|
||||
@@ -33,6 +33,9 @@ RED = 2
|
||||
RISING = 31
|
||||
FALLING = 32
|
||||
BOTH = 33
|
||||
INP = 300
|
||||
OUT = 301
|
||||
MEM = 302
|
||||
|
||||
warnings.simplefilter(action="always")
|
||||
|
||||
@@ -58,6 +61,12 @@ def consttostr(value):
|
||||
return "FALLING"
|
||||
elif value == 33:
|
||||
return "BOTH"
|
||||
elif value == 300:
|
||||
return "INP"
|
||||
elif value == 301:
|
||||
return "OUT"
|
||||
elif value == 302:
|
||||
return "MEM"
|
||||
else:
|
||||
return ""
|
||||
|
||||
|
||||
@@ -114,16 +114,16 @@ class Device(object):
|
||||
# IOM-Objekte erstellen und Adressen in SLCs speichern
|
||||
if simulator:
|
||||
self._slc_inp = self._buildio(
|
||||
dict_device.pop("out"), iomodule.Type.INP)
|
||||
dict_device.pop("out"), INP)
|
||||
self._slc_out = self._buildio(
|
||||
dict_device.pop("inp"), iomodule.Type.OUT)
|
||||
dict_device.pop("inp"), OUT)
|
||||
else:
|
||||
self._slc_inp = self._buildio(
|
||||
dict_device.pop("inp"), iomodule.Type.INP)
|
||||
dict_device.pop("inp"), INP)
|
||||
self._slc_out = self._buildio(
|
||||
dict_device.pop("out"), iomodule.Type.OUT)
|
||||
dict_device.pop("out"), OUT)
|
||||
self._slc_mem = self._buildio(
|
||||
dict_device.pop("mem"), iomodule.Type.MEM
|
||||
dict_device.pop("mem"), MEM
|
||||
)
|
||||
|
||||
# SLCs mit offset berechnen
|
||||
@@ -180,9 +180,7 @@ class Device(object):
|
||||
def __iter__(self):
|
||||
"""Gibt Iterator aller IOs zurueck.
|
||||
@return <class 'iter'> aller IOs"""
|
||||
for lst_io in self._modio.io[self._slc_devoff]:
|
||||
for io in lst_io:
|
||||
yield io
|
||||
return self.__getioiter(self._slc_devoff)
|
||||
|
||||
def __len__(self):
|
||||
"""Gibt Anzahl der Bytes zurueck, die dieses Device belegt.
|
||||
@@ -194,11 +192,20 @@ class Device(object):
|
||||
@return Devicename"""
|
||||
return self._name
|
||||
|
||||
def __getioiter(self, ioslc):
|
||||
"""Gibt <class 'iter'> mit allen IOs zurueck.
|
||||
@param ioslc IO Abschnitt <class 'slice'>
|
||||
@return IOs als Iterator"""
|
||||
for lst_io in self._modio.io[ioslc]:
|
||||
for io in lst_io:
|
||||
if io is not None:
|
||||
yield io
|
||||
|
||||
def _buildio(self, dict_io, iotype):
|
||||
"""Erstellt aus der piCtory-Liste die IOs fuer dieses Device.
|
||||
|
||||
@param dict_io <class 'dict'>-Objekt aus piCtory Konfiguration
|
||||
@param iotype <class 'Type'> Wert
|
||||
@param iotype <class 'int'> Wert
|
||||
@return <class 'slice'> mit Start und Stop Position dieser IOs
|
||||
|
||||
"""
|
||||
@@ -211,11 +218,11 @@ class Device(object):
|
||||
# Neuen IO anlegen
|
||||
if bool(dict_io[key][7]) or self._producttype == 95:
|
||||
# Bei Bitwerten oder Core RevPiIOBase verwenden
|
||||
io_new = iomodule.IOBase(
|
||||
io_new = IOBase(
|
||||
self, dict_io[key], iotype, "little", False
|
||||
)
|
||||
else:
|
||||
io_new = iomodule.IntIO(
|
||||
io_new = IntIO(
|
||||
self, dict_io[key],
|
||||
iotype,
|
||||
"little",
|
||||
@@ -295,59 +302,64 @@ class Device(object):
|
||||
def get_allios(self):
|
||||
"""Gibt eine Liste aller Inputs und Outputs zurueck, keine MEMs.
|
||||
@return <class 'list'> Input und Output, keine MEMs"""
|
||||
lst_return = []
|
||||
for lst_io in self._modio.io[
|
||||
self._slc_inpoff.start:self._slc_outoff.stop]:
|
||||
lst_return += lst_io
|
||||
return lst_return
|
||||
return list(self.__getioiter(
|
||||
slice(self._slc_inpoff.start, self._slc_outoff.stop)
|
||||
))
|
||||
|
||||
def get_inputs(self):
|
||||
"""Gibt eine Liste aller Inputs zurueck.
|
||||
@return <class 'list'> Inputs"""
|
||||
lst_return = []
|
||||
for lst_io in self._modio.io[self._slc_inpoff]:
|
||||
lst_return += lst_io
|
||||
return lst_return
|
||||
return list(self.__getioiter(self._slc_inpoff))
|
||||
|
||||
def get_outputs(self):
|
||||
"""Gibt eine Liste aller Outputs zurueck.
|
||||
@return <class 'list'> Outputs"""
|
||||
lst_return = []
|
||||
for lst_io in self._modio.io[self._slc_outoff]:
|
||||
lst_return += lst_io
|
||||
return lst_return
|
||||
return list(self.__getioiter(self._slc_outoff))
|
||||
|
||||
def get_memories(self):
|
||||
"""Gibt eine Liste aller mems zurueck.
|
||||
@return <class 'list'> Mems"""
|
||||
lst_return = []
|
||||
for lst_io in self._modio.io[self._slc_memoff]:
|
||||
lst_return += lst_io
|
||||
return lst_return
|
||||
return list(self.__getioiter(self._slc_memoff))
|
||||
|
||||
def readprocimg(self):
|
||||
"""Alle Inputs fuer dieses Device vom Prozessabbild einlesen.
|
||||
|
||||
@return True, wenn erfolgreich ausgefuehrt
|
||||
@see revpimodio2.modio#RevPiModIO.readprocimg
|
||||
RevPiModIO.readprocimg()"""
|
||||
self._modio.readprocimg(self)
|
||||
RevPiModIO.readprocimg()
|
||||
|
||||
"""
|
||||
return self._modio.readprocimg(self)
|
||||
|
||||
def setdefaultvalues(self):
|
||||
"""Alle Outputbuffer fuer dieses Device auf default Werte setzen.
|
||||
|
||||
@return True, wenn erfolgreich ausgefuehrt
|
||||
@see revpimodio2.modio#RevPiModIO.setdefaultvalues
|
||||
RevPiModIO.setdefaultvalues()"""
|
||||
RevPiModIO.setdefaultvalues()
|
||||
|
||||
"""
|
||||
self._modio.setdefaultvalues(self)
|
||||
|
||||
def syncoutputs(self):
|
||||
"""Lesen aller Outputs im Prozessabbild fuer dieses Device.
|
||||
|
||||
@return True, wenn erfolgreich ausgefuehrt
|
||||
@see revpimodio2.modio#RevPiModIO.syncoutputs
|
||||
RevPiModIO.syncoutputs()"""
|
||||
self._modio.syncoutputs(self)
|
||||
RevPiModIO.syncoutputs()
|
||||
|
||||
"""
|
||||
return self._modio.syncoutputs(self)
|
||||
|
||||
def writeprocimg(self):
|
||||
"""Schreiben aller Outputs dieses Devices ins Prozessabbild.
|
||||
|
||||
@return True, wenn erfolgreich ausgefuehrt
|
||||
@see revpimodio2.modio#RevPiModIO.writeprocimg
|
||||
RevPiModIO.writeprocimg()"""
|
||||
self._modio.writeprocimg(self)
|
||||
RevPiModIO.writeprocimg()
|
||||
|
||||
"""
|
||||
return self._modio.writeprocimg(self)
|
||||
|
||||
length = property(__len__)
|
||||
name = property(__str__)
|
||||
@@ -616,9 +628,9 @@ class Gateway(Device):
|
||||
super().__init__(parent, dict_device, simulator)
|
||||
|
||||
self._dict_slc = {
|
||||
iomodule.Type.INP: self._slc_inp,
|
||||
iomodule.Type.OUT: self._slc_out,
|
||||
iomodule.Type.MEM: self._slc_mem
|
||||
INP: self._slc_inp,
|
||||
OUT: self._slc_out,
|
||||
MEM: self._slc_mem
|
||||
}
|
||||
|
||||
def get_rawbytes(self):
|
||||
@@ -678,4 +690,5 @@ class Virtual(Gateway):
|
||||
|
||||
|
||||
# Nachträglicher Import
|
||||
from . import io as iomodule
|
||||
from .io import IOBase, IntIO
|
||||
from revpimodio2 import INP, OUT, MEM
|
||||
|
||||
@@ -8,16 +8,7 @@
|
||||
"""RevPiModIO Modul fuer die Verwaltung der IOs."""
|
||||
import struct
|
||||
from threading import Event
|
||||
from revpimodio2 import RISING, FALLING, BOTH, consttostr
|
||||
|
||||
|
||||
class Type(object):
|
||||
|
||||
"""IO Typen."""
|
||||
|
||||
INP = 300
|
||||
OUT = 301
|
||||
MEM = 302
|
||||
from revpimodio2 import RISING, FALLING, BOTH, INP, OUT, MEM, consttostr
|
||||
|
||||
|
||||
class IOList(object):
|
||||
@@ -120,7 +111,7 @@ class IOList(object):
|
||||
]:
|
||||
object.__setattr__(self, key, value)
|
||||
else:
|
||||
raise TypeError(
|
||||
raise ValueError(
|
||||
"direct assignment is not supported - use .value Attribute"
|
||||
)
|
||||
|
||||
@@ -227,6 +218,8 @@ class DeadIO(object):
|
||||
@see #IOBase.replace_io replace_io(...)"""
|
||||
self.__deadio.replace_io(name, frm, **kwargs)
|
||||
|
||||
_parentdevice = property(lambda self: None)
|
||||
|
||||
|
||||
class IOBase(object):
|
||||
|
||||
@@ -248,7 +241,7 @@ class IOBase(object):
|
||||
|
||||
@param parentdevice Parentdevice auf dem der IO liegt
|
||||
@param valuelist Datenliste fuer Instantiierung
|
||||
@param iotype <class 'Type'> Wert
|
||||
@param iotype <class 'int'> Wert
|
||||
@param byteorder Byteorder 'little'/'big' fuer <class 'int'> Berechnung
|
||||
@param sigend Intberechnung mit Vorzeichen durchfuehren
|
||||
|
||||
@@ -312,7 +305,8 @@ class IOBase(object):
|
||||
)
|
||||
return bool(int_byte & 1 << self._bitaddress)
|
||||
else:
|
||||
return bool(self._parentdevice._ba_devdata[self._slc_address])
|
||||
return self._parentdevice._ba_devdata[self._slc_address] != \
|
||||
bytearray(self._length)
|
||||
|
||||
def __len__(self):
|
||||
"""Gibt die Bytelaenge des IO zurueck.
|
||||
@@ -335,8 +329,8 @@ class IOBase(object):
|
||||
return self._byteorder
|
||||
|
||||
def _get_iotype(self):
|
||||
"""Gibt io.Type zurueck.
|
||||
@return <class 'int'> io.Type"""
|
||||
"""Gibt io type zurueck.
|
||||
@return <class 'int'> io type"""
|
||||
return self._iotype
|
||||
|
||||
def get_defaultvalue(self):
|
||||
@@ -549,7 +543,7 @@ class IOBase(object):
|
||||
def set_value(self, value):
|
||||
"""Setzt den Wert des IOs.
|
||||
@param value IO-Wert als <class bytes'> oder <class 'bool'>"""
|
||||
if self._iotype == Type.OUT:
|
||||
if self._iotype == OUT:
|
||||
if self._bitaddress >= 0:
|
||||
# Versuchen egal welchen Typ in Bool zu konvertieren
|
||||
value = bool(value)
|
||||
@@ -591,7 +585,7 @@ class IOBase(object):
|
||||
"".format(self._name, type(value))
|
||||
)
|
||||
|
||||
elif self._iotype == Type.INP:
|
||||
elif self._iotype == INP:
|
||||
if self._parentdevice._modio._simulator:
|
||||
raise AttributeError(
|
||||
"can not write to output '{}' in simulator mode"
|
||||
@@ -601,7 +595,8 @@ class IOBase(object):
|
||||
raise AttributeError(
|
||||
"can not write to input '{}'".format(self._name)
|
||||
)
|
||||
elif self._iotype == Type.MEM:
|
||||
|
||||
elif self._iotype == MEM:
|
||||
raise AttributeError(
|
||||
"can not write to memory '{}'".format(self._name)
|
||||
)
|
||||
@@ -906,7 +901,7 @@ class StructIO(IOBase):
|
||||
def _get_frm(self):
|
||||
"""Ruft die struct Formatierung ab.
|
||||
@return struct Formatierung"""
|
||||
return self.__frm
|
||||
return self.__frm[1]
|
||||
|
||||
def _get_signed(self):
|
||||
"""Ruft ab, ob der Wert Vorzeichenbehaftet behandelt werden soll.
|
||||
|
||||
@@ -142,11 +142,11 @@ class RevPiModIO(object):
|
||||
err_names = []
|
||||
for device in sorted(lst_devices, key=lambda x: x["position"]):
|
||||
|
||||
# Bei VDev in alter piCtory Version, Position eindeutig machen
|
||||
# VDev alter piCtory Versionen auf Kunbus-Standard ändern
|
||||
if device["position"] == "adap.":
|
||||
device["position"] = -1
|
||||
device["position"] = 64
|
||||
while device["position"] in self.device:
|
||||
device["position"] -= 1
|
||||
device["position"] += 1
|
||||
|
||||
if device["type"] == "BASE":
|
||||
# Core
|
||||
@@ -275,7 +275,7 @@ class RevPiModIO(object):
|
||||
)
|
||||
warnings.warn(
|
||||
"got io error during {} and count {} errors now".format(
|
||||
self._ioerror, self._ioerror
|
||||
action, self._ioerror
|
||||
),
|
||||
RuntimeWarning
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user