ネットワークの設定

このページは以前のファームウェアリリースについて説明しています。 このページを最新リリースで読む
このページは自動翻訳されたもので、まだレビューされていません。内容に相違がある場合は、英語の原文が優先されます。 英語の原文を読む

APIを介してネットワーク設定を読み出し、変更します。WebGUIでの操作は、ネットワーク設定を参照してください。 これらの例は Pythonライブラリを使用し、アプリケーションキーでサインインします。qb2-xxxxxxxxx をお使いのセンサーのホスト名またはIPアドレスに、application-key-for-qb2-xxxxxxxxx をキーのシークレットに置き換えてください。

ネットワーク設定の取得

GetConfigメソッドは、センサーに設定されたネットワーク設定を返します。

import blickfeld_qb2

token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")

# Open a secure and authenticated connection to the sensor
with blickfeld_qb2.Channel(fqdn_or_ip="qb2-xxxxxxxxx", token=token_factory) as channel:
    service = blickfeld_qb2.system.services.Network(channel)

    response = service.get_config()
    print(response)

出力:

Network()

ネットワーク設定が行われていないセンサーは空の設定を返し、DHCPとリンクローカルアドレスを使用します。 メソッドGetStatusは、使用中の設定をアドレスとともに active_config で返します。

固定アドレスの設定

DHCPサーバーのないネットワークでは、IPv4アドレスを手動で設定します。この例では、2つのアドレス、ゲートウェイ、2つのDNSサーバーを設定します。

変更後、センサーには新しいアドレスでのみ到達できます。到達できなくなった場合は、xref:operation:firmware_networking.adoc#_fall_back_ip_address192.168.26.26を介して接続してください。

import blickfeld_qb2

# Two addresses, the gateway and two DNS servers
manual = blickfeld_qb2.system.config.NetworkStatic(
    ipv4=blickfeld_qb2.system.config.NetworkStaticIpv4(
        ip=["10.0.2.15/24", "10.0.2.43/24"],
        gateway="10.0.2.2",
        dns_server=["8.8.4.4", "8.8.8.8"],
    )
)

token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")

# Open a secure and authenticated connection to the sensor
with blickfeld_qb2.Channel(fqdn_or_ip="qb2-xxxxxxxxx", token=token_factory) as channel:
    service = blickfeld_qb2.system.services.Network(channel)

    # Set the configuration
    service.set_config(manual=manual)

無線ネットワークの設定

この例では、センサーをWiFiネットワークに接続します。まずvalidateで設定を試し、センサーがWiFiネットワークでアドレスを取得した場合にのみ設定を適用します。

validateは設定を一時的に、最大で約1分間適用し、その後現在の設定に戻ります。その間、センサーは現在のネットワークから離れ、現在のアドレスでは到達できません。

import blickfeld_qb2
from grpclib import GRPCError

token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")

with blickfeld_qb2.Channel(fqdn_or_ip="qb2-xxxxxxxxx", token=token_factory) as channel:
    service = blickfeld_qb2.system.services.Network(channel)

    # Create a new configuration
    configuration = blickfeld_qb2.system.config.Network()

    # Activate DHCP
    configuration.dhcp = True

    # Configure the wifi settings
    configuration.wireless.ssid = "your_wifi_ssid"
    configuration.wireless.password = "your_wifi_password"

    # Validate the wireless configuration: the sensor applies it shortly and returns to its current configuration.
    # It raises an error with the reason when the sensor gets no address or no gateway in the WiFi network.
    try:
        response = service.validate(config=configuration)
    except GRPCError as error:
        raise SystemExit(f"Validation failed: {error.message}") from None
    print("Addresses in the WiFi network:", response.status.active_config.manual.ipv4.ip)

    # Apply the configuration
    service.set_config(dhcp=configuration.dhcp, wireless=configuration.wireless)

    # The sensor is now reachable under one of the addresses of the validation