本文へ進む

本体の情報を読む

名前・識別子・アドレス・バージョン・総走行距離を読みます。

必要な本体
1台以上。見つかった本体を順番に接続。
線路・準備
線路は不要。本体は停止させておきます。
終了方法
情報を表示し、各本体を切断して終了。走行の操作はありません。
保存する場所
matrain-python-async/examples/get_train_info.py
実行する場所
matrain-python-asyncフォルダー内。.venvのPythonを使います。

Mac:.venv/bin/python examples/get_train_info.pyWindows:.venv\Scripts\python.exe examples\get_train_info.py

通常終了の処理を原例のまま掲載しています。例外やCtrl+Cでの停止が保証されないため、異常時は本体のボタンを短く押して止めます。切断だけを停止とみなさないでください。

コード全文

get_train_info.py
# Matrain publication modification: licensing/source notice added.
# Executable behavior is unchanged except for the adaptations documented below.
# Source: https://github.com/intelino-code/intelino-trainlib-async-py
# Source commit: 298e1fd4091e069180c30f338b4c285b13accbd0
# This software is made available by Innokind, Inc. DBA Intelino, under the
# terms of the Intelino Public License Agreement, Version 1.0 located at
# https://intelino.com/intelino-public-license. BY INSTALLING, DOWNLOADING,
# ACCESSING, USING OR DISTRIBUTING ANY OF THE SOFTWARE, YOU AGREE TO THE TERMS OF
# SUCH LICENSE AGREEMENT.

import asyncio
from intelino.trainlib_async import TrainScanner, Train


async def print_info_card(train: Train, full: bool = True):
    print(f'{"="*40}')
    print(f'{"Train info":^40s}')
    print(f'{"="*40}')
    print(f'{"Advertised name":<15s}{train.name:>25s}')
    print(f'{"ID":<4s}{train.id:>36s}')
    print(f'{"="*40}')

    if not full:
        return

    # any further information requires the train to be connected
    if not train.is_connected:
        await train.connect()

    msg = await train.get_mac_address()
    print(f'{"MAC address":<15s}{msg.mac_address:>25s}')

    msg = await train.get_uuid()
    print(f'{"UUID":<15s}{msg.uuid:>25s}')

    msg = await train.get_version_info()
    print(f'{"FW version":<15s}{str(msg.fw_version):>25s}')
    print(f'{"BLE API version":<15s}{str(msg.ble_api_version):>25s}')

    msg = await train.get_stats_lifetime_odometer()
    print(f'{"Odometer":<15s}{msg.lifetime_odometer_meters:18.2f} meters')

    print(f'{"="*40}')


async def main():
    # collection of our trains
    # find all trains (search for max 2 seconds)
    trains = await TrainScanner(timeout=2.0).get_trains(
        connect=False,
    )

    # inspect them one by one
    for train in trains:
        await print_info_card(train, full=True)
        await train.disconnect()

    if len(trains) == 0:
        print("No trains found.")


if __name__ == "__main__":
    asyncio.run(main())

冒頭に配布条件と出典のコメントを追加しています。表示している全文と、個別保存・ZIP内のこのファイルは同じです。ライセンス・出典を読む

ファイルのダウンロード

ダウンロードの確認

ファイル名:

この端末に保存します。閲覧中のページはそのまま残ります。