def pid_mod_unique_id(unique_id, state): """ Manipulate output (using unique ID) """ if not utils_general.user_has_permission('edit_controllers'): return 'Insufficient user permissions to manipulate PID' pid = PID.query.filter(PID.unique_id == unique_id).first() daemon = DaemonControl() if state == 'activate_pid': pid.is_activated = True pid.save() return_val, return_str = daemon.controller_activate('PID', pid.unique_id) return return_str elif state == 'deactivate_pid': pid.is_activated = False pid.is_paused = False pid.is_held = False pid.save() return_val, return_str = daemon.controller_deactivate('PID', pid.unique_id) return return_str elif state == 'pause_pid': pid.is_paused = True pid.save() if pid.is_activated: return_str = daemon.pid_pause(pid.unique_id) else: return_str = "PID Paused (Note: PID is not currently active)" return return_str elif state == 'hold_pid': pid.is_held = True pid.save() if pid.is_activated: return_str = daemon.pid_hold(pid.unique_id) else: return_str = "PID Held (Note: PID is not currently active)" return return_str elif state == 'resume_pid': pid.is_held = False pid.is_paused = False pid.save() if pid.is_activated: return_str = daemon.pid_resume(pid.unique_id) else: return_str = "PID Resumed (Note: PID is not currently active)" return return_str elif 'set_setpoint_pid' in state: pid.setpoint = state.split('|')[1] pid.save() if pid.is_activated: return_str = daemon.pid_set(pid.unique_id, 'setpoint', float(state.split('|')[1])) else: return_str = "PID Setpoint changed (Note: PID is not currently active)" return return_str
def controller_activate_deactivate(controller_action, controller_type, controller_id): """ Activate or deactivate controller :param controller_action: Activate or deactivate :type controller_action: str :param controller_type: The controller type (Conditional, LCD, Math, PID, Input) :type controller_type: str :param controller_id: Controller with ID to activate or deactivate :type controller_id: str """ if not user_has_permission('edit_controllers'): return redirect(url_for('routes_general.home')) error = [] activated = bool(controller_action == 'activate') translated_names = { "Conditional": TRANSLATIONS['conditional']['title'], "Input": TRANSLATIONS['input']['title'], "LCD": TRANSLATIONS['lcd']['title'], "Math": TRANSLATIONS['math']['title'], "PID": TRANSLATIONS['pid']['title'], "Trigger": TRANSLATIONS['trigger']['title'] } mod_controller = None if controller_type == 'Conditional': mod_controller = Conditional.query.filter( Conditional.unique_id == controller_id).first() elif controller_type == 'Input': mod_controller = Input.query.filter( Input.unique_id == controller_id).first() if activated: error = check_for_valid_unit_and_conversion(controller_id, error) elif controller_type == 'LCD': mod_controller = LCD.query.filter( LCD.unique_id == controller_id).first() elif controller_type == 'Math': mod_controller = Math.query.filter( Math.unique_id == controller_id).first() if activated: error = check_for_valid_unit_and_conversion(controller_id, error) elif controller_type == 'PID': mod_controller = PID.query.filter( PID.unique_id == controller_id).first() if activated: error = check_for_valid_unit_and_conversion(controller_id, error) elif controller_type == 'Trigger': mod_controller = Trigger.query.filter( Trigger.unique_id == controller_id).first() if mod_controller is None: flash("{type} Controller {id} doesn't exist".format( type=controller_type, id=controller_id), "error") return redirect(url_for('routes_general.home')) try: if not error: mod_controller.is_activated = activated db.session.commit() if activated: flash( "{} {} (SQL)".format( translated_names[controller_type], TRANSLATIONS['activate']['title']), "success") else: flash( "{} {} (SQL)".format( translated_names[controller_type], TRANSLATIONS['deactivate']['title']), "success") except Exception as except_msg: flash(gettext("Error: %(err)s", err='SQL: {msg}'.format(msg=except_msg)), "error") try: if not error: control = DaemonControl() if controller_action == 'activate': return_values = control.controller_activate( controller_type,controller_id) else: return_values = control.controller_deactivate( controller_type, controller_id) if return_values[0]: flash("{err}".format(err=return_values[1]), "error") else: flash("{err}".format(err=return_values[1]), "success") except Exception as except_msg: flash('{}: {}'.format(TRANSLATIONS['error']['title'], except_msg), "error") for each_error in error: flash(each_error, 'error')
def controller_activate_deactivate(controller_action, controller_type, controller_id): """ Activate or deactivate controller :param controller_action: Activate or deactivate :type controller_action: str :param controller_type: The controller type (Conditional, LCD, Math, PID, Input) :type controller_type: str :param controller_id: Controller with ID to activate or deactivate :type controller_id: str """ if not user_has_permission('edit_controllers'): return redirect(url_for('routes_general.home')) activated = bool(controller_action == 'activate') translated_names = { "Conditional": gettext("Conditional"), "Input": gettext("Input"), "LCD": gettext("LCD"), "Math": gettext("Math"), "PID": gettext("PID") } mod_controller = None if controller_type == 'Conditional': mod_controller = Conditional.query.filter( Conditional.unique_id == controller_id).first() elif controller_type == 'Input': mod_controller = Input.query.filter( Input.unique_id == controller_id).first() elif controller_type == 'LCD': mod_controller = LCD.query.filter( LCD.unique_id == controller_id).first() elif controller_type == 'Math': mod_controller = Math.query.filter( Math.unique_id == controller_id).first() elif controller_type == 'PID': mod_controller = PID.query.filter( PID.unique_id == controller_id).first() if mod_controller is None: flash( "{type} Controller {id} doesn't exist".format(type=controller_type, id=controller_id), "error") return redirect(url_for('routes_general.home')) try: mod_controller.is_activated = activated db.session.commit() if activated: flash( gettext("%(cont)s controller activated in SQL database", cont=translated_names[controller_type]), "success") else: flash( gettext("%(cont)s controller deactivated in SQL database", cont=translated_names[controller_type]), "success") except Exception as except_msg: flash( gettext("Error: %(err)s", err='SQL: {msg}'.format(msg=except_msg)), "error") try: control = DaemonControl() if controller_action == 'activate': return_values = control.controller_activate( controller_type, controller_id) else: return_values = control.controller_deactivate( controller_type, controller_id) if return_values[0]: flash("{err}".format(err=return_values[1]), "error") else: flash("{err}".format(err=return_values[1]), "success") except Exception as except_msg: flash( gettext("Error: %(err)s", err='Daemon: {msg}'.format(msg=except_msg)), "error")
def controller_activate_deactivate(controller_action, controller_type, controller_id): """ Activate or deactivate controller :param controller_action: Activate or deactivate :type controller_action: str :param controller_type: The controller type (Conditional, LCD, Math, PID, Input) :type controller_type: str :param controller_id: Controller with ID to activate or deactivate :type controller_id: str """ if not user_has_permission('edit_controllers'): return redirect(url_for('routes_general.home')) error = [] activated = bool(controller_action == 'activate') translated_names = { "Conditional": TRANSLATIONS['conditional']['title'], "Input": TRANSLATIONS['input']['title'], "LCD": TRANSLATIONS['lcd']['title'], "Math": TRANSLATIONS['math']['title'], "PID": TRANSLATIONS['pid']['title'], "Trigger": TRANSLATIONS['trigger']['title'] } mod_controller = None if controller_type == 'Conditional': mod_controller = Conditional.query.filter( Conditional.unique_id == controller_id).first() elif controller_type == 'Input': mod_controller = Input.query.filter( Input.unique_id == controller_id).first() if activated: error = check_for_valid_unit_and_conversion(controller_id, error) elif controller_type == 'LCD': mod_controller = LCD.query.filter( LCD.unique_id == controller_id).first() elif controller_type == 'Math': mod_controller = Math.query.filter( Math.unique_id == controller_id).first() if activated: error = check_for_valid_unit_and_conversion(controller_id, error) elif controller_type == 'PID': mod_controller = PID.query.filter( PID.unique_id == controller_id).first() if activated: error = check_for_valid_unit_and_conversion(controller_id, error) elif controller_type == 'Trigger': mod_controller = Trigger.query.filter( Trigger.unique_id == controller_id).first() if mod_controller is None: flash("{type} Controller {id} doesn't exist".format( type=controller_type, id=controller_id), "error") return redirect(url_for('routes_general.home')) try: if not error: mod_controller.is_activated = activated db.session.commit() if activated: flash( "{} {} (SQL)".format( translated_names[controller_type], TRANSLATIONS['activate']['title']), "success") else: flash( "{} {} (SQL)".format( translated_names[controller_type], TRANSLATIONS['deactivate']['title']), "success") except Exception as except_msg: flash(gettext("Error: %(err)s", err='SQL: {msg}'.format(msg=except_msg)), "error") try: if not error: control = DaemonControl() if controller_action == 'activate': return_values = control.controller_activate( controller_type,controller_id) else: return_values = control.controller_deactivate( controller_type, controller_id) if return_values[0]: flash("{err}".format(err=return_values[1]), "error") else: flash("{err}".format(err=return_values[1]), "success") except Exception as except_msg: flash('{}: {}'.format(TRANSLATIONS['error']['title'], except_msg), "error") for each_error in error: flash(each_error, 'error')