forked from wirenboard/wb-mqtt-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_port_driver.h
63 lines (51 loc) · 1.98 KB
/
serial_port_driver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once
#include <memory>
#include <unordered_map>
#include <wbmqtt/mqtt_wrapper.h>
#include "serial_config.h"
#include "serial_client.h"
#include "register_handler.h"
#include <chrono>
struct TDeviceChannel : public TDeviceChannelConfig
{
TDeviceChannel(PSerialDevice device, PDeviceChannelConfig config)
: TDeviceChannelConfig(*config)
, Device(device)
{
for (const auto ®_config: config->RegisterConfigs) {
Registers.push_back(TRegister::Intern(device, reg_config));
}
}
PSerialDevice Device;
std::vector<PRegister> Registers;
};
typedef std::shared_ptr<TDeviceChannel> PDeviceChannel;
class TMQTTWrapper;
class TSerialPortDriver
{
public:
TSerialPortDriver(PMQTTClientBase mqtt_client, PPortConfig port_config, PPort port_override = 0);
~TSerialPortDriver();
void Cycle();
void PubSubSetup();
bool HandleMessage(const std::string& topic, const std::string& payload);
std::string GetChannelTopic(const TDeviceChannelConfig& channel);
bool WriteInitValues();
private:
bool NeedToPublish(PRegister reg, bool changed);
void OnValueRead(PRegister reg, bool changed);
TRegisterHandler::TErrorState RegErrorState(PRegister reg);
void UpdateError(PRegister reg, TRegisterHandler::TErrorState errorState);
PMQTTClientBase MQTTClient;
PPortConfig Config;
PPort Port;
PSerialClient SerialClient;
std::vector<PSerialDevice> Devices;
std::unordered_map<PRegister, PDeviceChannel> RegisterToChannelMap;
std::unordered_map<PDeviceChannelConfig, std::vector<PRegister>> ChannelRegistersMap;
std::unordered_map<PRegister, TRegisterHandler::TErrorState> RegErrorStateMap;
std::unordered_map<PRegister, std::chrono::time_point<std::chrono::steady_clock>> RegLastPublishTimeMap;
std::unordered_map<std::string, std::string> PublishedErrorMap;
std::unordered_map<std::string, PDeviceChannel> NameToChannelMap;
};
typedef std::shared_ptr<TSerialPortDriver> PSerialPortDriver;