Beispiel #1
0
def download(data):
    db = DbClient()
    if not validate_token(request):
        return render_template("error_page.html")
    user = get_data_by_token(request.cookies.get('token', None))
    if data == 'full':
        sensors = db.sensors.get_sensors()
        sensors = [tuple(sensor.values()) for sensor in sensors]
        utils.write_sensors_to_csv(sensors)
    return send_file('export.csv', attachment_filename='export.csv')
Beispiel #2
0
def new_databins():
    db = DbClient()
    if not validate_token(request):
        return render_template("error_page.html")
    user = get_data_by_token(request.cookies.get('token', None))
    if request.method == 'POST':
        result = request.form
        if result['radio-stacked'] == "capacity":
            sensors = db.sensors.get_sensor_between_capacity(
                result['capacity'], 100)
            if not sensors:
                return render_template("databins.html")
        elif result['radio-stacked'] == 'id':
            sensors = db.sensors.get_sensor_by_id(result['Bin_ID'])
            if sensors is not None:
                pass
            else:
                return render_template("databins.html")
        else:
            sensors = db.sensors.get_sensor_by_address(result['address'])
            if not sensors:
                return render_template("databins.html")
    else:
        sensors_low = db.sensors.get_sensor_between_capacity(0, 25)
        if not sensors_low:
            sensors_low = []
        sensors_mid = db.sensors.get_sensor_between_capacity(26, 75)
        if not sensors_mid:
            sensors_mid = []
        sensors_full = db.sensors.get_sensor_between_capacity(76, 100)
        if not sensors_full:
            sensors_full = []
        sensors = sensors_low + sensors_mid + sensors_full
    if type(sensors) is dict:
        tuple_sensors = [
            tuple(sensors.values()),
        ]
        sensors = [
            sensors,
        ]
    else:
        tuple_sensors = [tuple(sensor.values()) for sensor in sensors]
    utils.write_sensors_to_csv(tuple_sensors)
    return render_template("databins.html", sensors=sensors)
Beispiel #3
0
def new_about():
    if not validate_token(request):
        return render_template("error_page.html")
    user = get_data_by_token(request.cookies.get('token', None))
    return render_template("about.html")
Beispiel #4
0
def new_stats():
    db = DbClient()
    if not validate_token(request):
        return render_template("error_page.html")
    user = get_data_by_token(request.cookies.get('token', None))
    return render_template("analytics.html")
Beispiel #5
0
def new_calc():
    db = DbClient()
    if not validate_token(request):
        return render_template("error_page.html")
    user = get_data_by_token(request.cookies.get('token', None))
    username = db.users.get_user_by_id(user['user_id'])['user']
    config_trashold = conf.trash_threshold
    present_treshold = conf.trash_threshold
    if request.method == 'POST':
        if request.form.get('range'):
            present_percent = request.form.get('range')
            capacity = int(present_percent)
            present_treshold = capacity
        if request.form.get('trashold'):
            result = request.form.get('trashold')
            if '%' in result:
                threshold = int(result[:-1])
            else:
                threshold = int(result)
            conf.trash_threshold = int(threshold)
            capacity = int(conf.trash_threshold)
            config_trashold = capacity
            present_treshold = capacity
    else:
        capacity = conf.trash_threshold
    pickup_sensors = db.sensors.get_sensor_over_x_capacity(capacity)
    if not pickup_sensors:
        pickup_sensors = []
    remain_sensors = db.sensors.get_sensor_under_x_capacity(capacity)
    if not remain_sensors:
        remain_sensors = []
    threshold = conf.trash_threshold
    risk_sensors = []
    total_trash_to_pickup = 0
    for sensor in remain_sensors:
        fill_avg = utils.get_avg_fill_per_sensor(
            db.statistics.get_sensor_stat_by_id(sensor['id']))
        if int(sensor['capacity']) + fill_avg >= threshold:
            risk_sensors.append(sensor)
        sensor['fill_avg'] = fill_avg
    for sensor in pickup_sensors:
        fill_avg = utils.get_avg_fill_per_sensor(
            db.statistics.get_sensor_stat_by_id(sensor['id']))
        total_trash_to_pickup += sensor['capacity']
        sensor['fill_avg'] = fill_avg
        calculate_capacity = sensor['capacity']
        days_until_full = 0
        while calculate_capacity <= capacity:
            calculate_capacity += fill_avg
            days_until_full += 1
        sensor['day_until_full'] = days_until_full
    truck_needed = round(total_trash_to_pickup / 1000)
    if truck_needed == 0:
        truck_needed = 1

    return render_template("calc.html",
                           sensors=pickup_sensors,
                           capacityint=capacity,
                           total_to_pickup=len(pickup_sensors),
                           config_trashold=config_trashold,
                           present_treshold=present_treshold,
                           risked=len(risk_sensors),
                           truck_needed=truck_needed,
                           unrisked=len(pickup_sensors) + len(remain_sensors) -
                           len(risk_sensors),
                           username=username)
Beispiel #6
0
def main():
    db = DbClient()
    try:
        if not validate_token(request):
            return render_template(
                "error_page.html",
                error_msg=
                "You are not login, please enter to login page to access to the system"
            )
        user = get_data_by_token(request.cookies.get('token', None))
        if request.method == 'POST':
            sensors_low = []
            sensors_mid = []
            sensors_full = []
            sensors, checked_list = utils.get_sensors_by_main_filters(request)
            for sensor in sensors:
                sensor = [
                    sensor,
                ]
                if int(sensor[0][2]) < 25:
                    sensors_low = sensors_low + sensor
                elif int(sensor[0][2]) < 75:
                    sensors_mid = sensors_mid + sensor
                else:
                    sensors_full = sensors_full + sensor
        else:
            sensors = []
            sensors_low = db.sensors.get_sensor_between_capacity(0, 25)
            if sensors_low:
                sensors += sensors_low
            sensors_mid = db.sensors.get_sensor_between_capacity(26, 75)
            if sensors_mid:
                sensors += sensors_mid
            sensors_full = db.sensors.get_sensor_between_capacity(76, 100)
            if sensors_full:
                sensors += sensors_full
            sensors = [tuple(sensor.values()) for sensor in sensors]
            utils.write_sensors_to_csv(sensors)
            sensors = [[x[1], x[4], x[5], x[3], x[2], x[0]] for x in sensors]
            return render_template("index.html",
                                   sensors=sensors,
                                   persent_count=len(sensors),
                                   total_count=len(sensors))
        utils.write_sensors_to_csv(sensors)
        sensor_count = db.sensors.get_count_sensors()['count(*)']
        sensors = [[x[1], x[4], x[5], x[3], x[2], x[0]] for x in sensors]
        return render_template("index.html",
                               sensors=sensors,
                               capacity_empty=checked_list[0],
                               capacity_mid=checked_list[1],
                               capacity_full=checked_list[2],
                               over_trashold=checked_list[3],
                               below_trashold=checked_list[4],
                               ConnectedBins=checked_list[5],
                               FailedBins=checked_list[6],
                               persent_count=len(sensors),
                               total_count=sensor_count)

    except Exception as e:
        print(traceback.format_exc())
        print(e)