Skip to content

Commit

Permalink
Merge pull request #2 from matmair/working-0.2.1
Browse files Browse the repository at this point in the history
Turn off LEDs
  • Loading branch information
matmair authored Jun 25, 2023
2 parents 9bebc32 + c3a9c37 commit a14a9df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "inventree-wled-locator"
version = "0.2.0"
version = "0.2.1"
description="Use WLED to locate InvenTree StockLocations."
readme = "README.md"
license = {text = "MIT license"}
Expand Down
53 changes: 37 additions & 16 deletions src/inventree_wled_locator/WledPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@

import requests
from common.notifications import NotificationBody
from django.conf.urls import url
from django.contrib.auth import get_user_model
from django.core.validators import MinValueValidator
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from InvenTree.helpers_model import notify_users
from plugin import InvenTreePlugin
from plugin.mixins import LocateMixin, SettingsMixin
from plugin.mixins import LocateMixin, SettingsMixin, UrlsMixin
from stock.models import StockLocation

logger = logging.getLogger('inventree')


class WledPlugin(LocateMixin, SettingsMixin, InvenTreePlugin):
class WledPlugin(UrlsMixin, LocateMixin, SettingsMixin, InvenTreePlugin):
"""Use WLED to locate InvenTree StockLocations.."""

NAME = 'WledPlugin'
Expand Down Expand Up @@ -43,19 +46,6 @@ class WledPlugin(LocateMixin, SettingsMixin, InvenTreePlugin):

superusers = list(get_user_model().objects.filter(is_superuser=True).all())

def set_led(self, target_led: int = None):
"""Turn on a specific LED."""
base_url = f'http://{self.get_setting("ADDRESS")}/json/state'
color_black = '000000'
color_marked = 'FF0000'

# Turn off all segments
requests.post(base_url, json={"seg": {"i": [0, self.get_setting("MAX_LEDS"), color_black]}})

# Turn on target led
if target_led:
requests.post(base_url, json={"seg": {"i": [target_led, color_marked]}})

def locate_stock_location(self, location_pk):
"""Locate a StockLocation.
Expand All @@ -68,11 +58,42 @@ def locate_stock_location(self, location_pk):
location = StockLocation.objects.get(pk=location_pk)
led_nbr = location.get_metadata('wled_led')
if led_nbr:
self.set_led(led_nbr)
self._set_led(led_nbr)
else:
# notify superusers that a location has no LED number
logger.error(f"Location ID {location_pk} has no WLED LED number!")
notify_users(self.superusers, location, StockLocation, content=self.NO_LED_NOTIFICATION)

except (ValueError, StockLocation.DoesNotExist): # pragma: no cover
logger.error(f"Location ID {location_pk} does not exist!")

def view_off(self, request):
"""Turn off all LEDs."""
self._set_led()
return redirect(self.settings_url)

def setup_urls(self):
"""Return the URLs defined by this plugin."""
return [
url(r'off/', self.view_off, name='off'),
]

def get_settings_content(self, request):
"""Add context to the settings panel."""
return f"""
<h3>WLED controlls</h3>
<p>Turn off all LEDs: <a href="{reverse('plugin:inventree-wled-locator:off')}">turn off</a></p>
"""

def _set_led(self, target_led: int = None):
"""Turn on a specific LED."""
base_url = f'http://{self.get_setting("ADDRESS")}/json/state'
color_black = '000000'
color_marked = 'FF0000'

# Turn off all segments
requests.post(base_url, json={"seg": {"i": [0, self.get_setting("MAX_LEDS"), color_black]}})

# Turn on target led
if target_led:
requests.post(base_url, json={"seg": {"i": [target_led, color_marked]}})

0 comments on commit a14a9df

Please sign in to comment.