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 | |
| 6 | import asyncio |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 7 | import ssl |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 8 | import websockets |
| 9 | import base64 |
| 10 | import json |
| 11 | import argparse |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 12 | |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 13 | parser = argparse.ArgumentParser() |
| 14 | parser.add_argument("--host", help="Host to connect to", required=True) |
| 15 | parser.add_argument( |
| 16 | "--username", help="Username to connect with", default="root") |
| 17 | parser.add_argument("--password", help="Password to use", default="0penBmc") |
Nan Zhou | 4a750f4 | 2022-02-24 13:25:01 -0800 | [diff] [blame] | 18 | parser.add_argument('--ssl', default=True, |
| 19 | action=argparse.BooleanOptionalAction) |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 20 | |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 21 | args = parser.parse_args() |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 22 | |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 23 | sensor_type_map = { |
| 24 | "voltage": "Volts", |
| 25 | "power": "Watts", |
| 26 | "fan": "RPM", |
| 27 | "fan_tach": "RPM", |
| 28 | "temperature": "Degrees C", |
| 29 | "altitude": "Meters", |
| 30 | "current": "Amps", |
| 31 | "energy": "Joules", |
| 32 | "cfm": "CFM" |
| 33 | } |
| 34 | |
| 35 | |
| 36 | async def hello(): |
Nan Zhou | 4a750f4 | 2022-02-24 13:25:01 -0800 | [diff] [blame] | 37 | protocol = 'ws' |
| 38 | if args.ssl: |
| 39 | protocol += 's' |
| 40 | uri = '{}://{}/subscribe'.format(protocol, args.host) |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 41 | ssl_context = ssl.SSLContext() |
| 42 | authbytes = "{}:{}".format(args.username, args.password).encode('ascii') |
| 43 | auth = "Basic {}".format(base64.b64encode(authbytes).decode('ascii')) |
| 44 | headers = {"Authorization": auth} |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 45 | async with \ |
| 46 | websockets.connect(uri, ssl=ssl_context, extra_headers=headers) \ |
Nan Zhou | 4a750f4 | 2022-02-24 13:25:01 -0800 | [diff] [blame] | 47 | if args.ssl else websockets.connect(uri, extra_headers=headers) \ |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 48 | as websocket: |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 49 | request = json.dumps({ |
| 50 | "paths": ["/xyz/openbmc_project/sensors"], |
| 51 | "interfaces": ["xyz.openbmc_project.Sensor.Value"] |
| 52 | }) |
| 53 | await websocket.send(request) |
| 54 | |
| 55 | while True: |
| 56 | payload = await websocket.recv() |
| 57 | j = json.loads(payload) |
| 58 | path = j.get("path", "unknown/unknown") |
| 59 | name = path.split("/")[-1] |
| 60 | sensor_type = path.split("/")[-2] |
| 61 | units = sensor_type_map.get(sensor_type, "") |
| 62 | properties = j.get("properties", []) |
| 63 | value = properties.get("Value", None) |
| 64 | if value is None: |
| 65 | continue |
| 66 | print(f"{name:<20} {value:4.02f} {units}") |
| 67 | |
Nan Zhou | 4a750f4 | 2022-02-24 13:25:01 -0800 | [diff] [blame] | 68 | |
Ed Tanous | 70ee8cb | 2019-10-03 11:36:05 -0700 | [diff] [blame] | 69 | asyncio.get_event_loop().run_until_complete(hello()) |