Skip to content

Commit

Permalink
feat: get epoch time from websocket server (closes #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipton committed Jun 6, 2024
1 parent 9fe290d commit c3c27a8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion firmware/src/radio/radio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void initWifi() {
wm.setConnectRetries(10);
wm.setConnectTimeout(3);

bool res = wm.autoConnect(generatedDeviceName, WIFI_PASSWORD);;
bool res = wm.autoConnect(generatedDeviceName, WIFI_PASSWORD);
if (res) {
Logger.printf("Connected!\n");
} else {
Expand Down
7 changes: 7 additions & 0 deletions firmware/src/radio/websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ void parseDeviceSettings(JsonChildDocument doc) {
stateHasChanged = true;
}

void parseEpochTime(JsonChildDocument doc) {
epochBase = doc["current_epoch"];
epochBase -= millis() / 1000;
}

void parseStartUpdate(JsonChildDocument doc) {
if (update) {
ESP.restart();
Expand Down Expand Up @@ -279,6 +284,8 @@ void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) {
parseApiError(doc["api_error"]);
} else if (doc.containsKey("test_packet")) {
parseTestPacket(doc["test_packet"]);
} else if (doc.containsKey("epoch_time")) {
parseEpochTime(doc["epoch_time"]);
}
} else if (type == WStype_BIN) {
parseUpdateData(payload, length);
Expand Down
9 changes: 1 addition & 8 deletions firmware/src/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,7 @@ void readState(bool skipEpoch = false) {
}

void initState() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Logger.println("Failed to obtain time");
}
time_t epoch;
time(&epoch);

uuid.seed(epoch, getEspId());
uuid.seed(getEpoch(), getEspId());

readState();
if (state.solveTime > 0) {
Expand Down
11 changes: 3 additions & 8 deletions firmware/src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,10 @@ void sendAddDevice() {
webSocket.sendTXT(json);
}

unsigned long epochBase = 0;
unsigned long getEpoch() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Logger.println("Failed to obtain time");
}
time_t epoch;
time(&epoch);

return epoch;
if (epochBase == 0) return 0;
return epochBase + (millis() / 1000);
}

void clearDisplay(uint8_t filler = 255) {
Expand Down

0 comments on commit c3c27a8

Please sign in to comment.