Skip to content

Commit 3ad0505

Browse files
committed
Added Unit Tests for Value Converters
Created Interface for translators Renamed some converters from German to English Renamed the namespaces
1 parent 6d99a89 commit 3ad0505

File tree

24 files changed

+1964
-716
lines changed

24 files changed

+1964
-716
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ can2mqtt_core/can2mqtt_socketcandtest/bin/
88
can2mqtt_core/can2mqtt_socketcandtest/obj/
99
can2mqtt_core/can2mqtt_ttytool/bin/Debug/
1010
can2mqtt_core/can2mqtt_ttytool/bin/
11+
can2mqtt_core/can2mqtt_tests/bin/
12+
can2mqtt_core/can2mqtt_tests/obj/can2mqtt,tests.csproj.nuget.dgspec.json
13+
can2mqtt_core/can2mqtt_tests/obj/

README.md

Lines changed: 101 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,10 @@ Note: This whole readme assumes the following environment:
1616
- You are using the user "Pi" (if you have something else, just replace the username wherever stated)
1717
- You are using a fresh installation of the OS
1818
- You are using a USBtin device to connect to the CAN Bus (others might work but are not tested by me)
19+
- In thoery (and also during development I did this), socketcand and canutils can run the Raspberry Pi while can2mqtt is running on a another device in the network. That way you can also use a Raspberry Pi Zero (1st Gen) for socketcand and can-utils while running can2mqtt on another system that supports the Dotnet Framework (requires ARMv7 or later architecture).
1920

20-
## Install socketcand
21-
```
22-
sudo apt-get install git autoconf
23-
cd ~
24-
git clone https://github.com/linux-can/socketcand.git
25-
cd socketcand
26-
./autogen.sh
27-
./configure
28-
make
29-
make install
30-
sudo mv ~/socketcand /opt
31-
```
32-
33-
## Install can-utils
34-
```
35-
sudo apt-get install git
36-
cd ~
37-
git clone https://github.com/linux-can/can-utils.git
38-
cd can-utils
39-
make
40-
```
4121

22+
# Installation and Setup
4223
## Install .NET 6.0 Runtime
4324
Note, the packagelink may be differnt if you are not using a OS based on ubuntu 21.04 or 21.10. Check this page for other releases: https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu
4425
This are the links from https://dotnet.microsoft.com/en-us/download/dotnet/6.0 for the differente Processor architectures for Linux:
@@ -54,60 +35,58 @@ sudo ln -s /opt/dotnet/dotnet /usr/local/bin
5435

5536
Check the setup by run "dotnet --info". It should return the installed version and some other details.
5637

57-
## Setup the CAN Bus connection
38+
## can-utils installation and setup
39+
### Install can-utils
40+
```
41+
sudo apt-get install git
42+
cd ~
43+
git clone https://github.com/linux-can/can-utils.git
44+
cd can-utils
45+
make
46+
```
47+
48+
### Setup the CAN Bus connection
5849
Assuming your device has the ID ttyACM0:
5950
```
6051
sudo /opt/can-utils/slcan_attach -f -s1 -b 11 -o /dev/ttyACM0
6152
sudo /opt/can-utils/slcand ttyACM0 slcan0
6253
sudo ifconfig slcan0 up
6354
```
6455

65-
To add this to autostart and setup the adapter on every reboot, run "sudo nano /etc/rc.local"
66-
paste three lines above at the end before the "EXIT 0".
56+
To add this to autostart and setup the adapter on every reboot, run 'sudo nano /etc/rc.local'
57+
Paste three lines above at the end before the "EXIT 0".
6758

68-
## Start socketcand
69-
This is only required to test or if you like to take care of socketcand on your own. You can also configure a daemon to do this automatically (see below).
59+
## socketcand installation and setup
60+
### Install socketcand
61+
```
62+
sudo apt-get install git autoconf
63+
cd ~
64+
git clone https://github.com/linux-can/socketcand.git
65+
cd socketcand
66+
./autogen.sh
67+
./configure
68+
make
69+
make install
70+
sudo mv ~/socketcand /opt
71+
```
72+
73+
### Start socketcand
74+
This is only required to test or if you like to take care of socketcand on your own.
7075
You need to replace eth0 if your network interface is called different than eth0.
7176
```
7277
/opt/socketcand/socketcand -i slcan0 -l eth0 -v
7378
```
7479

75-
## MQTT Broker
76-
You need an MQTT Broker, that is handling the MQTT Traffic. You need to define this in the settings.
77-
78-
## Start can2mqtt:
79-
This is only required to test or if you like to take care of can2mqtt on your own. You can also configure a daemon to do this automatically (see below).
80-
81-
Minimum parameter: `./can2mqtt_core --Daemon:MqttServer="192.168.0.192"`
82-
83-
All parameter: `./can2mqtt_core --Daemon:Name="Can2MqttSE" --Daemon:CanServer="192.168.0.192" --Daemon:CanServerPort=28700 --Daemon:MqttServer="192.168.0.192" --Daemon:MqttClientId="Can2Mqtt" --Daemon:MqttTopic="Heating" --Daemon:MqttTranslator="StiebelEltron" --Daemon:CanForwardWrite=true --Daemon:CanForwardRead=false --Daemon:CanForwardResponse=true`
84-
85-
### Startup Parameters:
86-
87-
| Parameter | Description | Default Value | Required | Example |
88-
|-------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|----------|-------------------------------------------|
89-
| `--Daemon:Name` | Define the name of your Daemon | Can2Mqtt | No | `--Daemon:Name="Can2MqttSE"` |
90-
| `--Daemon:CanServer` | This is the address where your canlogserver is running | 127.0.0.1 | No | `--Daemon:CanServer="192.168.0.192"` |
91-
| `--Daemon:CanServerPort` | This is the port of the canlogserver | 28700 | No | `--Daemon:CanServerPort=28700` |
92-
| `--Daemon:MqttServer` | This is the address of the MQTT Broker. | | Yes | `--Daemon:MqttServer="192.168.0.192"` |
93-
| `--Daemon:MqttClientId` | This is the clientId of the MQTT Client. Choose any name you like | Can2Mqtt | No | `--Daemon:MqttClientId="Can2Mqtt"` |
94-
| `--Daemon:MqttTopic` | This is the MQTT Root Topic of all MQTT message | Can2Mqtt | No | `--Daemon:MqttTopic="Heating"` |
95-
| `--Daemon:MqttTranslator` | For some CAN Bus Clients are translators to translate the CAN Messages into a readable value and publish them via MQTT including the correct topic. Leave empty to publish every CAN frame without any further handling. Implemented translators right now: StiebelEltron | | No | `--Daemon:MqttTranslator="StiebelEltron"` |
96-
| `--Daemon:CanForwardWrite` | Should CAN frames of type "Write" be forwarded to MQTT? | true | No | `--Daemon:CanForwardWrite=true` |
97-
| `--Daemon:CanForwardRead` | Should CAN frames of type "Read" be forwarded to MQTT? | false | No | `--Daemon:CanForwardRead=false` |
98-
| `--Daemon:CanForwardResponse` | Should CAN frames of type "Response" be forwarded to MQTT? | true | No | `--Daemon:CanForwardResponse=true` |
99-
100-
101-
## Configure and Register Daemon for can2mqtt and canlogserver
102-
Execute `sudo nano /etc/systemd/system/canlogserver.service` and paste the following into the file. Replace the slcan0 in case your socket has a different name:
80+
### Setup socketcand as daemon
81+
Execute 'sudo nano /etc/systemd/system/socketcand.service', replace the network interface name if it is not eth0, replace the user with a username the daemon will use to run and paste the following:
10382
```
10483
[Unit]
105-
Description=canlogserver
84+
Description=socketcand
10685
After=network.target
10786
10887
[Service]
109-
ExecStart=/home/pi/can-utils/canlogserver slcan0
110-
WorkingDirectory=/home/pi/can-utils/canlogserver
88+
ExecStart=/opt/socketcand/socketcand -i slcan0 -l eth0 -v
89+
WorkingDirectory=/opt/socketcand/
11190
StandardOutput=inherit
11291
StandardError=inherit
11392
Restart=always
@@ -116,17 +95,39 @@ User=pi
11695
[Install]
11796
WantedBy=multi-user.target
11897
```
119-
Run `sudo systemctl start canlogserver.service` to test if the service starts (it should). To see if it runs, execute `sudo systemctl status canlogserver.service`. You should see something similar like this if everything went well in the last line: `Jul 03 19:09:16 raspi-test systemd[1]: Started canlogserver.`
12098

121-
Now we repeat this for the can2mqtt daemon. Execute `sudo nano /etc/systemd/system/can2mqtt.service` and paste the following into the file. Replace the placeholder with your parameters:
99+
Finally run the following commands:
100+
```
101+
sudo systemctl enable socketcand
102+
sudo systemctl start socketcand
103+
```
104+
105+
## MQTT Broker
106+
You need an MQTT Broker, that is handling the MQTT Traffic. You need to define the MQTT Broker IP in the can2mqtt config file later.
107+
If you don't have any MQTT broker, Mosquitto is a common MQTT Broker.
108+
109+
## can2mqtt
110+
### Download can2mqtt
111+
wget <Latest release at https://github.com/Hunv/can2mqtt/releases> -O can2mqtt.zip
112+
sudo unzip can2mqtt.zip /opt/can2mqtt
113+
114+
### Start can2mqtt:
115+
Optional: This is only required to test or if you like to take care of can2mqtt on your own by running can2mqtt interactivly.
116+
You can also configure a daemon to do this automatically (see below).
117+
```
118+
dotnet /opt/can2mqtt/can2mqtt
119+
```
120+
121+
### Setup can2mqtt as daemon
122+
Execute 'sudo nano /etc/systemd/system/can2mqtt.service', replace the network interface name if it is not eth0, replace the user with a username the daemon will use to run and paste the following:
122123
```
123124
[Unit]
124125
Description=can2mqtt
125126
After=network.target
126127
127128
[Service]
128-
ExecStart=/usr/local/bin/dotnet /home/pi/can2mqtt_core/can2mqtt_core.dll >>>STATE YOUR PARAMETERS HERE<<<
129-
WorkingDirectory=/home/pi/can2mqtt_core/
129+
ExecStart=dotnet /opt/can2mqtt/can2mqtt
130+
WorkingDirectory=/opt/can2mqtt/
130131
StandardOutput=inherit
131132
StandardError=inherit
132133
Restart=always
@@ -135,13 +136,45 @@ User=pi
135136
[Install]
136137
WantedBy=multi-user.target
137138
```
138-
An example for the ExecStart line is:
139-
```ExecStart=/usr/local/bin/dotnet /home/pi/can2mqtt_core/can2mqtt_core.dll --Daemon:MqttServer="127.0.0.1" --Daemon:MqttClientId="Can2Mqtt" --Daemon:MqttTopic="Heating" --Daemon:MqttTranslator="StiebelEltron" --Daemon:CanlogserverPath="/home/pi/can-utils" --Daemon:CanlogserverSocket="slcan0"```
140139

141-
To test if it works, run the following command: `sudo systemctl start can2mqtt.service`. To test if the service is running, execute `sudo systemctl status can2mqtt.service`. You will see the last lines of output. If you do not see any errors after about 10 seconds, everything is fine.
140+
Finally run the following commands:
141+
```
142+
sudo systemctl enable can2mqtt
143+
sudo systemctl start can2mqtt
144+
```
142145

143-
Finally if everything is fine and you want to autostart the canlogserver and can2mqtt application, execute this:
146+
### Configure config.json:
147+
First copy the sample config.json file: 'sudo cp /opt/can2mqtt/config-sample.json /opt/can2mqtt/config.json'
148+
Edit the config.json with your favorite editor (i.e. nano): 'sudo nano /opt/can2mqtt/config.json'
144149
```
145-
sudo systemctl enable canlogserver.service
146-
sudo systemctl enable can2mqtt.service
147-
```
150+
{
151+
"CanServer": "192.168.0.10", < This is the System where socketcand is running on
152+
"CanServerPort": 29536, < This is the port socketcand is using (29536 is default)
153+
"CanForwardWrite": true, < This defines if can2mqtt will handle CAN bus packages, that have the "write" flag
154+
"CanForwardRead": true, < This defines if can2mqtt will handle CAN bus packages, that have the "read" flag
155+
"CanForwardResponse": true, < This defines if can2mqtt will handle CAN bus packages, that have the "response" flag
156+
"CanReceiveBufferSize": 48, < The buffer size of receiving commands. 48 is default.
157+
"CanSenderId":"6A2", < The ID can2mqtt will use at the CAN bus in case of writing to the CAN bus
158+
159+
"MqttServer": "192.168.0.10", < This is the IP of the MQTT Broker
160+
"MqttClientId": "Can2Mqtt", < This is the ID the MQTT Client will use when register at MQTT Broker
161+
"MqttTopic": "Heating", < This is the first path item of the MQTT topic path can2mqtt will use for send/receive information
162+
"MqttTranslator": "StiebelEltron",< This is the translator used to translate the CAN bus data to values and send it via MQTT
163+
"MqttUser": "", < This is the user that is required to register at the MQTT broker. Leave empty for none.
164+
"MqttPassword": "", < This is the password that is required to register at the MQTT broker. Leave empty for none.
165+
"MqttAcceptSet": false, < This is a setting, that defines if can2mqtt will send write-commands to the CAN bus. For safety reasons the default setting is set to false.
166+
167+
"NoUnits": false < This defines if sending MQTT messages will contain the unit defined in the translator config or not (i.e. "25°C" or just "25")
168+
}
169+
```
170+
171+
# MQTT Data format
172+
can2mqtt sends the data in the topics, that are defined in the config of the translator. In case of the Stiebel Eltron translator it is the 'StiebelEltron.json'. There will be a prefix-topic in case there are other instances of can2mqtt or other MQTT applications. This one is configured in the config.json at the setting 'MqttTopic'.
173+
An example MQTT message may look like this:
174+
Topic: heating/outside/temperature/measured
175+
Value: 21°C
176+
177+
If you like to set data, add a /set at the end of the topic.
178+
An example MQTT message to can2mqtt to set the desired room temperature of the primary heat cycle to 23°C:
179+
Topic: heating/room/hc1/temperature/day/set
180+
Value: 23 (Important: Without the unit!)

can2mqtt_core/can2mqtt_core.sln

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.1.32407.343
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "can2mqtt_core", "can2mqtt_core\can2mqtt_core.csproj", "{26F0D3B8-CCC2-4339-B73D-56E1939C37C9}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "can2mqtt", "can2mqtt_core\can2mqtt.csproj", "{26F0D3B8-CCC2-4339-B73D-56E1939C37C9}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "can2mqtt_tool", "can2mqtt_tool\can2mqtt_tool.csproj", "{4F751CDC-4ED8-48B9-BA92-EC74BA2099EE}"
99
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "can2mqtt.Tests", "can2mqtt_tests\can2mqtt.Tests.csproj", "{3CA6268F-F918-4DEA-B95A-EC35255B351B}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "can2mqtt.Playgroupd", "can2mqtt.Playgroupd\can2mqtt.Playgroupd.csproj", "{FEEAF908-095D-459F-8CAD-D3C52C61E179}"
13+
EndProject
1014
Global
1115
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1216
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +25,14 @@ Global
2125
{4F751CDC-4ED8-48B9-BA92-EC74BA2099EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
2226
{4F751CDC-4ED8-48B9-BA92-EC74BA2099EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
2327
{4F751CDC-4ED8-48B9-BA92-EC74BA2099EE}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{3CA6268F-F918-4DEA-B95A-EC35255B351B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{3CA6268F-F918-4DEA-B95A-EC35255B351B}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{3CA6268F-F918-4DEA-B95A-EC35255B351B}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{3CA6268F-F918-4DEA-B95A-EC35255B351B}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{FEEAF908-095D-459F-8CAD-D3C52C61E179}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{FEEAF908-095D-459F-8CAD-D3C52C61E179}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{FEEAF908-095D-459F-8CAD-D3C52C61E179}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{FEEAF908-095D-459F-8CAD-D3C52C61E179}.Release|Any CPU.Build.0 = Release|Any CPU
2436
EndGlobalSection
2537
GlobalSection(SolutionProperties) = preSolution
2638
HideSolutionNode = FALSE

can2mqtt_core/can2mqtt_core/CanFrame.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Globalization;
44
using System.Text;
55

6-
namespace can2mqtt_core
6+
namespace can2mqtt
77
{
88
public class CanFrame
99
{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using can2mqtt;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace can2mqtt
9+
{
10+
public interface ITranslator
11+
{
12+
CanFrame Translate(CanFrame rawData, bool noUnit);
13+
string TranslateBack(string mqttTopic, string value, string senderId);
14+
}
15+
}

can2mqtt_core/can2mqtt_core/LogType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text;
44

5-
namespace can2mqtt_core
5+
namespace can2mqtt
66
{
77
public enum LogType
88
{

can2mqtt_core/can2mqtt_core/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using can2mqtt_core;
1+
using can2mqtt;
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Hosting;
44

0 commit comments

Comments
 (0)