Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added grafana/.DS_Store
Binary file not shown.
Binary file added monitor/.DS_Store
Binary file not shown.
40 changes: 25 additions & 15 deletions monitor/app/monitor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
"""
Read on the TCP server for data
"""

import socket
import json
import time
import random
from datetime import datetime
from can_data import Signal, DBC
from influx_writer import write_signal, write_dtc
Expand Down Expand Up @@ -35,7 +30,7 @@ def read_tcp(self):
# Extract the message up to the delimiter
message = buffer[:delimiter_position]
# Check it is well formed
if message[:2] != "0x":
if 'x' not in message or len(message.split('x')) != 2:
buffer = buffer[delimiter_position + len(','):]
continue
# Process the message
Expand All @@ -45,26 +40,37 @@ def read_tcp(self):


def _process_message(self, message: str) -> tuple:
if message[:2] != "0x":
return
if 'x' not in message or len(message.split('x')) != 2:
return None, None, None
else:
clean_hex = message[2:]
timestamp = message[:8]
clean_hex = message[9:]

id_hex = clean_hex[:8]
data_hex = clean_hex[8:]

id_int = int(id_hex, 16)
data_int = int(data_hex, 16)
print("Received msg id: ", id_int, " data: ", data_int)
return id_int, data_hex
print("Received msg id: ", id_int, " data: ", data_int, " timestamp: ", timestamp)
return id_int, data_hex, timestamp

def process_can_message(self, message: str):
def is_dtc(msg_name: str) -> bool:
return 'DTC' in msg_name

can_id, can_data = self._process_message(message)
# data_int = int(data_hex, 16)
msg = self.dbc.cantools_db.get_message_by_frame_id(can_id)
can_id, can_data, timestamp = self._process_message(message)
if can_id is None:
return
if can_id == 218103553:
print(f"Skipping message with frame ID: {can_id}")
return

try:
msg = self.dbc.cantools_db.get_message_by_frame_id(can_id)
except KeyError:
print(f"Frame ID {can_id} not found in DBC file")
return

data_bytes = bytes.fromhex(can_data)
decoded_signals = msg.decode(data_bytes)

Expand Down Expand Up @@ -139,4 +145,8 @@ def simulate_random(self):
pass
time.sleep(0.1)
# for signal in frame.signals:
# write_signal(Signal(signal.name, random.randint(0, 100), datetime.now().timestamp(), self.dbc))
# write_signal(Signal(signal.name, random.randint(0, 100), datetime.now().timestamp(), self.dbc))

if __name__ == '__main__':
monitor = Monitor('localhost', 12345, 'path_to_dbc_file.dbc')
monitor.read_tcp()
Binary file added monitor/data/.DS_Store
Binary file not shown.
2,392 changes: 1,196 additions & 1,196 deletions monitor/data/dbc/2024CAR.dbc

Large diffs are not rendered by default.

Binary file added scripts/.DS_Store
Binary file not shown.
Loading