#!/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())