diff --git a/openpilot/common/params_keys.h b/openpilot/common/params_keys.h index 704bb3ad5b..f2f8aeb310 100644 --- a/openpilot/common/params_keys.h +++ b/openpilot/common/params_keys.h @@ -317,6 +317,7 @@ inline static std::unordered_map keys = { {"EnableRemoteUI", {PERSISTENT | BACKUP, BOOL, "0"}}, {"KonikApi", {PERSISTENT | BACKUP, BOOL, "0"}}, {"KonikDongleId", {PERSISTENT, STRING}}, + {"SidebarNetworkIP", {PERSISTENT | BACKUP, BOOL, "0"}}, {"SidebarShowBattery", {PERSISTENT | BACKUP, BOOL, "0"}}, {"SidebarShowConnect", {PERSISTENT | BACKUP, BOOL, "1"}}, {"SidebarShowGps", {PERSISTENT | BACKUP, BOOL, "0"}}, diff --git a/openpilot/selfdrive/ui/cloudypilot/layouts/settings/personalize.py b/openpilot/selfdrive/ui/cloudypilot/layouts/settings/personalize.py index cd0283b59c..b3a1d1817a 100644 --- a/openpilot/selfdrive/ui/cloudypilot/layouts/settings/personalize.py +++ b/openpilot/selfdrive/ui/cloudypilot/layouts/settings/personalize.py @@ -19,6 +19,12 @@ def _initialize_items(self): description=lambda: tr("Show the signal strength and connection type in the sidebar."), param="SidebarShowNetwork", ) + network_ip = toggle_item_sp( + title=lambda: tr("Sidebar: Network IP Address"), + description=lambda: tr("Show the device's IP address instead of Wi-Fi when connected to Wi-Fi."), + param="SidebarNetworkIP", + enabled=lambda: network.action_item.get_state(), + ) tile_toggles = [ ("SidebarShowTemp", lambda: tr("Sidebar: Temperature"), lambda: tr("Show the device temperature status in the sidebar.")), @@ -42,7 +48,7 @@ def _initialize_items(self): enabled=lambda: temp.action_item.get_state(), ) - return [network, temp, temp_degrees, *self._tile_items[1:]] + return [network, network_ip, temp, temp_degrees, *self._tile_items[1:]] def _tile_item(self, param, title, desc): item = toggle_item_sp(title=title, description=desc, param=param) diff --git a/openpilot/selfdrive/ui/cloudypilot/layouts/sidebar.py b/openpilot/selfdrive/ui/cloudypilot/layouts/sidebar.py index 469a85f255..4085719aa1 100644 --- a/openpilot/selfdrive/ui/cloudypilot/layouts/sidebar.py +++ b/openpilot/selfdrive/ui/cloudypilot/layouts/sidebar.py @@ -1,4 +1,5 @@ import pyray as rl +import socket import time from collections.abc import Callable from openpilot.cereal import log @@ -31,6 +32,16 @@ NetworkType = log.DeviceState.NetworkType +def wifi_ip_address() -> str: + try: + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: + # Connecting a UDP socket only resolves the route, nothing is sent + sock.connect(("8.8.8.8", 80)) + return sock.getsockname()[0] + except OSError: + return "" + + class Colors: WHITE = rl.WHITE WHITE_DIM = rl.Color(255, 255, 255, 85) @@ -73,6 +84,7 @@ def __init__(self): SidebarSP.__init__(self) self._net_type = NETWORK_TYPES[NetworkType.none] self._net_strength = 0 + self._net_ip = "" self._temp_status = MetricData(tr_noop("TEMP"), tr_noop("GOOD"), Colors.GOOD) self._panda_status = MetricData(tr_noop("VEHICLE"), tr_noop("ONLINE"), Colors.GOOD) @@ -134,7 +146,9 @@ def _update_state(self): self._update_load_status(device_state) def _update_network_status(self, device_state): - self._net_type = NETWORK_TYPES.get(device_state.networkType.raw, tr_noop("Unknown")) + net_type = device_state.networkType.raw + self._net_type = NETWORK_TYPES.get(net_type, tr_noop("Unknown")) + self._net_ip = wifi_ip_address() if ui_state.sidebar_network_ip and net_type == NetworkType.wifi else "" strength = device_state.networkStrength self._net_strength = max(0, min(5, strength.raw + 1)) if strength.raw > 0 else 0 @@ -329,7 +343,13 @@ def _draw_network_indicator(self, rect: rl.Rectangle): # Network type text text_y = rect.y + 247 text_pos = rl.Vector2(rect.x + 58, text_y) - rl.draw_text_ex(self._font_regular, tr(self._net_type), text_pos, FONT_SIZE, 0, Colors.WHITE) + text, font_size = tr(self._net_type), FONT_SIZE + # The mic indicator sits on the same row, so the IP only shows while it's hidden + if self._net_ip and not self._recording_audio: + text = self._net_ip + max_width = rect.width - 58 - METRIC_MARGIN + font_size = min(FONT_SIZE, FONT_SIZE * max_width / measure_text_cached(self._font_regular, text, FONT_SIZE).x) + rl.draw_text_ex(self._font_regular, text, text_pos, font_size, 0, Colors.WHITE) def _draw_metrics(self, rect: rl.Rectangle): if gui_app.cloudypilot_ui(): diff --git a/openpilot/selfdrive/ui/cloudypilot/ui_state.py b/openpilot/selfdrive/ui/cloudypilot/ui_state.py index dc89cd8e34..3d6d4f2ef8 100644 --- a/openpilot/selfdrive/ui/cloudypilot/ui_state.py +++ b/openpilot/selfdrive/ui/cloudypilot/ui_state.py @@ -7,6 +7,7 @@ def __init__(self): self.use_feet: bool = False self.remote_ui: bool = False self.sidebar_show_network: bool = True + self.sidebar_network_ip: bool = False self.sidebar_show_temp: bool = True self.sidebar_show_vehicle: bool = True self.sidebar_show_connect: bool = True @@ -34,6 +35,7 @@ def update_params(self) -> None: self.use_feet = self.params.get_bool("UseFeetGPS") self.remote_ui = self.params.get_bool("EnableRemoteUI") self.sidebar_show_network = self.params.get_bool("SidebarShowNetwork") + self.sidebar_network_ip = self.params.get_bool("SidebarNetworkIP") self.sidebar_show_temp = self.params.get_bool("SidebarShowTemp") self.sidebar_show_vehicle = self.params.get_bool("SidebarShowVehicle") self.sidebar_show_connect = self.params.get_bool("SidebarShowConnect")