packet or dict can be loaded into packet objects
This commit is contained in:
parent
a04cb87a75
commit
50335ddf40
|
|
@ -1,10 +1,15 @@
|
||||||
|
from aprslib import parse
|
||||||
|
from aprslib.parsing.common import _parse_header
|
||||||
|
|
||||||
class APRSPacket(object):
|
class APRSPacket(object):
|
||||||
def __init__(self, fromcall, tocall, path=[]):
|
format = 'raw'
|
||||||
self.fromcall = fromcall
|
fromcall = 'N0CALL'
|
||||||
self.tocall = tocall
|
tocall = 'N0CALL'
|
||||||
self.path = path
|
path = []
|
||||||
|
|
||||||
|
def __init__(self, data=None):
|
||||||
|
if data:
|
||||||
|
self.load(data)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s(%s)>" % (
|
return "<%s(%s)>" % (
|
||||||
|
|
@ -12,6 +17,15 @@ class APRSPacket(object):
|
||||||
repr(str(self)),
|
repr(str(self)),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "%s:%s" % (
|
||||||
|
self._serialize_header(),
|
||||||
|
self._serialize_body(),
|
||||||
|
)
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return str(self) == str(other)
|
||||||
|
|
||||||
def _serialize_header(self):
|
def _serialize_header(self):
|
||||||
header = "%s>%s" % (self.fromcall, self.tocall)
|
header = "%s>%s" % (self.fromcall, self.tocall)
|
||||||
|
|
||||||
|
|
@ -23,9 +37,20 @@ class APRSPacket(object):
|
||||||
def _serialize_body(self):
|
def _serialize_body(self):
|
||||||
return getattr(self, 'body', '')
|
return getattr(self, 'body', '')
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return "%s:%s" % (
|
|
||||||
self._serialize_header(),
|
|
||||||
self._serialize_body(),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
def load(self, obj):
|
||||||
|
if not isinstance(obj, dict):
|
||||||
|
if self.format == 'raw':
|
||||||
|
header, self.body = obj.split(":", 1)
|
||||||
|
obj = _parse_header(header)
|
||||||
|
else:
|
||||||
|
obj = parse(obj)
|
||||||
|
|
||||||
|
for k, v in obj.items():
|
||||||
|
if k == 'format':
|
||||||
|
continue
|
||||||
|
if k == 'to' or k == 'from':
|
||||||
|
k += 'call'
|
||||||
|
|
||||||
|
if hasattr(self, k):
|
||||||
|
setattr(self, k, v)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ from aprslib.util import latitude_to_ddm, longitude_to_ddm, comment_altitude
|
||||||
|
|
||||||
|
|
||||||
class PositionReport(APRSPacket):
|
class PositionReport(APRSPacket):
|
||||||
|
format = 'uncompressed'
|
||||||
latitude = 0
|
latitude = 0
|
||||||
longitude = 0
|
longitude = 0
|
||||||
symbol_table = '/'
|
symbol_table = '/'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue