Exemplo n.º 1
0
def page_output():
    """ Display relay status and config """
    camera = Camera.query.all()
    lcd = LCD.query.all()
    relay = Relay.query.all()
    user = User.query.all()

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

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

    form_add_relay = flaskforms.RelayAdd()
    form_mod_relay = flaskforms.RelayMod()

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

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

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

        if form_add_relay.relay_add.data:
            flaskutils.relay_add(form_add_relay)
        elif form_mod_relay.save.data:
            flaskutils.relay_mod(form_mod_relay)
        elif (form_mod_relay.turn_on.data or form_mod_relay.turn_off.data
              or form_mod_relay.on_submit.data):
            flaskutils.relay_on_off(form_mod_relay)
        elif form_mod_relay.delete.data:
            flaskutils.relay_del(form_mod_relay)
        elif form_mod_relay.order_up.data:
            flaskutils.relay_reorder(form_mod_relay.relay_id.data,
                                     display_order, 'up')
        elif form_mod_relay.order_down.data:
            flaskutils.relay_reorder(form_mod_relay.relay_id.data,
                                     display_order, 'down')
        elif form_conditional.add_cond.data:
            flaskutils.conditional_add(form_conditional.conditional_type.data,
                                       form_conditional.quantity.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.activate_cond.data:
            flaskutils.conditional_activate(form_conditional)
        elif form_conditional.deactivate_cond.data:
            flaskutils.conditional_deactivate(form_conditional)
        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_output'))

    return render_template('pages/output.html',
                           camera=camera,
                           conditional=conditional,
                           conditional_actions=conditional_actions,
                           conditional_actions_list=CONDITIONAL_ACTIONS,
                           displayOrder=display_order,
                           form_conditional=form_conditional,
                           form_conditional_actions=form_conditional_actions,
                           form_add_relay=form_add_relay,
                           form_mod_relay=form_mod_relay,
                           lcd=lcd,
                           relay=relay,
                           relay_templates=relay_templates,
                           user=user)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
def page_relay():
    """ Display relay status and config """
    camera = Camera.query.all()
    lcd = LCD.query.all()
    relay = Relay.query.all()
    user = User.query.all()

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

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

    form_add_relay = flaskforms.RelayAdd()
    form_mod_relay = flaskforms.RelayMod()

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

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

        if form_add_relay.relay_add.data:
            flaskutils.relay_add(form_add_relay)
        elif form_mod_relay.save.data:
            flaskutils.relay_mod(form_mod_relay)
        elif (form_mod_relay.turn_on.data or
                form_mod_relay.turn_off.data or
                form_mod_relay.sec_on_submit.data):
            flaskutils.relay_on_off(form_mod_relay)
        elif form_mod_relay.delete.data:
            flaskutils.relay_del(form_mod_relay)
        elif form_mod_relay.order_up.data:
            flaskutils.relay_reorder(form_mod_relay.relay_id.data,
                                     display_order, 'up')
        elif form_mod_relay.order_down.data:
            flaskutils.relay_reorder(form_mod_relay.relay_id.data,
                                     display_order, 'down')
        elif form_conditional.add_cond.data:
            flaskutils.conditional_add(form_conditional.conditional_type.data,
                                       form_conditional.quantity.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.activate_cond.data:
            flaskutils.conditional_activate(form_conditional)
        elif form_conditional.deactivate_cond.data:
            flaskutils.conditional_deactivate(form_conditional)
        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('/relay')

    return render_template('pages/relay.html',
                           camera=camera,
                           conditional=conditional,
                           conditional_actions=conditional_actions,
                           conditional_actions_list=CONDITIONAL_ACTIONS,
                           displayOrder=display_order,
                           form_conditional=form_conditional,
                           form_conditional_actions=form_conditional_actions,
                           form_add_relay=form_add_relay,
                           form_mod_relay=form_mod_relay,
                           lcd=lcd,
                           relay=relay,
                           user=user)