示例#1
0
def dual_commands_to_sensor(input_sel,
                            first_cmd,
                            amount,
                            second_cmd,
                            current_stage,
                            next_stage=None):
    """
    Handles the Atlas Scientific sensor calibration.
    Sends two consecutive commands to the sensor board.
    Denies advancement to the next stage if any commands fail.
    Permits advancement to the next stage if all commands succeed.
    """
    if not utils_general.user_has_permission('edit_settings'):
        return redirect(url_for('routes_general.home'))

    return_error = None
    set_amount = None

    if first_cmd == 'temperature':
        unit = '°C'
        set_amount = amount
    elif first_cmd in ['ec_dry', 'ec_low', 'ec_high']:
        unit = 'μS'
        set_amount = amount
    else:
        unit = 'pH'

    atlas_command = AtlasScientificCommand(input_sel)

    sensor_measurement = atlas_command.get_sensor_measurement()

    first_status, first_return_str = atlas_command.calibrate(
        first_cmd, set_amount=set_amount)

    if isinstance(first_return_str, tuple):
        message_status = first_return_str[0]
        message_info = first_return_str[1]
        first_return_message = "{}".format(message_status)
        if message_info:
            first_return_message += ": {}".format(message_info)
    else:
        first_return_message = first_return_str

    first_info_str = "{act}: {lvl} ({amt} {unit}): {resp}".format(
        act=TRANSLATIONS['calibration']['title'],
        lvl=first_cmd,
        amt=amount,
        unit=unit,
        resp=first_return_message)

    if sensor_measurement != 'NA':
        first_info_str = "{} {}".format(sensor_measurement, first_info_str)

    if first_status:
        flash(first_info_str, "error")
        return_error = first_return_str
        return_stage = current_stage
    else:
        flash(first_info_str, "success")
        time.sleep(0.1)  # Add space between commands
        second_status, second_return_str = atlas_command.calibrate(second_cmd)

        if isinstance(second_return_str, tuple):
            message_status = second_return_str[0]
            message_info = second_return_str[1]
            second_return_message = "{}".format(message_status)
            if message_info:
                second_return_message += ": {}".format(message_info)
        else:
            second_return_message = second_return_str

        second_info_str = "{act}: {cmd}: {resp}".format(
            act=gettext('Command'), cmd=second_cmd, resp=second_return_message)

        if sensor_measurement != 'NA':
            second_info_str = "{} {}".format(sensor_measurement,
                                             second_info_str)

        if second_status:
            flash(second_info_str, "error")
            return_error = second_return_str
            return_stage = current_stage
        else:
            flash(second_info_str, "success")
            # Advance to the next stage
            return_stage = next_stage

    return return_stage, return_error
示例#2
0
def setup_atlas_ph():
    """
    Step-by-step tool for calibrating the Atlas Scientific pH sensor
    """
    if not utils_general.user_has_permission('edit_controllers'):
        return redirect(url_for('routes_general.home'))

    form_ph_calibrate = forms_calibration.CalibrationAtlasph()

    input_dev = Input.query.filter(Input.device == 'ATLAS_PH').all()

    ui_stage = 'start'
    backend_stage = None
    next_stage = None
    selected_input = None
    selected_point_calibration = None
    input_device_name = None
    complete_with_error = None

    if form_ph_calibrate.hidden_current_stage.data:
        backend_stage = form_ph_calibrate.hidden_current_stage.data

    if form_ph_calibrate.hidden_selected_point_calibration.data:
        selected_point_calibration = form_ph_calibrate.hidden_selected_point_calibration.data
    elif form_ph_calibrate.point_calibration.data:
        selected_point_calibration = form_ph_calibrate.point_calibration.data

    if selected_point_calibration:
        list_point_calibrations = selected_point_calibration.split(',')
    else:
        list_point_calibrations = []

    # Clear Calibration memory
    if form_ph_calibrate.clear_calibration.data:
        selected_input = Input.query.filter_by(
            unique_id=form_ph_calibrate.selected_input_id.data).first()
        atlas_command = AtlasScientificCommand(selected_input)
        status, message = atlas_command.calibrate('clear_calibration')

        sensor_measurement = atlas_command.get_sensor_measurement()

        if isinstance(message, tuple):
            message_status = message[0]
            message_info = message[1]
            message = "Calibration command returned from sensor: {}".format(
                message_status)
            if message_info:
                message += ": {}".format(message_info)
        else:
            message = "Calibration command returned from sensor: {}".format(
                message)

        if sensor_measurement != 'NA':
            message = "{} {}".format(sensor_measurement, message)

        if status:
            flash(message, "error")
        else:
            flash(message, "success")

    # Begin calibration from Selected input
    elif form_ph_calibrate.start_calibration.data:
        ui_stage = 'temperature'
        selected_input = Input.query.filter_by(
            unique_id=form_ph_calibrate.selected_input_id.data).first()
        dict_inputs = parse_input_information()
        list_inputs_sorted = generate_form_input_list(dict_inputs)
        if not selected_input:
            flash(
                'Input not found: {}'.format(
                    form_ph_calibrate.selected_input_id.data), 'error')
        else:
            for each_input in list_inputs_sorted:
                if selected_input.device == each_input[0]:
                    input_device_name = each_input[1]

    # Continue calibration from selected input
    elif (form_ph_calibrate.go_to_next_stage.data
          or form_ph_calibrate.go_to_last_stage.data
          or (backend_stage is not None
              and backend_stage not in ['start', 'temperature'])):
        selected_input = Input.query.filter_by(
            unique_id=form_ph_calibrate.hidden_input_id.data).first()
        dict_inputs = parse_input_information()
        list_inputs_sorted = generate_form_input_list(dict_inputs)
        for each_input in list_inputs_sorted:
            if selected_input.device == each_input[0]:
                input_device_name = each_input[1]

    if backend_stage in ['temperature', 'low', 'mid', 'high']:
        time.sleep(2)  # Sleep makes querying sensor more stable

        # Determine next ui_stage
        if backend_stage == 'temperature':
            next_stage = list_point_calibrations[0]
            logger.error("next_stage: {}".format(next_stage))
        else:
            current_stage_index = list_point_calibrations.index(backend_stage)
            if current_stage_index == len(list_point_calibrations) - 1:
                next_stage = 'complete'
            else:
                next_stage = list_point_calibrations[current_stage_index + 1]

    if form_ph_calibrate.clear_calibration.data:
        pass
    elif backend_stage == 'temperature':
        if form_ph_calibrate.temperature.data is None:
            flash(
                gettext(
                    "A valid temperature is required: %(temp)s is invalid.",
                    temp=form_ph_calibrate.temperature.data), "error")
            ui_stage = 'start'
        else:
            temp = '{temp:.2f}'.format(
                temp=float(form_ph_calibrate.temperature.data))
            ui_stage, complete_with_error = dual_commands_to_sensor(
                selected_input,
                'temperature',
                temp,
                'continuous',
                current_stage='temperature',
                next_stage=next_stage)
    elif backend_stage == 'low':
        ui_stage, complete_with_error = dual_commands_to_sensor(
            selected_input,
            'low',
            '4.0',
            'continuous',
            current_stage='low',
            next_stage=next_stage)
    elif backend_stage == 'mid':
        ui_stage, complete_with_error = dual_commands_to_sensor(
            selected_input,
            'mid',
            '7.0',
            'continuous',
            current_stage='mid',
            next_stage=next_stage)
    elif backend_stage == 'high':
        ui_stage, complete_with_error = dual_commands_to_sensor(
            selected_input,
            'high',
            '10.0',
            'end',
            current_stage='high',
            next_stage=next_stage)

    return render_template(
        'tools/calibration_options/atlas_ph.html',
        complete_with_error=complete_with_error,
        form_ph_calibrate=form_ph_calibrate,
        input=input_dev,
        input_device_name=input_device_name,
        selected_input=selected_input,
        selected_point_calibration=selected_point_calibration,
        ui_stage=ui_stage)