Files
storage_fresco/LEBE_tel.py
2019-05-23 13:36:01 +00:00

105 lines
3.3 KiB
Python

#!/usr/bin/python
"""
lifebeat (LEBE) - Telegram
.. module:: LEBE
.. todo: Test
.. _lebe-content:
Lifebeat
========
=============== ======== ======== ============================= ========================================================
ID Byte-Pos Type Value Description
=============== ======== ======== ============================= ========================================================
Sequence-Nr 0:2 SINT16 0..32767 Telegramcounter
Tel-Source 2:4 CHAR[2] ['TP','LV'] Source of Telegram (to be removed in next versions)
Tel-Destination 4:6 CHAR[2] ['TP','LV'] Destination of Telegram (to be removed in next versions)
Tel-Type 6:10 CHAR[4] 'LEBE' Ident of Telegram (prepared to multiplex Channels)
STATE 10:12 WORD irrelevant for future use
=============== ======== ======== ============================= ========================================================
"""
__date__ = "2006/02/15"
__email__ = "michi@rosstein.de"
__version__ = "$Revision: 1.1 $"[11:-2]
from telegram import TELEGRAM, TEL_LEBE
# String representation for a DT TPDU
DT_STRING = \
"""\
Length: %d
Number: 0x%02x
Contained Data: %s
"""
def fromstring (data):
"""
Decode the binary representation of a LEBE_Tel from the given
parameter and note the data in the instance of the object.
Parameters:
data: The binary form of the TPDU
"""
tel = TEL_LEBE ()
tel.attrib['nr'] = ord (data [0]) << 8 + ord (data [1])
tel.attrib['src'] = data [2:4]
tel.attrib['dst'] = data [4:6]
tel.attrib['type'] = data [6:10]
tel.attrib['state'] = data [10:12]
return tel
class TEL_LEBE (TELEGRAM):
def __init__ (self, nr = 0, src = 'LV', dst = 'TP', *args, **args2):
TELEGRAM.__init__(self)
self.code = TEL_LEBE
self.attrib['nr'] = nr
self.attrib['src'] = src
self.attrib['dst'] = dst
self.attrib['type'] = 'LEBE'
self.attrib['state'] = '%c%c' % (1, 0)
self.__buildstring ()
self.__recalclen ()
return
def identify (self):
"""
This method can be used to identify a TELEGRAM by string.
"""
return "LEBE TELEGRAM\n"
def __buildstring (self):
"""
Creates Datastring from Attribs
"""
self.data = chr ((self.attrib['nr'] >> 8) & 0xFF) + chr (self.attrib['nr'] & 0xFF) +\
self.attrib['src'] + self.attrib['dst'] + self.attrib['type'] + self.attrib['state']
def __recalclen (self):
"""
Return the length of the TPDU. This function is for
internal use only!
"""
self.len = 2
def __repr__ (self):
"""
Print a representation of the TPDU. Use this method via
`-pair to transfer it over the wire. This will just return
the header data and not the real data!
"""
# Note that the data is not included in the length
return "%c%c%c" % (2, self.code << 4, self.number) + self.data
def __str__ (self):
"""
Return a readable and quite verbose overview of the TPDU instance.
Use this for debugging purposes or if you're just curious.
"""
string = self.identify () # + DT_STRING % (self.len, self.number, self.data)
return string