2021-01-03 22:43:35 +01:00
|
|
|
#include <logger.h>
|
|
|
|
|
#include <TimeLib.h>
|
|
|
|
|
#include "project_configuration.h"
|
|
|
|
|
#include "TaskAprsIs.h"
|
2021-01-04 23:10:23 +01:00
|
|
|
#include "Task.h"
|
2021-01-03 22:43:35 +01:00
|
|
|
|
|
|
|
|
String create_lat_aprs(double lat);
|
|
|
|
|
String create_long_aprs(double lng);
|
|
|
|
|
|
|
|
|
|
AprsIsTask::AprsIsTask()
|
2021-01-09 13:38:48 +01:00
|
|
|
: Task(TASK_APRS_IS, TaskAprsIs), _beacon_next_time(0)
|
2021-01-03 22:43:35 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AprsIsTask::~AprsIsTask()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 23:10:23 +01:00
|
|
|
bool AprsIsTask::setup(std::shared_ptr<Configuration> config, std::shared_ptr<BoardConfig> boardConfig)
|
2021-01-03 22:43:35 +01:00
|
|
|
{
|
2021-03-07 21:45:38 +01:00
|
|
|
_aprs_is = std::shared_ptr<APRS_IS>(new APRS_IS(config->callsign, config->aprs_is.passcode , "ESP32-APRS-IS", "0.2"));
|
2021-01-03 22:43:35 +01:00
|
|
|
|
|
|
|
|
_beaconMsg = std::shared_ptr<APRSMessage>(new APRSMessage());
|
|
|
|
|
_beaconMsg->setSource(config->callsign);
|
2021-03-07 21:45:38 +01:00
|
|
|
_beaconMsg->setDestination("APLG1");
|
2021-01-03 22:43:35 +01:00
|
|
|
String lat = create_lat_aprs(config->beacon.positionLatitude);
|
|
|
|
|
String lng = create_long_aprs(config->beacon.positionLongitude);
|
2021-03-07 21:42:48 +01:00
|
|
|
_beaconMsg->getBody()->setData(String("=") + lat + "L" + lng + "a" + config->beacon.message);
|
2021-01-03 22:43:35 +01:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AprsIsTask::loop(std::shared_ptr<Configuration> config)
|
|
|
|
|
{
|
|
|
|
|
if(!_aprs_is->connected())
|
|
|
|
|
{
|
2021-01-30 22:47:24 +01:00
|
|
|
if(!connect(config))
|
|
|
|
|
{
|
|
|
|
|
_stateInfo = "not connected";
|
|
|
|
|
_state = Error;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_stateInfo = "connected";
|
|
|
|
|
_state = Okay;
|
|
|
|
|
return false;
|
2021-01-03 22:43:35 +01:00
|
|
|
}
|
2021-01-05 23:57:59 +01:00
|
|
|
|
2021-01-03 22:43:35 +01:00
|
|
|
_aprs_is->getAPRSMessage();
|
|
|
|
|
|
2021-01-04 23:10:23 +01:00
|
|
|
if(!inputQueue.empty())
|
|
|
|
|
{
|
|
|
|
|
std::shared_ptr<APRSMessage> msg = inputQueue.getElement();
|
|
|
|
|
_aprs_is->sendMessage(msg);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 22:43:35 +01:00
|
|
|
if(_beacon_next_time < now())
|
|
|
|
|
{
|
|
|
|
|
logPrintD("[" + timeString() + "] ");
|
|
|
|
|
logPrintlnD(_beaconMsg->encode());
|
|
|
|
|
_aprs_is->sendMessage(_beaconMsg);
|
2021-01-30 22:47:24 +01:00
|
|
|
Display::instance().addFrame(std::shared_ptr<DisplayFrame>(new TextFrame("BEACON", _beaconMsg->toString())));
|
2021-01-03 22:43:35 +01:00
|
|
|
_beacon_next_time = now() + config->beacon.timeout * 60UL;
|
|
|
|
|
}
|
2021-01-30 22:47:24 +01:00
|
|
|
time_t diff = _beacon_next_time - now();
|
|
|
|
|
_stateInfo = "beacon " + String(minute(diff)) + ":" + String(second(diff));
|
|
|
|
|
_state = Okay;
|
2021-01-03 22:43:35 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 23:57:59 +01:00
|
|
|
bool AprsIsTask::connect(std::shared_ptr<Configuration> config)
|
2021-01-03 22:43:35 +01:00
|
|
|
{
|
|
|
|
|
logPrintI("connecting to APRS-IS server: ");
|
|
|
|
|
logPrintI(config->aprs_is.server);
|
|
|
|
|
logPrintI(" on port: ");
|
|
|
|
|
logPrintlnI(String(config->aprs_is.port));
|
2021-01-05 23:57:59 +01:00
|
|
|
if(!_aprs_is->connect(config->aprs_is.server, config->aprs_is.port))
|
2021-01-03 22:43:35 +01:00
|
|
|
{
|
|
|
|
|
logPrintlnE("Connection failed.");
|
2021-01-05 23:57:59 +01:00
|
|
|
return false;
|
2021-01-03 22:43:35 +01:00
|
|
|
}
|
|
|
|
|
logPrintlnI("Connected to APRS-IS server!");
|
2021-01-05 23:57:59 +01:00
|
|
|
return true;
|
2021-01-03 22:43:35 +01:00
|
|
|
}
|