Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General cleanup and refactoring in preparation for batch transmission #434

Merged
merged 11 commits into from
Sep 14, 2023
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Changed
- **BREAKING** Refactored how the publisher transmit buffer works. This will require adjustment to custom data publishers.

### Added

Expand Down
34 changes: 9 additions & 25 deletions src/LoggerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,25 +305,11 @@ bool Logger::syncRTC() {
PRINTOUT(F("Could not wake modem for clock sync."));
}
watchDogTimer.resetWatchDog();
// Power down the modem - but only if there will be more than 15 seconds
// before the NEXT logging interval - it can take the modem that long to
// shut down

uint32_t setupFinishTime = getNowLocalEpoch();
if (setupFinishTime % (_loggingIntervalMinutes * 60) > 15) {
MS_DBG(F("At"), formatDateTime_ISO8601(setupFinishTime), F("with"),
setupFinishTime % (_loggingIntervalMinutes * 60),
F("seconds until next logging interval, putting modem to "
"sleep"));
_logModem->disconnectInternet();
_logModem->modemSleepPowerDown();
} else {
MS_DBG(F("At"), formatDateTime_ISO8601(setupFinishTime),
F("there are only"),
setupFinishTime % (_loggingIntervalMinutes * 60),
F("seconds until next logging interval; leaving modem on "
"and connected to the internet."));
}

// Power down the modem now that we are done with it
MS_DBG(F("Powering down modem after clock sync."));
_logModem->disconnectInternet();
_logModem->modemSleepPowerDown();
}
watchDogTimer.resetWatchDog();
return success;
Expand Down Expand Up @@ -1053,12 +1039,10 @@ bool Logger::initializeSDCard(void) {

// Protected helper function - This sets a timestamp on a file
void Logger::setFileTimestamp(File fileToStamp, uint8_t stampFlag) {
fileToStamp.timestamp(stampFlag, dtFromEpoch(getNowLocalEpoch()).year(),
dtFromEpoch(getNowLocalEpoch()).month(),
dtFromEpoch(getNowLocalEpoch()).date(),
dtFromEpoch(getNowLocalEpoch()).hour(),
dtFromEpoch(getNowLocalEpoch()).minute(),
dtFromEpoch(getNowLocalEpoch()).second());
DateTime dt = dtFromEpoch(getNowLocalEpoch());

fileToStamp.timestamp(stampFlag, dt.year(), dt.month(), dt.date(),
dt.hour(), dt.minute(), dt.second());
}


Expand Down
27 changes: 6 additions & 21 deletions src/LoggerModem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,31 +394,16 @@ float loggerModem::getModemTemperature() {

// Helper to get approximate RSSI from CSQ (assuming no noise)
int16_t loggerModem::getRSSIFromCSQ(int16_t csq) {
int16_t CSQs[33] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 99};
int16_t RSSIs[33] = {-113, -111, -109, -107, -105, -103, -101, -99, -97,
-95, -93, -91, -89, -87, -85, -83, -81, -79,
-77, -75, -73, -71, -69, -67, -65, -63, -61,
-59, -57, -55, -53, -51, 0};
for (uint8_t i = 0; i < 33; i++) {
if (CSQs[i] == csq) return RSSIs[i];
}
return 0;
if ((csq < 0) || (csq > 31)) return 0;
// equation matches previous table. not sure the original motivation.
return ((csq * 2) - 113);
}

// Helper to get signal percent from CSQ
int16_t loggerModem::getPctFromCSQ(int16_t csq) {
int16_t CSQs[33] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 99};
int16_t PCTs[33] = {0, 3, 6, 10, 13, 16, 19, 23, 26, 29, 32,
36, 39, 42, 45, 48, 52, 55, 58, 61, 65, 68,
71, 74, 78, 81, 84, 87, 90, 94, 97, 100, 0};
for (uint8_t i = 0; i < 33; i++) {
if (CSQs[i] == csq) return PCTs[i];
}
return 0;
if ((csq < 0) || (csq > 31)) return 0;
// equation matches previous table. not sure the original motivation.
return (csq * 827 + 127) >> 8;
}

// Helper to get signal percent from RSSI
Expand Down
3 changes: 2 additions & 1 deletion src/VariableBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ bool Variable::checkUUIDFormat(void) {
int first_invalid = strspn(_uuid, acceptableChars);
if (first_invalid != 36) {
MS_DBG(F("UUID for"), getVarCode(), '(', _uuid, ')',
F("has a bad character"), _uuid[first_invalid], F("at"), first_invalid);
F("has a bad character"), _uuid[first_invalid], F("at"),
first_invalid);
return false;
}
return true;
Expand Down
70 changes: 40 additions & 30 deletions src/dataPublisherBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
*/
#include "dataPublisherBase.h"

char dataPublisher::txBuffer[MS_SEND_BUFFER_SIZE] = {'\0'};
char dataPublisher::txBuffer[MS_SEND_BUFFER_SIZE];
Client* dataPublisher::txBufferOutClient = nullptr;
size_t dataPublisher::txBufferLen;

// Basic chunks of HTTP
const char* dataPublisher::getHeader = "GET ";
Expand All @@ -21,19 +23,16 @@ const char* dataPublisher::hostHeader = "\r\nHost: ";
// Constructors
dataPublisher::dataPublisher() {}

dataPublisher::dataPublisher(Logger& baseLogger, uint8_t sendEveryX,
uint8_t sendOffset)
dataPublisher::dataPublisher(Logger& baseLogger, int sendEveryX)
: _baseLogger(&baseLogger),
_sendEveryX(sendEveryX),
_sendOffset(sendOffset) {
_sendEveryX(sendEveryX) {
_baseLogger->registerDataPublisher(this); // register self with logger
}
dataPublisher::dataPublisher(Logger& baseLogger, Client* inClient,
uint8_t sendEveryX, uint8_t sendOffset)
int sendEveryX)
: _baseLogger(&baseLogger),
_inClient(inClient),
_sendEveryX(sendEveryX),
_sendOffset(sendOffset) {
_sendEveryX(sendEveryX) {
_baseLogger->registerDataPublisher(this); // register self with logger
}
// Destructor
Expand All @@ -53,11 +52,10 @@ void dataPublisher::attachToLogger(Logger& baseLogger) {
}


// Sets the parameters for frequency of sending and any offset, if needed
// NOTE: These parameters are not currently used!!
void dataPublisher::setSendFrequency(uint8_t sendEveryX, uint8_t sendOffset) {
// Sets the interval (in units of the logging interval) between attempted
// data transmissions
void dataPublisher::setSendInterval(int sendEveryX) {
_sendEveryX = sendEveryX;
_sendOffset = sendOffset;
}


Expand All @@ -71,34 +69,46 @@ void dataPublisher::begin(Logger& baseLogger) {
}


// Empties the outgoing buffer
void dataPublisher::emptyTxBuffer(void) {
MS_DBG(F("Dumping the TX Buffer"));
for (int i = 0; i < MS_SEND_BUFFER_SIZE; i++) { txBuffer[i] = '\0'; }
void dataPublisher::txBufferInit(Client* outClient) {
// remember client we are sending to
txBufferOutClient = outClient;

// reset buffer length to be empty
txBufferLen = 0;
}

void dataPublisher::txBufferAppend(const char* data, size_t length) {
while (length > 0) {
size_t remaining = MS_SEND_BUFFER_SIZE - txBufferLen;
size_t amount = remaining < length ? remaining : length;

memcpy(&txBuffer[txBufferLen], data, amount);
length -= amount;
data += amount;
txBufferLen += amount;

if (txBufferLen == MS_SEND_BUFFER_SIZE) { txBufferFlush(); }
}
}

// Returns how much space is left in the buffer
int dataPublisher::bufferFree(void) {
MS_DBG(F("Current TX Buffer Size:"), strlen(txBuffer));
return MS_SEND_BUFFER_SIZE - strlen(txBuffer);
void dataPublisher::txBufferAppend(const char* s) {
txBufferAppend(s, strlen(s));
}

void dataPublisher::txBufferAppend(char c) {
txBufferAppend(&c, 1);
}

// Sends the tx buffer to a stream and then clears it
void dataPublisher::printTxBuffer(Stream* stream, bool addNewLine) {
// Send the out buffer so far to the serial for debugging
void dataPublisher::txBufferFlush() {
#if defined(STANDARD_SERIAL_OUTPUT)
STANDARD_SERIAL_OUTPUT.write(txBuffer, strlen(txBuffer));
if (addNewLine) { PRINTOUT('\n'); }
// send to debugging stream
STANDARD_SERIAL_OUTPUT.write((const uint8_t*)txBuffer, txBufferLen);
STANDARD_SERIAL_OUTPUT.flush();
#endif
stream->write(txBuffer, strlen(txBuffer));
if (addNewLine) { stream->print("\r\n"); }
stream->flush();
txBufferOutClient->write((const uint8_t*)txBuffer, txBufferLen);
txBufferOutClient->flush();

// empty the buffer after printing it
emptyTxBuffer();
txBufferLen = 0;
}


Expand Down
Loading