Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 1 | #!/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 Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 6 | import argparse |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 7 | import asyncio |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 8 | import base64 |
| 9 | import json |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 10 | import ssl |
| 11 | |
| 12 | import websockets |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 13 | |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 14 | parser = argparse.ArgumentParser() |
| 15 | parser.add_argument("--host", help="Host to connect to", required=True) |
| 16 | parser.add_argument( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 17 | "--username", help="Username to connect with", default="root" |
| 18 | ) |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 19 | parser.add_argument("--password", help="Password to use", default="0penBmc") |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 20 | parser.add_argument( |
| 21 | "--ssl", default=True, action=argparse.BooleanOptionalAction |
| 22 | ) |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 23 | |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 24 | args = parser.parse_args() |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 25 | |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 26 | sensor_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 Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 35 | "cfm": "CFM", |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | |
| 39 | async def hello(): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 40 | protocol = "ws" |
Nan Zhou | 4a750f4 | 2022-02-24 13:25:01 -0800 | [diff] [blame] | 41 | if args.ssl: |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 42 | protocol += "s" |
| 43 | uri = "{}://{}/subscribe".format(protocol, args.host) |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 44 | ssl_context = ssl.SSLContext() |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 45 | authbytes = "{}:{}".format(args.username, args.password).encode("ascii") |
| 46 | auth = "Basic {}".format(base64.b64encode(authbytes).decode("ascii")) |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 47 | headers = {"Authorization": auth} |
Gunnar Mills | 72fff0a | 2023-02-06 12:29:52 -0600 | [diff] [blame] | 48 | 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 Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 52 | ) as websocket: |
| 53 | request = json.dumps( |
| 54 | { |
| 55 | "paths": ["/xyz/openbmc_project/sensors"], |
| 56 | "interfaces": ["xyz.openbmc_project.Sensor.Value"], |
| 57 | } |
| 58 | ) |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 59 | 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 Zhou | 4a750f4 | 2022-02-24 13:25:01 -0800 | [diff] [blame] | 74 | |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 75 | asyncio.get_event_loop().run_until_complete(hello()) |