#!/usr/bin/env python3 import os import asyncio import websockets import json from multiprocessing import Process, Value 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.120/0", data=b'0') requests.post("http://192.168.51.120/1", data=b'0') elif color=="BLANC": requests.post("http://192.168.51.120/0", data=b'0') requests.post("http://192.168.51.120/1", data=b'1') async def do_listen(): async with websockets.connect("wss://cof.ens.fr/ws/k-fet/open") 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())