Skip to content

Commit

Permalink
Improve json output
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner committed Dec 13, 2023
1 parent 829e72e commit 74e830c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
14 changes: 9 additions & 5 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@ board = esp01_1m
framework = arduino
monitor_filters = time, default
monitor_speed = 9600
build_flags =
-D EVENTUALLY_MAX_COMMANDS=5
-D EVENTUALLY_COMMAND_BUFFER_LENGTH=14
-D EVENTUALLY_DATA_BUFFER_LENGTH=11
build_flags =
-D EVENTUALLY_MAX_COMMANDS=5
-D EVENTUALLY_COMMAND_BUFFER_LENGTH=14
-D EVENTUALLY_DATA_BUFFER_LENGTH=11
lib_deps =
arduino-libraries/NTPClient@^3.2.1
matthewturner/Eventually2@^2.0
matthewturner/EventuallyCommand@^0.4.0
instanceofma/Fetch@^0.1.1
paulstoffregen/Time@^1.6.1

[env:native]
platform = native
test_build_src = false
lib_deps = instanceofma/Fetch@^0.1.1
lib_deps =
instanceofma/Fetch@^0.1.1
paulstoffregen/Time@^1.6.1

[env:debug]
platform = native
Expand All @@ -41,3 +44,4 @@ lib_deps =
matthewturner/EventuallyCommand@^0.4.0
fabiobatsilva/ArduinoFake@^0.3.1
instanceofma/Fetch@^0.1.1
paulstoffregen/Time@^1.6.1
47 changes: 36 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ bool handleWifiClient()
return true;
}

void formatEpochAsUtc(char buff[32], unsigned long epochTime)
{
sprintf(buff, "%02d-%02d-%02dT%02d:%02d:%02dZ", year(epochTime), month(epochTime), day(epochTime), hour(epochTime), minute(epochTime), second(epochTime));
}

void outputStatusAsJson(WiFiClient *pClient)
{
WiFiClient client = *pClient;
Expand All @@ -226,12 +231,16 @@ void outputStatusAsJson(WiFiClient *pClient)

unsigned long now = millis();

unsigned long epochTime = timeClient.getEpochTime();
char buff[32];
formatEpochAsUtc(buff, epochTime);

String body = F("{\n");
client.print(F("\"utc\": \""));
body += timeClient.getFormattedTime();
body += F("\"utc\": \"");
body += buff;
body += "\",\n";
body += F("\"epoch\": \"");
body += String(timeClient.getEpochTime());
body += F("\"epoch\": ");
body += String(epochTime);
body += ",\n";

bool scheduleRetrieved = false;
Expand All @@ -250,34 +259,50 @@ void outputStatusAsJson(WiFiClient *pClient)
{
int lastIndex = body.length() - 1;
body.remove(lastIndex);
body += F("\n");
}
body += F("],\n");

body += F("\"lastScheduleUpdate\": ");
body += F("\"scheduleUpdatedAt\": ");
if (scheduleRetrieved)
{
body += String(lastScheduleUpdate);
unsigned long scheduleUpdatedAt = epochTime - (lastScheduleUpdate / 1000);
formatEpochAsUtc(buff, scheduleUpdatedAt);
body += F("\"");
body += String(buff);
body += F("\"");
}
else
{
body += F("null");
}
body += F(",\n");

body += F("\"lastSync\": ");
body += F("\"timeSyncedAt\": ");
if (lastSync == 0)
{
body += F("null");
}
else
{
body += String(lastSync);
unsigned long lastSyncedAt = epochTime - (lastSync / 1000);
formatEpochAsUtc(buff, lastSyncedAt);
body += F("\"");
body += String(buff);
body += F("\"");
}
body += F(",\n");

unsigned long lastBoot = now - 0;
body += F("\"lastBoot\": ");
body += String(lastBoot);
unsigned long upTimeMs = now;
body += F("\"upTimeMs\": ");
body += String(upTimeMs);
body += F(",\n");

body += F("\"systemBootedAt\": \"");
unsigned long lastBoot = epochTime - (upTimeMs / 1000);
formatEpochAsUtc(buff, lastBoot);
body += String(buff);
body += F("\"");

body += F("\n}\n");

Expand Down
2 changes: 2 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <EventuallyCommand.h>
#include "secrets.h"
#include <Fetch.h>
#include <TimeLib.h>

#define LED 2
#define MAX_SCHEDULES 10
Expand Down Expand Up @@ -48,6 +49,7 @@ bool pong();
bool show();
void outputStatusAsJson(WiFiClient *pClient);
void outputStatusAsHtml(WiFiClient *pClient);
void formatEpochAsUtc(char buff[32], unsigned long epochTime);

EvtTimeListener pingListener(PING_SCHEDULE, true, (EvtAction)ping);
EvtTimeListener updateTimeListener(TIME_UPDATE_SCHEDULE, true, (EvtAction)updateTime);
Expand Down

0 comments on commit 74e830c

Please sign in to comment.