blob: 85b2c0d8effd0853dda4353c376ac99a222d17e6 [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
6import asyncio
Ed Tanous9b243a42018-08-03 14:33:10 -07007import ssl
Ed Tanous70ee8cb2019-10-03 11:36:05 -07008import websockets
9import base64
10import json
11import argparse
Ed Tanous9b243a42018-08-03 14:33:10 -070012
Ed Tanous70ee8cb2019-10-03 11:36:05 -070013parser = argparse.ArgumentParser()
14parser.add_argument("--host", help="Host to connect to", required=True)
15parser.add_argument(
16 "--username", help="Username to connect with", default="root")
17parser.add_argument("--password", help="Password to use", default="0penBmc")
Nan Zhou4a750f42022-02-24 13:25:01 -080018parser.add_argument('--ssl', default=True,
19 action=argparse.BooleanOptionalAction)
Ed Tanous9b243a42018-08-03 14:33:10 -070020
Ed Tanous70ee8cb2019-10-03 11:36:05 -070021args = parser.parse_args()
Ed Tanous9b243a42018-08-03 14:33:10 -070022
Ed Tanous70ee8cb2019-10-03 11:36:05 -070023sensor_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
36async def hello():
Nan Zhou4a750f42022-02-24 13:25:01 -080037 protocol = 'ws'
38 if args.ssl:
39 protocol += 's'
40 uri = '{}://{}/subscribe'.format(protocol, args.host)
Ed Tanous70ee8cb2019-10-03 11:36:05 -070041 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 Tanousf395daa2021-08-02 08:56:24 -070045 async with \
46 websockets.connect(uri, ssl=ssl_context, extra_headers=headers) \
Nan Zhou4a750f42022-02-24 13:25:01 -080047 if args.ssl else websockets.connect(uri, extra_headers=headers) \
Ed Tanousf395daa2021-08-02 08:56:24 -070048 as websocket:
Ed Tanous70ee8cb2019-10-03 11:36:05 -070049 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 Zhou4a750f42022-02-24 13:25:01 -080068
Ed Tanous70ee8cb2019-10-03 11:36:05 -070069asyncio.get_event_loop().run_until_complete(hello())