import json
import copy
from relay_api.core.relay import relay
from relay_api.conf.config import relays


def init_relays():
    for r in relays:
        relays[r]["object"] = relay(relays[r]["gpio"])
        relays[r]["state"] = relays[r]["object"].get_state()


def get_all_relays():
    relays_dict = __get_relay_dict()
    return json.dumps(relays_dict, indent=4)


def get_relay(relay_name):
    if relay_name not in relays:
        return None
    relay_dict = __get_relay_dict(relay_name)
    return json.dumps(relay_dict, indent=4)


def set_relay_on(relay_name):
    if relay_name not in relays:
        return None
    relays[relay_name]["object"].on()
    relays[relay_name]["state"] = relays[relay_name]["object"].get_state()
    relay_dict = __get_relay_dict(relay_name)
    return json.dumps(relay_dict, indent=4)


def set_relay_off(relay_name):
    if relay_name not in relays:
        return None
    relays[relay_name]["object"].off()
    relays[relay_name]["state"] = relays[relay_name]["object"].get_state()
    relay_dict = __get_relay_dict(relay_name)
    return json.dumps(relay_dict, indent=4)


def delete_relay(relay_name):
    if relay_name not in relays:
        return None
    relays[relay_name]["object"].cleanup()
    del(relays[relay_name])
    return json.dumps({relay_name: "deleted!"}, indent=4)


def create_relay(new_relay):
    try:
        r_name = new_relay["name"]
        r_gpio = new_relay["gpio"]
        r_type = new_relay["type"]
        r_desc = new_relay["desc"]
    except Exception:
        msg = {"name": "",
               "gpio": "",
               "type": "",
               "desc": ""}
        return json.dumps(msg, indent=4), True
    relays[r_name] = {"gpio": int(r_gpio),
                      "type": r_type,
                      "desc": r_desc}
    relays[r_name]["object"] = relay(relays[r_name]["gpio"])
    relays[r_name]["state"] = relays[r_name]["object"].get_state()
    relay_dict = __get_relay_dict(r_name)
    return json.dumps(relay_dict, indent=4), False


def __get_relay_dict(relay_name=None):
    if relay_name:
        relay_dict = copy.deepcopy(relays[relay_name])
        del(relay_dict["object"])
        return relay_dict
    relays_dict = copy.deepcopy(relays)
    for r in relays_dict:
        del(relays_dict[r]["object"])
    return relays_dict

@@ -56,16 +56,64 @@
 t relay%0A
+from relay_api.core.relay import MAX_RELAY_GPIO%0A
 from rel
@@ -1595,24 +1595,38 @@
 = %7B%22name%22: %22
+Must be unique
 %22,%0A         
@@ -1636,25 +1636,74 @@
    %22gpio%22: %22
-%22
+Must be an int between 0 - %22 + str(MAX_RELAY_GPIO)
 ,%0A          
@@ -1716,16 +1716,24 @@
 type%22: %22
+NO or NC
 %22,%0A     
@@ -1751,16 +1751,33 @@
 desc%22: %22
+Relay description
 %22%7D%0A     
@@ -1818,16 +1818,197 @@
 ), True%0A
+%0A    if r_name in relays:%0A        msg = %22Relay name is already in use!%22%0A        return json.dumps(%7B%22error%22: msg%7D, indent=4), True%0A%0A    try:%0A        r = relay(new_relay%5B%22gpio%22%5D)%0A    
     rela
@@ -2033,21 +2033,20 @@
 o%22: 
-int(
 r_gpio
-)
 ,%0A
+    
     
@@ -2093,32 +2093,36 @@
                 
+    
 %22desc%22: r_desc%7D%0A
@@ -2121,16 +2121,20 @@
 r_desc%7D%0A
+    
     rela
@@ -2161,36 +2161,97 @@
  = r
-elay(relays%5Br_name%5D%5B%22gpio%22%5D)
+%0A    except Exception as e:%0A        return json.dumps(%7B%22error%22: str(e)%7D, indent=4), True%0A
 %0A   

Fix issue related to commit a278f75461b917645e9c011d242983dd8bbb0e15