diff --git a/flake.nix b/flake.nix index 2c9fa13..92bbdc6 100644 --- a/flake.nix +++ b/flake.nix @@ -53,6 +53,8 @@ libaom = super.libaom.override { enableButteraugli = false; }; + + pin_control = self.callPackage ./pin_control { }; }) ]; diff --git a/ui/default.nix b/ui/default.nix index 6797106..28f3735 100644 --- a/ui/default.nix +++ b/ui/default.nix @@ -1,9 +1,17 @@ -{ buildPythonPackage, django_4 }: +{ buildPythonPackage, django_4, pin_control }: buildPythonPackage { pname = "homemanager"; version = "0.0.1"; src = ./.; + patchPhase = '' + substituteInPlace \ + main/views.py \ + --subst-var pin_control + ''; + + inherit pin_control; + propagatedBuildInputs = [ django_4 ]; } diff --git a/ui/homemanager/settings.py b/ui/homemanager/settings.py index df3756e..5b5a7b8 100644 --- a/ui/homemanager/settings.py +++ b/ui/homemanager/settings.py @@ -77,7 +77,7 @@ WSGI_APPLICATION = 'homemanager.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', + 'NAME': '/tmp/db.sqlite3', } } @@ -117,6 +117,7 @@ USE_TZ = True # https://docs.djangoproject.com/en/4.2/howto/static-files/ STATIC_URL = 'static/' +STATIC_ROOT = '/tmp/static' # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/ui/homemanager/urls.py b/ui/homemanager/urls.py index 13a4200..83ea464 100644 --- a/ui/homemanager/urls.py +++ b/ui/homemanager/urls.py @@ -18,7 +18,6 @@ from django.contrib import admin from django.urls import path, include urlpatterns = [ - path('admin/', admin.site.urls), path('', include('main.urls')), path("accounts/", include("django.contrib.auth.urls")), ] diff --git a/ui/homemanager/wsgi.py b/ui/homemanager/wsgi.py index 0c981e6..3adf15f 100644 --- a/ui/homemanager/wsgi.py +++ b/ui/homemanager/wsgi.py @@ -8,9 +8,19 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ """ import os - -from django.core.wsgi import get_wsgi_application - +from pathlib import Path os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'homemanager.settings') +data = Path("/tmp/db.sqlite3") +if not data.is_file(): + from django.core.management import call_command + + os.environ.setdefault('DJANGO_SUPERUSER_PASSWORD', 'password') + call_command("migrate --noinput") + call_command("collectstatic --noinput") + call_command("createsuperuser --noinput --username adp --email a@a.com") + call_command("createsuperuser --noinput --username fabian --email b@a.com") + + +from django.core.wsgi import get_wsgi_application application = get_wsgi_application() diff --git a/ui/main/forms.py b/ui/main/forms.py new file mode 100644 index 0000000..7a3fea3 --- /dev/null +++ b/ui/main/forms.py @@ -0,0 +1,21 @@ +from django import forms + +class Lights(forms.Form): + required_css_class = "list-group-item" + + MODES = [ + ('0', 'Apagar todo'), + ('1', 'Encender todo'), + ('2', 'Sala'), + ('3', 'Pasillo'), + ('4', 'Cocina'), + ('5', 'Cuarto 1'), + ('6', 'Cuarto 2'), + + ] + light = forms.ChoiceField( + widget=forms.RadioSelect, + choices=MODES, + label="Select lights to modify" + ) + \ No newline at end of file diff --git a/ui/main/static/1.jpg b/ui/main/static/1.jpg new file mode 100644 index 0000000..32de50e Binary files /dev/null and b/ui/main/static/1.jpg differ diff --git a/ui/main/static/2.jpg b/ui/main/static/2.jpg new file mode 100644 index 0000000..b454832 Binary files /dev/null and b/ui/main/static/2.jpg differ diff --git a/ui/main/static/3.jpg b/ui/main/static/3.jpg new file mode 100644 index 0000000..b5bc07c Binary files /dev/null and b/ui/main/static/3.jpg differ diff --git a/ui/main/templates/base.html b/ui/main/templates/base.html index 9a04e81..8a19203 100644 --- a/ui/main/templates/base.html +++ b/ui/main/templates/base.html @@ -22,9 +22,10 @@ diff --git a/ui/main/templates/home.html b/ui/main/templates/home.html index 308a126..53f031e 100644 --- a/ui/main/templates/home.html +++ b/ui/main/templates/home.html @@ -2,12 +2,35 @@ {% block title %} Home Manager {% endblock title%} {% block content %} + + {% if user.is_authenticated %} -
-

Welcome {{ user.username }}

+
+
+ {% csrf_token %} +
    + {{ form.as_ul }} +
+ +
+
+
+ {% load static %} + My image
{% else %} -

You are not logged in

- Log In +
+

You are not logged in

+

+ Log In +

+
{% endif %} + + {% endblock content %} \ No newline at end of file diff --git a/ui/main/views.py b/ui/main/views.py index 81ecc7d..4d78b20 100644 --- a/ui/main/views.py +++ b/ui/main/views.py @@ -1,7 +1,21 @@ from django.shortcuts import render +from .forms import Lights +import ctypes # Create your views here. -def home(request): - return render(request, 'home.html') +# pin_control = ctypes.CDLL("@pin_control@/lib/libpin_control.so") +def home(request): + if request.method == "POST": + form = Lights(request.POST) + if form.is_valid(): + opt = form.cleaned_data.get("light") + light_up_all() + return render(request, "home.html", {"form": form, "image": f"{opt}.jpg"}) + return render(request, "home.html", {"form": Lights(), "image": "1.jpg"}) + + +def light_up_all(): + print(f"INIT: {pin_control.init_gpio()}") + print(f"ALL_PINS: {pin_control.turn_on_all_pins()}") \ No newline at end of file