diff --git a/machines/hackens-milieu/_configuration.nix b/machines/hackens-milieu/_configuration.nix index 52e5982..632b923 100644 --- a/machines/hackens-milieu/_configuration.nix +++ b/machines/hackens-milieu/_configuration.nix @@ -7,20 +7,21 @@ { imports = [ # Include the results of the hardware scan. - ./hardware-configuration.nix ./aarch64.nix ./audio.nix ./dns ./gnome.nix + ./hardware-configuration.nix ./i18n.nix + ./lampion.nix + ./networking.nix ./no-sleep.nix + ./pixiecore ./programs.nix + ./secrets ./system.nix ./users.nix ./vim.nix - ./pixiecore - ./networking.nix - ./secrets ]; boot.loader.efi.canTouchEfiVariables = true; diff --git a/machines/hackens-milieu/lampion.nix b/machines/hackens-milieu/lampion.nix new file mode 100644 index 0000000..c495c38 --- /dev/null +++ b/machines/hackens-milieu/lampion.nix @@ -0,0 +1,21 @@ +{ + pkgs, + config, + lib, + ... +}: { +systemd.services.lampion-kfet = +{ + script = '' + ${pkgs.lampion-kfet}/bin/lampion_kfet.py + ''; + serviceConfig = { + Restart = "always"; + RestartSec = 20; + DynamicUser = true; + }; + description = "Drives the physical K-FĂȘt indicator."; + after = [ "network.target" ]; + wantedBy = [ "multi-user.target" ]; +}; +} diff --git a/pkgs/lampion/default.nix b/pkgs/lampion/default.nix new file mode 100644 index 0000000..1da0daf --- /dev/null +++ b/pkgs/lampion/default.nix @@ -0,0 +1,16 @@ +{ + python3, + stdenv +}: +stdenv.mkDerivation { + name = "lampion-kfet"; + version = "1.0"; + src = ./.; + installPhase = "install -Dm755 ./lampion_kfet.py $out/bin/lampion_kfet.py"; + buildInputs = [ + (python3.withPackages (p: [ + p.websockets + p.requests + ])) + ]; +} diff --git a/pkgs/lampion/lampion_kfet.py b/pkgs/lampion/lampion_kfet.py new file mode 100755 index 0000000..2da6397 --- /dev/null +++ b/pkgs/lampion/lampion_kfet.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +import os +import asyncio +import websockets +import json +import requests + +status = Value('i', 2) + +# Channel 1 est ROSE +# Channel 0 est BLANC +# L'interface est ACTIVE LOW +def changecolor(color): + if color=="ROSE": + requests.post("http://192.168.51.249/0", data=b'1') + requests.post("http://192.168.51.249/1", data=b'0') + elif color=="BLANC": + requests.post("http://192.168.51.249/0", data=b'0') + requests.post("http://192.168.51.249/1", data=b'1') + + +async def do_listen(): + async with websockets.connect("wss://kfet.sinavir.fr/ws/") as websocket: + while True: + try: + message = await websocket.recv() + unpacked = json.loads(message) + if unpacked['status'] == "opened": + changecolor("ROSE") + print("Kfet ouverte") + else: + changecolor("BLANC") + print("Kfet fermee") + except websockets.ConnectionClosedOK: + print("Connection error") + changecolor("BLANC") + break + +async def main(): + while True: + await do_listen() + asyncio.sleep(20) + print("Restarting websocket") + +asyncio.run(main()) diff --git a/pkgs/overlays.nix b/pkgs/overlays.nix index 709ad01..6fd1789 100644 --- a/pkgs/overlays.nix +++ b/pkgs/overlays.nix @@ -16,4 +16,7 @@ ragb-backend = final.callPackage ./ragb-backend.nix { src = final.ragb-src; }; ragb-src = sources.ragb; }) + (final: prev: { + lampion-kfet = final.callPackage ./lampion {}; + }) ]