Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bridget: check if ports are in use by autopilot when creating a new bridge #2240

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions core/frontend/src/components/bridges/BridgeCreationDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
:loading="updating_serial_ports"
item-text="name"
:item-value="(item) => item.by_path ? item.by_path : item.name"
:item-disabled="(item) => item.current_user !== null"
dense
>
<template #item="{ item }">
Expand All @@ -36,6 +37,16 @@
<v-list-item-content dense>
<v-list-item-title md-1>
Device: {{ item.name }}
<v-chip
v-if="item.current_user"
class="ma-2 pl-2 pr-2"
color="red"
pill
x-small
text-color="white"
>
In use by {{ item.current_user }}
</v-chip>
</v-list-item-title>
<v-list-item-subtitle class="text-wrap">
Path: {{ item.by_path ? item.by_path : item.name }}
Expand Down Expand Up @@ -115,8 +126,10 @@
import { formatDistanceToNow } from 'date-fns'
import Vue from 'vue'

import * as AutopilotManager from '@/components/autopilot/AutopilotManagerUpdater'
import DevicePathHelper from '@/components/common/DevicePathHelper.vue'
import Notifier from '@/libs/notifier'
import autopilot from '@/store/autopilot_manager'
import bridget from '@/store/bridget'
import system_information from '@/store/system-information'
import { Baudrate } from '@/types/common'
Expand Down Expand Up @@ -172,17 +185,17 @@ export default Vue.extend({
},
available_serial_ports(): SerialPortInfo[] {
const system_serial_ports: SerialPortInfo[] | undefined = system_information.serial?.ports
if (system_serial_ports === undefined || system_serial_ports.isEmpty()) {
return bridget.available_serial_ports.map((port) => ({
name: port,
by_path: port,
by_path_created_ms_ago: null,
udev_properties: null,
}))
if (!system_serial_ports) {
return []
}

return system_serial_ports
.filter((serial_info) => bridget.available_serial_ports.includes(serial_info.name))
.map((serial_info) => ({
...serial_info,
current_user: autopilot.autopilot_serials.some(
(serial) => serial.endpoint === serial_info.name,
) ? 'autopilot' : null,
}))
},
bridge_mode(): string {
switch (this.bridge.ip) {
Expand All @@ -204,6 +217,9 @@ export default Vue.extend({
return this.updating_serial_ports ? 'Fetching available serial ports...' : 'Serial port'
},
},
async mounted() {
await AutopilotManager.fetchAutopilotSerialConfiguration()
},
methods: {
create_time_ago(ms_time: number): string {
const time_now = new Date().valueOf()
Expand Down
2 changes: 2 additions & 0 deletions core/frontend/src/types/system-information/serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface SerialPortInfo {
by_path_created_ms_ago: number | null
// Udev information from the device
udev_properties: JSONValue | null,
// Is the port in use? by whom?
current_user: string | null,
}

/** Base structure that provides serial port information */
Expand Down
28 changes: 9 additions & 19 deletions core/services/bridget/bridget.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import logging
from typing import Dict, List

import requests
from bridges.bridges import Bridge
from bridges.serialhelper import Baudrate
from commonwealth.settings.manager import Manager
from pydantic import BaseModel, conint
from serial.tools.list_ports_linux import SysFS, comports
from serial.tools.list_ports_linux import SysFS

from settings import BridgeSettingsSpecV1, SettingsV1

Expand Down Expand Up @@ -48,25 +49,14 @@ def __init__(self) -> None:
except Exception as error:
logging.exception(f"Could not add bridge '{bridge_settings_spec}'. {error}")

def is_port_available(self, port: str) -> bool:
if port in [bridge.serial_path for bridge in self._bridges]:
return False
try:
with open(port, mode="r", encoding="utf-8"):
pass
return True
except Exception:
return False

def available_serial_ports(self) -> List[str]:
available_ports = []
for port in comports():
if not self.is_port_available(port.device):
logging.debug(f"Port {port.device} found but not available.")
continue
available_ports.append(port.device)
logging.debug(f"Port {port.device} found and available.")
return available_ports
try:
response = requests.get("http://localhost:6030/serial", timeout=1)
data = response.json()
return [port["name"] for port in data["ports"] if port["name"] is not None]
except requests.RequestException as e:
print(f"Error fetching data: {e}")
return []

def get_bridges(self) -> List[BridgeSpec]:
return [spec for spec, bridge in self._bridges.items()]
Expand Down