Skip to content

Commit

Permalink
Create BrewPi-Script device discovery HTTP API
Browse files Browse the repository at this point in the history
  • Loading branch information
thorrak authored Aug 20, 2023
2 parents 3d7072a + 909a305 commit 539050f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 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})

31 changes: 28 additions & 3 deletions brewpi-script/fermentrack_config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 @@ -127,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 @@ -168,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 @@ -197,10 +206,26 @@ def save_beer_log_point(self, beer_row):


def get_active_brewpi_devices() -> List[int]:
# 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:
active_devices = app.models.BrewPiDevice.objects.filter(status=app.models.BrewPiDevice.STATUS_ACTIVE
).values_list('id', flat=True)
except:
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
10 changes: 10 additions & 0 deletions docs/source/develop/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/) because it was the first relatively standard format to pop up when I googled "changelog formats".


[2023-08-20] - Additional BrewPi-Script Bugfixes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Changed
-------

- Changed BrewPi-Script process manager to get list of devices via an HTTP API rather than directly querying the database
- Force BrewPi-Script process manager to restart in certain cases where a deadlock is detected


[2023-07-04] - BrewPi-Script Bugfixes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
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 539050f

Please sign in to comment.