Exemplo n.º 1
0
def install(db):
    """Install view (first run)"""
    if db.query(database.User).all():
        redirect('/')

    db.query(database.MeasureType).delete()
    db.query(database.Provider).delete()
    db.query(database.Sensor).delete()

    electricity_type = database.MeasureType(name="Électricité")
    db.add(electricity_type)
    db.flush()

    providers = tools.update_providers(config.get("url_energy_providers"),
                                       True, db)

    for p in providers:
        p['is_day_night_rate'] = tools.is_day_night_rate(db, p)

    sensor = database.Sensor(name="CitizenWatt",
                             type_id=electricity_type.id,
                             last_timer=0)
    db.add(sensor)

    return {
        "login": '',
        "providers": providers,
        "start_night_rate": '',
        "end_night_rate": '',
        "base_address": '',
        "aes_key": '',
        "nrf_power": 'high'
    }
Exemplo n.º 2
0
def install(db):
    """Install view (first run)"""
    if db.query(database.User).all():
        redirect('/')

    db.query(database.MeasureType).delete()
    db.query(database.Provider).delete()
    db.query(database.Sensor).delete()

    electricity_type = database.MeasureType(name="Électricité")
    db.add(electricity_type)
    db.flush()

    providers = update_providers(True, db)

    sensor = database.Sensor(name="CitizenWatt",
                             type_id=electricity_type.id,
                             last_timer=0)
    db.add(sensor)

    return {"login": '',
            "providers": providers,
            "start_night_rate": '',
            "end_night_rate": '',
            "base_address": '',
            "aes_key": ''}
Exemplo n.º 3
0
def update_providers(url_energy_providers, fetch, db):
    """Updates the available providers. Simply returns them without updating if
    fetch is False.
    """
    try:
        assert (fetch)
        providers = requests.get(url_energy_providers).json()
    except (requests.ConnectionError, AssertionError):
        providers = db.query(database.Provider).all()
        if not providers:
            providers = []
        return to_dict(providers)

    old_current = db.query(database.Provider).filter_by(current=1).first()
    db.query(database.Provider).delete()

    providers = [
        dict(
            provider, **{
                'current': (1 if old_current
                            and old_current.name == provider["name"] else 0)
            }) for provider in providers
    ]

    for provider in providers:
        type_id = (db.query(database.MeasureType).filter_by(
            name=provider["type_name"]).first())
        if not type_id:
            type_db = database.MeasureType(name=provider["type_name"])
            db.add(type_db)
            db.flush()
            type_id = database.MeasureType(name=provider["type_name"]).first()

        provider_db = database.Provider(
            name=provider["name"],
            day_constant_watt_euros=provider["day_constant_watt_euros"],
            day_slope_watt_euros=provider["day_slope_watt_euros"],
            night_constant_watt_euros=provider["night_constant_watt_euros"],
            night_slope_watt_euros=provider["night_slope_watt_euros"],
            type_id=type_id.id,
            current=provider['current'],
            threshold=int(provider["threshold"]))
        db.add(provider_db)
    return providers