import argparse from pathlib import Path import pandas as pd import plotly.graph_objects as go from utils.extractor import load_data def _format_can_id(x): if pd.isna(x): return "UNKNOWN" s = str(x).strip() if not s: return "UNKNOWN" if s.lower().startswith('0x'): s = s[2:] return s.upper() def prepare_data(df, target_id): can_id_col = 'ID' if 'ID' in df.columns else 'Identifier' df['Formatted_ID'] = df[can_id_col].apply(_format_can_id) target_id_clean = _format_can_id(target_id) filtered = df[df['Formatted_ID'] == target_id_clean].copy() byte_cols = [f"b{i}" for i in range(8)] for col in byte_cols: filtered[col] = pd.to_numeric( filtered[col].apply(lambda x: int(x, 16) if pd.notna(x) else None), errors='coerce' ) filtered = filtered.sort_values('Timestamp') mask = (filtered[byte_cols] != filtered[byte_cols].shift()).any(axis=1) filtered = filtered[mask] return filtered, byte_cols def plot_bits(df, byte_cols, can_id, title): fig = go.Figure() colors = ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#ffff33', '#a65628', '#f781bf'] for i, col in enumerate(byte_cols): fig.add_trace(go.Scatter( x=df['Timestamp'], y=df[col], mode='lines', line=dict(shape='hv', width=2, color=colors[i]), name=col.upper(), hovertemplate=f"{col.upper()}
Time: %{{x}}
Value: %{{y}}" )) fig.update_layout( height=600, autosize=True, template='plotly_white', title=dict( text=f"{title} - ID: {can_id}", font=dict(size=20, color='#1a1a1a'), x=0.5, xanchor='center', pad=dict(b=20) ), font=dict(family="Segoe UI, Arial, sans-serif", size=12, color='#2a2a2a'), hoverlabel=dict( bgcolor="white", font_size=13, font_family="Segoe UI", bordercolor='#cccccc' ), margin=dict(l=60, r=40, t=120, b=60), legend=dict( title=dict(text="Bytes"), bgcolor="rgba(255,255,255,0.8)", bordercolor="#cccccc", borderwidth=1, font=dict(size=12, color="#2a2a2a") ), xaxis=dict( title=dict(text="Timestamp", font=dict(size=13, color="#1a1a1a")), showgrid=True, gridwidth=0.5, gridcolor='#e8e8e8', zeroline=False, linecolor="#bdbdbd", tickfont=dict(size=12, color="#2a2a2a"), ticks="outside", ticklen=4, tickcolor="#cccccc", minor=dict(showgrid=True, gridcolor='#f4f4f4', gridwidth=0.5) ), yaxis=dict( title=dict(text="Byte Value", font=dict(size=13, color="#1a1a1a")), showgrid=True, gridwidth=0.5, gridcolor='#e8e8e8', zeroline=False, linecolor="#bdbdbd", tickfont=dict(size=12, color="#2a2a2a"), ticks="outside", ticklen=4, tickcolor="#cccccc" ) ) return fig if __name__ == "__main__": parser = argparse.ArgumentParser(description="Visualize CAN bus byte changes over time") parser.add_argument("input", type=Path, help="Path to the input CAN log file") parser.add_argument("can_id", type=str, help="CAN ID to visualize") parser.add_argument("output", type=Path, nargs="?", default=Path("bits_report.html"), help="Path to the output HTML report") parser.add_argument("title", nargs="?", default="Byte Visualization", help="Title for the HTML report") args = parser.parse_args() df = load_data(args.input) filtered_df, byte_cols = prepare_data(df, args.can_id) fig = plot_bits(filtered_df, byte_cols, args.can_id, title=args.title) config = { 'responsive': True, 'displaylogo': False, 'scrollZoom': True, 'modeBarButtonsToAdd': ['toggleSpikelines'], 'toImageButtonOptions': {'format': 'png', 'scale': 2} } fig.write_html(str(args.output), include_plotlyjs='cdn', config=config)