Skip to content

Commit cd39550

Browse files
committed
Simplify CAN and websocket init
In the React app, consolidate DBC cache handling and initialize the WebSocket service only after the DBC is loaded (keeps cleanup on unmount). These changes simplify startup behavior and avoid attempting privileged network commands from the bridge process.
1 parent ae33670 commit cd39550

3 files changed

Lines changed: 8 additions & 24 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,4 @@ test-logs/
238238
# Ignore generated service worker files
239239
sw.js
240240
pecan/src/assets/WFR25.dbc
241+
WFR25.dbc

kvaser-bridge/src/bridge.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,9 @@ async def start(self) -> None:
9393
# Open CAN bus
9494
try:
9595
if config.DEFAULT_CAN_INTERFACE == 'socketcan':
96-
import subprocess, shlex
9796
ch = config.DEFAULT_SOCKETCAN_CHANNEL
98-
# Bring interface down first (ignore errors - may already be down or not exist)
99-
subprocess.run(shlex.split(f'ip link set {ch} down'), capture_output=True)
100-
result = subprocess.run(
101-
shlex.split(f'ip link set {ch} up type can bitrate {self._bitrate}'),
102-
capture_output=True,
103-
)
104-
if result.returncode != 0:
105-
stderr = result.stderr.decode(errors='replace').strip()
106-
# Exit 2 with RTNETLINK "busy" means the interface is already UP - that's fine
107-
if result.returncode == 2 and 'busy' in stderr.lower():
108-
log.info('can0 already up, continuing')
109-
else:
110-
# Try opening anyway - maybe it was brought up externally (e.g. with sudo)
111-
log.warning('ip link set %s up returned %d: %s - attempting to open anyway', ch, result.returncode, stderr)
97+
# can0 is expected to be already up (via udev rule or manually).
98+
# See README: install /etc/udev/rules.d/80-can.rules to auto-bring up on plug-in.
11299
self._bus = can.interface.Bus(channel=ch, interface='socketcan')
113100
else:
114101
self._bus = can.interface.Bus(

pecan/src/App.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,25 @@ function App() {
6363
if (isUsingCache) {
6464
setDisplayDefaultBanner(false);
6565
setDisplayCacheBanner(true);
66-
// Persist the state
6766
localStorage.setItem('dbc-cache-active', 'true');
6867
} else {
69-
// Check if we previously had cache active
7068
const wasCacheActive = localStorage.getItem('dbc-cache-active') === 'true';
7169
if (wasCacheActive) {
7270
console.log("[App] Cache was previously active but not found now");
7371
}
7472
localStorage.removeItem('dbc-cache-active');
7573
}
76-
})();
77-
}, []);
7874

79-
// Initialize WebSocket service once when app loads
80-
useEffect(() => {
81-
webSocketService.initialize();
75+
// Initialize WebSocket only after DBC is loaded so the CAN processor
76+
// is created with the correct DBC file.
77+
webSocketService.initialize();
78+
})();
8279

83-
// Cleanup on unmount
8480
return () => {
8581
webSocketService.disconnect();
8682
serialService.disconnect();
8783
};
88-
}, []); // Empty dependency array = runs once on mount
84+
}, []);
8985

9086
return (
9187
<div className="h-screen flex flex-row overflow-y-auto">

0 commit comments

Comments
 (0)