LoRa_APRS_iGate/src/station_utils.cpp

117 lines
4.3 KiB
C++
Raw Normal View History

2023-06-06 19:46:49 +02:00
#include "station_utils.h"
2023-10-08 14:39:44 +02:00
#include "aprs_is_utils.h"
2023-06-06 20:26:17 +02:00
#include "configuration.h"
#include "lora_utils.h"
2024-03-17 12:21:11 +01:00
#include "utils.h"
2023-06-08 06:58:10 +02:00
#include <vector>
2023-06-06 20:26:17 +02:00
extern Configuration Config;
extern uint32_t lastTxTime;
2024-04-20 15:37:22 +02:00
extern uint32_t lastRxTime;
2023-06-18 16:56:53 +02:00
extern String fourthLine;
2023-06-06 19:46:49 +02:00
2024-05-14 03:24:44 +02:00
std::vector<String> lastHeardStation;
std::vector<String> outputPacketBuffer;
2024-05-14 02:29:08 +02:00
std::vector<String> packet25SegBuffer;
2024-02-25 16:00:44 +01:00
2023-06-06 20:26:17 +02:00
namespace STATION_Utils {
2023-06-06 19:46:49 +02:00
2024-02-24 14:09:05 +01:00
void deleteNotHeard() {
2024-05-05 14:16:15 +02:00
std::vector<String> lastHeardStation_temp;
2024-03-17 12:21:11 +01:00
for (int i = 0; i < lastHeardStation.size(); i++) {
String deltaTimeString = lastHeardStation[i].substring(lastHeardStation[i].indexOf(",") + 1);
2024-02-24 14:09:05 +01:00
uint32_t deltaTime = deltaTimeString.toInt();
2024-03-17 12:21:11 +01:00
if ((millis() - deltaTime) < Config.rememberStationTime * 60 * 1000) {
2024-02-24 14:09:05 +01:00
lastHeardStation_temp.push_back(lastHeardStation[i]);
}
}
lastHeardStation.clear();
2024-03-17 12:21:11 +01:00
for (int j = 0; j < lastHeardStation_temp.size(); j++) {
2024-02-24 14:09:05 +01:00
lastHeardStation.push_back(lastHeardStation_temp[j]);
}
lastHeardStation_temp.clear();
2023-06-06 19:46:49 +02:00
}
2024-02-24 14:09:05 +01:00
void updateLastHeard(String station) {
deleteNotHeard();
bool stationHeard = false;
2024-03-17 12:21:11 +01:00
for (int i = 0; i < lastHeardStation.size(); i++) {
if (lastHeardStation[i].substring(0, lastHeardStation[i].indexOf(",")) == station) {
2024-02-24 14:09:05 +01:00
lastHeardStation[i] = station + "," + String(millis());
stationHeard = true;
}
}
if (!stationHeard) {
lastHeardStation.push_back(station + "," + String(millis()));
}
2023-06-06 20:26:17 +02:00
2024-02-24 14:09:05 +01:00
fourthLine = "Stations (" + String(Config.rememberStationTime) + "min) = ";
if (lastHeardStation.size() < 10) {
fourthLine += " ";
}
fourthLine += String(lastHeardStation.size());
2024-01-03 02:12:10 +01:00
}
2023-06-06 20:26:17 +02:00
2024-02-24 14:09:05 +01:00
bool wasHeard(String station) {
deleteNotHeard();
2024-03-17 12:21:11 +01:00
for (int i = 0; i < lastHeardStation.size(); i++) {
if (lastHeardStation[i].substring(0, lastHeardStation[i].indexOf(",")) == station) {
Utils::println(" ---> Listened Station");
2024-02-24 14:09:05 +01:00
return true;
2024-03-17 12:21:11 +01:00
}
2024-02-24 14:09:05 +01:00
}
2024-03-17 12:21:11 +01:00
Utils::println(" ---> Station not Heard for last 30 min (Not Tx)\n");
2024-02-24 14:09:05 +01:00
return false;
2024-01-03 02:12:10 +01:00
}
2023-06-06 20:26:17 +02:00
2024-05-14 02:29:08 +02:00
void clean25SegBuffer() {
2024-05-14 03:47:52 +02:00
if (!packet25SegBuffer.empty()) {
String deltaTimeString = packet25SegBuffer[0].substring(0, packet25SegBuffer[0].indexOf(","));
uint32_t deltaTime = deltaTimeString.toInt();
if ((millis() - deltaTime) > 25 * 1000) {
//Serial.print("Borrando : "); Serial.println(packet25SegBuffer[0]);
2024-05-14 02:29:08 +02:00
packet25SegBuffer.erase(packet25SegBuffer.begin());
}
}
}
bool check25SegBuffer(String station, String textMessage) {
if (!packet25SegBuffer.empty()) {
bool shouldBeIgnored = false;
for (int i = 0; i < packet25SegBuffer.size(); i++) {
2024-05-14 03:47:52 +02:00
String temp = packet25SegBuffer[i].substring(packet25SegBuffer[i].indexOf(",") + 1);
String bufferStation = temp.substring(0, temp.indexOf(","));
String bufferMessage = temp.substring(0, temp.indexOf(",") + 1);
if (bufferStation == station && bufferMessage == textMessage) {
2024-05-14 02:29:08 +02:00
shouldBeIgnored = true;
}
}
if (shouldBeIgnored) {
return false;
} else {
2024-05-14 03:47:52 +02:00
packet25SegBuffer.push_back(String(millis()) + "," + station + "," + textMessage);
2024-05-14 02:29:08 +02:00
return true;
}
} else {
2024-05-14 03:47:52 +02:00
packet25SegBuffer.push_back(String(millis()) + "," + station + "," + textMessage);
2024-05-14 02:29:08 +02:00
return true;
}
}
void processOutputPacketBuffer() {
2024-04-20 15:37:22 +02:00
int timeToWait = 3 * 1000; // 3 segs between packet Tx and also Rx ???
uint32_t lastRx = millis() - lastRxTime;
uint32_t lastTx = millis() - lastTxTime;
2024-04-20 18:38:45 +02:00
if (outputPacketBuffer.size() > 0 && lastTx > timeToWait && lastRx > timeToWait) {
LoRa_Utils::sendNewPacket(outputPacketBuffer[0]);
outputPacketBuffer.erase(outputPacketBuffer.begin());
lastTxTime = millis();
2024-02-24 14:09:05 +01:00
}
2024-01-03 02:12:10 +01:00
}
2023-09-05 06:08:18 +02:00
void addToOutputPacketBuffer(String packet) {
outputPacketBuffer.push_back(packet);
2024-01-03 02:12:10 +01:00
}
2023-09-05 06:08:18 +02:00
2023-06-06 19:46:49 +02:00
}