Exemplo n.º 1
0
def delete_grainbin(grainbin):

    rrd = RRD(grainbin.device_id, 'grainbin',
              service_number=grainbin.bus_number)
    rrd.remove()

    db_session.query(Grainbin).filter_by(id=grainbin.id).delete()
    db_session.commit()
    return
Exemplo n.º 2
0
def edit_device(id):
    device = db_session.query(Device).filter_by(id=id).first()
    edit_form = EditDevice(request.form, device, prefix="edit_form")
    delete_form = DeleteDevice(request.form, device, prefix="delete_form")

    if request.method == 'POST':
        if 'edit_form-id' in request.form and edit_form.validate():
            device.name = edit_form.name.data
            device.location = edit_form.location.data
            device.description = edit_form.description.data
            if device.user_configured is False:
                device.user_configured = True
                setup_device(device)
            db_session.commit()
            flash("Device updated succesfully", 'success')
            return redirect(url_for(".show_device", id=id))

        elif 'delete_form-id' in request.form and delete_form.validate():
            delete_device(device)
            flash("Device removed.", 'success')
            return redirect(url_for('.device_overview'))

    return render_template('device-edit.html',
                           device=device,
                           edit_form=edit_form,
                           delete_form=delete_form)
Exemplo n.º 3
0
def inject_details():
    g.devices = db_session.query(Device).order_by(Device.creation_time.desc())\
                                        .all()
    g.connected_devices = []
    g.configured_devices = []
    g.new_devices = []

    g.grainbin_service = Config.getboolean('services', 'grainbin_service')
    if g.grainbin_service:
        g.grainbins = []
        g.configured_grainbins = []
        g.new_grainbins = []

    for dev in g.devices:
        if dev.connected:
            g.connected_devices.append(dev)
        if dev.user_configured:
            g.configured_devices.append(dev)
        else:
            g.new_devices.append(dev)
        if g.grainbin_service:
            if dev.user_configured:
                bins = dev.bins
                g.grainbins.extend(bins)
                for grainbin in bins:
                    if grainbin.user_configured:
                        g.configured_grainbins.append(grainbin)
                    else:
                        g.new_grainbins.append(grainbin)
    return dict()
Exemplo n.º 4
0
def edit_grainbin(id):
    grainbin = db_session.query(Grainbin).filter_by(id=id).first()
    edit_form = EditGrainbin(request.form, grainbin, prefix="edit_form")
    delete_form = DeleteGrainbin(request.form, grainbin, prefix="delete_form")

    if request.method == 'POST':
        if 'edit_form-id' in request.form and edit_form.validate():
            grainbin.name = edit_form.name.data
            grainbin.location = edit_form.location.data
            grainbin.description = edit_form.description.data
            if grainbin.user_configured is False:
                grainbin.user_configured = True
                setup_grainbin(grainbin)
            db_session.commit()
            flash("Grainbin updated succesfully", 'success')
            return redirect(url_for(".show_grainbin", id=id))

        elif 'delete_form-id' in request.form and delete_form.validate():
            delete_grainbin(grainbin)
            flash("Grainbin removed", 'success')
            return redirect(url_for('.grainbin_overview'))

    return render_template('grainbin-edit.html',
                           grainbin=grainbin,
                           edit_form=edit_form,
                           delete_form=delete_form)
Exemplo n.º 5
0
def delete_device(device):
    address = Config.get('services', 'client_address')
    port = Config.get('services', 'client_port')
    address = "tcp://" + address + ":" + port

    socket = zmq.Context().socket(zmq.REQ)
    socket.setsockopt(zmq.LINGER, 0)
    socket.connect(address)

    message = ['00000000', 'control', 'remove', device.id]
    mdp_request(socket, 'device', message, 1)

    rrd = RRD(device.id, 'device')
    rrd.remove(remove_folder=True)

    db_session.query(Device).filter_by(id=device.id).delete()
    db_session.commit()

    return
Exemplo n.º 6
0
def login():
    if request.method == "POST":

        attempted_username = request.form["username"]
        attempted_password = request.form["password"]

        user = db_session.query(User) \
                         .filter_by(username=attempted_username) \
                         .first()
        print(attempted_username)
        print(user)
        # check that the supplied user exists
        if user:
            # check that the supplied password matches
            if user.password == attempted_password:
                # user is now logged in
                if request.form.get("remember"):
                    session["remember"] = True
                    current_app.permanent_session_lifetime = timedelta(days=30)
                else:
                    session["remember"] = False
                    # how long the session will last
                    current_app.\
                        permanent_session_lifetime = timedelta(minutes=30)
                session["logged_in"] = True
                session["username"] = user.username
                flash("Last login: %s" % user.last_login, 'success')
                if session["remember"]:
                    expire = user.last_login + timedelta(days=30)
                    flash("Your session will expire on: %s" % expire,
                          'info')
                user.last_login = datetime.now()
                db_session.commit()
                return redirect(redirect_url())
            # password does not match
            else:
                flash("Invalid username or password. Please try again")
                return render_template("login.html")
        # the user does not exist
        else:
            flash("Invalid username or password. Please try again")
            return render_template("login.html")

    else:
        return render_template("login.html")
Exemplo n.º 7
0
def _grainbin_chart():

    if request.method == 'POST':
        grainbin_id = request.json['id']
        date_range = request.json['date']
        grainbin = db_session.query(Grainbin).filter_by(id=grainbin_id).first()
        names = []
        ykeys = []
        labels = []
        for sensor in grainbin.sensors:
            names.append({'name': sensor.id})
            ykeys.append(sensor.id)
            labels.append(sensor.id)

        rrd = RRD(grainbin.device_id, 'grainbin', grainbin.bus_number)

        data = rrd.retrieve(names, date_range)

        if not data:
            return "Error fetching data!"

        return jsonify(data=data, ykeys=ykeys, labels=labels)

    return "Success!"
Exemplo n.º 8
0
def show_device(id):
    device = db_session.query(Device).filter_by(id=id).first()

    return render_template('device.html', device=device)
Exemplo n.º 9
0
def show_grainbin(id):
    grainbin = db_session.query(Grainbin).filter_by(id=id).first()

    return render_template('grainbin.html', grainbin=grainbin)