def controller_mod(form, request_form): """Modify a Custom Controller""" error = [] action = '{action} {controller}'.format( action=TRANSLATIONS['modify']['title'], controller=TRANSLATIONS['controller']['title']) dict_controllers = parse_controller_information() try: mod_controller = CustomController.query.filter( CustomController.unique_id == form.function_id.data).first() if mod_controller.is_activated: error.append( gettext("Deactivate controller before modifying its settings")) mod_controller.name = form.name.data mod_controller.log_level_debug = form.log_level_debug.data # Generate string to save from custom options error, custom_options = custom_options_return_string( error, dict_controllers, mod_controller, request_form) if not error: mod_controller.custom_options = custom_options db.session.commit() except sqlalchemy.exc.OperationalError as except_msg: error.append(except_msg) except sqlalchemy.exc.IntegrityError as except_msg: error.append(except_msg) except Exception as except_msg: error.append(except_msg) flash_success_errors(error, action, url_for('routes_page.page_function'))
def input_mod(form_mod, request_form): action = '{action} {controller}'.format( action=TRANSLATIONS['modify']['title'], controller=TRANSLATIONS['input']['title']) error = [] dict_inputs = parse_input_information() try: mod_input = Input.query.filter( Input.unique_id == form_mod.input_id.data).first() if mod_input.is_activated: error.append( gettext("Deactivate controller before modifying its settings")) if (mod_input.device == 'AM2315' and form_mod.period.data < 7): error.append( gettext("Choose a Read Period equal to or greater than 7. The " "AM2315 may become unresponsive if the period is " "below 7.")) if (mod_input.device != 'EDGE' and (mod_input.pre_output_duration and form_mod.period.data < mod_input.pre_output_duration)): error.append( gettext("The Read Period cannot be less than the Pre Output " "Duration")) if (form_mod.uart_location.data and not os.path.exists(form_mod.uart_location.data)): error.append( gettext( "Invalid device or improper permissions to read device")) if ('gpio_location' in dict_inputs[mod_input.device]['options_enabled'] and form_mod.gpio_location.data is None): error.append(gettext("Pin (GPIO) must be set")) mod_input.name = form_mod.name.data if form_mod.location.data: mod_input.location = form_mod.location.data if form_mod.i2c_location.data: mod_input.i2c_location = form_mod.i2c_location.data if form_mod.ftdi_location.data: mod_input.ftdi_location = form_mod.ftdi_location.data if form_mod.uart_location.data: mod_input.uart_location = form_mod.uart_location.data if form_mod.gpio_location.data and form_mod.gpio_location.data is not None: mod_input.gpio_location = form_mod.gpio_location.data if form_mod.power_output_id.data: mod_input.power_output_id = form_mod.power_output_id.data else: mod_input.power_output_id = None if form_mod.pre_output_id.data: mod_input.pre_output_id = form_mod.pre_output_id.data else: mod_input.pre_output_id = None # Enable/disable Channels measurements = DeviceMeasurements.query.filter( DeviceMeasurements.device_id == form_mod.input_id.data).all() if form_mod.measurements_enabled.data: for each_measurement in measurements: if each_measurement.unique_id in form_mod.measurements_enabled.data: each_measurement.is_enabled = True else: each_measurement.is_enabled = False mod_input.log_level_debug = form_mod.log_level_debug.data mod_input.i2c_bus = form_mod.i2c_bus.data mod_input.baud_rate = form_mod.baud_rate.data mod_input.pre_output_duration = form_mod.pre_output_duration.data mod_input.pre_output_during_measure = form_mod.pre_output_during_measure.data if form_mod.period.data: mod_input.period = form_mod.period.data if form_mod.start_offset.data: mod_input.start_offset = form_mod.start_offset.data mod_input.resolution = form_mod.resolution.data mod_input.resolution_2 = form_mod.resolution_2.data mod_input.sensitivity = form_mod.sensitivity.data mod_input.calibrate_sensor_measure = form_mod.calibrate_sensor_measure.data mod_input.cmd_command = form_mod.cmd_command.data mod_input.thermocouple_type = form_mod.thermocouple_type.data mod_input.ref_ohm = form_mod.ref_ohm.data # Serial options mod_input.pin_clock = form_mod.pin_clock.data mod_input.pin_cs = form_mod.pin_cs.data mod_input.pin_mosi = form_mod.pin_mosi.data mod_input.pin_miso = form_mod.pin_miso.data # Bluetooth options mod_input.bt_adapter = form_mod.bt_adapter.data mod_input.adc_gain = form_mod.adc_gain.data mod_input.adc_resolution = form_mod.adc_resolution.data mod_input.adc_sample_speed = form_mod.adc_sample_speed.data # Switch options mod_input.switch_edge = form_mod.switch_edge.data mod_input.switch_bouncetime = form_mod.switch_bouncetime.data mod_input.switch_reset_period = form_mod.switch_reset_period.data # PWM and RPM options mod_input.weighting = form_mod.weighting.data mod_input.rpm_pulses_per_rev = form_mod.rpm_pulses_per_rev.data mod_input.sample_time = form_mod.sample_time.data # Server options mod_input.port = form_mod.port.data mod_input.times_check = form_mod.times_check.data mod_input.deadline = form_mod.deadline.data # SHT sensor options if form_mod.sht_voltage.data: mod_input.sht_voltage = form_mod.sht_voltage.data if 'test_before_saving' in dict_inputs[mod_input.device]: (constraints_pass, constraints_errors, mod_input) = dict_inputs[mod_input.device]['test_before_saving']( mod_input, request_form) if constraints_pass: pass elif constraints_errors: for each_error in constraints_errors: flash(each_error, 'error') # Generate string to save from custom options error, custom_options = custom_options_return_string( error, dict_inputs, mod_input, request_form) if not error: mod_input.custom_options = custom_options # Add or delete channels for variable measurement Inputs if ('measurements_variable_amount' in dict_inputs[mod_input.device] and dict_inputs[ mod_input.device]['measurements_variable_amount']): channels = DeviceMeasurements.query.filter( DeviceMeasurements.device_id == form_mod.input_id.data) if channels.count() != form_mod.num_channels.data: # Delete channels if form_mod.num_channels.data < channels.count(): for index, each_channel in enumerate(channels.all()): if index + 1 >= channels.count(): delete_entry_with_id(DeviceMeasurements, each_channel.unique_id) # Add channels elif form_mod.num_channels.data > channels.count(): start_number = channels.count() for index in range(start_number, form_mod.num_channels.data): new_measurement = DeviceMeasurements() new_measurement.name = "" new_measurement.device_id = mod_input.unique_id new_measurement.measurement = "" new_measurement.unit = "" new_measurement.channel = index new_measurement.save() db.session.commit() except Exception as except_msg: error.append(except_msg) flash_success_errors(error, action, url_for('routes_page.page_data'))
def output_mod(form_output, request_form): action = '{action} {controller}'.format( action=TRANSLATIONS['modify']['title'], controller=TRANSLATIONS['output']['title']) error = [] dict_outputs = parse_output_information() try: mod_output = Output.query.filter( Output.unique_id == form_output.output_id.data).first() if (form_output.uart_location.data and not os.path.exists(form_output.uart_location.data)): error.append( gettext( "Invalid device or improper permissions to read device")) mod_output.name = form_output.name.data if form_output.location.data: mod_output.location = form_output.location.data if form_output.i2c_location.data: mod_output.i2c_location = form_output.i2c_location.data if form_output.ftdi_location.data: mod_output.ftdi_location = form_output.ftdi_location.data if form_output.uart_location.data: mod_output.uart_location = form_output.uart_location.data if form_output.gpio_location.data: if not is_int(form_output.gpio_location.data): error.append("BCM GPIO Pin must be an integer") else: mod_output.pin = form_output.gpio_location.data mod_output.i2c_bus = form_output.i2c_bus.data mod_output.baud_rate = form_output.baud_rate.data mod_output.log_level_debug = form_output.log_level_debug.data mod_output.amps = form_output.amps.data mod_output.trigger_functions_at_start = form_output.trigger_functions_at_start.data mod_output.location = form_output.location.data mod_output.output_mode = form_output.output_mode.data if form_output.on_state.data in ["0", "1"]: mod_output.on_state = bool(int(form_output.on_state.data)) # Wireless options mod_output.protocol = form_output.protocol.data mod_output.pulse_length = form_output.pulse_length.data # Command options mod_output.on_command = form_output.on_command.data mod_output.off_command = form_output.off_command.data mod_output.force_command = form_output.force_command.data # PWM options mod_output.pwm_hertz = form_output.pwm_hertz.data mod_output.pwm_library = form_output.pwm_library.data mod_output.pwm_invert_signal = form_output.pwm_invert_signal.data # Pump options if form_output.flow_rate.data: if (mod_output.output_type == 'atlas_ezo_pmp' and (form_output.flow_rate.data > 105 or form_output.flow_rate.data < 0.5)): error.append( "The Atlas Scientific Flow Rate must be between 0.5 and 105 ml/min" ) elif form_output.flow_rate.data <= 0: error.append("Flow Rate must be a positive value") else: mod_output.flow_rate = form_output.flow_rate.data mod_output.pwm_command = form_output.pwm_command.data mod_output.pwm_invert_signal = form_output.pwm_invert_signal.data mod_output.linux_command_user = form_output.linux_command_user.data if form_output.state_startup.data == '-1': mod_output.state_startup = None elif form_output.state_startup.data is not None: mod_output.state_startup = form_output.state_startup.data if (hasattr(form_output, 'startup_value') and form_output.startup_value.data): mod_output.startup_value = form_output.startup_value.data if form_output.state_shutdown.data == '-1': mod_output.state_shutdown = None elif form_output.state_shutdown.data is not None: mod_output.state_shutdown = form_output.state_shutdown.data if (hasattr(form_output, 'shutdown_value') and form_output.shutdown_value.data): mod_output.shutdown_value = form_output.shutdown_value.data if 'test_before_saving' in dict_outputs[mod_output.output_type]: (constraints_pass, constraints_errors, mod_input ) = dict_outputs[mod_output.output_type]['test_before_saving']( mod_output, request_form) if constraints_pass: pass elif constraints_errors: for each_error in constraints_errors: flash(each_error, 'error') # Generate string to save from custom options error, custom_options = custom_options_return_string( error, dict_outputs, mod_output, request_form) if not error: mod_output.custom_options = custom_options db.session.commit() manipulate_output('Modify', form_output.output_id.data) except Exception as except_msg: logger.exception(1) error.append(except_msg) flash_success_errors(error, action, url_for('routes_page.page_output'))
def controller_mod(form_mod, request_form): """Modify a Custom Function""" error = [] action = '{action} {controller}'.format( action=TRANSLATIONS['modify']['title'], controller=TRANSLATIONS['controller']['title']) dict_controllers = parse_function_information() try: mod_controller = CustomController.query.filter( CustomController.unique_id == form_mod.function_id.data).first() if mod_controller.is_activated: error.append( gettext("Deactivate controller before modifying its settings")) mod_controller.name = form_mod.name.data mod_controller.log_level_debug = form_mod.log_level_debug.data channels = FunctionChannel.query.filter( FunctionChannel.function_id == form_mod.function_id.data) # Add or delete channels for variable measurement Inputs if ('measurements_variable_amount' in dict_controllers[mod_controller.device] and dict_controllers[ mod_controller.device]['measurements_variable_amount']): measurements = DeviceMeasurements.query.filter( DeviceMeasurements.device_id == form_mod.function_id.data) if measurements.count() != form_mod.num_channels.data: # Delete measurements/channels if form_mod.num_channels.data < measurements.count(): for index, each_channel in enumerate(measurements.all()): if index + 1 >= measurements.count(): delete_entry_with_id(DeviceMeasurements, each_channel.unique_id) if ('channel_quantity_same_as_measurements' in dict_controllers[mod_controller.device] and dict_controllers[mod_controller.device] ["channel_quantity_same_as_measurements"]): if form_mod.num_channels.data < channels.count(): for index, each_channel in enumerate( channels.all()): if index + 1 >= channels.count(): delete_entry_with_id( FunctionChannel, each_channel.unique_id) # Add measurements/channels elif form_mod.num_channels.data > measurements.count(): start_number = measurements.count() for index in range(start_number, form_mod.num_channels.data): new_measurement = DeviceMeasurements() new_measurement.name = "" new_measurement.device_id = mod_controller.unique_id new_measurement.measurement = "" new_measurement.unit = "" new_measurement.channel = index new_measurement.save() if ('channel_quantity_same_as_measurements' in dict_controllers[mod_controller.device] and dict_controllers[mod_controller.device] ["channel_quantity_same_as_measurements"]): new_channel = FunctionChannel() new_channel.name = "" new_channel.function_id = mod_controller.unique_id new_measurement.channel = index error, custom_options = custom_channel_options_return_json( error, dict_controllers, request_form, mod_controller.unique_id, index, device=mod_controller.device, use_defaults=True) new_channel.custom_options = custom_options new_channel.save() # Generate string to save from custom options error, custom_options = custom_options_return_string( error, dict_controllers, mod_controller, request_form) if not error: mod_controller.custom_options = custom_options db.session.commit() except sqlalchemy.exc.OperationalError as except_msg: error.append(except_msg) except sqlalchemy.exc.IntegrityError as except_msg: error.append(except_msg) except Exception as except_msg: error.append(except_msg) flash_success_errors(error, action, url_for('routes_page.page_function'))