Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tillsc committed Jan 25, 2025
1 parent d683615 commit 3121d8f
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 18 deletions.
60 changes: 60 additions & 0 deletions can2mqtt.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Metadata-Version: 2.2
Name: can2mqtt
Version: 0.0.1
Summary: A python can to mqtt bridge
Home-page: https://github.com/tillsc/can2mqtt
Author: Till Schulte-Coerne
Author-email: [email protected]
Project-URL: Bug Reports, https://github.com/tillsc/can2mqtt/issues
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development ::
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Requires-Python: !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4
Description-Content-Type: text/markdown
Requires-Dist: python-can
Requires-Dist: cantools
Requires-Dist: paho-mqtt
Requires-Dist: pyyaml
Provides-Extra: dev
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: project-url
Dynamic: provides-extra
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

[DBC](http://socialledge.com/sjsu/index.php/DBC_Format) based CAN to MQTT brigde
===

This is a generic CAN 2 MQTT bridge build with Python 3

### Setup

This package is based upon the following dependencies:

* [python-can](https://python-can.readthedocs.io/en/master/)
* [cantools](https://github.com/eerimoq/cantools)
* [phao](http://www.eclipse.org/paho/)

Install all required Python 3 dependencies on a raspberry:

sudo apt-get install python3-pip python3-can
pip3 install paho-mqtt cantools

### Usage

Copy your DBC files into the root directory.

Copy config.example.yaml to config.yaml and modify it.

Run `./main.py` or `python3 main.py`
16 changes: 16 additions & 0 deletions can2mqtt.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
README.md
setup.py
can2mqtt/__init__.py
can2mqtt/app.py
can2mqtt/can_listener.py
can2mqtt/converter.py
can2mqtt/mqtt_handler.py
can2mqtt.egg-info/PKG-INFO
can2mqtt.egg-info/SOURCES.txt
can2mqtt.egg-info/dependency_links.txt
can2mqtt.egg-info/entry_points.txt
can2mqtt.egg-info/requires.txt
can2mqtt.egg-info/top_level.txt
can2mqtt/test/__init__.py
can2mqtt/test/mqtt_dummy.py
can2mqtt/test/test_main.py
1 change: 1 addition & 0 deletions can2mqtt.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions can2mqtt.egg-info/entry_points.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[console_scripts]
run = run:main
9 changes: 9 additions & 0 deletions can2mqtt.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
python-can
cantools
paho-mqtt
pyyaml

[dev]

[test]
pytest
1 change: 1 addition & 0 deletions can2mqtt.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
can2mqtt
4 changes: 2 additions & 2 deletions can2mqtt/test/simple.dbc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
BO_ 500 IO_DEBUG: 4 IO
SG_ IO_DEBUG_test_unsigned : 0|8@1+ (1,0) [0|0] "" DBG
BO_ 500 IO_DEBUG_msg1: 4 IO
SG_ IO_DEBUG_msg1_sig1 : 0|8@1+ (1,0) [0|0] "" DBG
32 changes: 16 additions & 16 deletions can2mqtt/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from can2mqtt.test.mqtt_dummy import MqttDummy
from can2mqtt.can_listener import CanListener
from can2mqtt.converter import Converter
from can2mqtt.app import load_dbc_db

class TestStringMethods(unittest.TestCase):
Expand All @@ -14,16 +15,15 @@ def setUp(self):
self.mqtt_dummy = MqttDummy()
self.dbc_db = load_dbc_db(['can2mqtt/test/simple.dbc'])
self.default_config = {
'mqtt': {
'topic_names': {
'first_underscores_to_slash': 2
}
}
}
'name_conversion': {
'first_underscores_to_slash': 2
},
'only_one_signal_per_message': True
}
self.converter = Converter(self.dbc_db, self.default_config)

def build_can_listener(self, special_config = {}):
cnf = self.default_config | special_config
return CanListener(self.dbc_db, self.mqtt_dummy, cnf)
def build_can_listener(self, resend_unchanged_events_after):
return CanListener(self.mqtt_dummy, self.converter, resend_unchanged_events_after)


def build_message(self, message_id, data_byte_1 = 0, timestamp=None):
Expand All @@ -33,18 +33,18 @@ def build_message(self, message_id, data_byte_1 = 0, timestamp=None):
data=struct.pack('4b', data_byte_1, 0, 0, 0))

def test_can_listener_converts_topic_names(self):
can_listener = self.build_can_listener()
can_listener = self.build_can_listener(30)
msg = self.build_message(500, 50)
can_listener.on_message_received(msg)
self.assertEqual(self.mqtt_dummy.last_topic, 'io/debug/test_unsigned')
self.assertEqual(self.mqtt_dummy.last_topic, 'io/debug/msg1')
self.assertEqual(self.mqtt_dummy.last_data, 50)

def test_no_resend_same_data(self):
can_listener = self.build_can_listener()
can_listener = self.build_can_listener(30)

msg = self.build_message(500, 50)
can_listener.on_message_received(msg)
self.assertEqual(self.mqtt_dummy.last_topic, 'io/debug/test_unsigned')
self.assertEqual(self.mqtt_dummy.last_topic, 'io/debug/msg1')
self.assertEqual(self.mqtt_dummy.last_data, 50)

self.mqtt_dummy.reset()
Expand All @@ -59,15 +59,15 @@ def test_no_resend_same_data(self):
self.mqtt_dummy.reset()
msg3 = self.build_message(500, 50, msg.timestamp + 31)
can_listener.on_message_received(msg3)
self.assertEqual(self.mqtt_dummy.last_topic, 'io/debug/test_unsigned')
self.assertEqual(self.mqtt_dummy.last_topic, 'io/debug/msg1')

def test_force_resend_same_data(self):
can_listener = self.build_can_listener({'resend_unchanged_events_after': 0})
can_listener = self.build_can_listener(0)

msg = self.build_message(500, 50)
for x in range(0, 3):
can_listener.on_message_received(msg)
self.assertEqual(self.mqtt_dummy.last_topic, 'io/debug/test_unsigned')
self.assertEqual(self.mqtt_dummy.last_topic, 'io/debug/msg1')
self.assertEqual(self.mqtt_dummy.last_data, 50)
self.mqtt_dummy.reset()

Expand Down

0 comments on commit 3121d8f

Please sign in to comment.