diff --git a/urbantz/delivery.py b/urbantz/delivery.py index f100307..fccc555 100644 --- a/urbantz/delivery.py +++ b/urbantz/delivery.py @@ -8,6 +8,19 @@ import requests DATE_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ' +class Location(DictObject): + """ + A delivery destination. + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + if 'location' not in self: + return + point = self.pop('location')['geometry'] + self['coordinates'] = Coordinates.fromJSON(point) + + class Delivery(JSONSerializable): """ A UrbanTZ delivery with a unique ID. @@ -141,9 +154,9 @@ class Delivery(JSONSerializable): self.destination = None """ - Coordinates of the delivery destination. + The delivery's destination. - :type: urbantz.base.Coordinates or None + :type: urbantz.delivery.Location or None """ self.recipient = None @@ -290,8 +303,7 @@ class Delivery(JSONSerializable): ] self.position = Coordinates.fromJSON(self.payload['position']) - self.destination = Coordinates.fromJSON( - self.payload['location']['location']['geometry']) + self.destination = Location.fromJSON(self.payload['location']) self.recipient = DictObject(self.payload['contact']) self.features = DictObject(self.payload['features']) self.template = DictObject(self.payload['template']) diff --git a/urbantz/tracker.py b/urbantz/tracker.py index fed35d8..6875ffe 100755 --- a/urbantz/tracker.py +++ b/urbantz/tracker.py @@ -33,9 +33,10 @@ def main(): raise SystemExit('Invalid delivery ID') print('Error while fetching data:', str(e)) + distance = delivery.position.distance(delivery.destination.coordinates) print("{} {} meters".format( delivery.last_updated.isoformat(), - round(delivery.position.distance(delivery.destination), 1), + round(distance, 1), )) sleep(options['frequency'])