def execute_at_creation(error, new_func, dict_functions=None): try: dict_controllers = parse_function_information() for channel in range(lcd_lines): new_channel = FunctionChannel() new_channel.name = "Set 0 Line {}".format(channel) new_channel.function_id = new_func.unique_id new_channel.channel = channel error, custom_options = custom_channel_options_return_json( error, dict_controllers, None, new_func.unique_id, channel, device=new_func.device, use_defaults=True) custom_options_dict = json.loads(custom_options) custom_options_dict["name"] = new_channel.name new_channel.custom_options = json.dumps(custom_options_dict) new_channel.save() except Exception: error.append("execute_at_modification() Error: {}".format( traceback.print_exc())) return error, new_func
def execute_at_modification(mod_function, request_form, custom_options_dict_presave, custom_options_channels_dict_presave, custom_options_dict_postsave, custom_options_channels_dict_postsave): """ This function allows you to view and modify the output and channel settings when the user clicks save on the user interface. Both the output and channel settings are passed to this function, as dictionaries. Additionally, both the pre-saved and post-saved options are available, as it's sometimes useful to know what settings changed and from what values. You can modify the post-saved options and these will be stored in the database. :param mod_output: The post-saved output database entry, minus the custom_options settings :param request_form: The requests.form object the user submitted :param custom_options_dict_presave: dict of pre-saved custom output options :param custom_options_channels_dict_presave: dict of pre-saved custom output channel options :param custom_options_dict_postsave: dict of post-saved custom output options :param custom_options_channels_dict_postsave: dict of post-saved custom output channel options :return: """ allow_saving = True page_refresh = False success = [] error = [] try: dict_controllers = parse_function_information() channels = FunctionChannel.query.filter( FunctionChannel.function_id == mod_function.unique_id) # Ensure name doesn't get overwritten selector_set = 0 selector_line = 0 for channel in range(channels.count()): custom_options_channels_dict_postsave[channel][ "name"] = "Set {} Line {}".format(selector_set, selector_line) selector_line += 1 if selector_line == lcd_lines: selector_set += 1 selector_line = 0 end_channel = custom_options_dict_postsave[ 'number_line_sets'] * lcd_lines # Increase number of channels if (custom_options_dict_postsave['number_line_sets'] > custom_options_dict_presave['number_line_sets']): page_refresh = True start_channel = channels.count() for index in range(start_channel, end_channel): new_channel = FunctionChannel() new_channel.name = "Set {} Line {}".format( math.trunc(index / lcd_lines), index - (math.trunc(index / lcd_lines) * lcd_lines)) new_channel.function_id = mod_function.unique_id new_channel.channel = index error, custom_options = custom_channel_options_return_json( error, dict_controllers, request_form, mod_function.unique_id, index, device=mod_function.device, use_defaults=False) custom_options_dict = json.loads(custom_options) custom_options_dict["name"] = new_channel.name new_channel.custom_options = json.dumps(custom_options_dict) new_channel.save() # Decrease number of channels elif (custom_options_dict_postsave['number_line_sets'] < custom_options_dict_presave['number_line_sets']): page_refresh = True for index, each_channel in enumerate(channels.all()): if index >= end_channel: delete_entry_with_id(FunctionChannel, each_channel.unique_id) except Exception: error.append("execute_at_modification() Error: {}".format( traceback.print_exc())) allow_saving = False for each_error in error: flash(each_error, 'error') for each_success in success: flash(each_success, 'success') return (allow_saving, page_refresh, mod_function, custom_options_dict_postsave, custom_options_channels_dict_postsave)
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: channels = FunctionChannel.query.filter( FunctionChannel.function_id == form_mod.function_id.data).all() 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 # Enable/disable Channels measurements = DeviceMeasurements.query.filter( DeviceMeasurements.device_id == form_mod.function_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 # 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_channel.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() # Parse pre-save custom options for function device and its channels try: custom_options_dict_presave = json.loads( mod_controller.custom_options) except: logger.error("Malformed JSON") custom_options_dict_presave = {} custom_options_channels_dict_presave = {} for each_channel in channels: if each_channel.custom_options and each_channel.custom_options != "{}": custom_options_channels_dict_presave[ each_channel.channel] = json.loads( each_channel.custom_options) else: custom_options_channels_dict_presave[each_channel.channel] = {} # Parse post-save custom options for function device and its channels error, custom_options_json_postsave = custom_options_return_json( error, dict_controllers, request_form=request_form, device=mod_controller.device, use_defaults=True) custom_options_dict_postsave = json.loads(custom_options_json_postsave) custom_options_channels_dict_postsave = {} for each_channel in channels: error, custom_options_channels_json_postsave_tmp = custom_channel_options_return_json( error, dict_controllers, request_form, form_mod.function_id.data, each_channel.channel, device=mod_controller.device, use_defaults=True) custom_options_channels_dict_postsave[ each_channel.channel] = json.loads( custom_options_channels_json_postsave_tmp) if 'execute_at_modification' in dict_controllers[ mod_controller.device]: # pass custom options to module prior to saving to database (allow_saving, mod_controller, custom_options_dict, custom_options_channels_dict) = dict_controllers[ mod_controller.device]['execute_at_modification']( mod_controller, request_form, custom_options_dict_presave, custom_options_channels_dict_presave, custom_options_dict_postsave, custom_options_channels_dict_postsave) custom_options = json.dumps( custom_options_dict) # Convert from dict to JSON string custom_channel_options = custom_options_channels_dict if not allow_saving: error.append( "execute_at_modification() would not allow function options to be saved" ) else: # Don't pass custom options to module custom_options = json.dumps(custom_options_dict_postsave) custom_channel_options = custom_options_channels_dict_postsave # Finally, save custom options for both function and channels mod_controller.custom_options = custom_options for each_channel in channels: if 'name' in custom_channel_options[each_channel.channel]: each_channel.name = custom_channel_options[ each_channel.channel]['name'] each_channel.custom_options = json.dumps( custom_channel_options[each_channel.channel]) if not error: 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 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 # Enable/disable Channels measurements = DeviceMeasurements.query.filter( DeviceMeasurements.device_id == form_mod.function_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 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_json( error, dict_controllers, request_form=request_form, device=mod_controller.device, use_defaults=True) mod_controller.custom_options = custom_options if not error: 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'))