hackens-org-configurations/pkgs/lampion/lampion_kfet.py

48 lines
1.3 KiB
Python
Raw Normal View History

2024-12-17 20:04:24 +01:00
#!/usr/bin/env python3
import os
import asyncio
import websockets
import json
2024-12-29 13:58:29 +01:00
from multiprocessing import Process, Value
2024-12-17 20:04:24 +01:00
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":
2024-12-29 13:58:29 +01:00
requests.post("http://192.168.51.249/0", data=b'0')
2024-12-17 20:04:24 +01:00
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():
2025-03-02 22:50:31 +01:00
async with websockets.connect("wss://cof.ens.fr/ws/k-fet/open") as websocket:
2024-12-17 20:04:24 +01:00
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())