xref: /openbmc/bmcweb/scripts/websocket_test.py (revision 72fff0a87162a166a7d7ea721dbfeeaf567d8670)
170ee8cbdSEd Tanous#!/usr/bin/env python3
270ee8cbdSEd Tanous
370ee8cbdSEd Tanous# Example of streaming sensor values from openbmc using the /subscribe api
470ee8cbdSEd Tanous# requires websockets package to be installed
570ee8cbdSEd Tanous
6dfa3fdc3SPatrick Williamsimport argparse
770ee8cbdSEd Tanousimport asyncio
870ee8cbdSEd Tanousimport base64
970ee8cbdSEd Tanousimport json
10dfa3fdc3SPatrick Williamsimport ssl
11dfa3fdc3SPatrick Williams
12dfa3fdc3SPatrick Williamsimport websockets
139b243a4eSEd Tanous
1470ee8cbdSEd Tanousparser = argparse.ArgumentParser()
1570ee8cbdSEd Tanousparser.add_argument("--host", help="Host to connect to", required=True)
1670ee8cbdSEd Tanousparser.add_argument(
17dfa3fdc3SPatrick Williams    "--username", help="Username to connect with", default="root"
18dfa3fdc3SPatrick Williams)
1970ee8cbdSEd Tanousparser.add_argument("--password", help="Password to use", default="0penBmc")
20dfa3fdc3SPatrick Williamsparser.add_argument(
21dfa3fdc3SPatrick Williams    "--ssl", default=True, action=argparse.BooleanOptionalAction
22dfa3fdc3SPatrick Williams)
239b243a4eSEd Tanous
2470ee8cbdSEd Tanousargs = parser.parse_args()
2570ee8cbdSEd Tanous
2670ee8cbdSEd Tanoussensor_type_map = {
2770ee8cbdSEd Tanous    "voltage": "Volts",
2870ee8cbdSEd Tanous    "power": "Watts",
2970ee8cbdSEd Tanous    "fan": "RPM",
3070ee8cbdSEd Tanous    "fan_tach": "RPM",
3170ee8cbdSEd Tanous    "temperature": "Degrees C",
3270ee8cbdSEd Tanous    "altitude": "Meters",
3370ee8cbdSEd Tanous    "current": "Amps",
3470ee8cbdSEd Tanous    "energy": "Joules",
35dfa3fdc3SPatrick Williams    "cfm": "CFM",
3670ee8cbdSEd Tanous}
3770ee8cbdSEd Tanous
3870ee8cbdSEd Tanous
3970ee8cbdSEd Tanousasync def hello():
40dfa3fdc3SPatrick Williams    protocol = "ws"
414a750f43SNan Zhou    if args.ssl:
42dfa3fdc3SPatrick Williams        protocol += "s"
43dfa3fdc3SPatrick Williams    uri = "{}://{}/subscribe".format(protocol, args.host)
4470ee8cbdSEd Tanous    ssl_context = ssl.SSLContext()
45dfa3fdc3SPatrick Williams    authbytes = "{}:{}".format(args.username, args.password).encode("ascii")
46dfa3fdc3SPatrick Williams    auth = "Basic {}".format(base64.b64encode(authbytes).decode("ascii"))
4770ee8cbdSEd Tanous    headers = {"Authorization": auth}
48*72fff0a8SGunnar Mills    async with (
49*72fff0a8SGunnar Mills        websockets.connect(uri, ssl=ssl_context, extra_headers=headers)
50*72fff0a8SGunnar Mills        if args.ssl
51*72fff0a8SGunnar Mills        else websockets.connect(uri, extra_headers=headers)
52dfa3fdc3SPatrick Williams    ) as websocket:
53dfa3fdc3SPatrick Williams        request = json.dumps(
54dfa3fdc3SPatrick Williams            {
5570ee8cbdSEd Tanous                "paths": ["/xyz/openbmc_project/sensors"],
56dfa3fdc3SPatrick Williams                "interfaces": ["xyz.openbmc_project.Sensor.Value"],
57dfa3fdc3SPatrick Williams            }
58dfa3fdc3SPatrick Williams        )
5970ee8cbdSEd Tanous        await websocket.send(request)
609b243a4eSEd Tanous
619b243a4eSEd Tanous        while True:
6270ee8cbdSEd Tanous            payload = await websocket.recv()
6370ee8cbdSEd Tanous            j = json.loads(payload)
6470ee8cbdSEd Tanous            path = j.get("path", "unknown/unknown")
6570ee8cbdSEd Tanous            name = path.split("/")[-1]
6670ee8cbdSEd Tanous            sensor_type = path.split("/")[-2]
6770ee8cbdSEd Tanous            units = sensor_type_map.get(sensor_type, "")
6870ee8cbdSEd Tanous            properties = j.get("properties", [])
6970ee8cbdSEd Tanous            value = properties.get("Value", None)
7070ee8cbdSEd Tanous            if value is None:
7170ee8cbdSEd Tanous                continue
7270ee8cbdSEd Tanous            print(f"{name:<20} {value:4.02f} {units}")
7370ee8cbdSEd Tanous
744a750f43SNan Zhou
7570ee8cbdSEd Tanousasyncio.get_event_loop().run_until_complete(hello())
76