Beispiel #1
0
def save_device(db):
    if 'save' in request.form:
        devicename = request.form['name']
        devicegroups = []
        deviceid = request.form['deviceid']
        address = request.form['address']
        subid = request.form['subid']
        devicetype = request.form['devicetype']
        paired = 0
        for f in request.form:
            if f.startswith('checkbox_'):
                devicegroupid = f.replace('checkbox_', '')
                devicegroups.append(int(devicegroupid))

        # need to check we have at least 1 group here
        df = devicefactory.DeviceFactory()
        if deviceid == '':
            deviceid = df.create(db, devicename, address, subid, devicetype,
                                 paired, devicegroups)
        else:
            df.update(db, deviceid, devicename, address, subid, devicetype,
                      paired, devicegroups)

        return edit('device', deviceid)
    else:
        return device_list()
Beispiel #2
0
    def get_room(self, db, roomid):
        cur = db.execute('select room_id, name from rooms where room_id = ?',
                         [roomid])
        rooms = cur.fetchall()
        rm = rooms[0]
        cur = db.execute(
            'select room_roomgroup.roomgroup_id, room_roomgroup.room_id, roomgroups.roomgroup_name from room_roomgroup natural join roomgroups where room_roomgroup.room_id =?',
            [roomid])
        roomgrouprows = cur.fetchall()
        roomgroups = []

        for row in roomgrouprows:
            rmgrp = roomgroup.Roomgroup(row['roomgroup_id'],
                                        row['roomgroup_name'])
            roomgroups.append(rmgrp)

        cur = db.execute('select * from room_device where room_id = ?',
                         [roomid])
        devicerows = cur.fetchall()
        devices = []
        df = devicefactory.DeviceFactory()
        for row in devicerows:
            deviceid = row['device_id']
            device = df.get_device(db, deviceid)
            devices.append(device)

        return room.Room(rm['room_id'], rm['name'], roomgroups, devices)
Beispiel #3
0
def edit(item, id):
    if isloggedin() == True:
        db = get_db()
        uf = userfactory.Userfactory()
        user = uf.getuser(db, session['currentuserid'])
        item = item.lower()
        if item == 'room':
            rgf = roomgroupfactory.Roomgroupfactory()
            roomgroups = rgf.getroomgroups(db)
            rf = roomfactory.RoomFactory()
            room = rf.get_room(db, id)
            df = devicefactory.DeviceFactory()
            available_devices = df.get_available_devices(db, room)
            return render_template('addeditroom.html',
                                   user=user,
                                   roomgroups=roomgroups,
                                   room=room,
                                   available_devices=available_devices)
        elif item == 'roomgroup':
            pass
        elif item == 'device':
            dgf = devicegroupfactory.DeviceGroupFactory()
            devicegroups = dgf.get_devicegroups(db)
            df = devicefactory.DeviceFactory()
            device = df.get_device(db, id)
            return render_template('addeditdevice.html',
                                   user=user,
                                   devicegroups=devicegroups,
                                   device=device)
        elif item == 'user':
            edituser = uf.getuser(db, id)
            ugf = usergroupfactory.UserGroupFactory()
            usergroups = ugf.get_user_groups(db)
            return render_template('addedituser.html',
                                   user=user,
                                   edituser=edituser,
                                   usergroups=usergroups)
        elif item == 'devicegroup':
            dgf = devicegroupfactory.DeviceGroupFactory()
            devicegroup = dgf.get_devicegroup(db, id)
            return render_template('addeditdevicegroup.html',
                                   user=user,
                                   devicegroup=devicegroup)
    else:
        error = 'Not authorised'
        return render_template('home.html', error=error)
Beispiel #4
0
def device_list():
    if isloggedin() == True:
        uf = userfactory.Userfactory()
        db = get_db()
        user = uf.getuser(db, session['currentuserid'])
        df = devicefactory.DeviceFactory()
        devices = df.get_devices(db)
        return render_template('devicelist.html', user=user, devices=devices)
    else:
        error = 'Not authorised'
        return render_template('home.html', error=error)
Beispiel #5
0
    def __get_dg(self, db, devicegroup_id, devicegroup_name):

        cur = db.execute('select * from device_devicegroup where device_devicegroup.devicegroup_id = ?',
                         [devicegroup_id])
        rows = cur.fetchall()
        df = devicefactory.DeviceFactory()
        devices = []
        for row in rows:
            device = df.get_device(db, row['device_id'])
            devices.append(device)

        dg = devicegroup.DeviceGroup(devicegroup_id, devicegroup_name, devices)
        return dg
Beispiel #6
0
def unlink(item, id1, id2):
    if isloggedin() == True:
        db = get_db()
        item = item.lower()
        rf = roomfactory.RoomFactory()

        if item == 'roomdevice':
            room = rf.get_room(db, id1)
            df = devicefactory.DeviceFactory()
            device = df.get_device(db, id2)
            room.remove_device(db, device)
            return edit('room', id1)

        else:
            error = 'Not authorised'
            return render_template('home.html', error=error)
Beispiel #7
0
def auto_allocate():
    deviceaddress = 0
    subid = 0
    if isloggedin() == True:
        db = get_db()
        df = devicefactory.DeviceFactory()
        d = df.get_next_available_device_address(db)
        deviceaddress = d['deviceaddress']
        subid = d['subid']
        return jsonify(success=True,
                       deviceaddress=deviceaddress,
                       subid=subid,
                       error=None)
    else:
        error = 'Not authorised'
        return jsonify(success=False,
                       deviceaddress=deviceaddress,
                       subid=subid,
                       error=error)
Beispiel #8
0
def device_action(action, id):
    if isloggedin() == True:
        db = get_db()
        action = action.lower()

        if 'devicegroup' in action:
            dgf = devicegroupfactory.DeviceGroupFactory()
            devicegroup = dgf.get_devicegroup(db, id)
            if 'devicegroup_on' in action:
                devicegroup.on()
            elif 'devicegroup_off' in action:
                devicegroup.off()
        else:
            df = devicefactory.DeviceFactory()
            device = df.get_device(db, id)
            if action == 'pair':
                device.pair(db)
            elif action == 'unpair':
                device.unpair(db)
            elif action == 'on':
                device.on()
            elif action == 'off':
                device.off()
            elif action == 'dim_on':
                device.dim_on()
            elif action == 'dim_off':
                device.dim_off()
            elif action == 'dim_up':
                device.dim_up()
            elif action == 'dim_down':
                device.dim_down()
            elif 'dim_set_' in action:
                device.dim_set(action)

        return show_home()
    else:
        error = 'Not authorised'
        return render_template('home.html', error=error)
Beispiel #9
0
def delete(item, id):
    if isloggedin() == True:
        db = get_db()
        item = item.lower()
        if item == 'room':
            rf = roomfactory.RoomFactory()
            rf.delete_room(db, id)
            return room_list()
        elif item == 'roomgroup':
            pass
        elif item == 'device':
            df = devicefactory.DeviceFactory()
            df.delete(db, id)
            return device_list()
        elif item == 'user':
            return user_list()
        elif item == 'devicegroup':
            dgf = devicegroupfactory.DeviceGroupFactory()
            dgf.delete(db, id)
            return device_group_list()

    else:
        error = 'Not authorised'
        return render_template('home.html', error=error)