Farm-Data-Relay-System/fdrs_gateway.h
2022-06-29 17:59:49 +02:00

172 lines
3.7 KiB
C++

/* FARM DATA RELAY SYSTEM
*
* "fdrs_sensor.h"
*
* Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
* Condensed and refactored to a single file pair by Binder Tronics (info@bindertronics.com).
*/
#ifndef __FDRS_GATEWAY_H__
#define __FDRS_GATEWAY_H__
#include "HardwareSerial.h"
#include "fdrs_types.h"
#include <string.h>
#include <vector>
#include <memory>
#include <algorithm>
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#include "ArduinoJson.h"
#include "PubSubClient.h"
#include "LoRa.h"
#define USE_LORA
#define DEBUG
#ifdef DEBUG
#define DBG(a) (Serial.println(a))
#else
#define DBG(a)
#endif
class FDRSGateWayBase{
public:
FDRSGateWayBase();
~FDRSGateWayBase();
static void add_data(DataReading_t *data);
void release(uint8_t *peer_mac = NULL);
void flush(void);
protected:
std::vector<DataReading_t> *get_data();
private:
static uint32_t peer_id;
static std::vector<DataReading_t> _data;
virtual void send(std::vector<DataReading_t> data) = 0;
virtual void forward(uint8_t *peer_mac ,std::vector<DataReading_t> data) = 0;
};
class ESP_FDRSGateWay: public FDRSGateWayBase{
public:
ESP_FDRSGateWay(void);
void init(uint8_t inturnal_mac[5]);
void add_peer(uint8_t peer_mac[6]);
void remove_peer(uint8_t peer_mac[6]);
static void OnDataRecv(uint8_t * mac, const uint8_t *incomingData, int len);
private:
static bool is_init;
uint8_t _broadcast_mac[6];
uint8_t _inturnal_mac[6];
static std::vector<Peer_t> peer_list;
static std::vector<Peer_t> unknow_peer;
static void setup(void);
void send(std::vector<DataReading_t> data) override;
void forward(uint8_t *peer_mac ,std::vector<DataReading_t> data) override;
void list_peer(uint8_t peer_mac[6]);
void unlist_peer(uint8_t peer_mac[6]);
};
class MQTT_FDRSGateWay: public FDRSGateWayBase{
public:
MQTT_FDRSGateWay(const char *ssid, const char *password, const char *server,int port = 1883);
~MQTT_FDRSGateWay(void);
void init(void);
private:
#define TOPIC_DATA "fdrs/data"
#define TOPIC_STATUS "fdrs/status"
#define TOPIC_COMMAND "fdrs/command"
static void mqtt_callback(char* topic, byte * message, unsigned int length);
const char *_ssid;
const char *_password;
const char *_server;
int _port;
WiFiClient espClient;
PubSubClient *_client;
void reconnect();
void send(std::vector<DataReading_t> data) override;
void forward(uint8_t *peer_mac ,std::vector<DataReading_t> data) override;
};
class Serial_FDRSGateWay: public FDRSGateWayBase{
public:
Serial_FDRSGateWay(HardwareSerial *serial, uint32_t baud);
void init(void);
#if defined(ESP32)
void init(int mode, int rx_pin, int tx_pin);
#endif
void get(void);
private:
HardwareSerial *_serial;
uint32_t _baud;
static void setup(void);
void pull(void);
void send(std::vector<DataReading_t> data) override;
void forward(uint8_t *peer_mac ,std::vector<DataReading_t> data) override;
};
class LoRa_FDRSGateWay: public FDRSGateWayBase{
public:
LoRa_FDRSGateWay(uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,double band,uint8_t sf);
void init(uint8_t mac[6]);
void get(void);
void add_peer(uint8_t peer_mac[6]);
void remove_peer(uint8_t peer_mac[6]);
private:
uint8_t _mac[6];
uint8_t _miso;
uint8_t _mosi;
uint8_t _sck;
uint8_t _ss;
uint8_t _rst;
uint8_t _dio0;
uint32_t _band;
uint8_t _sf;
std::vector<Peer_t> _peer_list;
void transmit(DataReading_t *packet, uint8_t len);
void send(std::vector<DataReading_t> data) override;
void forward(uint8_t *peer_mac ,std::vector<DataReading_t> data) override;
};
#endif