File: fetch_param.py

package info (click to toggle)
python-bsblan 5.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: python: 7,035; makefile: 3
file content (67 lines) | stat: -rw-r--r-- 1,840 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Fetch one or more BSB-LAN parameters and print the raw API response.

Usage:
    export BSBLAN_HOST=10.0.2.60
    export BSBLAN_PASSKEY=your_passkey  # if needed

    # Single parameter
    cd examples && python fetch_param.py 3113

    # Multiple parameters
    cd examples && python fetch_param.py 3113 8700 8740
"""

from __future__ import annotations

import argparse
import asyncio
import json

from bsblan import BSBLAN, BSBLANConfig
from discovery import get_bsblan_host, get_config_from_env


async def fetch_parameters(param_ids: list[str]) -> None:
    """Fetch and print raw API response for the given parameter IDs.

    Args:
        param_ids: List of BSB-LAN parameter IDs to fetch.

    """
    host, port = await get_bsblan_host()
    env = get_config_from_env()

    config = BSBLANConfig(
        host=host,
        port=port,
        passkey=str(env["passkey"]) if env.get("passkey") else None,
        username=str(env["username"]) if env.get("username") else None,
        password=str(env["password"]) if env.get("password") else None,
    )

    params_string = ",".join(param_ids)

    async with BSBLAN(config) as client:
        result = await client._request(  # noqa: SLF001
            params={"Parameter": params_string},
        )
        print(f"Raw API response for parameter(s) {params_string}:")
        print(json.dumps(result, indent=2, ensure_ascii=False))


def main() -> None:
    """Parse arguments and run the fetch."""
    parser = argparse.ArgumentParser(
        description="Fetch BSB-LAN parameters and print raw JSON response.",
    )
    parser.add_argument(
        "params",
        nargs="+",
        help="One or more BSB-LAN parameter IDs (e.g. 3113 8700)",
    )
    args = parser.parse_args()
    asyncio.run(fetch_parameters(args.params))


if __name__ == "__main__":
    main()