Skip to content

Commit

Permalink
Merge pull request #756 from thorrak/dev_api
Browse files Browse the repository at this point in the history
Refactor communication between script caller & Fermentrack
  • Loading branch information
thorrak committed Aug 20, 2023
2 parents df354ea + 7ec9f1b commit 1da0c12
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-hub-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: 'Build & Push to Docker Hub (Testing)'
on:
push:
branches:
- script
- dev_api

jobs:
buildx:
Expand Down
11 changes: 11 additions & 0 deletions app/api/devices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.http import JsonResponse
from django.urls import reverse
from constance import config

from app.models import BrewPiDevice


def get_devices(req):
active_devices = list(BrewPiDevice.objects.filter(status=BrewPiDevice.STATUS_ACTIVE).values_list('id', flat=True))
return JsonResponse(active_devices, safe=False, json_dumps_params={'indent': 4})

34 changes: 32 additions & 2 deletions brewpi-script/fermentrack_config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import os
import sys
from pathlib import Path
from time import sleep
from typing import List

import requests
from django.core.exceptions import ObjectDoesNotExist

# Load up the Django specific stuff
Expand Down Expand Up @@ -126,6 +128,10 @@ def save_host_ip(self, ip_to_save):
brewpi_devices = app.models.BrewPiDevice.objects.filter(wifi_host_ip=ip_to_save)
except ObjectDoesNotExist:
return # cannot load the object from the database (deleted?)
except:
# To try to avoid the deadlocking
# TODO - Remove this catch-all
exit(1)

for brewpi_device in brewpi_devices:
brewpi_device.wifi_host_ip = None
Expand Down Expand Up @@ -167,6 +173,10 @@ def save_beer_log_point(self, beer_row):
brewpi_device = app.models.BrewPiDevice.objects.get(id=self.brewpi_device_id)
except ObjectDoesNotExist:
return # cannot load the object from the database (deleted?)
except:
# To try to avoid the deadlocking
# TODO - Remove this catch-all
exit(1)

new_log_point = app.models.BeerLogPoint()

Expand Down Expand Up @@ -196,6 +206,26 @@ def save_beer_log_point(self, beer_row):


def get_active_brewpi_devices() -> List[int]:
active_devices = app.models.BrewPiDevice.objects.filter(status=app.models.BrewPiDevice.STATUS_ACTIVE
).values_list('id', flat=True)
# TODO - Figure out how to get this to toggle between port 8000 and 5000 in a local environment vs a hosted one
url = "http://127.0.0.1:5000/api/devices/"

try:
response = requests.get(url)
except requests.exceptions.ConnectionError:
print(f"Unable to access Fermentrack API at {url} - Exiting.")
sleep(5)
exit(1)

# Ensure the request was successful and the content type is JSON
response.raise_for_status()
if "json" not in response.headers.get("content-type", "").lower():
raise ValueError("API response is not in JSON format")

# Decode the JSON content to a Python list
active_devices = response.json()

# Check if the result is a list of integers
if not all(isinstance(item, int) for item in active_devices):
raise ValueError("API response is not a list of integers")

return active_devices
3 changes: 2 additions & 1 deletion fermentrack_django/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import app.beer_views
import app.api.lcd
import app.api.clog
import app.api.devices

import firmware_flash.urls
import gravity.urls
Expand Down Expand Up @@ -107,7 +108,7 @@
url(r'^api/log/(?P<return_type>text|json)/(?P<device_type>\w{1,20})/(?P<logfile>stdout|stderr)/$', app.api.clog.get_device_log_combined, name="get_app_log"),
url(r'^api/log/(?P<return_type>text|json)/(?P<device_type>\w{1,20})/(?P<logfile>stdout|stderr)/l(?P<lines>\d{1,20})/$', app.api.clog.get_device_log_combined, name="get_app_log_lines"),
# api/gravity views are located in the gravity app

url(r'^api/devices/$', app.api.devices.get_devices, name="getDevices"), # For all devices/LCDs

# Login/Logout Views
url(r'^accounts/login/$', app.views.login, name='login'), # This is also settings.LOGIN_URL
Expand Down

0 comments on commit 1da0c12

Please sign in to comment.