feat: generate DBC json

This commit is contained in:
anjingyu 2025-07-14 15:05:35 +08:00
parent 1f51b32cf0
commit 602abeddc1
1 changed files with 89 additions and 14 deletions

View File

@ -21,6 +21,8 @@
import os
import json
import platform
from collections import OrderedDict
import tkinter as tk
import tkinter.filedialog
@ -69,6 +71,7 @@ class App(ttk.Window):
self.__debug_mods['LOG2FILE'] = ttk.BooleanVar()
self.__msgs = {}
self.__dp = None
self.__dbc_file_path = ""
# Update font for Linux
self.__change_font()
@ -144,6 +147,8 @@ class App(ttk.Window):
message=f"Failed to load {file_path}",
title="Failed to load DBC file")
self.__dbc_file_path = file_path
for msg in self.__dp:
if msg.dlc == 0:
# Skip the invalid message, maybe only contains some description.
@ -215,26 +220,96 @@ class App(ttk.Window):
self.__menu_bar.entryconfig("Debug", state="disabled")
def __stop(self):
pass
def __start(self):
"""Generate the JSON file"""
def __gen_feedback(self):
"""Generate the JSON file for feedback signals"""
checked = self.tv.get_checked()
if len(checked) == 0:
return
result = {}
if not os.path.isfile(self.__dbc_file_path):
ttk.dialogs.Messagebox.show_error(
message=f"Please open a DBC file firstly",
title="Failed to generate Feedback signals")
return
json_desc_path = os.path.join(os.path.dirname(self.__dbc_file_path), "dbc.json")
if os.path.isfile(json_desc_path):
json_obj = json.loads(open(json_desc_path, encoding='utf-8').read(), object_pairs_hook=OrderedDict)
else:
json_obj = OrderedDict()
result = OrderedDict()
for idd in checked:
msgid, sig = idd.split('.')
msg = self.__msgs[int(msgid)][0]
if msgid in result:
result[msgid]['sigs'].append(sig)
if msg.name in result:
result[msg.name][sig] = ""
else:
result[msgid] = {'name': msg.name, 'sigs': [sig]}
result[msg.name] = {sig: ""}
print(json.dumps(result, indent=2))
json_obj['feedback'] = result
try:
s = json.dumps(json_obj, indent = 2)
with open(json_desc_path, 'w+') as f:
f.write(s)
ttk.dialogs.Messagebox.ok(
message=
f"Generate Feedback signals successfully",
title="Successfully")
except Exception as e:
ttk.dialogs.Messagebox.show_error(
message=f"{str(e)}, Failed to generate Feedback signals",
title="Failed to generate Feedback signals")
def __gen_control(self):
"""Generate the JSON file for control signals"""
checked = self.tv.get_checked()
if len(checked) == 0:
return
if not os.path.isfile(self.__dbc_file_path):
ttk.dialogs.Messagebox.show_error(
message=f"Please open a DBC file firstly",
title="Failed to generate Control signals")
return
json_desc_path = os.path.join(os.path.dirname(self.__dbc_file_path), "dbc.json")
if os.path.isfile(json_desc_path):
json_obj = json.loads(open(json_desc_path, encoding='utf-8').read(), object_pairs_hook=OrderedDict)
else:
json_obj = OrderedDict()
result = OrderedDict()
for idd in checked:
msgid, sig = idd.split('.')
msg = self.__msgs[int(msgid)][0]
if msg.name in result:
result[msg.name][sig] = ""
else:
result[msg.name] = {sig: ""}
json_obj['control'] = result
try:
s = json.dumps(json_obj, indent = 2)
with open(json_desc_path, 'w+') as f:
f.write(s)
ttk.dialogs.Messagebox.ok(
message=
f"Generate Control signals successfully",
title="Successfully")
except Exception as e:
ttk.dialogs.Messagebox.show_error(
message=f"{str(e)}, Failed to generate Control signals",
title="Failed to generate Control signals")
def __select_item(self, event):
@ -332,15 +407,15 @@ class App(ttk.Window):
self._btn_frame = ttk.Frame(master=self.main_frame)
self._btn_frame.grid(row=1, column=0, padx=1, pady=1, sticky=NSEW)
self.__upload_btn = ttk.Button(self._btn_frame,
text="Generate",
text="Control",
bootstyle="success-outline",
command=self.__start)
command=self.__gen_control)
self.__upload_btn.pack(side=RIGHT, padx=5, pady=5)
self.__delete_btn = ttk.Button(self._btn_frame,
text="Delete",
text="Feedback",
style="danger-outline",
command=self.__stop)
command=self.__gen_feedback)
self.__delete_btn.pack(side=LEFT, padx=5, pady=5)
self.__statusbar = StatusBar(self.main_frame)