114 lines
4.0 KiB
Python
114 lines
4.0 KiB
Python
#!/usr/bin/env python2.6
|
|
#-*- coding: utf-8 -*-
|
|
|
|
__author__ = "Michael Rest"
|
|
__date__ = "2006/02/15"
|
|
__email__ = "michi@rosstein.de"
|
|
__version__ = "$Revision: 1.1 $"[11:-2]
|
|
|
|
|
|
from telegram import TELEGRAM, TEL_IPKT
|
|
from utility import *
|
|
# 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 IPKT from the given
|
|
parameter and note the data in the instance of the object.
|
|
|
|
Parameters:
|
|
data: The binary form of the TELEGRAM
|
|
"""
|
|
tel = TEL_IPKT ()
|
|
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['box_nr'] = data [12:12 + ord (data [11])]
|
|
#tel.attrib['box_nr'] = (ord (data [10]) << 24) + (ord (data [11]) << 16) + (ord (data [12]) << 8) + ord (data [13])
|
|
tel.attrib['location'] = data [22:26].strip ().lstrip ('0')
|
|
tel.attrib['gtin'] = data [28:28 + ord (data [27])].strip ().lstrip ('0')
|
|
tel.attrib['article'] = data [44:44 + ord (data [43])].strip ().lstrip ('0')
|
|
tel.attrib['duedate'] = data [56:56 + ord (data [55])].strip ().lstrip ('0')
|
|
tel.attrib['lotnr'] = data [66:66 + ord (data [65])].strip ().lstrip ('0')
|
|
tel.attrib['lotnr2'] = data [74:74 + ord (data [73])].replace (' ','0') #99,99 Kilos
|
|
tel.attrib['pieces'] = data [82:82 + ord (data [81])].replace (' ','0').lstrip ('0')
|
|
tel.attrib['destination'] = (ord (data [86]) << 8) + ord (data [87])
|
|
return tel
|
|
|
|
|
|
class TEL_IPKT (TELEGRAM):
|
|
def __init__ (self, nr = 0, src = 'LV', dst = 'TP', location = 'I001', boxnr = 123 , artnr ='12345', lotnr='123', duedate='123456', weight='12.34', pieces='1234', destination = 123, *args, **args2):
|
|
TELEGRAM.__init__(self)
|
|
self.code = TEL_IPKT
|
|
self.data = ''
|
|
self.attrib['nr'] = nr
|
|
self.attrib['src'] = src
|
|
self.attrib['dst'] = dst
|
|
self.attrib['type'] = 'IPKT'
|
|
self.attrib['box_nr'] = boxnr
|
|
self.attrib['location'] = location
|
|
self.attrib['gtin'] = artnr
|
|
self.attrib['article'] = artnr
|
|
self.attrib['duedate'] = duedate
|
|
self.attrib['lotnr'] = lotnr
|
|
self.attrib['lotnr2'] = lotnr
|
|
self.attrib['pieces'] = pieces
|
|
self.attrib['destination'] = destination
|
|
|
|
self.__buildstring ()
|
|
self.__recalclen ()
|
|
return
|
|
|
|
def identify (self):
|
|
"""
|
|
This method can be used to identify a TELEGRAM by string.
|
|
"""
|
|
return "IPKT TELEGRAM\n"
|
|
|
|
def __buildstring (self):
|
|
"""
|
|
Creates Datastring from Attributes
|
|
"""
|
|
self.data = chr ((self.attrib['nr'] >> 8) & 0xFF) + chr (self.attrib['nr'] & 0xFF) +\
|
|
self.attrib['src'] +\
|
|
self.attrib['dst'] +\
|
|
self.attrib['type'] +\
|
|
chr ((self.attrib['box_nr'] >> 24) & 0xFF) + chr ((self.attrib['box_nr'] >> 16) & 0xFF) + chr((self.attrib['box_nr'] >> 8) & 0xFF) + chr (self.attrib['box_nr'] & 0xFF) +\
|
|
self.attrib['location'] +\
|
|
fillchar (str (self.attrib['article']), 10) +\
|
|
fillchar (str (self.attrib['lotnr']), 10) +\
|
|
fillchar (str (self.attrib['pieces']), 10) +\
|
|
chr ((self.attrib['destination'] >> 8) & 0xFF) + chr (self.attrib['destination'] & 0xFF)
|
|
|
|
|
|
def __recalclen (self):
|
|
"""
|
|
Return the length of the TPDU. This function is for
|
|
internal use only!
|
|
"""
|
|
self.len = 2 + 4l
|
|
|
|
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
|