comm & dc11 devices serialization
This commit is contained in:
parent
35b5e5c459
commit
c31c74164e
12 changed files with 177 additions and 4 deletions
|
@ -50,4 +50,23 @@ void comm_arduino::send_data(const uint8_t *const in, const size_t n)
|
|||
{
|
||||
s->write(in, n);
|
||||
}
|
||||
|
||||
JsonDocument comm_arduino::serialize() const
|
||||
{
|
||||
JsonDocument j;
|
||||
|
||||
j["comm-backend-type"] = "arduino";
|
||||
|
||||
j["name"] = name;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
comm_arduino *comm_arduino::deserialize(const JsonVariantConst j)
|
||||
{
|
||||
comm_arduino *r = new comm_arduino(&Serial, j["name"].as<std::string>());
|
||||
r->begin(); // TODO error-checking
|
||||
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -18,6 +18,9 @@ public:
|
|||
|
||||
bool begin() override;
|
||||
|
||||
JsonDocument serialize() const override;
|
||||
static comm_arduino *deserialize(const JsonVariantConst j);
|
||||
|
||||
std::string get_identifier() const;
|
||||
|
||||
bool is_connected() override;
|
||||
|
|
|
@ -89,4 +89,26 @@ void comm_esp32_hardwareserial::send_data(const uint8_t *const in, const size_t
|
|||
{
|
||||
uart_write_bytes(uart_nr, in, n); // error checking?
|
||||
}
|
||||
|
||||
JsonDocument comm_esp32_hardwareserial::serialize() const
|
||||
{
|
||||
JsonDocument j;
|
||||
|
||||
j["comm-backend-type"] = "hardware-serial";
|
||||
|
||||
j["uart"] = uart_nr;
|
||||
j["rx-pin"] = rx_pin;
|
||||
j["tx-pin"] = tx_pin;
|
||||
j["bitratre"] = bitrate;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
comm_esp32_hardwareserial *comm_esp32_hardwareserial::deserialize(const JsonVariantConst j)
|
||||
{
|
||||
comm_esp32_hardwareserial *r = new comm_esp32_hardwareserial(j["uart"].as<int>(), j["rx-pin"].as<int>(), j["tx-pin"].as<int>(), j["bitrate"].as<int>());
|
||||
r->begin(); // TODO error checking
|
||||
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -20,6 +20,9 @@ public:
|
|||
|
||||
bool begin() override;
|
||||
|
||||
JsonDocument serialize() const override;
|
||||
static comm_esp32_hardwareserial *deserialize(const JsonVariantConst j);
|
||||
|
||||
std::string get_identifier() const;
|
||||
|
||||
bool is_connected() override;
|
||||
|
|
44
comm.cpp
44
comm.cpp
|
@ -5,6 +5,15 @@
|
|||
#include <cstring>
|
||||
|
||||
#include "comm.h"
|
||||
#if defined(ARDUINO)
|
||||
#include "comm_arduino.h"
|
||||
#endif
|
||||
#if defined(ESP32)
|
||||
#include "comm_esp32_hardwareserial.h"
|
||||
#endif
|
||||
#include "comm_tcp_socket_client.h"
|
||||
#include "comm_tcp_socket_server.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
comm::comm()
|
||||
|
@ -26,3 +35,38 @@ void comm::println(const std::string & in)
|
|||
send_data(reinterpret_cast<const uint8_t *>(in.c_str()), in.size());
|
||||
send_data(reinterpret_cast<const uint8_t *>("\r\n"), 2);
|
||||
}
|
||||
|
||||
comm *comm::deserialize(const JsonVariantConst j, bus *const b)
|
||||
{
|
||||
std::string type = j["comm-backend-type"];
|
||||
|
||||
comm *d = nullptr;
|
||||
|
||||
if (type == "tcp-server")
|
||||
d = comm_tcp_socket_server::deserialize(j);
|
||||
else if (type == "tcp-client")
|
||||
d = comm_tcp_socket_client::deserialize(j);
|
||||
#if defined(ESP32)
|
||||
else if (type == "hardware-serial")
|
||||
d = comm_esp32_hardwareserial::deserialize(j);
|
||||
#endif
|
||||
#if defined(ARDUINO)
|
||||
else if (type == "arduino")
|
||||
d = comm_arduino::deserialize(j);
|
||||
#endif
|
||||
else {
|
||||
DOLOG(warning, false, "comm::deserialize: \"%s\" not de-serialized", type.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
assert(d);
|
||||
|
||||
if (!d->begin()) {
|
||||
delete d;
|
||||
DOLOG(warning, false, "comm::deserialize: begin() failed");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return d;
|
||||
|
||||
}
|
||||
|
|
7
comm.h
7
comm.h
|
@ -8,6 +8,10 @@
|
|||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "ArduinoJson.h"
|
||||
|
||||
|
||||
class bus;
|
||||
|
||||
class comm
|
||||
{
|
||||
|
@ -17,6 +21,9 @@ public:
|
|||
|
||||
virtual bool begin() = 0;
|
||||
|
||||
virtual JsonDocument serialize() const = 0;
|
||||
static comm *deserialize(const JsonVariantConst j, bus *const b);
|
||||
|
||||
virtual std::string get_identifier() const = 0;
|
||||
|
||||
virtual bool is_connected() = 0;
|
||||
|
|
|
@ -185,3 +185,23 @@ void comm_tcp_socket_client::operator()()
|
|||
|
||||
close(cfd);
|
||||
}
|
||||
|
||||
JsonDocument comm_tcp_socket_client::serialize() const
|
||||
{
|
||||
JsonDocument j;
|
||||
|
||||
j["comm-backend-type"] = "tcp-client";
|
||||
|
||||
j["host"] = host;
|
||||
j["port"] = port;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
comm_tcp_socket_client *comm_tcp_socket_client::deserialize(const JsonVariantConst j)
|
||||
{
|
||||
comm_tcp_socket_client *r = new comm_tcp_socket_client(j["host"].as<std::string>(), j["port"].as<int>());
|
||||
r->begin(); // TODO error-checking
|
||||
|
||||
return r;
|
||||
}
|
||||
|
|
|
@ -35,6 +35,9 @@ public:
|
|||
|
||||
bool begin() override;
|
||||
|
||||
JsonDocument serialize() const override;
|
||||
static comm_tcp_socket_client *deserialize(const JsonVariantConst j);
|
||||
|
||||
std::string get_identifier() const override { return host + format(":%d", port) + " (client)"; }
|
||||
|
||||
bool is_connected() override;
|
||||
|
|
|
@ -231,3 +231,22 @@ void comm_tcp_socket_server::operator()()
|
|||
close(cfd);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
JsonDocument comm_tcp_socket_server::serialize() const
|
||||
{
|
||||
JsonDocument j;
|
||||
|
||||
j["comm-backend-type"] = "tcp-server";
|
||||
|
||||
j["port"] = port;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
comm_tcp_socket_server *comm_tcp_socket_server::deserialize(const JsonVariantConst j)
|
||||
{
|
||||
comm_tcp_socket_server *r = new comm_tcp_socket_server(j["port"].as<int>());
|
||||
r->begin(); // TODO error-checking
|
||||
|
||||
return r;
|
||||
}
|
||||
|
|
|
@ -34,6 +34,9 @@ public:
|
|||
|
||||
bool begin() override;
|
||||
|
||||
JsonDocument serialize() const override;
|
||||
static comm_tcp_socket_server *deserialize(const JsonVariantConst j);
|
||||
|
||||
std::string get_identifier() const override { return format(":%d", port) + " (server)"; }
|
||||
|
||||
bool is_connected() override;
|
||||
|
|
32
dc11.cpp
32
dc11.cpp
|
@ -271,3 +271,35 @@ void dc11::write_word(const uint16_t addr, const uint16_t v)
|
|||
|
||||
registers[reg] = v;
|
||||
}
|
||||
|
||||
JsonDocument dc11::serialize() const
|
||||
{
|
||||
JsonDocument j;
|
||||
|
||||
JsonDocument j_interfaces;
|
||||
JsonArray j_interfaces_work = j_interfaces.to<JsonArray>();
|
||||
for(auto & c: comm_interfaces)
|
||||
j_interfaces_work.add(c->serialize());
|
||||
j["interfaces"] = j_interfaces;
|
||||
|
||||
for(int regnr=0; regnr<4; regnr++)
|
||||
j[format("register-%d", regnr)] = registers[regnr];
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
dc11 *dc11::deserialize(const JsonVariantConst j, bus *const b)
|
||||
{
|
||||
std::vector<comm *> interfaces;
|
||||
|
||||
JsonArrayConst j_interfaces = j["interfaces"];
|
||||
for(auto v: j_interfaces)
|
||||
interfaces.push_back(comm::deserialize(v, b));
|
||||
|
||||
dc11 *r = new dc11(b, interfaces);
|
||||
|
||||
for(int regnr=0; regnr<4; regnr++)
|
||||
r->registers[regnr] = j[format("register-%d", regnr)];
|
||||
|
||||
return r;
|
||||
}
|
||||
|
|
6
dc11.h
6
dc11.h
|
@ -46,10 +46,8 @@ public:
|
|||
dc11(bus *const b, const std::vector<comm *> & comm_interfaces);
|
||||
virtual ~dc11();
|
||||
|
||||
#if IS_POSIX
|
||||
// json_t *serialize();
|
||||
// static tty *deserialize(const json_t *const j, bus *const b, console *const cnsl);
|
||||
#endif
|
||||
JsonDocument serialize() const;
|
||||
static dc11 *deserialize(const JsonVariantConst j, bus *const b);
|
||||
|
||||
std::vector<comm *> *get_comm_interfaces() { return &comm_interfaces; }
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue