Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
Merge DBCParser into Mapper
Browse files Browse the repository at this point in the history
The readers require a Mapper as well as information provided by the
DBCParser. The Mapper itself also requires information from the parser.

The Mapper has therefore been changed to use the DBCParser class as a
mixin. The readers and the Feeder have been adapted accordingly.
  • Loading branch information
sophokles73 committed Sep 11, 2023
1 parent fd9a176 commit 6fda436
Show file tree
Hide file tree
Showing 24 changed files with 390 additions and 237 deletions.
48 changes: 20 additions & 28 deletions dbc2val/dbcfeeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@
import asyncio

from signal import SIGINT, SIGTERM, signal
from typing import Any, Dict
from typing import Any, Dict, Optional

from dbcfeederlib import canclient
from dbcfeederlib import canplayer
from dbcfeederlib import dbc2vssmapper
from dbcfeederlib import dbcreader
from dbcfeederlib import dbcparser
from dbcfeederlib import j1939reader
from dbcfeederlib import databrokerclientwrapper
from dbcfeederlib import serverclientwrapper
Expand Down Expand Up @@ -128,9 +127,9 @@ class Feeder:
"""
def __init__(self, client_wrapper: clientwrapper.ClientWrapper,
elmcan_config: Dict[str, Any], dbc2val: bool = True, val2dbc: bool = False):
self._shutdown = False
self._running = False
self._reader = None
self._player = None
self._player: Optional[canplayer.CANplayer] = None
self._mapper = None
self._registered = False
self._can_queue: queue.Queue[dbc2vssmapper.VSSObservation] = queue.Queue()
Expand All @@ -141,7 +140,6 @@ def __init__(self, client_wrapper: clientwrapper.ClientWrapper,
self._val2dbc = val2dbc
self._canclient = None
self._transmit = False
self._dbc_parser = None

def start(
self,
Expand All @@ -154,11 +152,13 @@ def start(
use_strict_parsing=False
):

# Read DBC file
self._dbc_parser = dbcparser.DBCParser(dbc_file_names, use_strict_parsing, use_j1939)

log.info("Using mapping: {}".format(mappingfile))
self._mapper = dbc2vssmapper.Mapper(mappingfile, self._dbc_parser, dbc_default_file)
self._running = True
self._mapper = dbc2vssmapper.Mapper(
mapping_definitions_file=mappingfile,
dbc_file_names=dbc_file_names,
use_strict_parsing=use_strict_parsing,
expect_extended_frame_ids=use_j1939,
can_signal_default_values_file=dbc_default_file)

self._client_wrapper.start()
threads = []
Expand All @@ -167,18 +167,10 @@ def start(
log.info("Setting up reception of CAN signals")
if use_j1939:
log.info("Using J1939 reader")
self._reader = j1939reader.J1939Reader(
rxqueue=self._can_queue,
mapper=self._mapper,
dbc_parser=self._dbc_parser
)
self._reader = j1939reader.J1939Reader(self._can_queue, self._mapper)
else:
log.info("Using DBC reader")
self._reader = dbcreader.DBCReader(
rxqueue=self._can_queue,
mapper=self._mapper,
dbc_parser=self._dbc_parser
)
self._reader = dbcreader.DBCReader(self._can_queue, self._mapper)

if candumpfile:
# use dumpfile
Expand All @@ -199,7 +191,7 @@ def start(
if canport == 'elmcan':

log.info("Using elmcan. Trying to set up elm2can bridge")
elm2canbridge.elm2canbridge(canport, self._elmcan_config, self._reader.canidwl)
elm2canbridge.elm2canbridge(canport, self._elmcan_config, self._reader._canidwl)

# use socketCAN
log.info("Using socket CAN device '%s'", canport)
Expand Down Expand Up @@ -233,7 +225,7 @@ def start(

def stop(self):
log.info("Shutting down...")
self._shutdown = True
self._running = False
# Tell others to stop
if self._reader is not None:
self._reader.stop()
Expand All @@ -246,8 +238,8 @@ def stop(self):
self._mapper = None
self._transmit = False

def is_stopping(self):
return self._shutdown
def is_running(self):
return self._running

def _register_datapoints(self) -> bool:
"""
Expand All @@ -272,7 +264,7 @@ def _run_receiver(self):
messages_sent = 0
last_sent_log_entry = 0
queue_max_size = 0
while self._shutdown is False:
while self._running is True:
if self._client_wrapper.is_connected():
self._disconnect_time = 0.0
else:
Expand Down Expand Up @@ -345,13 +337,13 @@ async def vss_update(self, updates):

can_ids = set()
for dbc_id in dbc_ids:
can_id = self._dbc_parser.get_canid_for_signal(dbc_id)
can_id = self._mapper.get_canid_for_signal(dbc_id)
can_ids.add(can_id)

for can_id in can_ids:
log.debug("CAN id to be sent, this is %#x", can_id)
sig_dict = self._mapper.get_value_dict(can_id)
message_definition = self._dbc_parser.get_message_for_canid(can_id)
message_definition = self._mapper.get_message_for_canid(can_id)
if message_definition is not None:
data = message_definition.encode(sig_dict)
self._canclient.send(arbitration_id=message_definition.frame_id, data=data)
Expand Down Expand Up @@ -620,7 +612,7 @@ def signal_handler(signal_received, *_):
log.info("Received signal %s, stopping...", signal_received)

# If we get told to shutdown a second time. Just do it.
if feeder.is_stopping():
if not feeder.is_running():
log.warning("Shutting down now!")
sys.exit(-1)

Expand Down
2 changes: 1 addition & 1 deletion dbc2val/dbcfeederlib/canclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import logging
from typing import Optional
import can
import can # type: ignore
from dbcfeederlib import canmessage

log = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion dbc2val/dbcfeederlib/canmessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
########################################################################

import logging
import can
import can # type: ignore

log = logging.getLogger(__name__)

Expand Down
8 changes: 3 additions & 5 deletions dbc2val/dbcfeederlib/canplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@

import logging
import threading
import can

# fixes issues with pytinstaller not detecting can.interfaces.virtual usage
from can.interfaces.virtual import VirtualBus # noqa: F401
import can # type: ignore
from can.interfaces.virtual import VirtualBus # type: ignore

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -70,8 +69,7 @@ def txWorker(self):

def start_replaying(self, canport):
log.debug("Using virtual bus to replay CAN messages (channel: %s)", canport)
self.bus = can.interface.Bus(bustype="virtual", # pylint: disable=abstract-class-instantiated
channel=canport, bitrate=500000)
self.bus = VirtualBus(channel=canport, bitrate=500000)
self.run = True
txThread = threading.Thread(target=self.txWorker)
txThread.start()
Expand Down
7 changes: 3 additions & 4 deletions dbc2val/dbcfeederlib/databrokerclientwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
#################################################################################

import logging
from typing import Any
from typing import Dict, List
from typing import Any, Dict, List, Optional

import os
import contextlib
Expand Down Expand Up @@ -52,7 +51,7 @@ def __init__(self, ip: str = "127.0.0.1", port: int = 55555,
"""
super().__init__(ip, port, token_path, tls)
self._grpc_client = None
self._name_to_type: dict[str, DataType] = {}
self._name_to_type: Dict[str, DataType] = {}
self._rpc_kwargs: Dict[str, str] = {}
self._connected = False
self._exit_stack = contextlib.ExitStack()
Expand Down Expand Up @@ -211,7 +210,7 @@ async def subscribe(self, vss_names: List[str], callback):

# If there is a path VSSClient will request a secure connection
if self._tls and self._root_ca_path:
root_path = Path(self._root_ca_path)
root_path: Optional[Path] = Path(self._root_ca_path)
else:
root_path = None

Expand Down
Loading

0 comments on commit 6fda436

Please sign in to comment.