#!/usr/bin/env python
ipadres="192.168.1.57"
import asyncio
import json
import logging
import websockets
import RPi.GPIO as GPIO
from time import sleep
import sys
import time
import threading
logging.basicConfig()
STATE = {"value": "OK"}
USERS = set()
val=["aan","uit"]
pin=[False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False]
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(3,GPIO.OUT)
GPIO.output(3,GPIO.LOW)
GPIO.setup(5,GPIO.OUT)
GPIO.output(5,GPIO.LOW)
GPIO.setup(7,GPIO.OUT)
GPIO.output(7,GPIO.LOW)
GPIO.setup(8,GPIO.OUT)
GPIO.output(8,GPIO.LOW)
GPIO.setup(10,GPIO.OUT)
GPIO.output(10,GPIO.LOW)
GPIO.setup(11,GPIO.OUT)
GPIO.output(11,GPIO.LOW)
GPIO.setup(12,GPIO.OUT)
GPIO.output(12,GPIO.LOW)
GPIO.setup(13,GPIO.OUT)
GPIO.output(13,GPIO.LOW)
GPIO.setup(15,GPIO.OUT)
GPIO.output(15,GPIO.LOW)
GPIO.setup(16,GPIO.OUT)
GPIO.output(16,GPIO.LOW)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.LOW)
GPIO.setup(19,GPIO.OUT)
GPIO.output(19,GPIO.LOW)
GPIO.setup(21,GPIO.OUT)
GPIO.output(21,GPIO.LOW)
GPIO.setup(22,GPIO.OUT)
GPIO.output(22,GPIO.LOW)
GPIO.setup(23,GPIO.OUT)
GPIO.output(23,GPIO.LOW)
GPIO.setup(24,GPIO.OUT)
GPIO.output(24,GPIO.LOW)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26,GPIO.LOW)
#GPIO.setup(29,GPIO.OUT)
#GPIO.setup(31,GPIO.OUT)
#GPIO.setup(32,GPIO.OUT)
#GPIO.setup(33,GPIO.OUT)
#GPIO.setup(35,GPIO.OUT)
#GPIO.setup(36,GPIO.OUT)
#GPIO.setup(37,GPIO.OUT)
#GPIO.setup(38,GPIO.OUT)
#GPIO.setup(40,GPIO.OUT)

def pressed(t):
    print (t)
    mes=str(t)
    if pin[t]==False:
      GPIO.output(t,GPIO.HIGH)
      pin[t]=True
      print ("aan")
      mes+=" aan"
    else:
      GPIO.output(t,GPIO.LOW)
      pin[t]=False
      print("uit")
      mes+=" uit"
    return mes

def state_event():
    return json.dumps({"type": "state", **STATE})


def users_event():
    return json.dumps({"type": "users", "count": len(USERS)})


async def notify_state():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = state_event()
        await asyncio.wait([user.send(message) for user in USERS])


async def notify_users():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = users_event()
        await asyncio.wait([user.send(message) for user in USERS])

async def register(websocket):
    USERS.add(websocket)
    await notify_users()


async def unregister(websocket):
    USERS.remove(websocket)
    await notify_users()


async def counter(websocket, path):
    global val
    await register(websocket)
    try:
        await websocket.send(state_event())
        async for message in websocket:
            data = json.loads(message)
            if int(data["action"]) >2:
                mes=pressed(int(data["action"]))
                STATE["value"]=(mes)
                await notify_state()
            else:
                logging.error("unsupported event: {}", data)
    finally:
        await unregister(websocket)


start_server = websockets.serve(counter, ipadres, 6789)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
