#!/usr/env/bin python

import requests
import hmac
import hashlib
import json
import os
import datetime

from throttle import throttle
from RPi import GPIO

GPIO.setmode(GPIO.BOARD)

HMAC_KEY = open(os.path.join(os.path.dirname(__file__),
                             ".hmac_key")).read().strip()

leds = {"r": 8, "g": 10, "b": 12}
switches = [Switch(p) for p in (22, 24, 26)]

for c, p in leds.iteritems():
    GPIO.setup(p, GPIO.OUT)

GPIO.output(leds["b"], False)

class Switch(object):
    def __init__(self, pin):
        GPIO.setup(pin, GPIO.IN)
        self.pin = pin
        self.prev_state = self._state

    @property
    def _state(self):
        return not GPIO.input(self.pin)

    @throttle(3, persist_return_value=True)
    def is_open(self):
        return self._state

    def has_changed_state(self):
        current_state = self.is_open()
        has_changed = current_state != self.prev_state
        self.prev_state = current_state
        return has_changed

def call_api(url_params):
    data = json.dumps(url_params)
    requests.post(os.getenv("ITTF_API_URL"), params={
        "data": data,
        "token": hmac.new(
            HMAC_KEY,
            data,
            hashlib.sha256
        ).hexdigest()
    })

if __name__ == "__main__":
    try:
        while True:
            url_params = []
            for i, s in enumerate(switches):
                if s.has_changed_state():
                    url_params.append({
                        "toilet_id": i,
                        "is_free": "yes" if not state else "no",
                        "timestamp": datetime.datetime.now().isoformat()
                    })
            if len(url_params):
                call_api(url_params)
            has_free = any(s.prev_state for s in switches)
            GPIO.output(RED_PIN, not has_free)
            GPIO.output(GREEN_PIN, has_free)

    except KeyboardInterrupt:
        pass

@@ -295,178 +295,8 @@
 ()%0A%0A
-leds = %7B%22r%22: 8, %22g%22: 10, %22b%22: 12%7D%0Aswitches = %5BSwitch(p) for p in (22, 24, 26)%5D%0A%0Afor c, p in leds.iteritems():%0A    GPIO.setup(p, GPIO.OUT)%0A%0AGPIO.output(leds%5B%22b%22%5D, False)%0A%0A
 clas
@@ -1064,16 +1064,186 @@
     %7D)%0A%0A
+leds = %7B%22r%22: 8, %22g%22: 10, %22b%22: 12%7D%0Aswitches = %5BSwitch(p) for p in (22, 24, 26)%5D%0A%0Afor c, p in leds.iteritems():%0A    GPIO.setup(p, GPIO.OUT)%0A%0AGPIO.output(leds%5B%22b%22%5D, False)%0A%0A
 if __nam
@@ -1727,18 +1727,17 @@
         
-ha
+i
 s_free =
@@ -1810,18 +1810,17 @@
 IN, not 
-ha
+i
 s_free)%0A
@@ -1858,18 +1858,17 @@
 IN, 
-ha
+i
 s_free)%0A
 %0A   
@@ -1863,17 +1863,16 @@
 s_free)%0A
-%0A
     exce

Fix code order