Skip to content

Commit 4fe9a80

Browse files
committed
Switch to json auto deserialization, add some first PahoMQTT stuff
1 parent 6197de0 commit 4fe9a80

10 files changed

+85
-17
lines changed

CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ include_directories(${Boost_INCLUDE_DIRS})
2020

2121
find_package(nlohmann_json 3.2.0 REQUIRED)
2222
find_package(PkgConfig)
23+
find_package(PahoMqttCpp REQUIRED)
2324

2425
set(THREADS_HAVE_PTHREAD_ARG 1)
2526
find_package(Threads REQUIRED)
2627

27-
add_executable(bsbd bsbd.cpp bus.cpp serial.cpp crc.cpp AddressMap.cpp vtype.cpp)
28+
add_executable(bsbd bsbd.cpp bus.cpp serial.cpp crc.cpp AddressMap.cpp vtype.cpp mqtt.cpp)
2829
target_include_directories(bsbd PRIVATE ${SERIAL_INCLUDE_DIRS})
29-
target_link_libraries(bsbd PRIVATE ${Boost_LIBRARIES} ${SERIAL_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} nlohmann_json::nlohmann_json)
30+
target_link_libraries(bsbd PRIVATE ${Boost_LIBRARIES} ${SERIAL_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} nlohmann_json::nlohmann_json PahoMqttCpp::paho-mqttpp3)
3031

bsbd.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
#include <nlohmann/json.hpp>
77
#include <boost/program_options.hpp>
88

9+
#include <mqtt/async_client.h>
10+
911
#include "bus.h"
1012
#include "message.h"
1113
#include "vtype.h"
1214
#include "AddressMap.h"
15+
#include "mqtt.h"
16+
17+
#include "config.h"
1318

1419
namespace po = boost::program_options;
1520

@@ -75,10 +80,13 @@ int main(int argc, char **argv) {
7580
messageformat_parse(config["messageformat"].get<std::string>());
7681
std::vector<Bus> busses;
7782

78-
for(auto &busconfig : config["busses"]) {
79-
std::cout << busconfig.dump() << std::endl;
83+
auto mqttconfig=config["mqtt"].get<config::mqtt>();
84+
auto mqtt=MQTT(mqttconfig);
85+
86+
for(auto &busconfigjson : config["busses"]) {
87+
auto busconfig=busconfigjson.get<config::bus>();
8088
Bus newbus(busconfig);
8189
}
8290

8391
std::cout << config.dump() << std::endl;
84-
}
92+
}

bus.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#include <iostream>
22

33
#include "bus.h"
4+
#include "config.h"
45
#include "frame.h"
56
#include "message.h"
67

7-
Bus::Bus(json& busconfig) : busconfig(busconfig) {
8+
Bus::Bus(config::bus& config) : config(config) {
89
// FIXME - Parameter checking?
9-
serial=new Serial(busconfig["device"].get<std::string>(),
10-
busconfig["baudrate"].get<std::string>(),
11-
busconfig["mode"].get<std::string>());
10+
serial=new Serial(config.device, config.baudrate, config.mode);
1211

1312
reader=new std::thread(&Bus::Reader, this);
1413
}

bus.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@
44
#include <iostream>
55
#include <thread>
66

7-
#include <nlohmann/json.hpp>
8-
97
#include "serial.h"
8+
#include "config.h"
109
#include "message.h"
1110

12-
using json = nlohmann::json;
13-
1411
class Bus {
1512
private:
1613
Serial *serial;
17-
json& busconfig;
14+
config::bus config;
1815
std::thread *reader;
1916

20-
2117
void Reader();
2218
public:
23-
Bus(json& busconfig);
19+
Bus(config::bus& config);
2420
~Bus();
2521
};
2622

config.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#ifndef CONFIG_H
3+
#define CONFIG_H
4+
5+
#include <string>
6+
#include <nlohmann/json.hpp>
7+
8+
namespace config {
9+
class mqtt {
10+
public:
11+
std::string broker;
12+
NLOHMANN_DEFINE_TYPE_INTRUSIVE(mqtt, broker)
13+
};
14+
class bus {
15+
public:
16+
int id;
17+
std::string device;
18+
std::string baudrate;
19+
std::string mode;
20+
std::string address;
21+
22+
NLOHMANN_DEFINE_TYPE_INTRUSIVE(bus, id, device, baudrate, mode, address)
23+
};
24+
};
25+
26+
#endif

config.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
2+
"mqtt": {
3+
"broker": "nuc:1883"
4+
},
25
"busses": [
36
{
4-
"id": "1",
7+
"id": 1,
58
"device": "/dev/ttyUSB0",
69
"baudrate": "4800",
710
"mode": "8o1",

messageformat.json

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
"type": "bit-anaus",
4747
"access": "ro",
4848
"name": "Trinkwasserstatus"
49+
},
50+
{
51+
"address": "0x2d3d0574",
52+
"name": "HK1 Betriebsart",
53+
"type": "enum",
54+
"enum": {
55+
"0": "Schutzbetrieb",
56+
"1": "Automatik",
57+
"3": "Komfort"
58+
}
4959
}
5060
]
5161
}

mqtt.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
#include "mqtt.h"
3+
#include "config.h"
4+

mqtt.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef MQTT_H
2+
#define MQTT_H
3+
4+
#include "config.h"
5+
#include <iostream>
6+
7+
class MQTT {
8+
private:
9+
config::mqtt config;
10+
public:
11+
MQTT(config::mqtt& config) : config(config) {
12+
std::cout << config.broker << std::endl;
13+
}
14+
};
15+
16+
#endif

vtype.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class Bit : public Default {
6464
}
6565
};
6666

67+
class Enum : public Default {
68+
public:
69+
Enum(std::string name) : Default(name, this) {}
70+
};
6771

6872
} // Namespace VT
6973

@@ -74,3 +78,4 @@ static VT::Hexdump hkinfo("hkinfo");
7478
static VT::DateTime datetime("datetime");
7579
static VT::Float float3div64tempC("float3-div64-tempC", 3, "°C", 64);
7680
static VT::Bit bitanaus("bit-anaus", 1, "an", "aus");
81+
static VT::Enum enumlist("enum");

0 commit comments

Comments
 (0)