Update to #588 - Change "hour" to "period"

This commit is contained in:
Jm 2020-12-27 10:50:52 -08:00
parent 6e4cf22cf0
commit 15a0b3694d
3 changed files with 49 additions and 43 deletions

View File

@ -1,47 +1,41 @@
#include "airtime.h" #include "airtime.h"
#include <Arduino.h> #include <Arduino.h>
#define hoursToLog 48 #define periodsToLog 48
// A reminder that there are 3600 seconds in an hour so I don't have // A reminder that there are 3600 seconds in an hour so I don't have
// to keep googling it. // to keep googling it.
// This can be changed to a smaller number to speed up testing. // This can be changed to a smaller number to speed up testing.
// //
uint16_t secondsPerHour = 3600; uint32_t secondsPerPeriod = 3600;
uint32_t lastMillis = 0; uint32_t lastMillis = 0;
uint32_t secSinceBoot = 0; uint32_t secSinceBoot = 0;
// Don't read out of this directly. Use the helper functions. // Don't read out of this directly. Use the helper functions.
struct airtimeStruct { struct airtimeStruct {
uint16_t hourTX[hoursToLog]; uint16_t periodTX[periodsToLog];
uint16_t hourRX[hoursToLog]; uint16_t periodRX[periodsToLog];
uint16_t hourRX_ALL[hoursToLog]; uint16_t periodRX_ALL[periodsToLog];
uint8_t lastHourIndex; uint8_t lastPeriodIndex;
} airtimes; } airtimes;
void logAirtime(reportTypes reportType, uint32_t airtime_ms) void logAirtime(reportTypes reportType, uint32_t airtime_ms)
{ {
if (reportType == TX_LOG) { if (reportType == TX_LOG) {
airtimes.hourTX[0] = airtimes.hourTX[0] + round(airtime_ms / 1000); airtimes.periodTX[0] = airtimes.periodTX[0] + round(airtime_ms / 1000);
} else if (reportType == RX_LOG) { } else if (reportType == RX_LOG) {
airtimes.hourRX[0] = airtimes.hourRX[0] + round(airtime_ms / 1000); airtimes.periodRX[0] = airtimes.periodRX[0] + round(airtime_ms / 1000);
} else if (reportType == RX_ALL_LOG) { } else if (reportType == RX_ALL_LOG) {
airtimes.hourRX_ALL[0] = airtimes.hourRX_ALL[0] + round(airtime_ms / 1000); airtimes.periodRX_ALL[0] = airtimes.periodRX_ALL[0] + round(airtime_ms / 1000);
} else { } else {
// Unknown report type // Unknown report type
} }
} }
uint32_t getSecondsSinceBoot() uint8_t currentPeriodIndex()
{ {
return secSinceBoot; return ((getSecondsSinceBoot() / secondsPerPeriod) % periodsToLog);
}
uint8_t currentHourIndex()
{
// return ((secondsSinceBoot() - (secondsSinceBoot() / (hoursToLog * secondsPerHour))) / secondsPerHour);
return ((getSecondsSinceBoot() / secondsPerHour) % hoursToLog);
} }
void airtimeCalculator() void airtimeCalculator()
@ -49,18 +43,17 @@ void airtimeCalculator()
if (millis() - lastMillis > 1000) { if (millis() - lastMillis > 1000) {
lastMillis = millis(); lastMillis = millis();
secSinceBoot++; secSinceBoot++;
// DEBUG_MSG("------- lastHourIndex %i currentHourIndex %i\n", airtimes.lastHourIndex, currentHourIndex()); if (airtimes.lastPeriodIndex != currentPeriodIndex()) {
if (airtimes.lastHourIndex != currentHourIndex()) { for (int i = periodsToLog - 2; i >= 0; --i) {
for (int i = hoursToLog - 2; i >= 0; --i) { airtimes.periodTX[i + 1] = airtimes.periodTX[i];
airtimes.hourTX[i + 1] = airtimes.hourTX[i]; airtimes.periodRX[i + 1] = airtimes.periodRX[i];
airtimes.hourRX[i + 1] = airtimes.hourRX[i]; airtimes.periodRX_ALL[i + 1] = airtimes.periodRX_ALL[i];
airtimes.hourRX_ALL[i + 1] = airtimes.hourRX_ALL[i];
} }
airtimes.hourTX[0] = 0; airtimes.periodTX[0] = 0;
airtimes.hourRX[0] = 0; airtimes.periodRX[0] = 0;
airtimes.hourRX_ALL[0] = 0; airtimes.periodRX_ALL[0] = 0;
airtimes.lastHourIndex = currentHourIndex(); airtimes.lastPeriodIndex = currentPeriodIndex();
} }
} }
} }
@ -70,16 +63,26 @@ uint16_t *airtimeReport(reportTypes reportType)
// currentHourIndexReset(); // currentHourIndexReset();
if (reportType == TX_LOG) { if (reportType == TX_LOG) {
return airtimes.hourTX; return airtimes.periodTX;
} else if (reportType == RX_LOG) { } else if (reportType == RX_LOG) {
return airtimes.hourRX; return airtimes.periodRX;
} else if (reportType == RX_ALL_LOG) { } else if (reportType == RX_ALL_LOG) {
return airtimes.hourRX_ALL; return airtimes.periodRX_ALL;
} }
return 0; return 0;
} }
uint8_t getHoursToLog() uint8_t getPeriodsToLog()
{ {
return hoursToLog; return periodsToLog;
}
uint32_t getSecondsPerPeriod()
{
return secondsPerPeriod;
}
uint32_t getSecondsSinceBoot()
{
return secSinceBoot;
} }

View File

@ -28,9 +28,11 @@ void logAirtime(reportTypes reportType, uint32_t airtime_ms);
void airtimeCalculator(); void airtimeCalculator();
uint8_t currentHourIndex(); uint8_t currentPeriodIndex();
uint8_t getHoursToLog(); uint8_t getPeriodsToLog();
uint32_t getSecondsSinceBoot(); uint32_t getSecondsSinceBoot();
uint16_t *airtimeReport(reportTypes reportType); uint16_t *airtimeReport(reportTypes reportType);
uint32_t getSecondsPerPeriod();

View File

@ -1075,11 +1075,11 @@ void handleReport(HTTPRequest *req, HTTPResponse *res)
res->print("\"tx_log\": ["); res->print("\"tx_log\": [");
logArray = airtimeReport(TX_LOG); logArray = airtimeReport(TX_LOG);
for (int i = 0; i < getHoursToLog(); i++) { for (int i = 0; i < getPeriodsToLog(); i++) {
uint16_t tmp; uint16_t tmp;
tmp = *(logArray + i); tmp = *(logArray + i);
res->printf("%d", tmp); res->printf("%d", tmp);
if (i != getHoursToLog() - 1) { if (i != getPeriodsToLog() - 1) {
res->print(", "); res->print(", ");
} }
} }
@ -1088,11 +1088,11 @@ void handleReport(HTTPRequest *req, HTTPResponse *res)
res->print("\"rx_log\": ["); res->print("\"rx_log\": [");
logArray = airtimeReport(RX_LOG); logArray = airtimeReport(RX_LOG);
for (int i = 0; i < getHoursToLog(); i++) { for (int i = 0; i < getPeriodsToLog(); i++) {
uint16_t tmp; uint16_t tmp;
tmp = *(logArray + i); tmp = *(logArray + i);
res->printf("%d", tmp); res->printf("%d", tmp);
if (i != getHoursToLog() - 1) { if (i != getPeriodsToLog() - 1) {
res->print(", "); res->print(", ");
} }
} }
@ -1101,18 +1101,19 @@ void handleReport(HTTPRequest *req, HTTPResponse *res)
res->print("\"rx_all_log\": ["); res->print("\"rx_all_log\": [");
logArray = airtimeReport(RX_ALL_LOG); logArray = airtimeReport(RX_ALL_LOG);
for (int i = 0; i < getHoursToLog(); i++) { for (int i = 0; i < getPeriodsToLog(); i++) {
uint16_t tmp; uint16_t tmp;
tmp = *(logArray + i); tmp = *(logArray + i);
res->printf("%d", tmp); res->printf("%d", tmp);
if (i != getHoursToLog() - 1) { if (i != getPeriodsToLog() - 1) {
res->print(", "); res->print(", ");
} }
} }
res->println("],"); res->println("],");
res->printf("\"seconds_since_boot\": %u,\n", getSecondsSinceBoot()); res->printf("\"seconds_since_boot\": %u,\n", getSecondsSinceBoot());
res->printf("\"hours_to_log\": %u\n", getHoursToLog()); res->printf("\"seconds_per_period\": %u,\n", getSecondsPerPeriod());
res->printf("\"periods_to_log\": %u\n", getPeriodsToLog());
res->println("},"); res->println("},");