예제 #1
0
파일: page_routes.py 프로젝트: qwekw/Mycodo
def page_input():
    """ Display sensor settings """
    # TCA9548A I2C multiplexer
    multiplexer_addresses = [
        '0x70', '0x71', '0x72', '0x73', '0x74', '0x75', '0x76', '0x77'
    ]
    multiplexer_channels = list(range(0, 9))

    camera = Camera.query.all()
    lcd = LCD.query.all()
    pid = PID.query.all()
    relay = Relay.query.all()
    sensor = Sensor.query.all()
    user = User.query.all()

    conditional = Conditional.query.filter(
        Conditional.conditional_type == 'sensor').all()
    conditional_actions = ConditionalActions.query.all()

    display_order = csv_to_list_of_int(DisplayOrder.query.first().sensor)

    form_add_sensor = flaskforms.SensorAdd()
    form_mod_sensor = flaskforms.SensorMod()

    form_conditional = flaskforms.Conditional()
    form_conditional_actions = flaskforms.ConditionalActions()

    # If DS18B20 sensors added, compile a list of detected sensors
    ds18b20_sensors = []
    if Sensor.query.filter(Sensor.device == 'DS18B20').count():
        try:
            for each_sensor in W1ThermSensor.get_available_sensors():
                ds18b20_sensors.append(each_sensor.id)
        except OSError:
            flash("Unable to detect sensors in '/sys/bus/w1/devices'", "error")

    # Create list of file names from the input_options directory
    # Used in generating the correct options for each sensor/device
    sensor_templates = []
    sensor_path = os.path.join(
        INSTALL_DIRECTORY, 'mycodo/mycodo_flask/templates/pages/input_options')
    for (_, _, file_names) in os.walk(sensor_path):
        sensor_templates.extend(file_names)
        break

    if request.method == 'POST':
        if not flaskutils.user_has_permission('edit_controllers'):
            return redirect(url_for('page_routes.page_input'))

        if form_add_sensor.sensorAddSubmit.data:
            flaskutils.sensor_add(form_add_sensor)
        elif form_mod_sensor.modSensorSubmit.data:
            flaskutils.sensor_mod(form_mod_sensor)
        elif form_mod_sensor.delSensorSubmit.data:
            flaskutils.sensor_del(form_mod_sensor)
        elif form_mod_sensor.orderSensorUp.data:
            flaskutils.sensor_reorder(form_mod_sensor.modSensor_id.data,
                                      display_order, 'up')
        elif form_mod_sensor.orderSensorDown.data:
            flaskutils.sensor_reorder(form_mod_sensor.modSensor_id.data,
                                      display_order, 'down')
        elif form_mod_sensor.activateSensorSubmit.data:
            flaskutils.sensor_activate(form_mod_sensor)
        elif form_mod_sensor.deactivateSensorSubmit.data:
            flaskutils.sensor_deactivate(form_mod_sensor)

        elif form_conditional.deactivate_cond.data:
            flaskutils.conditional_deactivate(form_conditional)
        elif form_conditional.activate_cond.data:
            flaskutils.conditional_activate(form_conditional)
        elif form_mod_sensor.sensorCondAddSubmit.data:
            flaskutils.conditional_add(
                'sensor', 1, sensor_id=form_mod_sensor.modSensor_id.data)
        elif form_conditional.delete_cond.data:
            flaskutils.conditional_mod(form_conditional, 'delete')
        elif form_conditional.save_cond.data:
            flaskutils.conditional_mod(form_conditional, 'modify')
        elif form_conditional_actions.add_action.data:
            flaskutils.conditional_action_add(form_conditional_actions)
        elif form_conditional_actions.save_action.data:
            flaskutils.conditional_action_mod(form_conditional_actions,
                                              'modify')
        elif form_conditional_actions.delete_action.data:
            flaskutils.conditional_action_mod(form_conditional_actions,
                                              'delete')
        return redirect(url_for('page_routes.page_input'))

    return render_template('pages/input.html',
                           camera=camera,
                           conditional=conditional,
                           conditional_actions=conditional_actions,
                           conditional_actions_list=CONDITIONAL_ACTIONS,
                           displayOrder=display_order,
                           ds18b20_sensors=ds18b20_sensors,
                           form_add_sensor=form_add_sensor,
                           form_conditional=form_conditional,
                           form_conditional_actions=form_conditional_actions,
                           form_mod_sensor=form_mod_sensor,
                           lcd=lcd,
                           measurements=MEASUREMENTS,
                           multiplexer_addresses=multiplexer_addresses,
                           multiplexer_channels=multiplexer_channels,
                           pid=pid,
                           relay=relay,
                           sensor=sensor,
                           sensor_templates=sensor_templates,
                           units=MEASUREMENT_UNITS,
                           user=user)
예제 #2
0
def page_sensor():
    """ Display sensor settings """
    if not logged_in():
        return redirect(url_for('general_routes.home'))

    lcd = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], LCD, 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')
    sensor_conditional = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], SensorConditional, entry='all')
    users = db_retrieve_table(
        current_app.config['USER_DB_PATH'], Users, entry='all')

    display_order_unsplit = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], DisplayOrder, entry='first').sensor
    if display_order_unsplit:
        display_order = display_order_unsplit.split(",")
    else:
        display_order = []

    form_add_sensor = flaskforms.AddSensor()
    form_mod_sensor = flaskforms.ModSensor()
    form_mod_sensor_cond = flaskforms.ModSensorConditional()

    # Create list of file names from the sensor_options directory
    # Used in generating the correct options for each sensor/device
    sensor_template_list = []
    sensor_path = "{path}/mycodo/mycodo_flask/templates/pages/sensor_options/".format(
        path=INSTALL_DIRECTORY)
    for (_, _, file_names) in os.walk(sensor_path):
        sensor_template_list.extend(file_names)
        break
    sensor_templates = []
    for each_file_name in sensor_template_list:
        sensor_templates.append(each_file_name.split(".")[0])

    if request.method == 'POST':
        form_name = request.form['form-name']
        if form_name == 'addSensor':
            flaskutils.sensor_add(form_add_sensor, display_order)
        elif form_name == 'modSensor':
            if form_mod_sensor.modSensorSubmit.data:
                flaskutils.sensor_mod(form_mod_sensor)
            elif form_mod_sensor.delSensorSubmit.data:
                flaskutils.sensor_del(form_mod_sensor, display_order)
            elif (form_mod_sensor.orderSensorUp.data or
                    form_mod_sensor.orderSensorDown.data):
                flaskutils.sensor_reorder(form_mod_sensor, display_order)
            elif form_mod_sensor.activateSensorSubmit.data:
                flaskutils.sensor_activate(form_mod_sensor)
            elif form_mod_sensor.deactivateSensorSubmit.data:
                flaskutils.sensor_deactivate(form_mod_sensor)
            elif form_mod_sensor.sensorCondAddSubmit.data:
                flaskutils.sensor_conditional_add(form_mod_sensor)
        elif form_name == 'modSensorConditional':
            flaskutils.sensor_conditional_mod(form_mod_sensor_cond)
        return redirect('/sensor')

    return render_template('pages/sensor.html',
                           lcd=lcd,
                           pid=pid,
                           relay=relay,
                           sensor=sensor,
                           sensor_conditional=sensor_conditional,
                           sensor_templates=sensor_templates,
                           users=users,
                           displayOrder=display_order,
                           form_add_sensor=form_add_sensor,
                           form_mod_sensor=form_mod_sensor,
                           form_mod_sensor_cond=form_mod_sensor_cond)