Add lampion

This commit is contained in:
hackens-bot 2024-12-17 20:04:24 +01:00
parent 4baab56308
commit 9f06b15675
5 changed files with 91 additions and 4 deletions

View file

@ -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;

View file

@ -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" ];
};
}

16
pkgs/lampion/default.nix Normal file
View file

@ -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
]))
];
}

46
pkgs/lampion/lampion_kfet.py Executable file
View file

@ -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())

View file

@ -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 {};
})
]