2 Commits

4 changed files with 5 additions and 383 deletions
+5 -79
View File
@@ -11,7 +11,6 @@ import dash
from dash import dcc, html, Input, Output, State from dash import dcc, html, Input, Output, State
import dash_bootstrap_components as dbc import dash_bootstrap_components as dbc
import numpy as np import numpy as np
import pandas as pd
from parser import parse_log, parse_csv from parser import parse_log, parse_csv
from decoder import decode_j1939_frames from decoder import decode_j1939_frames
@@ -21,7 +20,6 @@ from stats.frequency import calculate_frequency, plot_frequency
from stats.correlation import calculate_correlation, plot_correlation_heatmap from stats.correlation import calculate_correlation, plot_correlation_heatmap
from stats.entropy import calculate_byte_entropy, plot_entropy_heatmap from stats.entropy import calculate_byte_entropy, plot_entropy_heatmap
from logs.view import get_logs_table_component, prepare_logs_data from logs.view import get_logs_table_component, prepare_logs_data
from vehicle import get_vehicle_module
RAW_LOG_DIR = "data/logs" RAW_LOG_DIR = "data/logs"
PAGE_SIZE = 25000 PAGE_SIZE = 25000
@@ -133,28 +131,16 @@ app.layout = dbc.Container([
dbc.Tab(label="Overview", tab_id="overview", children=[ dbc.Tab(label="Overview", tab_id="overview", children=[
html.Div(id="overview-content") html.Div(id="overview-content")
]), ]),
dbc.Tab(label="Vehicles", tab_id="vehicles", children=[
dbc.Row([
dbc.Col(html.Label("Vehicle:", className="mt-2"), width="auto"),
dbc.Col(dcc.Dropdown(
id='vehicles-vehicle-selector',
options=[{'label': v, 'value': v} for v in DATA.keys()],
value=list(DATA.keys())[0] if DATA else None,
clearable=False
), width=3, className="me-4"),
], className="mb-3 mt-3", align="end"),
html.Div(id='vehicles-content', className="mt-3")
]),
dbc.Tab(label="Logs", tab_id="logs", children=[ dbc.Tab(label="Logs", tab_id="logs", children=[
dbc.Row([ dbc.Row([
dbc.Col(html.Label("Vehicle:", className="mt-2"), width="auto"), dbc.Col(html.Label("Select Vehicle:", className="mt-2"), width="auto"),
dbc.Col(dcc.Dropdown( dbc.Col(dcc.Dropdown(
id='logs-vehicle-selector', id='logs-vehicle-selector',
options=[{'label': v, 'value': v} for v in DATA.keys()], options=[{'label': v, 'value': v} for v in DATA.keys()],
value=list(DATA.keys())[0] if DATA else None, value=list(DATA.keys())[0] if DATA else None,
clearable=False clearable=False
), width=3, className="me-4"), ), width=3, className="me-4"),
dbc.Col(html.Label("Bus:", className="mt-2"), width="auto"), dbc.Col(html.Label("Select Bus:", className="mt-2"), width="auto"),
dbc.Col(dcc.Dropdown( dbc.Col(dcc.Dropdown(
id='logs-bus-selector', id='logs-bus-selector',
options=[{'label': 'Bus 1', 'value': 'Bus 1'}, {'label': 'Bus 2', 'value': 'Bus 2'}], options=[{'label': 'Bus 1', 'value': 'Bus 1'}, {'label': 'Bus 2', 'value': 'Bus 2'}],
@@ -166,14 +152,14 @@ app.layout = dbc.Container([
]), ]),
dbc.Tab(label="Statistics", tab_id="statistics", children=[ dbc.Tab(label="Statistics", tab_id="statistics", children=[
dbc.Row([ dbc.Row([
dbc.Col(html.Label("Vehicle:", className="mt-2"), width="auto"), dbc.Col(html.Label("Select Vehicle:", className="mt-2"), width="auto"),
dbc.Col(dcc.Dropdown( dbc.Col(dcc.Dropdown(
id='vehicle-selector', id='vehicle-selector',
options=[{'label': v, 'value': v} for v in DATA.keys()], options=[{'label': v, 'value': v} for v in DATA.keys()],
value=list(DATA.keys())[0] if DATA else None, value=list(DATA.keys())[0] if DATA else None,
clearable=False clearable=False
), width=3, className="me-4"), ), width=3, className="me-4"),
dbc.Col(html.Label("Bus:", className="mt-2"), width="auto"), dbc.Col(html.Label("Select Bus:", className="mt-2"), width="auto"),
dbc.Col(dcc.Dropdown( dbc.Col(dcc.Dropdown(
id='bus-selector', id='bus-selector',
options=[{'label': 'Bus 1', 'value': 'Bus 1'}, {'label': 'Bus 2', 'value': 'Bus 2'}], options=[{'label': 'Bus 1', 'value': 'Bus 1'}, {'label': 'Bus 2', 'value': 'Bus 2'}],
@@ -321,7 +307,7 @@ def render_content(tab, vehicle, bus):
elif tab == 'id_viewer': elif tab == 'id_viewer':
ids = sorted(DATA_BY_ID.get((vehicle, bus), {}).keys()) ids = sorted(DATA_BY_ID.get((vehicle, bus), {}).keys())
return html.Div([ return html.Div([
html.Label("CAN ID:"), html.Label("Select CAN ID:"),
dcc.Dropdown( dcc.Dropdown(
id='id-selector', id='id-selector',
options=[{'label': i, 'value': i} for i in ids], options=[{'label': i, 'value': i} for i in ids],
@@ -405,65 +391,5 @@ def update_corr(method, target, vehicle, bus, tab):
return plot_correlation_heatmap(corr_df, target_id=target_id, title=title) return plot_correlation_heatmap(corr_df, target_id=target_id, title=title)
@app.callback(
Output('vehicles-content', 'children'),
Input('vehicles-vehicle-selector', 'value')
)
def render_vehicles(vehicle):
if not vehicle or vehicle not in DATA:
return html.Div("No data available", className="text-muted")
brand = VEHICLE_META.get(vehicle, {}).get("brand", "")
vehicle_module = get_vehicle_module(brand)
dfs = []
for bus_df in DATA[vehicle].values():
dfs.append(bus_df)
if not dfs:
return html.Div("No data available", className="text-muted")
df = pd.concat(dfs, ignore_index=True)
if 'Timestamp' in df.columns:
df = df.sort_values('Timestamp', kind='stable').reset_index(drop=True)
cards = []
for nid, frame_def in vehicle_module.DECODER_RULES.items():
decoded = vehicle_module.decode_dataframe(df, frame_def.can_id)
for item in frame_def.signals:
if hasattr(item, 'plot_func') and callable(item.plot_func):
title = f"{frame_def.can_id} - {item.name}"
fig = item.plot_func(decoded, frame_def.color)
else:
sig = item
if getattr(sig, 'skip_plot', False):
continue
unit_str = f" ({sig.unit})" if sig.unit else ""
title = f"{frame_def.can_id} - {sig.name}{unit_str}"
fig = vehicle_module.plot_signal(decoded, sig.name, title=title, color=frame_def.color)
card = dbc.Card([
dbc.CardBody([
dcc.Graph(figure=fig, config={'displayModeBar': False},
style={'height': '280px'})
], className="p-2"),
], className="shadow-sm border-0 h-100")
cards.append(
dbc.Col(card, xs=12, sm=6, md=4, lg=3, className="mb-3")
)
if not cards:
return html.Div(
"No decoded signals available. Add rules in the vehicle module.",
className="text-muted"
)
return dbc.Row(cards)
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=False) app.run(debug=False)
-19
View File
@@ -1,19 +0,0 @@
# File: vehicle/__init__.py
# Copyright (C) 2026 Erick Ahmed
# SPDX-License-Identifier: AGPL-3.0-or-later
import importlib
def get_vehicle_module(brand: str):
"""
Dynamically imports the correct decoder module based on the vehicle brand.
Falls back to 'vehicle.generic' if a specific brand module is not found.
"""
if not brand:
return importlib.import_module("vehicle.generic")
module_name = f"vehicle.{brand.lower().replace(' ', '_')}"
try:
return importlib.import_module(module_name)
except ModuleNotFoundError:
return importlib.import_module("vehicle.generic")
-139
View File
@@ -1,139 +0,0 @@
# File: vehicle/base.py
# Copyright (C) 2026 Erick Ahmed
# SPDX-License-Identifier: AGPL-3.0-or-later
from dataclasses import dataclass, field
from typing import List
import numpy as np
import pandas as pd
import plotly.graph_objects as go
@dataclass
class SignalDef:
name: str
bit_start: int
bit_length: int
factor: float = 1.0
offset: float = 0.0
is_signed: bool = False
byte_order: str = "little"
unit: str = ""
@dataclass
class FrameDef:
can_id: str
description: str = ""
color: str = "#377eb8"
signals: List[SignalDef] = field(default_factory=list)
def normalize_id(can_id: str) -> str:
s = str(can_id).strip().upper()
if s.startswith("0X"):
s = s[2:]
return s.lstrip("0") or "0"
def _extract_signal(bytes_arr: np.ndarray, sig: SignalDef) -> np.ndarray:
if bytes_arr.size == 0:
return np.zeros(0, dtype=np.float64)
byte_lo = sig.bit_start // 8
byte_hi = (sig.bit_start + sig.bit_length - 1) // 8
byte_indices = [i for i in range(byte_lo, byte_hi + 1) if 0 <= i < 8]
if not byte_indices:
return np.full(bytes_arr.shape[0], np.nan, dtype=np.float64)
raw = np.zeros(bytes_arr.shape[0], dtype=np.int64)
if sig.byte_order == "little":
for shift, bi in enumerate(byte_indices):
raw += bytes_arr[:, bi].astype(np.int64) << (shift * 8)
else:
for shift, bi in enumerate(reversed(byte_indices)):
raw += bytes_arr[:, bi].astype(np.int64) << (shift * 8)
intra_byte_shift = sig.bit_start % 8
raw = raw >> intra_byte_shift
mask = (1 << sig.bit_length) - 1
raw = raw & mask
if sig.is_signed and sig.bit_length < 64:
sign_bit = 1 << (sig.bit_length - 1)
raw = (raw ^ sign_bit) - sign_bit
return raw.astype(np.float64) * sig.factor + sig.offset
def decode_dataframe(df: pd.DataFrame, can_id: str, decoder_rules: dict) -> pd.DataFrame:
norm = normalize_id(can_id)
if norm not in decoder_rules:
return pd.DataFrame()
frame_def = decoder_rules[norm]
id_col = "ID" if "ID" in df.columns else "Identifier"
df_ids = df[id_col].astype(str).map(normalize_id)
mask = df_ids == norm
sub = df.loc[mask].copy()
if sub.empty:
return pd.DataFrame()
byte_cols = [f"b{i}" for i in range(8) if f"b{i}" in sub.columns]
if not byte_cols:
return pd.DataFrame()
arr = np.zeros((len(sub), 8), dtype=np.int64)
for i, c in enumerate(byte_cols):
arr[:, i] = pd.to_numeric(sub[c], errors="coerce").fillna(0).astype(np.int64).to_numpy()
out = pd.DataFrame()
out["Timestamp"] = sub["Timestamp"].to_numpy() if "Timestamp" in sub.columns else np.arange(len(sub))
for sig in frame_def.signals:
out[sig.name] = _extract_signal(arr, sig)
return out
def plot_signal(df: pd.DataFrame, signal_name: str, title: str, color: str = "#377eb8", height: int = 280) -> go.Figure:
fig = go.Figure()
if df.empty or signal_name not in df.columns:
fig.update_layout(
title=dict(text=title, font=dict(size=14)),
annotations=[dict(text="No data", showarrow=False, x=0.5, y=0.5,
font=dict(size=13, color="#888"))],
height=height,
template="plotly_white",
)
return fig
fig.add_trace(go.Scatter(
x=df["Timestamp"],
y=df[signal_name],
mode="lines",
line=dict(width=2, color=color),
name=signal_name,
hovertemplate=f"<b>{signal_name}</b><br>Time: %{{x}}<br>Value: %{{y:.2f}}<extra></extra>",
))
fig.update_layout(
title=dict(text=title, font=dict(size=14, color="#1a1a1a"),
x=0.5, xanchor="center", pad=dict(b=10)),
height=height,
autosize=True,
template="plotly_white",
margin=dict(l=55, r=20, t=55, b=45),
xaxis=dict(
title=dict(text="Time", font=dict(size=11)),
showgrid=True, gridwidth=0.5, gridcolor="#eee",
zeroline=False, linecolor="#bdbdbd",
),
yaxis=dict(
title=dict(text=signal_name, font=dict(size=11)),
showgrid=True, gridwidth=0.5, gridcolor="#eee",
zeroline=False, linecolor="#bdbdbd",
),
font=dict(family="Segoe UI, Arial, sans-serif", size=11, color="#2a2a2a"),
hoverlabel=dict(bgcolor="white", font_size=12,
font_family="Segoe UI", bordercolor="#cccccc"),
)
return fig
-146
View File
@@ -1,146 +0,0 @@
# File: vehicle/komatsu.py
# Copyright (C) 2026 Erick Ahmed
# SPDX-License-Identifier: AGPL-3.0-or-later
import plotly.express as px
import pandas as pd
from dataclasses import dataclass
from typing import Callable
from copy import deepcopy
from vehicle.base import (
SignalDef, FrameDef, normalize_id, decode_dataframe as _decode_dataframe, plot_signal
)
@dataclass
class CustomPlotDef:
name: str
plot_func: Callable
load_state_sig = SignalDef(
name="Engine Load State",
bit_start=24,
bit_length=8,
factor=1,
offset=0.0,
is_signed=False,
byte_order="big",
unit="",
)
load_state_sig.skip_plot = True
DECODER_RULES = {
normalize_id("0x011F"): FrameDef(
can_id="0x011F",
description="Engine ECM Main Broadcast",
color="#e41a1c",
signals=[
SignalDef(
name="Engine",
bit_start=0,
bit_length=16,
factor=0.125,
offset=0.0,
is_signed=False,
byte_order="big",
unit="RPM",
),
SignalDef(
name="Pressure / Load",
bit_start=16,
bit_length=16,
factor=0.05,
offset=0,
is_signed=False,
byte_order="little",
unit="%",
),
],
),
normalize_id("0x0CFF3300"): FrameDef(
can_id="0x0CFF3300",
description="Engine temperature and load state block",
color="#0080fe",
signals=[
SignalDef(
name="Engine coolant temp",
bit_start=8,
bit_length=8,
factor=1,
offset=0.0,
is_signed=False,
byte_order="big",
unit="",
),
SignalDef(
name="Engine oil temp",
bit_start=40,
bit_length=8,
factor=1,
offset=0.0,
is_signed=False,
byte_order="big",
unit="",
),
load_state_sig,
CustomPlotDef(
name="Engine Load State",
plot_func=lambda decoded, color: plot_load_state_pie(decoded, color)
),
],
),
}
LOAD_STATE_MAP = {
0: "Boot up",
16: "Normal load",
32: "High load"
}
def plot_load_state_pie(decoded_df, color):
if decoded_df is None or decoded_df.empty or "Engine Load State" not in decoded_df.columns:
return px.pie(title="No data for Engine Load State")
states = pd.to_numeric(decoded_df["Engine Load State"], errors='coerce').dropna().astype(int)
labels = states.map(LOAD_STATE_MAP).fillna("Unknown")
counts = labels.value_counts().reset_index()
counts.columns = ['State', 'Count']
total = counts['Count'].sum()
counts['Percentage'] = (counts['Count'] / total * 100).round(1)
counts['Legend'] = counts['State'] + " (" + counts['Percentage'].astype(str) + "%)"
fig = px.pie(
counts,
values='Count',
names='Legend',
color='State',
title='Engine Load State Distribution',
color_discrete_map={
"Boot up": "#ff9900",
"Normal load": "#00cc00",
"High load": "#cc0000",
"Unknown": "#808080"
}
)
fig.update_traces(
textinfo='none',
hoverinfo='label+percent+value',
domain={'x': [0.05, 0.55], 'y': [0.05, 0.95]}
)
fig.update_layout(
margin=dict(l=0, r=10, t=40, b=0),
legend=dict(x=0.6, y=0.5)
)
return fig
def decode_dataframe(df, can_id):
filtered_rules = {}
for nid, frame in DECODER_RULES.items():
filtered_signals = [sig for sig in frame.signals if isinstance(sig, SignalDef)]
new_frame = deepcopy(frame)
new_frame.signals = filtered_signals
filtered_rules[nid] = new_frame
return _decode_dataframe(df, can_id, filtered_rules)