Esempio n. 1
0
def add_ongoing_rule():
    """Used in add rule modal window."""
    rules = request.json['rules']
    now = datetime.datetime.now()

    for rule in rules:
        rule['line_id'] = int(rule['line_id'])
        rule['line_name'] = rule['line_name']
        rule['time'] = convert_to_datetime(rule['time'])
        rule['intervals'] = int(rule['intervals'])
        rule['time_wait'] = int(rule['time_wait'])
        rule['repeat_value'] = int(rule['repeat_value'])
        rule['date_start'] = convert_to_datetime(rule['date_start'])
        rule['time_start'] = convert_to_datetime(rule['time_start'])
        rule['date_time_start'] = datetime.datetime.combine(
            rule['date_start'], rule['time_start'].time())
        rule['end_date'] = convert_to_datetime(rule['end_date'])
        rule['active'] = 1
        rule['rule_id'] = str(uuid.uuid4())
        rule['days'] = -1

        if rule['date_start'].date() == rule['end_date'].date():
            date_delta = rule['end_date'].date() - now.date()
            if date_delta.days == 0:
                rule['days'] = 0
            if date_delta.days == 1:
                rule['days'] = 1

        # "INSERT INTO life(line_id, time, intervals, time_wait, repeat_value, date_start, "
        # "time_start, end_date, active, rule_id) "
        # "VALUES ({0}, '{1}', {2}, '{3}', {4}, {5}, '{6}', {7}, {8}, {9}")
        # insert into ongoing table
        database.update(database.QUERY[mn()].format(
            rule['line_id'], rule['time'], rule['intervals'],
            rule['time_wait'], rule['repeat_value'], rule['date_time_start'],
            rule['end_date'], rule['active'], rule['rule_id']))

        # update rules;
        update_rules_from_ongoing_rules(rule)
        logging.info("Ongoing rule added. {0}".format(str(rule)))

        template = render_template('ongoing_rule_single.html', n=rule)
        send_ongoing_rule_message('add_ongoing_rule', {
            'template': template,
            'rule_id': rule['rule_id'],
            'days': rule['days']
        })

    update_all_rules()
    try:
        response_status = garden_controller.branch_status()

        arr = form_responce_for_branches(response_status)
        send_branch_status_message('branch_status', arr)
    except Exception as e:
        logging.error(e)
        logging.error("Can't send updated rules. Exception occured")

    return json.dumps({'status': 'OK'})
Esempio n. 2
0
def rissing(channel):
    """Fillup rain table"""
    global iteraion
    time.sleep(0.005)
    if GPIO.input(Input_Sig) == 1:
        logging.info("Event:{0}".format(iteraion))
        iteraion += 1

        database.update(database.QUERY[mn()].format(10))
Esempio n. 3
0
def remove_ongoing_rule():
    """User can remove ongoing rule from ui."""
    rule_id = request.args.get('id')
    database.update(database.QUERY[mn() + '_remove_from_life'].format(rule_id))
    database.update(database.QUERY[mn() +
                                   '_delete_ongoing_rule'].format(rule_id))
    update_all_rules()

    send_ongoing_rule_message('remove_ongoing_rule', {'rule_id': rule_id})

    return json.dumps({'status': 'OK'})
Esempio n. 4
0
def deactivate_ongoing_rule():
    """User can deactivate ongoing rule from ui."""
    rule_id = request.args.get('id')
    database.update(database.QUERY[mn() + '_ongoing'].format(rule_id))
    database.update(database.QUERY[mn() + '_life'].format(rule_id))
    update_all_rules()

    send_ongoing_rule_message('ongoing_rule_state', {
        'rule_id': rule_id,
        'status': 0
    })
    return json.dumps({'status': 'OK'})
Esempio n. 5
0
def update_rules_from_ongoing_rules(rule):
    """Form rules from ongoing rule."""
    database.update(database.QUERY[mn() + '_remove_from_life'].format(
        rule['rule_id']))

    _delta = rule['end_date'] - rule['date_time_start']
    _days = _delta.days + 1
    logging.info("number of days: {0}".format(_days))

    ongoing_rule_id = rule['rule_id']

    for days_to_add in range(0, _days + 1, rule['repeat_value']):
        date_datetime = rule['date_time_start'] + datetime.timedelta(
            days=days_to_add)

        # start_time = rule['date_time_start']
        branch_id = int(rule['line_id'])
        time_min = int(rule['time'])
        time_wait = int(rule['time_wait'])
        num_of_intervals = int(rule['intervals'])
        interval_id = str(uuid.uuid4())

        stop_datetime = date_datetime + datetime.timedelta(minutes=time_min)

        database.update(database.QUERY[mn() + '_add_rule_to_life'].format(
            branch_id, START_RULE, ENABLED_RULE, date_datetime.date(),
            date_datetime, interval_id, time_min, ongoing_rule_id))
        database.update(database.QUERY[mn() + '_add_rule_to_life'].format(
            branch_id, STOP_RULE, ENABLED_RULE, date_datetime.date(),
            stop_datetime, interval_id, 0, ongoing_rule_id))

        logging.info(
            "Start time: {0}. Stop time: {1} added to database".format(
                str(date_datetime), str(stop_datetime)))

        # first interval is executed
        for x in range(2, num_of_intervals + 1):
            date_datetime = stop_datetime + datetime.timedelta(
                minutes=time_wait)
            stop_datetime = date_datetime + datetime.timedelta(
                minutes=time_min)

            database.update(database.QUERY[mn() + '_add_rule_to_life'].format(
                branch_id, START_RULE, ENABLED_RULE, date_datetime.date(),
                date_datetime, interval_id, time_min, ongoing_rule_id))
            database.update(database.QUERY[mn() + '_add_rule_to_life'].format(
                branch_id, STOP_RULE, ENABLED_RULE, date_datetime.date(),
                stop_datetime, interval_id, 0, ongoing_rule_id))

            logging.info(
                "Start time: {0}. Stop time: {1} added to database".format(
                    str(date_datetime), str(stop_datetime)))
Esempio n. 6
0
def cancel_rule():
    """User can remove rule from ongoing rules table."""
    content = request.json['list']

    for rule_id in content:
        # select l.interval_id, li.name from life as l, lines as li where id = {0} and l.line_id = li.number
        res = database.select(database.QUERY[mn() + "_1"].format(rule_id),
                              'fetchone')
        if (res is None):
            logging.error(
                "No {0} rule/interval id in database".format(rule_id))
            abort(500)

        interval_id = res[0]
        ongoing_rule_id = res[2]
        # branch_name = res[1]
        # "UPDATE life SET state=4 WHERE interval_id = '{0}' and state = 1 and rule_id = 1"
        database.update(database.QUERY[mn() + '_2'].format(interval_id))

        res = database.select(
            database.QUERY[mn() +
                           "_select_ongoing_rule"].format(ongoing_rule_id),
            'fetchone')
        if res is None:
            logging.info('No intervals for {0} ongoing rule. Remove it'.format(
                ongoing_rule_id))
            database.update(
                database.QUERY[mn() +
                               "_delete_ongoing_rule"].format(ongoing_rule_id))

        logging.info("Rule '{0}' canceled".format(rule_id))

    update_all_rules()

    try:
        response_status = garden_controller.branch_status()

        arr = form_responce_for_branches(response_status)
        send_branch_status_message('branch_status', arr)
    except Exception as e:
        logging.error(e)
        logging.error("Can't get Raspberri Pi pin status. Exception occured")
        abort(500)

    return json.dumps({'status': 'OK'})
Esempio n. 7
0
def edit_ongoing_rule():
    """User can edit ongoing rule from ui."""
    rules = request.json['rules']
    now = datetime.datetime.now()

    for rule in rules:
        rule['line_id'] = int(rule['line_id'])
        rule['time'] = convert_to_datetime(rule['time'])
        rule['intervals'] = int(rule['intervals'])
        rule['time_wait'] = int(rule['time_wait'])
        rule['repeat_value'] = int(rule['repeat_value'])
        rule['date_start'] = convert_to_datetime(rule['date_start'])
        rule['time_start'] = convert_to_datetime(rule['time_start'])
        rule['date_time_start'] = datetime.datetime.combine(
            rule['date_start'], rule['time_start'].time())
        rule['end_date'] = convert_to_datetime(rule['end_date'])
        rule['rule_id'] = rule['rule_id']
        rule['days'] = -1

        if rule['date_start'].date() == rule['end_date'].date():
            date_delta = rule['end_date'].date() - now.date()
            if date_delta.days == 0:
                rule['days'] = 0
            if date_delta.days == 1:
                rule['days'] = 1

        # "UPDATE ongoing_rules
        # SET line_id = {0}, time = {1}, intervals = {2}, time_wait = {3}, repeat_value={4}, date_time_start='{5}'"
        # end_date = '{6}' WHERE rule_id = '{7}'"
        database.update(database.QUERY[mn() + '_ongoing'].format(
            rule['line_id'], rule['time'], rule['intervals'],
            rule['time_wait'], rule['repeat_value'], rule['date_time_start'],
            rule['end_date'], rule['rule_id']))

        # update rules;
        update_rules_from_ongoing_rules(rule)
        # update_all_rules()
        logging.info("Ongoing rule modified. {0}".format(str(rule)))

        send_ongoing_rule_message('edit_ongoing_rule', rule)

    return json.dumps({'status': 'OK'})
Esempio n. 8
0
def deactivate_branch():
    """Route is used to disable branch."""
    """Can be executed manaully - row will be added to database
    or with rules service - no new row will be added to database"""
    branch_id = int(request.args.get('id'))
    mode = request.args.get('mode')
    if (mode is None):
        logging.error("no 'mode' parameter passed")
        abort(500)

    try:
        response_off = retry_branch_off(branch_id=branch_id)
    except Exception as e:
        logging.error(e)
        logging.error("Can't turn off branch id={0}. Exception occured".format(
            branch_id))
        abort(500)

    if (mode == 'manually'):
        now = datetime.datetime.now()
        if get_next_rule_from_redis(branch_id) is not None:
            database.update(database.QUERY[mn() + '_1'].format(
                get_next_rule_from_redis(branch_id)['interval_id']))
        else:
            database.update(database.QUERY[mn() + '_2'].format(
                branch_id, 2, 4, now.date(), now, None))

        set_next_rule_to_redis(branch_id,
                               database.get_next_active_rule(branch_id))
        logging.info("Rule '{0}' added".format(
            str(get_next_rule_from_redis(branch_id))))

        logging.info("Branch '{0}' deactivated manually".format(branch_id))
    else:
        logging.info('No new entries is added to database.')

    arr = form_responce_for_branches(response_off)
    send_branch_status_message('branch_status', arr)

    return jsonify(branches=arr)
Esempio n. 9
0
def enable_rule():
    """Synch with redis each 10 seconds. Execute rules if any."""
    try:
        logging.info("enable rule thread started.")

        logging.info("Updating rules on start.")
        update_all_rules()
        logging.debug("rules updated")

        sync_rules_from_redis()
        rules_to_log()

        now_time = datetime.datetime.now()
        while True:
            # logging.info("enable_rule_daemon heartbeat. RULES_FOR_BRANCHES: {0}".format(str(RULES_FOR_BRANCHES)))
            time.sleep(10)
            sync_rules_from_redis()

            for rule in RULES_FOR_BRANCHES:
                if rule is None:
                    continue

                delta = datetime.datetime.now() - now_time
                if delta.seconds >= 60 * 10:
                    rules_to_log()
                    now_time = datetime.datetime.now()

                if (datetime.datetime.now() >=
                    (rule['timer'] -
                     datetime.timedelta(minutes=VIBER_SENT_TIMEOUT))):
                    try:
                        send_to_viber_bot(rule)
                    except Exception as e:
                        logging.error(
                            "Can't send rule {0} to viber. Exception occured. {1}"
                            .format(str(rule), e))

                if (datetime.datetime.now() >= rule['timer']):
                    if (inspect_conditions(rule) is False):
                        logging.info(
                            "Rule can't be executed cause of rain volume too high"
                        )
                        database.update(
                            database.QUERY[mn() + "_canceled_by_rain"].format(
                                rule['id']))
                        set_next_rule_to_redis(
                            rule['line_id'],
                            database.get_next_active_rule(rule['line_id']))
                        continue

                    logging.info("Rule '{0}' execution started".format(
                        str(rule)))

                    try:
                        if rule['rule_id'] == 1:
                            branch_on(rule['line_id'], rule['time'])

                        if rule['rule_id'] == 2:
                            branch_off(rule['line_id'])

                    except Exception as e:
                        logging.error(
                            "Rule '{0}' can't be executed. Exception occured. {1}"
                            .format(str(rule), e))
                        # Set failed state
                        database.update(
                            database.QUERY[mn() + '_cancel_interval'].format(
                                rule['interval_id'], 3))
                        database.update(database.QUERY[mn()].format(
                            rule['id'], 3))
                    else:
                        logging.info("Rule '{0}' is done.".format(str(rule)))
                        # Set ok state
                        database.update(database.QUERY[mn()].format(
                            rule['id'], 2))
                    finally:
                        logging.info("get next active rule")
                        set_next_rule_to_redis(
                            rule['line_id'],
                            database.get_next_active_rule(rule['line_id']))
    except Exception as e:
        logging.error("enable rule thread exception occured. {0}".format(e))
    finally:
        logging.info("enable rule thread stopped.")
Esempio n. 10
0
def activate_branch():
    """Blablbal."""
    # ============ check input params =======================
    mode = request.args.get('mode')
    if (mode is None):
        logging.error("no 'mode' parameter passed")
        abort(500)

    if (mode == 'single'):
        branch_id = int(request.args.get('id'))
        time_min = int(request.args.get('time_min'))
    elif (mode == 'interval'):
        branch_id = int(request.args.get('id'))
        time_min = int(request.args.get('time_min'))
        time_wait = int(request.args.get('time_wait'))
        num_of_intervals = int(request.args.get('quantity'))
    elif (mode == 'auto'):
        branch_id = int(request.args.get('id'))
        time_min = int(request.args.get('time_min'))
    else:
        logging.error("incorrect mode parameter passed: {0}".format(mode))
        abort(500)
    # ============ check input params =======================

    try:
        response_arr = retry_branch_on(branch_id=branch_id, time_min=time_min)
    except Exception as e:
        logging.error(e)
        logging.error(
            "Can't turn on branch id={0}. Exception occured".format(branch_id))
        abort(500)

    # needs to be executed in both cases single and interval, but in in auto
    if (mode != 'auto'):
        interval_id = str(uuid.uuid4())
        now = datetime.datetime.now()
        stop_time = now + datetime.timedelta(minutes=time_min)

        database.update(database.QUERY[mn() + '_1'].format(
            branch_id, 1, 2, now.date(), now, interval_id, time_min))
        lastid = database.update(database.QUERY[mn() + '_1'].format(
            branch_id, 2, 1, now.date(), stop_time, interval_id, 0))
        logging.debug("lastid:{0}".format(lastid))

        res = database.select(database.QUERY[mn() + '_2'].format(lastid),
                              'fetchone')
        logging.debug("res:{0}".format(res[0]))

        set_next_rule_to_redis(
            branch_id, {
                'id': res[0],
                'line_id': res[1],
                'rule_id': res[2],
                'user_friendly_name': res[6],
                'timer': res[3],
                'interval_id': res[4],
                'time': res[5]
            })
        logging.info("Rule '{0}' added".format(
            str(database.get_next_active_rule(branch_id))))

    if (mode == 'interval'):
        # first interval is already added
        for x in range(2, num_of_intervals + 1):
            start_time = stop_time + datetime.timedelta(minutes=time_wait)
            stop_time = start_time + datetime.timedelta(minutes=time_min)
            database.update(database.QUERY[mn() + '_1'].format(
                branch_id, 1, 1, now.date(), start_time, interval_id,
                time_min))
            database.update(database.QUERY[mn() + '_1'].format(
                branch_id, 2, 1, now.date(), stop_time, interval_id, 0))
            logging.info(
                "Start time: {0}. Stop time: {1} added to database".format(
                    str(start_time), str(stop_time)))

    if (mode == 'auto'):
        logging.info(
            "Branch '{0}' activated from rules service".format(branch_id))
    else:
        logging.info("Branch '{0}' activated manually".format(branch_id))

    arr = form_responce_for_branches(response_arr)
    send_branch_status_message('branch_status', arr)

    return jsonify(branches=arr)