LoRa_APRS_iGate/src/syslog_utils.cpp

75 lines
4.6 KiB
C++
Raw Normal View History

2023-06-13 05:36:39 +02:00
#include <WiFiUdp.h>
#include <WiFi.h>
2023-06-12 07:31:18 +02:00
#include "configuration.h"
#include "syslog_utils.h"
#include "gps_utils.h"
2023-06-13 05:36:39 +02:00
2023-06-12 07:31:18 +02:00
extern Configuration Config;
2023-06-13 05:36:39 +02:00
extern int stationMode;
WiFiUDP udpClient;
namespace SYSLOG_Utils {
2024-01-03 02:12:10 +01:00
void log(String type, String packet, int rssi, float snr, int freqError) {
2024-01-22 13:25:53 +01:00
String syslogPacket = "<165>1 - " + Config.callsign + " CA2RXU_LoRa_iGate_1.2" + " - - - "; //RFC5424 The Syslog Protocol
2024-01-03 02:12:10 +01:00
if (Config.syslog.active && (stationMode==1 || stationMode==2 || (stationMode==5 && WiFi.status()==WL_CONNECTED))) {
if (type == "APRSIS Tx") {
if (packet.indexOf(":>") > 10) {
2024-01-22 13:29:29 +01:00
syslogPacket += type + " / StartUp_Status / " + packet.substring(packet.indexOf(":>")+2);
} else {
syslogPacket += type + " / QUERY / " + packet;
2024-01-03 02:12:10 +01:00
}
2024-01-22 12:11:27 +01:00
} else if (type == "Rx") {
2024-01-03 02:12:10 +01:00
if (packet.indexOf("::") > 10) {
2024-01-05 02:12:09 +01:00
syslogPacket += type + " / MESSAGE / " + packet.substring(3,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf("::")+2);
2024-01-03 02:12:10 +01:00
syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz";
} else if (packet.indexOf(":!") > 10 || packet.indexOf(":=") > 10) {
2024-01-05 02:12:09 +01:00
syslogPacket += type + " / GPS / " + packet.substring(3,packet.indexOf(">")) + " / ";
2024-01-03 02:12:10 +01:00
if (packet.indexOf("WIDE1-1") > 10) {
syslogPacket += packet.substring(packet.indexOf(">")+1,packet.indexOf(",")) + " / WIDE1-1 / ";
} else {
2024-01-22 17:54:26 +01:00
syslogPacket += packet.substring(packet.indexOf(">")+1,packet.indexOf(":")) + " / - / ";
2024-01-03 02:12:10 +01:00
}
syslogPacket += String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz / " + GPS_Utils::getDistance(packet);
} else if (packet.indexOf(":>") > 10) {
2024-01-05 02:12:09 +01:00
syslogPacket += type + " / STATUS / " + packet.substring(3,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf(":>")+2);
2024-01-03 02:12:10 +01:00
syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz";
} else if (packet.indexOf(":`") > 10) {
2024-01-05 02:12:09 +01:00
syslogPacket += type + " / MIC-E / " + packet.substring(3,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf(":`")+2);
2024-01-03 02:12:10 +01:00
syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz";
} else if (packet.indexOf(":T#") >= 10 && packet.indexOf(":=/") == -1) {
2024-01-05 02:12:09 +01:00
syslogPacket += type + " / TELEMETRY / " + packet.substring(3,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf(":T#")+3);
2024-01-03 02:12:10 +01:00
syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz";
} else if (packet.indexOf(":;") > 10) {
2024-01-05 02:12:09 +01:00
syslogPacket += type + " / OBJECT / " + packet.substring(3,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf(":;")+2);
2024-01-03 02:12:10 +01:00
syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz";
2023-06-13 05:36:39 +02:00
} else {
2024-01-05 02:12:09 +01:00
syslogPacket += type + " / " + packet;
2024-01-03 02:12:10 +01:00
syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz";
}
2024-01-22 12:11:27 +01:00
} else if (type == "Tx") {
2024-01-03 02:12:10 +01:00
if (packet.indexOf("RFONLY") > 10) {
2024-01-13 16:48:44 +01:00
syslogPacket += type + " / RFONLY / " + packet;
2024-01-03 02:12:10 +01:00
} else if (packet.indexOf("::") > 10) {
2024-01-05 02:12:09 +01:00
syslogPacket += type + " / MESSAGE / " + packet.substring(0,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf("::")+2);
2024-01-03 02:12:10 +01:00
} else {
2024-01-05 02:12:09 +01:00
syslogPacket += type + " / " + packet;
2023-06-13 05:36:39 +02:00
}
} else {
2024-01-10 16:39:13 +01:00
syslogPacket = "<165>1 - ERROR LoRa - - - ERROR / Error in Syslog Packet"; //RFC5424 The Syslog Protocol
2023-06-13 05:36:39 +02:00
}
2024-01-03 02:12:10 +01:00
udpClient.beginPacket(Config.syslog.server.c_str(), Config.syslog.port);
udpClient.write((const uint8_t*)syslogPacket.c_str(), syslogPacket.length());
udpClient.endPacket();
2023-06-13 05:36:39 +02:00
}
}
2023-06-12 07:31:18 +02:00
2024-01-03 02:12:10 +01:00
void setup() {
if (Config.syslog.active && (stationMode==1 || stationMode==2 || (stationMode==5 && WiFi.status()==WL_CONNECTED))) {
udpClient.begin(WiFi.localIP(), 0);
Serial.println("init : Syslog Server ... done! (at " + Config.syslog.server + ")");
}
2023-06-12 07:31:18 +02:00
}
}