blob: fce9cd7e1431ed4b4ae45c8d98bfe10cd8028fd2 [file] [log] [blame]
Ed Tanous70ee8cb2019-10-03 11:36:05 -07001#!/usr/bin/env python3
2
3# Example of streaming sensor values from openbmc using the /subscribe api
4# requires websockets package to be installed
5
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -06006import argparse
Ed Tanous70ee8cb2019-10-03 11:36:05 -07007import asyncio
Ed Tanous70ee8cb2019-10-03 11:36:05 -07008import base64
9import json
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060010import ssl
11
12import websockets
Ed Tanous9b243a42018-08-03 14:33:10 -070013
Ed Tanous70ee8cb2019-10-03 11:36:05 -070014parser = argparse.ArgumentParser()
15parser.add_argument("--host", help="Host to connect to", required=True)
16parser.add_argument(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060017 "--username", help="Username to connect with", default="root"
18)
Ed Tanous70ee8cb2019-10-03 11:36:05 -070019parser.add_argument("--password", help="Password to use", default="0penBmc")
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060020parser.add_argument(
21 "--ssl", default=True, action=argparse.BooleanOptionalAction
22)
Ed Tanous9b243a42018-08-03 14:33:10 -070023
Ed Tanous70ee8cb2019-10-03 11:36:05 -070024args = parser.parse_args()
Ed Tanous9b243a42018-08-03 14:33:10 -070025
Ed Tanous70ee8cb2019-10-03 11:36:05 -070026sensor_type_map = {
27 "voltage": "Volts",
28 "power": "Watts",
29 "fan": "RPM",
30 "fan_tach": "RPM",
31 "temperature": "Degrees C",
32 "altitude": "Meters",
33 "current": "Amps",
34 "energy": "Joules",
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060035 "cfm": "CFM",
Ed Tanous70ee8cb2019-10-03 11:36:05 -070036}
37
38
39async def hello():
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060040 protocol = "ws"
Nan Zhou4a750f42022-02-24 13:25:01 -080041 if args.ssl:
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060042 protocol += "s"
43 uri = "{}://{}/subscribe".format(protocol, args.host)
Ed Tanous70ee8cb2019-10-03 11:36:05 -070044 ssl_context = ssl.SSLContext()
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060045 authbytes = "{}:{}".format(args.username, args.password).encode("ascii")
46 auth = "Basic {}".format(base64.b64encode(authbytes).decode("ascii"))
Ed Tanous70ee8cb2019-10-03 11:36:05 -070047 headers = {"Authorization": auth}
Gunnar Mills72fff0a2023-02-06 12:29:52 -060048 async with (
49 websockets.connect(uri, ssl=ssl_context, extra_headers=headers)
50 if args.ssl
51 else websockets.connect(uri, extra_headers=headers)
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060052 ) as websocket:
53 request = json.dumps(
54 {
55 "paths": ["/xyz/openbmc_project/sensors"],
56 "interfaces": ["xyz.openbmc_project.Sensor.Value"],
57 }
58 )
Ed Tanous70ee8cb2019-10-03 11:36:05 -070059 await websocket.send(request)
60
61 while True:
62 payload = await websocket.recv()
63 j = json.loads(payload)
64 path = j.get("path", "unknown/unknown")
65 name = path.split("/")[-1]
66 sensor_type = path.split("/")[-2]
67 units = sensor_type_map.get(sensor_type, "")
68 properties = j.get("properties", [])
69 value = properties.get("Value", None)
70 if value is None:
71 continue
72 print(f"{name:<20} {value:4.02f} {units}")
73
Nan Zhou4a750f42022-02-24 13:25:01 -080074
Ed Tanous70ee8cb2019-10-03 11:36:05 -070075asyncio.get_event_loop().run_until_complete(hello())