Exemplo n.º 1
0
def page_graph_async():
    """ Generate graphs using asynchronous data retrieval """
    sensor = Sensor.query.all()
    sensor_choices = flaskutils.choices_sensors(sensor)
    sensor_choices_split = OrderedDict()
    for key in sensor_choices:
        order = key.split(",")
        # Separate sensor IDs and measurement types
        sensor_choices_split.update({order[0]: order[1]})

    selected_id = None
    selected_measure = None
    selected_unique_id = None

    if request.method == 'POST':
        selected_id = request.form['selected_measure'].split(",")[0]
        selected_measure = request.form['selected_measure'].split(",")[1]
        selected_unique_id = Sensor.query.filter(
            Sensor.unique_id == selected_id).first().unique_id

    return render_template('pages/graph-async.html',
                           sensor=sensor,
                           sensor_choices=sensor_choices,
                           sensor_choices_split=sensor_choices_split,
                           selected_id=selected_id,
                           selected_measure=selected_measure,
                           selected_unique_id=selected_unique_id)
Exemplo n.º 2
0
def page_graph_async():
    """ Generate graphs using asynchronous data retrieval """
    if not logged_in():
        return redirect(url_for('general_routes.home'))

    sensor = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], Sensor, entry='all')
    sensor_choices = flaskutils.choices_sensors(sensor)
    sensor_choices_split = OrderedDict()
    for key, _ in sensor_choices.iteritems():
        order = key.split(",")
        # Separate sensor IDs and measurement types
        sensor_choices_split.update({order[0]: order[1]})

    selected_id = None
    selected_measure = None
    if request.method == 'POST':
        selected_id = request.form['selected_measure'].split(",")[0]
        selected_measure = request.form['selected_measure'].split(",")[1]

    return render_template('pages/graph-async.html',
                           sensor=sensor,
                           sensor_choices=sensor_choices,
                           sensor_choices_split=sensor_choices_split,
                           selected_id=selected_id,
                           selected_measure=selected_measure)
Exemplo n.º 3
0
def page_export():
    """
    Export measurement data in CSV format
    """
    export_options = flaskforms.ExportOptions()
    relay = Relay.query.all()
    sensor = Sensor.query.all()
    relay_choices = flaskutils.choices_id_name(relay)
    sensor_choices = flaskutils.choices_sensors(sensor)

    if request.method == 'POST':
        start_time = export_options.date_range.data.split(' - ')[0]
        start_seconds = int(
            time.mktime(time.strptime(start_time, '%m/%d/%Y %H:%M')))
        end_time = export_options.date_range.data.split(' - ')[1]
        end_seconds = int(
            time.mktime(time.strptime(end_time, '%m/%d/%Y %H:%M')))

        unique_id = export_options.measurement.data.split(',')[0]
        measurement = export_options.measurement.data.split(',')[1]

        url = '/export_data/{meas}/{id}/{start}/{end}'.format(
            meas=measurement,
            id=unique_id,
            start=start_seconds,
            end=end_seconds)
        return redirect(url)

    # Generate start end end times for date/time picker
    end_picker = datetime.datetime.now().strftime('%m/%d/%Y %H:%M')
    start_picker = datetime.datetime.now() - datetime.timedelta(hours=6)
    start_picker = start_picker.strftime('%m/%d/%Y %H:%M')

    return render_template('tools/export.html',
                           start_picker=start_picker,
                           end_picker=end_picker,
                           exportOptions=export_options,
                           relay_choices=relay_choices,
                           sensor_choices=sensor_choices)
Exemplo n.º 4
0
def page_export():
    """
    Export measurement data in CSV format
    """
    if not logged_in():
        return redirect(url_for('general_routes.home'))

    export_options = flaskforms.ExportOptions()
    relay = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], Relay, entry='all')
    sensor = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], Sensor, entry='all')
    relay_choices = flaskutils.choices_id_name(relay)
    sensor_choices = flaskutils.choices_sensors(sensor)

    if request.method == 'POST':
        start_time = export_options.date_range.data.split(' - ')[0]
        start_seconds = int(time.mktime(
            time.strptime(start_time, '%m/%d/%Y %H:%M')))
        end_time = export_options.date_range.data.split(' - ')[1]
        end_seconds = int(time.mktime(
            time.strptime(end_time, '%m/%d/%Y %H:%M')))
        url = '/export_data/{meas}/{id}/{start}/{end}'.format(
            meas=export_options.measurement.data.split(',')[1],
            id=export_options.measurement.data.split(',')[0],
            start=start_seconds, end=end_seconds)
        return redirect(url)

    # Generate start end end times for date/time picker
    end_picker = datetime.datetime.now().strftime('%m/%d/%Y %H:%M')
    start_picker = datetime.datetime.now() - datetime.timedelta(hours=6)
    start_picker = start_picker.strftime('%m/%d/%Y %H:%M')

    return render_template('tools/export.html',
                           start_picker=start_picker,
                           end_picker=end_picker,
                           exportOptions=export_options,
                           relay_choices=relay_choices,
                           sensor_choices=sensor_choices)
Exemplo n.º 5
0
def page_graph():
    """
    Generate custom graphs to display sensor data retrieved from influxdb.
    """
    # Create form objects
    form_mod_graph = flaskforms.GraphMod()
    form_del_graph = flaskforms.GraphDel()
    form_order_graph = flaskforms.GraphOrder()
    form_add_graph = flaskforms.GraphAdd()

    # Retrieve the order to display graphs
    display_order = csv_to_list_of_int(DisplayOrder.query.first().graph)

    # Retrieve tables from SQL database
    graph = Graph.query.all()
    pid = PID.query.all()
    relay = Relay.query.all()
    sensor = Sensor.query.all()

    # Retrieve all choices to populate form drop-down menu
    pid_choices = flaskutils.choices_pids(pid)
    output_choices = flaskutils.choices_outputs(relay)
    sensor_choices = flaskutils.choices_sensors(sensor)

    # Add multi-select values as form choices, for validation
    form_mod_graph.pid_ids.choices = []
    form_mod_graph.relay_ids.choices = []
    form_mod_graph.sensor_ids.choices = []
    for key, value in pid_choices.items():
        form_mod_graph.pid_ids.choices.append((key, value))
    for key, value in output_choices.items():
        form_mod_graph.relay_ids.choices.append((key, value))
    for key, value in sensor_choices.items():
        form_mod_graph.sensor_ids.choices.append((key, value))

    # Generate dictionary of custom colors for each graph
    dict_colors = dict_custom_colors()

    # Detect which form on the page was submitted
    if request.method == 'POST':
        if not flaskutils.user_has_permission('edit_controllers'):
            return redirect(url_for('general_routes.home'))

        form_name = request.form['form-name']
        if form_name == 'modGraph':
            flaskutils.graph_mod(form_mod_graph, request.form)
        elif form_name == 'delGraph':
            flaskutils.graph_del(form_del_graph)
        elif form_order_graph.orderGraphUp.data:
            flaskutils.graph_reorder(form_order_graph.orderGraph_id.data,
                                     display_order, 'up')
        elif form_order_graph.orderGraphDown.data:
            flaskutils.graph_reorder(form_order_graph.orderGraph_id.data,
                                     display_order, 'down')
        elif form_name == 'addGraph':
            flaskutils.graph_add(form_add_graph, display_order)
        return redirect('/graph')

    return render_template('pages/graph.html',
                           graph=graph,
                           pid=pid,
                           relay=relay,
                           sensor=sensor,
                           pid_choices=pid_choices,
                           output_choices=output_choices,
                           sensor_choices=sensor_choices,
                           dict_colors=dict_colors,
                           measurement_units=MEASUREMENT_UNITS,
                           displayOrder=display_order,
                           form_mod_graph=form_mod_graph,
                           form_del_graph=form_del_graph,
                           form_order_graph=form_order_graph,
                           form_add_graph=form_add_graph)
Exemplo n.º 6
0
def page_graph():
    """
    Generate custom graphs to display sensor data retrieved from influxdb.
    """
    if not logged_in():
        return redirect(url_for('general_routes.home'))

    # Create form objects
    form_mod_graph = flaskforms.ModGraph()
    form_del_graph = flaskforms.DelGraph()
    form_order_graph = flaskforms.OrderGraph()
    form_add_graph = flaskforms.AddGraph()

    # Retrieve the order to display graphs
    display_order_unsplit = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], DisplayOrder, entry='first').graph
    if display_order_unsplit:
        display_order = display_order_unsplit.split(",")
    else:
        display_order = []

    # Retrieve tables from SQL database
    graph = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], Graph, entry='all')
    pid = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], PID, entry='all')
    relay = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], Relay, entry='all')
    sensor = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], Sensor, entry='all')

    # Retrieve all choices to populate form drop-down menu
    pid_choices = flaskutils.choices_id_name(pid)
    relay_choices = flaskutils.choices_id_name(relay)
    sensor_choices = flaskutils.choices_sensors(sensor)

    # Add multi-select values as form choices, for validation
    form_mod_graph.pidIDs.choices = []
    form_mod_graph.relayIDs.choices = []
    form_mod_graph.sensorIDs.choices = []
    for key, value in pid_choices.iteritems():
        form_mod_graph.pidIDs.choices.append((key, value))
    for key, value in relay_choices.iteritems():
        form_mod_graph.relayIDs.choices.append((key, value))
    for key, value in sensor_choices.iteritems():
        form_mod_graph.sensorIDs.choices.append((key, value))

    # Generate dictionary of custom colors for each graph
    dict_colors = dict_custom_colors(graph)

    # Detect which form on the page was submitted
    if request.method == 'POST':
        form_name = request.form['form-name']
        if form_name == 'modGraph':
            flaskutils.graph_mod(form_mod_graph, request.form)
        elif form_name == 'delGraph':
            flaskutils.graph_del(form_del_graph, display_order)
        elif form_name == 'orderGraph':
            flaskutils.graph_reorder(form_order_graph, display_order)
        elif form_name == 'addGraph':
            flaskutils.graph_add(form_add_graph, display_order)
        return redirect('/graph')

    return render_template('pages/graph.html',
                           graph=graph,
                           pid=pid,
                           relay=relay,
                           sensor=sensor,
                           pid_choices=pid_choices,
                           relay_choices=relay_choices,
                           sensor_choices=sensor_choices,
                           dict_colors=dict_colors,
                           measurement_units=MEASUREMENT_UNITS,
                           displayOrder=display_order,
                           form_mod_graph=form_mod_graph,
                           form_del_graph=form_del_graph,
                           form_order_graph=form_order_graph,
                           form_add_graph=form_add_graph)