Skip to content

Commit b40d271

Browse files
committed
Fix for #294
1 parent d146b7f commit b40d271

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/SinricPro.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "SinricProStrings.h"
1717
#include "SinricProUDP.h"
1818
#include "SinricProWebsocket.h"
19+
#include "Timestamp.h"
1920
namespace SINRICPRO_NAMESPACE {
2021

2122
/**
@@ -107,7 +108,7 @@ class SinricProClass : public SinricProInterface {
107108
SinricProQueue_t receiveQueue;
108109
SinricProQueue_t sendQueue;
109110

110-
unsigned long baseTimestamp = 0;
111+
Timestamp timestamp;
111112

112113
bool _begin = false;
113114
String responseMessageStr = "";
@@ -356,7 +357,7 @@ void SinricProClass::handleReceiveQueue() {
356357

357358
void SinricProClass::handleSendQueue() {
358359
if (!isConnected()) return;
359-
if (!baseTimestamp) return;
360+
if (!timestamp.getTimestamp()) return;
360361
while (sendQueue.size() > 0) {
361362
DEBUG_SINRIC("[SinricPro:handleSendQueue()]: %i message(s) in sendQueue\r\n", sendQueue.size());
362363
DEBUG_SINRIC("[SinricPro:handleSendQueue()]: Sending message...\r\n");
@@ -366,7 +367,7 @@ void SinricProClass::handleSendQueue() {
366367

367368
DynamicJsonDocument jsonMessage(1024);
368369
deserializeJson(jsonMessage, rawMessage->getMessage());
369-
jsonMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_createdAt] = getTimestamp();
370+
jsonMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_createdAt] = timestamp.getTimestamp();
370371
signMessage(appSecret, jsonMessage);
371372

372373
String messageStr;
@@ -467,7 +468,7 @@ void SinricProClass::extractTimestamp(JsonDocument& message) {
467468
// extract timestamp from timestamp message right after websocket connection is established
468469
tempTimestamp = message["timestamp"] | 0;
469470
if (tempTimestamp) {
470-
baseTimestamp = tempTimestamp - (millis() / 1000);
471+
timestamp.setTimestamp(tempTimestamp);
471472
DEBUG_SINRIC("[SinricPro:extractTimestamp(): Got Timestamp %lu\r\n", tempTimestamp);
472473
return;
473474
}
@@ -476,7 +477,7 @@ void SinricProClass::extractTimestamp(JsonDocument& message) {
476477
tempTimestamp = message[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_createdAt] | 0;
477478
if (tempTimestamp) {
478479
DEBUG_SINRIC("[SinricPro:extractTimestamp(): Got Timestamp %lu\r\n", tempTimestamp);
479-
baseTimestamp = tempTimestamp - (millis() / 1000);
480+
timestamp.setTimestamp(tempTimestamp);
480481
return;
481482
}
482483
}
@@ -535,7 +536,7 @@ void SinricProClass::setResponseMessage(String&& message) {
535536
* @return unsigned long current timestamp (unix epoch time)
536537
*/
537538
unsigned long SinricProClass::getTimestamp() {
538-
return baseTimestamp + (millis() / 1000);
539+
return timestamp.getTimestamp();
539540
}
540541

541542
DynamicJsonDocument SinricProClass::prepareResponse(JsonDocument& requestMessage) {

src/Timestamp.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "Timestamp.h"
2+
3+
uint32_t Timestamp::getTimestamp() {
4+
update();
5+
return timestamp_ms / 1000UL;
6+
}
7+
8+
void Timestamp::setTimestamp(uint32_t new_timestamp) {
9+
timestamp_ms = uint64_t(new_timestamp) * 1000;
10+
last_update = millis();
11+
}
12+
13+
void Timestamp::update() {
14+
if (!timestamp_ms) return;
15+
uint32_t current_millis = millis();
16+
uint32_t diff_millis = current_millis - last_update;
17+
last_update = current_millis;
18+
}

src/Timestamp.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <Arduino.h>
4+
5+
class Timestamp {
6+
public:
7+
uint32_t getTimestamp();
8+
void setTimestamp(uint32_t);
9+
10+
protected:
11+
void update();
12+
13+
protected:
14+
uint64_t timestamp_ms = 0;
15+
uint32_t last_update = 0;
16+
};

0 commit comments

Comments
 (0)