Example #1
0
def getColor():
    device_id = bottle.request.params.get('id')

    if HomerHelper.idCheck('Devices', device_id):
        device_function = HomerHelper.getDeviceFunction(device_id)
        state_location = HomerHelper.lookupDeviceAttribute(device_function, 'State')
        color_location = HomerHelper.lookupDeviceAttribute(device_function, 'Color')
        if state_location is not None:
            if color_location is not None:
                try:
                    sql_query = "SELECT id, %s, %s FROM Devices " % (state_location, color_location)
                    sql_query += "WHERE id = %s"
                    cur = db.query(sql_query, device_id)

                    row = cur.fetchone()  # device IDs unique so just get one record

                    objects_list = []
                    d = collections.OrderedDict()
                    d['id'] = row['id']
                    d['mode'] = row[state_location]
                    d['color'] = row[color_location]
                    objects_list.append(d)

                    return json.dumps(objects_list)

                except MySQLdb.IntegrityError:
                    bottle.abort(400, "Doh! Device doesnt exist")
            else:
                bottle.abort(400, "Device does not have color attribute")
        else:
            bottle.abort(400, "Device does not have mode attribute")
    else:
        bottle.abort(400, "Doh! Device ID was not found")
Example #2
0
def setState():
    #MANDATORY FIELDS FROM REQUEST
    #DEVICE ID AS id
    #STATE TO ASSIGN TO DEVICE AS state

    device_id = bottle.request.params.get('id')  # get device ID from request
    device_state = bottle.request.params.get('state')  # get state to assign

    if HomerHelper.idCheck('Devices', device_id):  # validate the ID
        device_function = HomerHelper.getDeviceFunction(device_id)
        state_location = HomerHelper.lookupDeviceAttribute(device_function, 'State')
        if state_location is not None:
            try:
                sql_update = "UPDATE `Devices` SET %s " % state_location  # write it the same column in devices
                sql_update += "= %s  WHERE id = %s"
                db.query(sql_update, (device_state, device_id))

                #call state.action or scene here.  TBD
                device_name = HomerHelper.getDeviceName(device_id)  # lookup the name, needed for history
                device_type = HomerHelper.getDeviceType(device_id)  # lookup the device type needed for history
                history_event = "set state to: " + device_state
                HomerHelper.insert_history(device_name, device_id, history_event)
                return "OK"

            except MySQLdb.IntegrityError:
                bottle.abort(400, "Doh! Device doesnt exist")
        else:
            bottle.abort(400, "device does not have state attribute")
    else:
        bottle.abort(400, "Doh! Device ID was not found") # lookup the device function
Example #3
0
def viewBrightness():
    nav = HomerHelper.buildNav()
    cur = db.query("SELECT * FROM `Devices` WHERE function = %s", 'lamp')
    row = cur.fetchall()
    brightness_location = HomerHelper.lookupDeviceAttribute('lamp', 'Brightness')
    html = []  # parser for the information returned
    for col in row:
        devicename = col['name']
        devicename = devicename.replace(" ", "_")
        lampdevices = (col['name'], devicename, str((int(col[brightness_location])*100 )/255),col['id'])
        html.append(lampdevices)
    return bottle.template('Lamp', nav, devices=html)
Example #4
0
def viewaddDevice():
    nav = HomerHelper.buildNav()
    cur = db.query("SELECT * FROM `Devices` WHERE function = %s", 'lamp')
    row = cur.fetchall()
    brightness_location = HomerHelper.lookupDeviceAttribute(con, 'lamp', 'Brightness')
    html = []  # parser for the information returned
    for col in row:
        devicename = col['name']
        devicename = devicename.replace(" ", "_")
        lampdevices = (col['name'], devicename, str((int(col[brightness_location])*100 )/255),col['id'])
        print "viewbrightness"
        print lampdevices
        html.append(lampdevices)
    return bottle.template('brightness', devices=html, webroot=nav['webroot'], rooms=nav['rooms'], functions=nav['functions'])
Example #5
0
def setColor():
    device_id = bottle.request.params.get('id')
    device_color = bottle.request.params.get('color')
    device_state = bottle.request.params.get('mode')

    if HomerHelper.idCheck('Devices', device_id):  # validate the ID
        device_name = HomerHelper.getDeviceName(device_id)
        device_type = HomerHelper.getDeviceType(device_id)
        device_function = HomerHelper.getDeviceFunction(device_id)
        state_location = HomerHelper.lookupDeviceAttribute(device_function, 'State')
        if state_location is not None:
            try:
                sql_update = "UPDATE `Devices` SET %s " % state_location  # write it the same column in devices
                sql_update += "= %s  WHERE id = %s"
                db.query(sql_update, (device_state, device_id))
            except MySQLdb.IntegrityError:
                bottle.abort(400, "Doh! Device doesnt exist")

            color_location = HomerHelper.lookupDeviceAttribute(device_function, 'Color')
            if color_location is not None:
                try:
                    sql_update = "UPDATE `Devices` SET %s " % color_location  # write it the same column in devices
                    sql_update += "= %s  WHERE id = %s"
                    db.query(sql_update, (device_color, device_id))
                except MySQLdb.IntegrityError:
                    bottle.abort(400, "Doh! Device doesnt exist")
            else:
                bottle.abort(400, "Device does not have color attribute")
        else:
            bottle.abort(400, "Device does not have mode attribute")

        DeviceComunication.sendDeviceColor(device_id, device_color, device_state)
        history_event = "set color to: " + device_color + "set mode to " + device_state
        HomerHelper.insert_history(device_name, device_id, history_event)
        return "OK"
    else:
        bottle.abort(400, "Doh! Device ID was not found")
Example #6
0
def getBrightness():
    device_id = bottle.request.params.get('id')

    if HomerHelper.idCheck('Devices', device_id):
        device_function = HomerHelper.getDeviceFunction(device_id)
        brightness_location = HomerHelper.lookupDeviceAttribute(device_function, 'Brightness')
        if brightness_location is not None:   #"SELECT `value1` FROM Devices WHERE id = %s"
            try:
                sql_query = "SELECT %s FROM Devices " % brightness_location  # write it the same column in devices
                sql_query += "WHERE id = %s"
                cur = db.query(sql_query, device_id)

                row = cur.fetchone()  # device IDs unique so just get one record
                device_brightness = 'brightness', row[brightness_location]
                return json.dumps(device_brightness)
            except MySQLdb.IntegrityError:
                bottle.abort(400, "Doh! Device doesnt exist")
        else:
            bottle.abort(400, "device does not have brightness attribute")
    else:
        bottle.abort(400, "Doh! Device ID was not found")
Example #7
0
def setBrightness():
    #MANDATORY FIELDS FROM REQUEST
    #DEVICE ID AS id
    #STATE TO ASSIGN TO DEVICE AS state
    device_id = bottle.request.params.get('id')
    brightness = bottle.request.params.get('brightness')
    device_group = bottle.request.params.get('group')

    if device_group is None:
        if HomerHelper.idCheck('Devices', device_id):  # validate the ID
            device_function = HomerHelper.getDeviceFunction(device_id)
            brightness_location = HomerHelper.lookupDeviceAttribute(device_function, 'Brightness')
            if brightness_location is not None:
                try:
                    sql_update = "UPDATE `Devices` SET %s " % brightness_location  # write it the same column in devices
                    sql_update += "= %s  WHERE id = %s"
                    db.query(sql_update, (brightness, device_id))
                    #con.commit()

                    device_name = HomerHelper.getDeviceName(device_id)
                    device_type = HomerHelper.getDeviceType(device_id)
                    DeviceComunication.sendDeviceBrightness(device_id, device_type, brightness)
                    history_event = "set brightness to: " + brightness
                    HomerHelper.insert_history(device_name, device_id, history_event)


                    d = collections.OrderedDict()
                    d['name'] = device_name
                    d['brightness'] = brightness

                    return json.dumps(d)

                except MySQLdb.IntegrityError:
                    bottle.abort(400, "Doh! Device doesnt exist")
            else:
                bottle.abort(400, "Device does not have brightness attribute")
        else:
            bottle.abort(400, "Doh! Device ID was not found")
    else:
        if HomerHelper.deviceGroupCheck(device_group):  # validate the group
            try:
                sql_query = "SELECT `id` FROM Devices WHERE `group` = %s"  #get a list of device IDs from the group
                cur = db.query(sql_query, device_group)
                row = cur.fetchall()

                objects_list = []
                d = collections.OrderedDict()

                for device in row:
                    device_id = device['id']
                    print device_id
                    device_function = HomerHelper.getDeviceFunction(device_id)
                    brightness_location = HomerHelper.lookupDeviceAttribute(device_function, 'Brightness')
                    if brightness_location is not None:
                        try:
                            sql_update = "UPDATE `Devices` SET %s " % brightness_location  # write it the same column in devices
                            sql_update += "= %s  WHERE id = %s"
                            db.query(sql_update, (brightness, device_id))

                            device_name = HomerHelper.getDeviceName(device_id)
                            device_type = HomerHelper.getDeviceType(device_id)
                            DeviceComunication.sendDeviceBrightness(device_id, device_type, brightness)
                            history_event = "set brightness to: " + brightness
                            HomerHelper.insert_history(device_name, device_id, history_event)

                            d = collections.OrderedDict()
                            d['name'] = device_name
                            d['brightness'] = brightness
                            objects_list.append(d)

                            print objects_list

                        except MySQLdb.IntegrityError:
                            bottle.abort(400, "Something went horribly wrong, data loaded from DB was bad...uh oh")

                return json.dumps(objects_list)


            except MySQLdb.IntegrityError:
                    bottle.abort(400, "No Devices in the group: %s" % device_group)