Add destination information

This commit is contained in:
Lucidiot 2019-02-03 20:56:24 +01:00
parent 0023cd7292
commit 9c1d438eb1
No known key found for this signature in database
GPG Key ID: AE3F7205692FA205
2 changed files with 18 additions and 5 deletions

View File

@ -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'])

View File

@ -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'])