예제 #1
0
파일: wifi.py 프로젝트: nstoik/fd_device
def set_interfaces(interfaces: List):
    """Set interface information into database and configure hardware accordingly.

    Args:
        interfaces (List): A list of dictionaries with the required information.
    """

    session = get_session()
    wifi_ap_present = False

    for interface in interfaces:
        try:
            db_result = (session.query(Interface).filter_by(
                interface=interface["name"]).one())
        except NoResultFound:
            db_result = Interface(interface["name"])
            session.add(db_result)
        db_result.is_active = True
        db_result.is_for_fm = interface["is_for_fm"]
        db_result.state = interface["state"]
        if interface["state"] == "ap":
            wifi_ap_present = True
        if "creds" in interface:
            add_wifi_network(
                wifi_name=interface["creds"]["ssid"],
                wifi_password=interface["creds"]["password"],
                interface=db_result,
            )
    session.commit()

    if wifi_ap_present:
        set_ap_mode()
    else:
        set_wpa_mode()
예제 #2
0
def populate_interfaces(tables, dbsession):
    """Populate interfaces into the database."""

    interface = Interface("wlan0")
    interface.is_active = True
    interface.is_for_fm = True
    interface.is_external = True
    interface.state = "dhcp"

    interface.save(dbsession)
예제 #3
0
def test_add_wifi_network_with_interface(dbsession):
    """Test the add_wifi_netwrok function passing in an Interface."""

    interface = Interface("wlan0")
    interface.state = "dhcp"
    interface.save(dbsession)

    wifi = add_wifi_network(
        wifi_name="TestWiFiName", wifi_password="******", interface=interface
    )

    assert wifi.name == "TestWiFiName"
    assert wifi.password == "password"
    assert wifi.interface.id == interface.id
    assert wifi.mode == "dhcp"
    assert wifi in interface.credentials
예제 #4
0
def test_add_wifi_network(dbsession):
    """Test the add_wifi_network function."""

    interface = Interface("wlan0")
    interface.state = "dhcp"
    interface.save(dbsession)

    wifi = add_wifi_network(wifi_name="TestWiFiName", wifi_password="******")

    dbsession.add(wifi)

    assert wifi.name == "TestWiFiName"
    assert wifi.password == "password"
    assert wifi.interface == interface
    assert wifi.mode == "dhcp"
    assert wifi in interface.credentials
예제 #5
0
파일: wifi.py 프로젝트: nstoik/fd_device
def refresh_interfaces():
    """Refresh all interfaces. Update with current information."""

    session = get_session()
    ap_present = False

    interfaces = get_interfaces()

    # update all interfaces.active to be False by default
    session.query(Interface).update({Interface.is_active: False})

    for my_interface in interfaces:
        try:
            interface = session.query(Interface).filter_by(
                interface=my_interface).one()

            interface.is_active = True
            # see if there is an interface that is configured for an ap
            if interface.state == "ap":
                ap_present = True

        # must be a new interface so lets add it
        except NoResultFound:
            new_interface = Interface(my_interface)
            new_interface.is_active = True
            new_interface.is_for_fm = False
            new_interface.state = "dhcp"
            session.add(new_interface)

    session.commit()
    session.close()

    if ap_present:
        set_ap_mode()
    else:
        set_wpa_mode()