Exemplo n.º 1
0
def input_del(input_id):
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['delete']['title'],
        controller=TRANSLATIONS['input']['title'])
    error = []

    try:
        input_dev = Input.query.filter(
            Input.unique_id == input_id).first()

        if input_dev.is_activated:
            input_deactivate_associated_controllers(input_id)
            controller_activate_deactivate('deactivate', 'Input', input_id)

        device_measurements = DeviceMeasurements.query.filter(
            DeviceMeasurements.device_id == input_id).all()

        for each_measurement in device_measurements:
            delete_entry_with_id(DeviceMeasurements, each_measurement.unique_id)

        delete_entry_with_id(Input, input_id)
        try:
            display_order = csv_to_list_of_str(
                DisplayOrder.query.first().inputs)
            display_order.remove(input_id)
            DisplayOrder.query.first().inputs = list_to_csv(display_order)
        except Exception:  # id not in list
            pass
        db.session.commit()
    except Exception as except_msg:
        error.append(except_msg)

    flash_success_errors(error, action, url_for('routes_page.page_data'))
Exemplo n.º 2
0
def math_del(form_mod_math):
    action = '{action} {controller}'.format(
        action=gettext("Delete"),
        controller=gettext("Math"))
    error = []

    try:
        math = Math.query.filter(
            Math.unique_id == form_mod_math.math_id.data).first()
        if math.is_activated:
            controller_activate_deactivate(
                'deactivate',
                'Math',
                form_mod_math.math_id.data)

        delete_entry_with_id(Math, form_mod_math.math_id.data)
        try:
            display_order = csv_to_list_of_str(DisplayOrder.query.first().math)
            display_order.remove(form_mod_math.math_id.data)
            DisplayOrder.query.first().math = list_to_csv(display_order)
        except Exception:  # id not in list
            pass
        db.session.commit()
    except Exception as except_msg:
        error.append(except_msg)

    flash_success_errors(error, action, url_for('routes_page.page_data'))
Exemplo n.º 3
0
def input_del(form_mod):
    action = '{action} {controller}'.format(
        action=gettext("Delete"),
        controller=gettext("Input"))
    error = []

    input_id = form_mod.input_id.data

    try:
        input_dev = Input.query.filter(
            Input.unique_id == input_id).first()
        if input_dev.is_activated:
            input_deactivate_associated_controllers(input_id)
            controller_activate_deactivate('deactivate', 'Input', input_id)

        delete_entry_with_id(Input, input_id)
        try:
            display_order = csv_to_list_of_str(
                DisplayOrder.query.first().inputs)
            display_order.remove(input_id)
            DisplayOrder.query.first().inputs = list_to_csv(display_order)
        except Exception:  # id not in list
            pass
        db.session.commit()
    except Exception as except_msg:
        error.append(except_msg)

    flash_success_errors(error, action, url_for('routes_page.page_data'))
Exemplo n.º 4
0
def math_del(form_mod_math):
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['delete']['title'],
        controller=TRANSLATIONS['math']['title'])
    error = []

    math_id = form_mod_math.math_id.data

    try:
        math = Math.query.filter(Math.unique_id == math_id).first()
        if math.is_activated:
            controller_activate_deactivate('deactivate', 'Math',
                                           form_mod_math.math_id.data)

        device_measurements = DeviceMeasurements.query.filter(
            DeviceMeasurements.device_id == math_id).all()

        for each_measurement in device_measurements:
            delete_entry_with_id(DeviceMeasurements,
                                 each_measurement.unique_id)

        delete_entry_with_id(Math, math_id)
        try:
            display_order = csv_to_list_of_str(DisplayOrder.query.first().math)
            display_order.remove(math_id)
            DisplayOrder.query.first().math = list_to_csv(display_order)
        except Exception:  # id not in list
            pass
        db.session.commit()
    except Exception as except_msg:
        error.append(except_msg)

    flash_success_errors(error, action, url_for('routes_page.page_input'))
Exemplo n.º 5
0
def conditional_activate(cond_id):
    """Activate a Conditional"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['activate']['title'],
        controller=TRANSLATIONS['conditional']['title'])

    conditions = ConditionalConditions.query.filter(
        ConditionalConditions.conditional_id == cond_id).all()

    for each_condition in conditions:
        # Check for errors in the Conditional settings
        error = check_cond_measurements(each_condition, error)

    conditions = ConditionalConditions.query.filter(
        ConditionalConditions.conditional_id == cond_id)
    if not conditions.count():
        error.append("No Conditions found: Add at least one Condition before activating.")

    actions = Actions.query.filter(
        Actions.function_id == cond_id)
    if not actions.count():
        error.append("No Actions found: Add at least one Action before activating.")

    for each_action in actions.all():
        error = check_actions(each_action, error)

    if not error:
        controller_activate_deactivate('activate', 'Conditional', cond_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 6
0
def trigger_activate(trigger_id):
    """Activate a Trigger"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['activate']['title'],
        controller=TRANSLATIONS['trigger']['title'])

    mod_trigger = Trigger.query.filter(Trigger.unique_id == trigger_id).first()

    # Check for errors in the Trigger settings
    if mod_trigger.trigger_type == 'edge':
        error = check_cond_edge(mod_trigger, error)
    elif mod_trigger.trigger_type == 'output':
        error = check_cond_output(mod_trigger, error)

    actions = Actions.query.filter(Actions.function_id == trigger_id)

    if not actions.count():
        error.append(
            "No Actions found: Add at least one Action before activating.")

    for each_action in actions.all():
        error = check_actions(each_action, error)

    if mod_trigger.trigger_type == 'run_pwm_method':
        mod_trigger_ready = Trigger.query.filter(
            Trigger.unique_id == trigger_id).first()
        mod_trigger_ready.method_start_time = 'Ready'
        db.session.commit()

    if not error:
        controller_activate_deactivate('activate', 'Trigger', trigger_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 7
0
def lcd_activate(lcd_id):
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['activate']['title'],
        controller=TRANSLATIONS['lcd']['title'])
    error = []

    try:
        # All display lines must be filled to activate display
        lcd = LCD.query.filter(
            LCD.unique_id == lcd_id).first()
        lcd_data = LCDData.query.filter(
            LCDData.lcd_id == lcd_id).all()
        blank_line_detected = False

        for each_lcd_data in lcd_data:
            if (
                    (lcd.y_lines in [2, 4] and
                        (not each_lcd_data.line_1_id or
                         not each_lcd_data.line_2_id)
                     ) or
                    (lcd.y_lines == 4 and
                        (not each_lcd_data.line_3_id or
                         not each_lcd_data.line_4_id))
                    ):
                blank_line_detected = True

        if blank_line_detected:
            error.append(gettext(
                "Cannot activate LCD if there are blank lines"))

        if not error:
            controller_activate_deactivate('activate', 'LCD', lcd_id)
    except Exception as except_msg:
        error.append(except_msg)
    flash_success_errors(error, action, url_for('routes_page.page_lcd'))
Exemplo n.º 8
0
def input_del(input_id):
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['delete']['title'],
        controller=TRANSLATIONS['input']['title'])
    error = []

    try:
        input_dev = Input.query.filter(Input.unique_id == input_id).first()

        if input_dev.is_activated:
            input_deactivate_associated_controllers(input_id)
            controller_activate_deactivate('deactivate', 'Input', input_id)

        device_measurements = DeviceMeasurements.query.filter(
            DeviceMeasurements.device_id == input_id).all()

        for each_measurement in device_measurements:
            delete_entry_with_id(DeviceMeasurements,
                                 each_measurement.unique_id)

        delete_entry_with_id(Input, input_id)
        try:
            display_order = csv_to_list_of_str(
                DisplayOrder.query.first().inputs)
            display_order.remove(input_id)
            DisplayOrder.query.first().inputs = list_to_csv(display_order)
        except Exception:  # id not in list
            pass
        db.session.commit()
    except Exception as except_msg:
        error.append(except_msg)

    flash_success_errors(error, action, url_for('routes_page.page_data'))
Exemplo n.º 9
0
def math_del(form_mod_math):
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['delete']['title'],
        controller=TRANSLATIONS['math']['title'])
    error = []

    math_id = form_mod_math.math_id.data

    try:
        math = Math.query.filter(
            Math.unique_id == math_id).first()
        if math.is_activated:
            controller_activate_deactivate(
                'deactivate',
                'Math',
                form_mod_math.math_id.data)

        device_measurements = DeviceMeasurements.query.filter(
            DeviceMeasurements.device_id == math_id).all()

        for each_measurement in device_measurements:
            delete_entry_with_id(DeviceMeasurements, each_measurement.unique_id)

        delete_entry_with_id(Math, math_id)
        try:
            display_order = csv_to_list_of_str(DisplayOrder.query.first().math)
            display_order.remove(math_id)
            DisplayOrder.query.first().math = list_to_csv(display_order)
        except Exception:  # id not in list
            pass
        db.session.commit()
    except Exception as except_msg:
        error.append(except_msg)

    flash_success_errors(error, action, url_for('routes_page.page_data'))
Exemplo n.º 10
0
def math_del(form_mod_math):
    action = '{action} {controller}'.format(action=gettext("Delete"),
                                            controller=gettext("Math"))
    error = []

    try:
        math = Math.query.filter(Math.id == form_mod_math.math_id.data).first()
        if math.is_activated:
            controller_activate_deactivate('deactivate', 'Math',
                                           form_mod_math.math_id.data)

        # Delete any conditionals associated with the controller
        conditionals = Conditional.query.filter(
            Conditional.math_id == form_mod_math.math_id.data).all()
        for each_cond in conditionals:
            conditional_actions = ConditionalActions.query.filter(
                ConditionalActions.conditional_id == each_cond.id).all()
            for each_cond_action in conditional_actions:
                db.session.delete(each_cond_action)
            db.session.delete(each_cond)
        db.session.commit()

        delete_entry_with_id(Math, form_mod_math.math_id.data)
        try:
            display_order = csv_to_list_of_int(DisplayOrder.query.first().math)
            display_order.remove(int(form_mod_math.math_id.data))
            DisplayOrder.query.first().math = list_to_csv(display_order)
        except Exception:  # id not in list
            pass
        db.session.commit()
    except Exception as except_msg:
        error.append(except_msg)

    flash_success_errors(error, action, url_for('routes_page.page_data'))
Exemplo n.º 11
0
def trigger_activate(trigger_id):
    """Activate a Trigger"""
    messages = {"success": [], "info": [], "warning": [], "error": []}

    mod_trigger = Trigger.query.filter(Trigger.unique_id == trigger_id).first()

    # Check for errors in the Trigger settings
    if mod_trigger.trigger_type == 'edge':
        messages["error"] = check_cond_edge(mod_trigger, messages["error"])
    elif mod_trigger.trigger_type == 'output':
        messages["error"] = check_cond_output(mod_trigger, messages["error"])

    actions = Actions.query.filter(Actions.function_id == trigger_id)

    if not actions.count(
    ) and mod_trigger.trigger_type != 'trigger_run_pwm_method':
        messages["error"].append(
            "No Actions found: Add at least one Action before activating.")

    for each_action in actions.all():
        messages["error"] = check_actions(each_action, messages["error"])

    if not messages["error"]:
        controller_activate_deactivate('activate',
                                       'Trigger',
                                       trigger_id,
                                       flash_message=False)
        messages["success"].append('{action} {controller}'.format(
            action=TRANSLATIONS['activate']['title'],
            controller=TRANSLATIONS['trigger']['title']))

    return messages
Exemplo n.º 12
0
def conditional_activate(cond_id):
    """Activate a Conditional"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['activate']['title'],
        controller=TRANSLATIONS['conditional']['title'])

    conditions = ConditionalConditions.query.filter(
        ConditionalConditions.conditional_id == cond_id).all()

    for each_condition in conditions:
        # Check for errors in the Conditional settings
        error = check_cond_measurements(each_condition, error)

    conditions = ConditionalConditions.query.filter(
        ConditionalConditions.conditional_id == cond_id)
    if not conditions.count():
        error.append(
            "No Conditions found: Add at least one Condition before activating."
        )

    actions = Actions.query.filter(Actions.function_id == cond_id)
    if not actions.count():
        error.append(
            "No Actions found: Add at least one Action before activating.")

    for each_action in actions.all():
        error = check_actions(each_action, error)

    if not error:
        controller_activate_deactivate('activate', 'Conditional', cond_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 13
0
def controller_activate(controller_id):
    """Activate a Conditional"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['activate']['title'],
        controller=TRANSLATIONS['controller']['title'])

    function = CustomController.query.filter(
        CustomController.unique_id == controller_id).first()

    dict_controllers = parse_function_information()

    if ('enable_channel_unit_select' in dict_controllers[function.device] and
            dict_controllers[function.device]['enable_channel_unit_select']):
        device_measurements = DeviceMeasurements.query.filter(
            DeviceMeasurements.device_id == controller_id).all()
        for each_measure in device_measurements:
            if (None in [each_measure.measurement, each_measure.unit]
                    or "" in [each_measure.measurement, each_measure.unit]):
                error.append(
                    "Measurement CH{} ({}) measurement/unit not set. All Measurements need to have "
                    "the measurement and unit set before the Function can be activated."
                    .format(each_measure.channel, each_measure.name))

    if not error:
        controller_activate_deactivate('activate', 'Function', controller_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 14
0
def conditional_activate(cond_id):
    """Activate a Conditional"""
    error = []
    action = '{action} {controller}'.format(action=gettext("Activate"),
                                            controller=gettext("Conditional"))

    mod_cond = Conditional.query.filter(
        Conditional.unique_id == cond_id).first()

    # Check for errors in the Conditional settings
    if mod_cond.conditional_type == 'conditional_edge':
        error = check_cond_edge(mod_cond, error)
    elif mod_cond.conditional_type == 'conditional_measurement':
        error = check_cond_measurements(mod_cond, error)
    elif mod_cond.conditional_type == 'conditional_output':
        error = check_cond_output(mod_cond, error)

    # Check for errors in each Conditional Action
    cond_actions = ConditionalActions.query.filter(
        ConditionalActions.conditional_id == cond_id).all()
    for each_action in cond_actions:
        error = check_cond_actions(each_action, error)

    if mod_cond.conditional_type == 'conditional_run_pwm_method':
        mod_cond_ready = Conditional.query.filter(
            Conditional.unique_id == cond_id).first()
        mod_cond_ready.method_start_time = 'Ready'
        db.session.commit()

    if not error:
        controller_activate_deactivate('activate', 'Conditional', cond_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 15
0
def pid_deactivate(pid_id):
    messages = {
        "success": [],
        "info": [],
        "warning": [],
        "error": []
    }

    pid = PID.query.filter(
        PID.unique_id == pid_id).first()
    if not pid:
        messages["error"].append("PID Controller not found")

    if not pid.is_activated:
        messages["error"].append("PID Controller not active")

    if not messages["error"]:
        pid.is_activated = False
        pid.is_held = False
        pid.is_paused = False
        pid.method_start_time = None
        pid.method_end_time = None
        db.session.commit()
        time.sleep(1)
        controller_activate_deactivate(
            'deactivate', 'PID', pid_id, flash_message=False)
        messages["success"].append('{action} {controller}'.format(
            action=TRANSLATIONS['deactivate']['title'],
            controller=TRANSLATIONS['pid']['title']))

    return messages
Exemplo n.º 16
0
def input_activate(form_mod):
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['activate']['title'],
        controller=TRANSLATIONS['input']['title'])
    error = []
    dict_inputs = parse_input_information()
    input_id = form_mod.input_id.data
    input_dev = Input.query.filter(Input.unique_id == input_id).first()
    device_measurements = DeviceMeasurements.query.filter(
        DeviceMeasurements.device_id == input_dev.unique_id)
    custom_options_values = parse_custom_option_values(input_dev)

    #
    # General Input checks
    #
    if not input_dev.period:
        error.append("Period must be set")

    if (input_dev.pre_output_id and len(input_dev.pre_output_id) > 1
            and not input_dev.pre_output_duration):
        error.append(
            "Pre Output Duration must be > 0 if Pre Output is enabled")

    if not device_measurements.filter(
            DeviceMeasurements.is_enabled == True).count():
        error.append("At least one measurement must be enabled")

    #
    # Check if required custom options are set
    #
    if 'custom_options' in dict_inputs[input_dev.device]:
        for each_option in dict_inputs[input_dev.device]['custom_options']:
            value = custom_options_values[input_dev.unique_id][
                each_option['id']]
            if ('required' in each_option and each_option['required']
                    and not value):
                error.append("{} is required to be set".format(
                    each_option['name']))

    #
    # Input-specific checks
    #
    if input_dev.device == 'LinuxCommand' and not input_dev.cmd_command:
        error.append("Cannot activate Command Input without a Command set")

    elif ('measurements_variable_amount' in dict_inputs[input_dev.device]
          and dict_inputs[input_dev.device]['measurements_variable_amount']):
        measure_set = True
        for each_channel in device_measurements.all():
            if (not each_channel.name or not each_channel.measurement
                    or not each_channel.unit):
                measure_set = False
        if not measure_set:
            error.append(
                "All measurements must have a name and unit/measurement set")

    if not error:
        controller_activate_deactivate('activate', 'Input', input_id)
    flash_success_errors(error, action, url_for('routes_page.page_data'))
Exemplo n.º 17
0
def input_activate(form_mod):
    input_id = form_mod.input_id.data
    input_dev = Input.query.filter(Input.unique_id == input_id).first()
    if input_dev.device == 'LinuxCommand':
        if input_dev.cmd_command is '':
            flash("Cannot activate Input without Command set.", "error")
            return redirect(url_for('routes_page.page_data'))
    controller_activate_deactivate('activate', 'Input', input_id)
Exemplo n.º 18
0
def pid_deactivate(pid_id):
    pid = PID.query.filter(PID.unique_id == pid_id).first()
    pid.is_activated = False
    pid.is_held = False
    pid.is_paused = False
    db.session.commit()
    time.sleep(1)
    controller_activate_deactivate('deactivate', 'PID', pid_id)
Exemplo n.º 19
0
def input_deactivate_associated_controllers(input_id):
    # Deactivate any activated PIDs using this input
    sensor_unique_id = Input.query.filter(
        Input.id == input_id).first().unique_id
    pid = PID.query.filter(PID.is_activated == True).all()
    for each_pid in pid:
        if sensor_unique_id in each_pid.measurement:
            controller_activate_deactivate('deactivate', 'PID', each_pid.id)
Exemplo n.º 20
0
def input_activate(form_mod):
    input_id = form_mod.input_id.data
    input_dev = Input.query.filter(Input.unique_id == input_id).first()
    if input_dev.device == 'LinuxCommand':
        if input_dev.cmd_command is '':
            flash("Cannot activate Input without Command set.", "error")
            return redirect(url_for('routes_page.page_data'))
    controller_activate_deactivate('activate', 'Input',  input_id)
Exemplo n.º 21
0
def pid_deactivate(pid_id):
    pid = PID.query.filter(
        PID.unique_id == pid_id).first()
    pid.is_activated = False
    pid.is_held = False
    pid.is_paused = False
    db.session.commit()
    time.sleep(1)
    controller_activate_deactivate('deactivate', 'PID', pid_id)
Exemplo n.º 22
0
def input_deactivate_associated_controllers(input_id):
    # Deactivate any activated PIDs using this input
    sensor_unique_id = Input.query.filter(
        Input.unique_id == input_id).first().unique_id
    pid = PID.query.filter(PID.is_activated == True).all()
    for each_pid in pid:
        if sensor_unique_id in each_pid.measurement:
            controller_activate_deactivate('deactivate',
                                           'PID',
                                           each_pid.unique_id)
Exemplo n.º 23
0
def timer_activate(form_timer):
    timer = Timer.query.filter(Timer.id == form_timer.timer_id.data).first()
    if timer.timer_type == 'pwm_method':
        # Signal the duration method can run because it's been
        # properly initiated (non-power failure)
        mod_timer = Timer.query.filter(Timer.id == form_timer.timer_id.data).first()
        mod_timer.method_start_time = 'Ready'
        db.session.commit()
    controller_activate_deactivate(
        'activate', 'Timer', form_timer.timer_id.data)
Exemplo n.º 24
0
def conditional_deactivate(cond_id):
    """Deactivate a Conditional"""
    error = []
    action = '{action} {controller}'.format(action=gettext("Deactivate"),
                                            controller=gettext("Conditional"))

    if not error:
        controller_activate_deactivate('deactivate', 'Conditional', cond_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 25
0
def conditional_deactivate(cond_id):
    """Deactivate a Conditional"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['deactivate']['title'],
        controller=TRANSLATIONS['conditional']['title'])

    if not error:
        controller_activate_deactivate('deactivate', 'Conditional', cond_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 26
0
def conditional_deactivate(cond_id):
    """Deactivate a Conditional"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['deactivate']['title'],
        controller=TRANSLATIONS['conditional']['title'])

    if not error:
        controller_activate_deactivate('deactivate', 'Conditional', cond_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 27
0
def trigger_deactivate(trigger_id):
    """Deactivate a Trigger"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['deactivate']['title'],
        controller=TRANSLATIONS['trigger']['title'])

    if not error:
        controller_activate_deactivate('deactivate', 'Trigger', trigger_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 28
0
def input_activate(form_mod_sensor):
    input_id = form_mod_sensor.input_id.data
    input_dev = Input.query.filter(Input.id == input_id).first()
    if (input_dev.device != 'LinuxCommand' and not input_dev.location
            and input_dev.device not in DEVICES_DEFAULT_LOCATION):
        flash(
            "Cannot activate Input without the GPIO/I2C Address/Port "
            "to communicate with it set.", "error")
        return redirect(url_for('routes_page.page_data'))
    elif (input_dev.device == 'LinuxCommand' and input_dev.cmd_command is ''):
        flash("Cannot activate Input without a command set.", "error")
        return redirect(url_for('routes_page.page_data'))
    controller_activate_deactivate('activate', 'Input', input_id)
Exemplo n.º 29
0
def input_del(input_id):
    messages = {"success": [], "info": [], "warning": [], "error": []}

    try:
        input_dev = Input.query.filter(Input.unique_id == input_id).first()

        if input_dev.is_activated:
            input_deactivate_associated_controllers(input_id)
            controller_activate_deactivate('deactivate', 'Input', input_id)

        device_measurements = DeviceMeasurements.query.filter(
            DeviceMeasurements.device_id == input_id).all()

        for each_measurement in device_measurements:
            delete_entry_with_id(DeviceMeasurements,
                                 each_measurement.unique_id,
                                 flash_message=False)

        delete_entry_with_id(Input, input_id, flash_message=False)

        channels = InputChannel.query.filter(
            InputChannel.input_id == input_id).all()
        for each_channel in channels:
            delete_entry_with_id(InputChannel,
                                 each_channel.unique_id,
                                 flash_message=False)

        try:
            display_order = csv_to_list_of_str(
                DisplayOrder.query.first().inputs)
            display_order.remove(input_id)
            DisplayOrder.query.first().inputs = list_to_csv(display_order)
        except Exception:  # id not in list
            pass

        try:
            file_path = os.path.join(
                PATH_PYTHON_CODE_USER,
                'input_python_code_{}.py'.format(input_dev.unique_id))
            os.remove(file_path)
        except:
            pass

        db.session.commit()
        messages["success"].append('{action} {controller}'.format(
            action=TRANSLATIONS['delete']['title'],
            controller=TRANSLATIONS['input']['title']))
    except Exception as except_msg:
        messages["error"].append(str(except_msg))

    return messages
Exemplo n.º 30
0
def sensor_activate(form_mod_sensor):
    sensor = Input.query.filter(
        Input.id == form_mod_sensor.modSensor_id.data).first()
    if (sensor.device != u'LinuxCommand' and not sensor.location
            and sensor.device not in DEVICES_DEFAULT_LOCATION):
        flash(
            "Cannot activate sensor without the GPIO/I2C Address/Port "
            "to communicate with it set.", "error")
        return redirect(url_for('page_routes.page_input'))
    elif (sensor.device == u'LinuxCommand' and sensor.cmd_command is ''):
        flash("Cannot activate sensor without a command set.", "error")
        return redirect(url_for('page_routes.page_input'))
    controller_activate_deactivate('activate', 'Input',
                                   form_mod_sensor.modSensor_id.data)
Exemplo n.º 31
0
def trigger_deactivate(trigger_id):
    """Deactivate a Trigger"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['deactivate']['title'],
        controller=TRANSLATIONS['trigger']['title'])

    if not error:
        controller_activate_deactivate(
            'deactivate',
            'Trigger',
            trigger_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 32
0
def conditional_deactivate(cond_id):
    """Deactivate a Conditional"""
    messages = {"success": [], "info": [], "warning": [], "error": []}

    if not messages["error"]:
        controller_activate_deactivate('deactivate',
                                       'Conditional',
                                       cond_id,
                                       flash_message=False)
        messages["success"].append('{action} {controller}'.format(
            action=TRANSLATIONS['deactivate']['title'],
            controller=TRANSLATIONS['conditional']['title']))

    return messages
Exemplo n.º 33
0
def controller_activate(controller_id):
    """Activate a Conditional"""
    messages = {
        "success": [],
        "info": [],
        "warning": [],
        "error": [],
        "period_status": None
    }

    function = CustomController.query.filter(
        CustomController.unique_id == controller_id).first()

    if not function:
        messages["error"].append("Function not found")
    else:
        dict_controllers = parse_function_information()
        custom_options_values_controllers = parse_custom_option_values(
            CustomController.query.all(), dict_controller=dict_controllers)

        if (controller_id in custom_options_values_controllers
                and 'period_status'
                in custom_options_values_controllers[controller_id]):
            messages["period_status"] = custom_options_values_controllers[
                controller_id]['period_status']

        if ('enable_channel_unit_select' in dict_controllers[function.device]
                and dict_controllers[
                    function.device]['enable_channel_unit_select']):
            device_measurements = DeviceMeasurements.query.filter(
                DeviceMeasurements.device_id == controller_id).all()
            for each_measure in device_measurements:
                if (None in [each_measure.measurement, each_measure.unit] or ""
                        in [each_measure.measurement, each_measure.unit]):
                    messages["error"].append(
                        "Measurement CH{} ({}) measurement/unit not set. All Measurements need to have "
                        "the measurement and unit set before the Function can be activated."
                        .format(each_measure.channel, each_measure.name))

    if not messages["error"]:
        controller_activate_deactivate('activate',
                                       'Function',
                                       controller_id,
                                       flash_message=False)
        messages["success"].append('{action} {controller}'.format(
            action=TRANSLATIONS['activate']['title'],
            controller=TRANSLATIONS['controller']['title']))

    return messages
Exemplo n.º 34
0
def pid_activate(pid_id):
    if has_required_pid_values(pid_id):
        return redirect(url_for('page_routes.page_pid'))

    action = '{action} {controller}'.format(
        action=gettext(u"Actuate"),
        controller=gettext(u"PID"))
    error = []

    # Check if associated sensor is activated
    pid = PID.query.filter(
        PID.id == pid_id).first()

    error = can_set_relay(
        error, pid_id, pid.raise_relay_id, pid.lower_relay_id)

    sensor_unique_id = pid.measurement.split(',')[0]
    sensor = Input.query.filter(
        Input.unique_id == sensor_unique_id).first()

    if not sensor.is_activated:
        error.append(gettext(
            u"Cannot activate PID controller if the associated sensor "
            u"controller is inactive"))

    if ((pid.direction == 'both' and not (pid.lower_relay_id and pid.raise_relay_id)) or
                (pid.direction == 'lower' and not pid.lower_relay_id) or
                (pid.direction == 'raise' and not pid.raise_relay_id)):
        error.append(gettext(
            u"Cannot activate PID controller if raise and/or lower relay IDs "
            u"are not selected"))

    if not error:
        # Signal the duration method can run because it's been
        # properly initiated (non-power failure)
        method = Method.query.filter(
            Method.id == pid.method_id).first()
        if method and method.method_type == 'Duration':
            mod_pid = PID.query.filter(PID.id == pid_id).first()
            mod_pid.method_start_time = 'Ready'
            db.session.commit()

        time.sleep(1)
        controller_activate_deactivate('activate',
                                       'PID',
                                       pid_id)

    flash_success_errors(error, action, url_for('page_routes.page_pid'))
Exemplo n.º 35
0
def trigger_deactivate(trigger_id):
    """Deactivate a Trigger"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['deactivate']['title'],
        controller=TRANSLATIONS['trigger']['title'])

    if not error:
        controller_activate_deactivate('deactivate', 'Trigger', trigger_id)

        trigger = Trigger.query.filter(Trigger.unique_id == trigger_id).first()
        trigger.method_start_time = None
        trigger.method_end_time = None
        db.session.commit()

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 36
0
def pid_activate(pid_id):
    if has_required_pid_values(pid_id):
        return redirect(url_for('routes_page.page_function'))

    action = '{action} {controller}'.format(
        action=TRANSLATIONS['activate']['title'],
        controller=TRANSLATIONS['pid']['title'])
    error = []

    # Check if associated sensor is activated
    pid = PID.query.filter(
        PID.unique_id == pid_id).first()

    error = can_set_output(
        error, pid_id, pid.raise_output_id, pid.lower_output_id)

    device_unique_id = pid.measurement.split(',')[0]
    input_dev = Input.query.filter(
        Input.unique_id == device_unique_id).first()
    math = Math.query.filter(
        Math.unique_id == device_unique_id).first()

    if (input_dev and not input_dev.is_activated) or (math and not math.is_activated):
        error.append(gettext(
            "Cannot activate PID controller if the associated sensor "
            "controller is inactive"))

    if ((pid.direction == 'both' and not (pid.lower_output_id and pid.raise_output_id)) or
            (pid.direction == 'lower' and not pid.lower_output_id) or
            (pid.direction == 'raise' and not pid.raise_output_id)):
        error.append(gettext(
            "Cannot activate PID controller if raise and/or lower output IDs "
            "are not selected"))

    if not error:
        # Signal the duration method can run because it's been
        # properly initiated (non-power failure)
        method = Method.query.filter(
            Method.unique_id == pid.method_id).first()
        if method and method.method_type == 'Duration':
            mod_pid = PID.query.filter(PID.unique_id == pid_id).first()
            mod_pid.method_start_time = 'Ready'
            db.session.commit()
        time.sleep(1)
        controller_activate_deactivate('activate', 'PID', pid_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 37
0
def input_deactivate(form_mod):
    messages = {"success": [], "info": [], "warning": [], "error": []}

    try:
        input_id = form_mod.input_id.data
        input_deactivate_associated_controllers(input_id)
        controller_activate_deactivate('deactivate',
                                       'Input',
                                       input_id,
                                       flash_message=False)
        messages["success"].append('{action} {controller}'.format(
            action=TRANSLATIONS['deactivate']['title'],
            controller=TRANSLATIONS['input']['title']))
    except Exception as err:
        messages["error"].append("Error deactivating Input: {}".format(err))

    return messages
Exemplo n.º 38
0
def sensor_deactivate_associated_controllers(input_id):
    # Deactivate any activated PIDs using this input
    sensor_unique_id = Input.query.filter(
        Input.id == input_id).first().unique_id
    pid = PID.query.filter(PID.is_activated == True).all()
    for each_pid in pid:
        if sensor_unique_id in each_pid.measurement:
            controller_activate_deactivate('deactivate', 'PID', each_pid.id)

    # Deactivate any activated LCDs using this input
    for each_lcd_data in LCDData.query.all():
        if input_id in [
                each_lcd_data.line_1_id, each_lcd_data.line_2_id,
                each_lcd_data.line_3_id, each_lcd_data.line_4_id
        ]:
            lcd = LCD.query.filter(LCD.id == each_lcd_data.lcd_id).first()
            if lcd.is_activated:
                controller_activate_deactivate('deactivate', 'LCD', lcd.id)
Exemplo n.º 39
0
def trigger_activate(trigger_id):
    """Activate a Trigger"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['activate']['title'],
        controller=TRANSLATIONS['trigger']['title'])

    mod_trigger = Trigger.query.filter(
        Trigger.unique_id == trigger_id).first()

    # Check for errors in the Trigger settings
    if mod_trigger.trigger_type == 'edge':
        error = check_cond_edge(mod_trigger, error)
    elif mod_trigger.trigger_type == 'output':
        error = check_cond_output(mod_trigger, error)

    actions = Actions.query.filter(
        Actions.function_id == trigger_id)

    if not actions.count():
        error.append("No Actions found: Add at least one Action before activating.")

    for each_action in actions.all():
        error = check_actions(each_action, error)

    if mod_trigger.trigger_type == 'run_pwm_method':
        mod_trigger_ready = Trigger.query.filter(
            Trigger.unique_id == trigger_id).first()
        mod_trigger_ready.method_start_time = 'Ready'
        db.session.commit()

    if not error:
        controller_activate_deactivate(
            'activate',
            'Trigger',
            trigger_id)

    flash_success_errors(error, action, url_for('routes_page.page_function'))
Exemplo n.º 40
0
def input_deactivate(form_mod):
    input_id = form_mod.input_id.data
    input_deactivate_associated_controllers(input_id)
    controller_activate_deactivate('deactivate', 'Input', input_id)
Exemplo n.º 41
0
def lcd_deactivate(lcd_id):
    controller_activate_deactivate('deactivate', 'LCD', lcd_id)
Exemplo n.º 42
0
def math_deactivate(form_mod_math):
    controller_activate_deactivate(
        'deactivate', 'Math', form_mod_math.math_id.data)