-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdevice_info_display.py
More file actions
156 lines (138 loc) · 6.49 KB
/
device_info_display.py
File metadata and controls
156 lines (138 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from rich.console import Console
from rich.tree import Tree
from google.protobuf.json_format import MessageToDict
from synapse.client.device import Device
def visualize_configuration(info_dict, status):
nodes_status = status.get("signal_chain", {}).get("nodes", {})
config = info_dict.get("configuration", {})
if config:
tree = Tree("Configuration")
for index, node in enumerate(config.get("nodes", [])):
node_type = node.get("type", "").replace("k", "")
node_tree = tree.add(f"{node_type}")
node_tree.add(f"ID: {node.get('id', 'Unknown')}")
if node_type == "Application":
app = node.get("application", {})
name = app.get("name", "Unknown")
application_status = nodes_status[index].get("application", None)
running = application_status.get("running", False)
error_logs = application_status.get(
"error_logs", "Could not get error logs"
)
node_tree.add(f"Name: {name}")
node_tree.add(f"Running: {running}")
node_tree.add(f"Error Logs:\n{error_logs}")
elif node_type == "BroadbandSource":
source = node.get("broadband_source", {})
# Get the peripheral id and name
peripheral_id = source.get("peripheral_id", "Unknown")
peripherals = info_dict.get("peripherals", [])
peripheral_name = next(
(
p.get("name", "Unknown")
for p in peripherals
if p.get("peripheral_id") == peripheral_id
),
"Unknown",
)
node_tree.add(f"Connected to: {peripheral_name} (id: {peripheral_id})")
# Get the sample rate and bit width
node_tree.add(f"Sample Rate: {source.get('sample_rate_hz', 'Unknown')}")
node_tree.add(f"Bit Width: {source.get('bit_width', 'Unknown')}")
if "signal" in source and "electrode" in source["signal"]:
channels = source["signal"]["electrode"].get("channels", [])
electrode_ids = [
str(ch.get("electrode_id", "?")) for ch in channels
]
node_tree.add(
f"Electrodes ({len(channels)}): {', '.join(electrode_ids)}"
)
elif node_type == "OpticalStimulation":
source = node.get("optical_stimulation", {})
# Get the peripheral id and name
peripheral_id = source.get("peripheral_id", "Unknown")
peripherals = info_dict.get("peripherals", [])
peripheral_name = next(
(
p.get("name", "Unknown")
for p in peripherals
if p.get("peripheral_id") == peripheral_id
),
"Unknown",
)
node_tree.add(f"Connected to: {peripheral_name} (id: {peripheral_id})")
frame_rate = source.get("frame_rate", "Unknown")
node_tree.add(f"Frame Rate: {frame_rate} hz")
optical_stim_status = nodes_status[index].get(
"optical_stimulation", None
)
frames_written = optical_stim_status.get("frames_written", "None")
node_tree.add(f"Frames Written: {frames_written}")
return tree
def visualize_peripherals(info_dict):
tree = Tree("Peripherals")
peripherals = info_dict.get("peripherals", [])
if peripherals:
for peripheral in peripherals:
peripheral_tree = tree.add(
f"{peripheral.get('name', 'Unknown')} ({peripheral.get('vendor', 'Unknown')})"
)
peripheral_tree.add(f"ID: {peripheral.get('peripheral_id', 'Unknown')}")
peripheral_tree.add(f"Type: {peripheral.get('type', 'Unknown')}")
else:
tree.add("No peripherals found")
return tree
def visualize_storage_devices(status):
tree = Tree("Storage Devices")
storage_devices = status.get("storage", {}).get("storage_devices", [])
if storage_devices:
for storage_device in storage_devices:
storage_devices_tree = tree.add(
f"{storage_device.get('name', 'Unknown')}"
)
total = float(storage_device.get("total_gb", 0))
used = float(storage_device.get("used_gb", 0))
used_percent = (used / total * 100) if total > 0 else 0
storage_devices_tree.add(f"ID: {storage_device.get('storage_device_id', 'Unknown')}")
storage_devices_tree.add(f"Storage: {used_percent:.1f}% used ({used:.1f}GB / {total:.1f}GB)")
else:
tree.add("No storage devices found")
return tree
class DeviceInfoDisplay:
"""A class for displaying device information."""
def __init__(self):
self.console = Console()
def summary(self, device: Device):
info = device.info()
info_dict = MessageToDict(info, preserving_proto_field_name=True)
status = info_dict.get("status", {})
self.console.print(
f"Name: [bold cyan]{info_dict.get('name', 'Unknown')}[/bold cyan]",
)
if status:
state = status.get("state", "Unknown").replace("k", "")
state = {
"Running": "[green]Running[/green]",
"Stopped": "[red]Stopped[/red]",
"Unknown": "[yellow]Unknown[/yellow]",
}.get(state, "[yellow]Unknown[/yellow]")
self.console.print(f"Status: {state}")
self.console.print(
f"Serial: {info_dict.get('serial', 'Unknown')}",
highlight=False,
)
self.console.print(
f"Synapse Version: {info_dict.get('synapse_version', 'Unknown')}",
highlight=False,
)
self.console.print(
f"Firmware Version: {info_dict.get('firmware_version', 'Unknown')}",
highlight=False,
)
if status:
if "power" in status:
battery = status["power"].get("battery_level_percent", "N/A")
self.console.print(f"Battery: {battery}%", highlight=False)
self.console.print(visualize_storage_devices(status))
self.console.print(visualize_peripherals(info_dict))
self.console.print(visualize_configuration(info_dict, status))