Exemple #1
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")