Skip to content

Commit

Permalink
Merge pull request #166 from lora-aprs/logger_update
Browse files Browse the repository at this point in the history
Logger update
  • Loading branch information
peterus authored Mar 20, 2022
2 parents 817f8b9 + 7d24068 commit dc34f3b
Show file tree
Hide file tree
Showing 27 changed files with 220 additions and 208 deletions.
7 changes: 6 additions & 1 deletion data/is-cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"spreading_factor": 12,
"signal_bandwidth": 125000,
"coding_rate4": 5,
"tx_enable": true
"tx_enable": false
},
"display": {
"always_on": true,
Expand All @@ -74,5 +74,10 @@
"password": "",
"topic": "LoraAPRS/Data"
},
"syslog": {
"active": true,
"server": "syslog.lora-aprs.info",
"port": 514
},
"ntp_server": "pool.ntp.org"
}
17 changes: 7 additions & 10 deletions lib/APRS-IS/APRS-IS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,32 @@ void APRS_IS::setup(const String &user, const String &passcode, const String &to
_version = version;
}

bool APRS_IS::connect(const String &server, const int port) {
APRS_IS::ConnectionStatus APRS_IS::connect(const String &server, const int port) {
const String login = "user " + _user + " pass " + _passcode + " vers " + _tool_name + " " + _version + "\n\r";
return _connect(server, port, login);
}

bool APRS_IS::connect(const String &server, const int port, const String &filter) {
APRS_IS::ConnectionStatus APRS_IS::connect(const String &server, const int port, const String &filter) {
const String login = "user " + _user + " pass " + _passcode + " vers " + _tool_name + " " + _version + " filter " + filter + "\n\r";
return _connect(server, port, login);
}

bool APRS_IS::_connect(const String &server, const int port, const String &login_line) {
APRS_IS::ConnectionStatus APRS_IS::_connect(const String &server, const int port, const String &login_line) {
if (!_client.connect(server.c_str(), port)) {
logPrintlnE("Something went wrong on connecting! Is the server reachable?");
return false;
return ERROR_CONNECTION;
}
sendMessage(login_line);
while (true) {
String line = _client.readStringUntil('\n');
if (line.indexOf("logresp") != -1) {
if (line.indexOf("unverified") == -1) {
return true;
return SUCCESS;
} else {
logPrintlnE("User can not be verified with passcode!");
return false;
return ERROR_PASSCODE;
}
}
}
return true;
return SUCCESS;
}

bool APRS_IS::connected() {
Expand Down Expand Up @@ -76,7 +74,6 @@ std::shared_ptr<APRSMessage> APRS_IS::getAPRSMessage() {
line = _client.readStringUntil('\n');
}
if (line.startsWith("#")) {
logPrintlnD(line);
return 0;
}
if (line.length() == 0) {
Expand Down
15 changes: 11 additions & 4 deletions lib/APRS-IS/APRS-IS.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ class APRS_IS {
public:
void setup(const String &user, const String &passcode, const String &tool_name, const String &version);

bool connect(const String &server, const int port);
bool connect(const String &server, const int port, const String &filter);
bool connected();
enum ConnectionStatus
{
SUCCESS,
ERROR_CONNECTION,
ERROR_PASSCODE,
};

ConnectionStatus connect(const String &server, const int port);
ConnectionStatus connect(const String &server, const int port, const String &filter);
bool connected();

bool sendMessage(const String &message);
bool sendMessage(const std::shared_ptr<APRSMessage> message);
Expand All @@ -28,7 +35,7 @@ class APRS_IS {
String _version;
WiFiClient _client;

bool _connect(const String &server, const int port, const String &login_line);
ConnectionStatus _connect(const String &server, const int port, const String &login_line);
};

#endif
38 changes: 19 additions & 19 deletions lib/BoardFinder/BoardFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,50 @@
#include <logger.h>
#include <power_management.h>

#define MODULE_NAME "BoardFinder"

BoardConfig::BoardConfig(String name, BoardType type, uint8_t oledsda, uint8_t oledscl, uint8_t oledaddr, uint8_t oledreset, uint8_t lorasck, uint8_t loramiso, uint8_t loramosi, uint8_t loracs, uint8_t lorareset, uint8_t lorairq, bool needcheckpowerchip, bool powercheckstatus)
: Name(name), Type(type), OledSda(oledsda), OledScl(oledscl), OledAddr(oledaddr), OledReset(oledreset), LoraSck(lorasck), LoraMiso(loramiso), LoraMosi(loramosi), LoraCS(loracs), LoraReset(lorareset), LoraIRQ(lorairq), needCheckPowerChip(needcheckpowerchip), powerCheckStatus(powercheckstatus) {
}

BoardFinder::BoardFinder(const std::list<BoardConfig const *> &boardConfigs) : _boardConfigs(boardConfigs) {
}

BoardConfig const *BoardFinder::searchBoardConfig() {
logPrintlnI("looking for a board config.");
logPrintlnI("searching for OLED...");
BoardConfig const *BoardFinder::searchBoardConfig(logging::Logger &logger) {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "looking for a board config.");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "searching for OLED...");

for (BoardConfig const *boardconf : _boardConfigs) {
if (boardconf->needCheckPowerChip && checkPowerConfig(boardconf) == boardconf->powerCheckStatus) {
if (boardconf->needCheckPowerChip && checkPowerConfig(boardconf, logger) == boardconf->powerCheckStatus) {
PowerManagement powerManagement;
Wire.begin(boardconf->OledSda, boardconf->OledScl);
powerManagement.begin(Wire);
powerManagement.activateOLED();
} else if (boardconf->needCheckPowerChip) {
continue;
}
if (checkOledConfig(boardconf)) {
logPrintI("found a board config: ");
logPrintlnI(boardconf->Name);
if (checkOledConfig(boardconf, logger)) {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "found a board config: %s", boardconf->Name.c_str());
return boardconf;
}
}

logPrintlnI("could not find OLED, will search for the modem now...");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "could not find OLED, will search for the modem now...");

for (BoardConfig const *boardconf : _boardConfigs) {
if (boardconf->needCheckPowerChip && checkPowerConfig(boardconf) == boardconf->powerCheckStatus) {
if (boardconf->needCheckPowerChip && checkPowerConfig(boardconf, logger) == boardconf->powerCheckStatus) {
PowerManagement powerManagement;
Wire.begin(boardconf->OledSda, boardconf->OledScl);
powerManagement.begin(Wire);
powerManagement.activateLoRa();
}
if (checkModemConfig(boardconf)) {
logPrintI("found a board config: ");
logPrintlnI(boardconf->Name);
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "found a board config: %s", boardconf->Name.c_str());
return boardconf;
}
}

logPrintlnE("could not find a board config!");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, MODULE_NAME, "could not find a board config!");

return 0;
}
Expand All @@ -60,7 +60,7 @@ BoardConfig const *BoardFinder::getBoardConfig(String name) {
return *elem;
}

bool BoardFinder::checkOledConfig(BoardConfig const *boardConfig) {
bool BoardFinder::checkOledConfig(BoardConfig const *boardConfig, logging::Logger &logger) {
if (boardConfig->OledReset > 0) {
pinMode(boardConfig->OledReset, OUTPUT);
digitalWrite(boardConfig->OledReset, HIGH);
Expand All @@ -70,7 +70,7 @@ bool BoardFinder::checkOledConfig(BoardConfig const *boardConfig) {
digitalWrite(boardConfig->OledReset, HIGH);
}
if (!Wire.begin(boardConfig->OledSda, boardConfig->OledScl)) {
logPrintlnW("issue with wire");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_WARN, MODULE_NAME, "issue with wire");
return false;
}
Wire.beginTransmission(boardConfig->OledAddr);
Expand Down Expand Up @@ -107,9 +107,9 @@ bool BoardFinder::checkModemConfig(BoardConfig const *boardConfig) {
return false;
}

bool BoardFinder::checkPowerConfig(BoardConfig const *boardConfig) {
bool BoardFinder::checkPowerConfig(BoardConfig const *boardConfig, logging::Logger &logger) {
if (!Wire.begin(boardConfig->OledSda, boardConfig->OledScl)) {
logPrintlnW("issue with wire");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_WARN, MODULE_NAME, "issue with wire");
return false;
}
Wire.beginTransmission(0x34);
Expand All @@ -120,12 +120,12 @@ bool BoardFinder::checkPowerConfig(BoardConfig const *boardConfig) {
int response = Wire.read();
Wire.endTransmission();

logPrintlnD(String(response));
logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, MODULE_NAME, "wire response: %d", response);
if (response == 0x03) {
logPrintlnD("power chip found!");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, MODULE_NAME, "power chip found!");
return true;
}
logPrintlnD("power chip NOT found");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, MODULE_NAME, "power chip NOT found");
return false;
}

Expand Down
8 changes: 5 additions & 3 deletions lib/BoardFinder/BoardFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <SPI.h>
#include <Wire.h>

#include <logger.h>

enum BoardType
{
eHELTEC_WIFI_LORA_32_V1,
Expand Down Expand Up @@ -47,16 +49,16 @@ class BoardFinder {
public:
explicit BoardFinder(const std::list<BoardConfig const *> &boardConfigs);

BoardConfig const *searchBoardConfig();
BoardConfig const *searchBoardConfig(logging::Logger &logger);

BoardConfig const *getBoardConfig(String name);

private:
const std::list<BoardConfig const *> &_boardConfigs;

bool checkOledConfig(BoardConfig const *boardConfig);
bool checkOledConfig(BoardConfig const *boardConfig, logging::Logger &logger);
bool checkModemConfig(BoardConfig const *boardConfig);
bool checkPowerConfig(BoardConfig const *boardConfig);
bool checkPowerConfig(BoardConfig const *boardConfig, logging::Logger &logger);
};

extern BoardConfig TTGO_LORA32_V1;
Expand Down
20 changes: 11 additions & 9 deletions lib/ConfigurationManagement/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@
#include <SPIFFS.h>
#include <logger.h>

ConfigurationManagement::ConfigurationManagement(String FilePath) : mFilePath(FilePath) {
#define MODULE_NAME "ConfigurationManagement"

ConfigurationManagement::ConfigurationManagement(logging::Logger &logger, String FilePath) : mFilePath(FilePath) {
if (!SPIFFS.begin(true)) {
logPrintlnI("Mounting SPIFFS was not possible. Trying to format SPIFFS...");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "Mounting SPIFFS was not possible. Trying to format SPIFFS...");
SPIFFS.format();
if (!SPIFFS.begin()) {
logPrintlnE("Formating SPIFFS was not okay!");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, MODULE_NAME, "Formating SPIFFS was not okay!");
}
}
}

ConfigurationManagement::~ConfigurationManagement() {
}

void ConfigurationManagement::readConfiguration(Configuration &conf) {
void ConfigurationManagement::readConfiguration(logging::Logger &logger, Configuration &conf) {
File file = SPIFFS.open(mFilePath);
if (!file) {
logPrintlnE("Failed to open file for reading, using default configuration.");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, MODULE_NAME, "Failed to open file for reading, using default configuration.");
return;
}
DynamicJsonDocument data(2048);
DeserializationError error = deserializeJson(data, file);
if (error) {
logPrintlnW("Failed to read file, using default configuration.");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_WARN, MODULE_NAME, "Failed to read file, using default configuration.");
}
// serializeJson(data, Serial);
// Serial.println();
Expand All @@ -33,13 +35,13 @@ void ConfigurationManagement::readConfiguration(Configuration &conf) {
readProjectConfiguration(data, conf);

// update config in memory to get the new fields:
writeConfiguration(conf);
writeConfiguration(logger, conf);
}

void ConfigurationManagement::writeConfiguration(Configuration &conf) {
void ConfigurationManagement::writeConfiguration(logging::Logger &logger, Configuration &conf) {
File file = SPIFFS.open(mFilePath, "w");
if (!file) {
logPrintlnE("Failed to open file for writing...");
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, MODULE_NAME, "Failed to open file for writing...");
return;
}
DynamicJsonDocument data(2048);
Expand Down
8 changes: 5 additions & 3 deletions lib/ConfigurationManagement/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
#include <ArduinoJson.h>
#endif

#include <logger.h>

class Configuration;

class ConfigurationManagement {
public:
explicit ConfigurationManagement(String FilePath);
explicit ConfigurationManagement(logging::Logger &logger, String FilePath);
virtual ~ConfigurationManagement();

void readConfiguration(Configuration &conf);
void writeConfiguration(Configuration &conf);
void readConfiguration(logging::Logger &logger, Configuration &conf);
void writeConfiguration(logging::Logger &logger, Configuration &conf);

private:
virtual void readProjectConfiguration(DynamicJsonDocument &data, Configuration &conf) = 0;
Expand Down
4 changes: 4 additions & 0 deletions lib/System/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ bool System::isWifiEthConnected() const {
void System::connectedViaWifiEth(bool status) {
_isWifiEthConnected = status;
}

logging::Logger &System::getLogger() {
return _logger;
}
3 changes: 3 additions & 0 deletions lib/System/System.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SYSTEM_H_
#define SYSTEM_H_

#include <logger.h>
#include <memory>

#include "TaskManager.h"
Expand All @@ -22,13 +23,15 @@ class System {
Display & getDisplay();
bool isWifiEthConnected() const;
void connectedViaWifiEth(bool status);
logging::Logger & getLogger();

private:
BoardConfig const * _boardConfig;
Configuration const *_userConfig;
TaskManager _taskManager;
Display _display;
bool _isWifiEthConnected;
logging::Logger _logger;
};

#endif
13 changes: 5 additions & 8 deletions lib/System/TaskManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <FontConfig.h>
#include <logger.h>

#define MODULE_NAME "TaskManager"

TaskManager::TaskManager() {
}

Expand All @@ -20,26 +22,21 @@ std::list<Task *> TaskManager::getTasks() {
}

bool TaskManager::setup(System &system) {
logPrintlnV("will setup all tasks...");
system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "will setup all tasks...");
for (Task *elem : _alwaysRunTasks) {
logPrintD("call setup from ");
logPrintlnD(elem->getName());
system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, MODULE_NAME, "call setup for %s", elem->getName().c_str());
elem->setup(system);
}
for (Task *elem : _tasks) {
logPrintD("call setup from ");
logPrintlnD(elem->getName());
system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, MODULE_NAME, "call setup for %s", elem->getName().c_str());
elem->setup(system);
}
_nextTask = _tasks.begin();
return true;
}

bool TaskManager::loop(System &system) {
// logPrintlnD("will loop all tasks...");
for (Task *elem : _alwaysRunTasks) {
// logPrintD("call loop from ");
// logPrintlnD(elem->getName());
elem->loop(system);
}

Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ lib_deps =
bblanchon/ArduinoJson @ 6.17.0
lewisxhe/AXP202X_Library @ 1.1.2
peterus/APRS-Decoder-Lib @ 0.0.6
peterus/esp-logger @ 0.0.1
peterus/esp-logger @ 1.0.0
peterus/ESP-FTP-Server-Lib @ 0.9.5
knolleary/PubSubClient@^2.8
check_tool = cppcheck
Expand Down
Loading

0 comments on commit dc34f3b

Please sign in to comment.