Skip to content

Commit

Permalink
Merge pull request #232 from ropable/master
Browse files Browse the repository at this point in the history
Changes to API harvest functions, minor package updates
  • Loading branch information
ropable authored Mar 8, 2024
2 parents 1047f36 + aeb094f commit e644761
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 53 deletions.
28 changes: 14 additions & 14 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ django-geojson = "4.0.0"
unicodecsv = "0.14.1"
whitenoise = {version = "6.6.0", extras = ["brotli"]}
azure-storage-blob = "12.19.0"
sentry-sdk = {version = "1.40.5", extras = ["django"]}
sentry-sdk = {version = "1.40.6", extras = ["django"]}

[tool.poetry.group.dev.dependencies]
ipython = "^8.22.1"
ipython = "^8.22.2"
ipdb = "^0.13.13"
black = "^24.1.1"
black = "^24.2.0"
pre-commit = "^3.6.2"
mixer = "^7.2.2"

Expand Down
9 changes: 5 additions & 4 deletions tracking/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def has_add_permission(self, request, obj=None):
return False

def has_change_permission(self, request, obj=None):
if obj is not None and obj.source_device_type == 'tracplus':
# TracPlus and DFES vehicles have an external source of truth regarding metadata.
if obj is not None and obj.source_device_type in ["tracplus", "dfes"]:
return False
else:
return super(DeviceAdmin, self).has_change_permission(request, obj=obj)
Expand All @@ -62,13 +63,13 @@ def has_change_permission(self, request, obj=None):
class DeviceSSSAdmin(DeviceAdmin):

def add_view(self, request, obj=None):
return HttpResponseRedirect(reverse('sss_admin:tracking_device_changelist'))
return HttpResponseRedirect(reverse("sss_admin:tracking_device_changelist"))


class TrackingAdminSite(AdminSite):
site_header = 'SSS administration'
site_header = "SSS administration"
site_url = None


tracking_admin_site = TrackingAdminSite(name='sss_admin')
tracking_admin_site = TrackingAdminSite(name="sss_admin")
tracking_admin_site.register(Device, DeviceSSSAdmin)
58 changes: 26 additions & 32 deletions tracking/harvest.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def save_dplus(message):


def save_dfes_feed():
"""Download and process the DFES API endpoint (returns GeoJSON).
"""Download and process the DFES API endpoint (returns GeoJSON), create new devices, update existing.
"""
LOGGER.info("Querying DFES API")
resp = requests.get(url=settings.DFES_URL, auth=(settings.DFES_USER, settings.DFES_PASS))
Expand Down Expand Up @@ -317,25 +317,20 @@ def save_dfes_feed():
continue

device, created = Device.objects.get_or_create(deviceid=data["device_id"])
properties = feature["properties"]

if created:
created_device += 1
device.source_device_type = "dfes"
# Set some additional values on the Device from the feature data.
properties = feature["properties"]
device.callsign = properties["VehicleName"]
device.callsign_display = properties["VehicleName"]
device.model = properties["Model"]
if properties["Registration"]:
rego = properties["Registration"][:32].strip()
device.registration = f"DFES - {rego}"
else:
device.registration = "DFES - No Rego"
vehicle_type = properties["VehicleType"].strip()
if vehicle_type in DFES_SYMBOL_MAP:
device.symbol = DFES_SYMBOL_MAP[vehicle_type]
else:
device.symbol = "unknown"
device.save()
else:
updated_device += 1

device.callsign = properties["VehicleName"]
device.callsign_display = properties["VehicleName"]
if properties["Registration"]:
device.registration = properties["Registration"][:32].strip()
if properties["VehicleType"].strip() in DFES_SYMBOL_MAP:
device.symbol = DFES_SYMBOL_MAP[properties["VehicleType"].strip()]

seen = data["timestamp"]
point = f"POINT({data['longitude']} {data['latitude']})"
Expand All @@ -346,8 +341,8 @@ def save_dfes_feed():
device.heading = data["heading"]
device.velocity = data["velocity"]
device.altitude = data["altitude"]
device.save()
updated_device += 1

device.save()

loggedpoint, created = LoggedPoint.objects.get_or_create(device=device, seen=seen, point=point)
if created:
Expand All @@ -357,14 +352,12 @@ def save_dfes_feed():
loggedpoint.altitude = data["altitude"]
loggedpoint.save()
logged_points += 1
else:
skipped_device += 1

LOGGER.info(f"Created {created_device}, updated {updated_device}, skipped {skipped_device}, {logged_points} new logged points")


def save_tracplus_feed():
"""Query the TracPlus API, create logged points per device.
"""Query the TracPlus API, create logged points per device, update existing devices.
"""
LOGGER.info("Harvesting TracPlus feed")
content = requests.get(settings.TRACPLUS_URL).content.decode("utf-8")
Expand Down Expand Up @@ -395,17 +388,18 @@ def save_tracplus_feed():
continue

device, created = Device.objects.get_or_create(deviceid=data["device_id"])
rego = row["Asset Regn"][:32].strip()
symbol = row["Asset Type"] if row["Asset Type"] in tracplus_symbol_map else None

if created:
created_device += 1
device.source_device_type = "tracplus"
# Set some additional values on the Device from the CSV row data.
device.callsign = row["Asset Name"]
device.callsign_display = row["Asset Name"]
device.model = row["Asset Model"]
device.registration = row["Asset Regn"][:32]
if row["Asset Type"] in tracplus_symbol_map:
device.symbol = tracplus_symbol_map[row["Asset Type"]]
device.save()
else:
updated_device += 1

device.registration = rego
if symbol:
device.symbol = symbol

seen = data["timestamp"]
point = f"POINT({data['longitude']} {data['latitude']})"
Expand All @@ -416,8 +410,8 @@ def save_tracplus_feed():
device.heading = data["heading"]
device.velocity = data["velocity"]
device.altitude = data["altitude"]
updated_device += 1
device.save()

device.save()

loggedpoint, created = LoggedPoint.objects.get_or_create(device=device, seen=seen, point=point)
if created:
Expand Down

0 comments on commit e644761

Please sign in to comment.