import web #import the Web.py framework 
import Adafruit_BBIO.GPIO as GPIO #import the Adafruit GPIO Library

"""
We are creating a class called Appliance,
using classes will allow us to easily create
multiple objects without having to worry about creating different
variables in order to access different output objects. The Appliance
class is an object that can be turned HIGH or LOW
"""
class Appliance:

    #We initialize the class with a name and pin number 
    def __init__(self, name, pin_number):
        self.name = name
        self.pin_number = pin_number
        self.status = 0 #this variable is used to check if the object is on or off
        GPIO.setup(self.pin_number, GPIO.OUT) #We setup the pin number as an output

    #This function takes in a value of 1 or 0 to turn the Pin HIGH or LOW
    def output(self, status): 
        if(status == 1): # 1 is reserved for HIGH
            self.status = 1
            GPIO.output(self.pin_number, GPIO.HIGH)
        elif(status == 0): # 0 is reserved for LOW
            self.status = 0
            GPIO.output(self.pin_number, GPIO.LOW)

"""
We need to create a way for using variables that are dynamically created
in order to do this we are going to use a dictionary. A dictionary is
basically a group of values with a key or an identity which allows
you to differentiate each value
"""

d = {} #This is how we define a dictionary and we are naming the dictionary 'd'

"""
Since we are using a dictionary to store all of our objects in it,
we need to create a way of accessing them. We are going to use these
functions. The names are self explanatory.
"""

#This function is used to create an Appliance object and store it in the dictionary
def Access_Create(string, pin_number):
    d[string] = Appliance(string, pin_number)

#This function is used to access the name of the object and not the object itself
def Access_Name(string):
    return d[string].name

#This function is used to access the object itself and use its functions
def Access_Object(string):
    return d[string]

#This function is used to delete the object
def Access_Delete(string):
    d.pop(string)

""" Web.py requires us to route the urls and we
need to define them in this urls variable"""

urls = (
    '/add', 'add', #The url /add will be used to add an appliance object
    '/sw', 'sw',   #The url /sw will be used to switch an object HIGH or LOW
    '/delete', 'delete', #The url /delete will be used to delete an object
)


#In Web.py each page is programmed in a class of its own

class add: #The add page
    def GET(self):
        i = web.input(name=None, pin=None) #To get input using the GET request we need to use web.input
        Access_Create(i.name, i.pin) #We are creating an Appliance object with the Access_Create() function
        print "Created an Appliance: " + i.name + " at pin number: " + i.pin #print out the event to the console

class delete: #The delete page
    def GET(self):
        i = web.input(name=None) #Access the name of the appliance from the url
        item = Access_Object(i.name) #Use Access_Object() function to access the appliance object
        item.output(0) #Turn the Appliance object off
        Access_Delete(i.name) #Delete the Appliance object 
        print "Deleted object: " + i.name #print out the event to the console


class sw: #The sw page
    def GET(self):
        i = web.input(name=None) #Access the name of the appliance from the url
        item = Access_Object(i.name)#Use Access_Object() function to access the appliance object
        if(item.status == 0): #if the item is off turn it on
            item.output(1)
            print i.name + " is turned ON" #print out the event on the console
        elif(item.status == 1):#else if the item is on then turn it off
            item.output(0)
            print i.name + " is turned OFF" #print event to the console

"""
Don't worry too much about the next block of code all we are doing is telling WebPy to run using
the defined urls and classes
"""

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()
  




