タイムスタンプ調整
This commit is contained in:
parent
787f236fc6
commit
e83a7cff80
|
|
@ -47,6 +47,8 @@ public:
|
||||||
|
|
||||||
void onDisconnect(BLEServer* server) override {
|
void onDisconnect(BLEServer* server) override {
|
||||||
logger_->bluetoothConnected_ = false;
|
logger_->bluetoothConnected_ = false;
|
||||||
|
logger_->commandBuffer_ = "";
|
||||||
|
logger_->lastAdvertise_ = millis();
|
||||||
server->startAdvertising();
|
server->startAdvertising();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,12 +90,18 @@ void SensorLogger::begin(uint8_t soilPin) {
|
||||||
|
|
||||||
readAndStore();
|
readAndStore();
|
||||||
lastMeasure_ = millis();
|
lastMeasure_ = millis();
|
||||||
|
lastAdvertise_ = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SensorLogger::update() {
|
bool SensorLogger::update() {
|
||||||
processBluetooth();
|
processBluetooth();
|
||||||
|
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
|
if (!bluetoothConnected_ && now - lastAdvertise_ >= kAdvertiseRetryIntervalMs) {
|
||||||
|
lastAdvertise_ = now;
|
||||||
|
BLEDevice::startAdvertising();
|
||||||
|
}
|
||||||
|
|
||||||
if (now - lastMeasure_ >= kMeasureIntervalMs) {
|
if (now - lastMeasure_ >= kMeasureIntervalMs) {
|
||||||
lastMeasure_ = now;
|
lastMeasure_ = now;
|
||||||
readAndStore();
|
readAndStore();
|
||||||
|
|
@ -140,12 +148,11 @@ size_t SensorLogger::logCount() const {
|
||||||
return logCount_;
|
return logCount_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SensorLogger::getCurrentTime(char* buffer, size_t bufferSize) const {
|
bool SensorLogger::formatTimestamp(int64_t timestamp, char* buffer, size_t bufferSize) const {
|
||||||
if (bufferSize == 0) {
|
if (bufferSize == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t timestamp = currentTimestamp();
|
|
||||||
if (timeSynchronized_) {
|
if (timeSynchronized_) {
|
||||||
time_t unixTimestamp = static_cast<time_t>(timestamp);
|
time_t unixTimestamp = static_cast<time_t>(timestamp);
|
||||||
struct tm date;
|
struct tm date;
|
||||||
|
|
@ -177,6 +184,10 @@ bool SensorLogger::getCurrentTime(char* buffer, size_t bufferSize) const {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SensorLogger::getCurrentTime(char* buffer, size_t bufferSize) const {
|
||||||
|
return formatTimestamp(currentTimestamp(), buffer, bufferSize);
|
||||||
|
}
|
||||||
|
|
||||||
void SensorLogger::readAndStore() {
|
void SensorLogger::readAndStore() {
|
||||||
sensors_event_t humidityEvent;
|
sensors_event_t humidityEvent;
|
||||||
sensors_event_t temperatureEvent;
|
sensors_event_t temperatureEvent;
|
||||||
|
|
@ -230,6 +241,23 @@ void SensorLogger::receiveBluetoothData(const String& data) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SensorLogger::applyHistoricalTimeAdjustment(int64_t epoch) {
|
||||||
|
if (historicalAdjustmentApplied_ || timeSynchronized_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int64_t elapsedSeconds = millis() / 1000UL;
|
||||||
|
historicalEpochOffset_ = epoch - elapsedSeconds;
|
||||||
|
for (size_t index = 0; index < logCount_; ++index) {
|
||||||
|
size_t logIndex = (logStart_ + index) % kMaxLogEntries;
|
||||||
|
const int64_t storedSeconds = log_[logIndex].timestamp;
|
||||||
|
if (storedSeconds <= elapsedSeconds) {
|
||||||
|
log_[logIndex].timestamp = storedSeconds + historicalEpochOffset_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
historicalAdjustmentApplied_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
void SensorLogger::processCommand(const String& command) {
|
void SensorLogger::processCommand(const String& command) {
|
||||||
if (command == "GET_LOG") {
|
if (command == "GET_LOG") {
|
||||||
sendLog();
|
sendLog();
|
||||||
|
|
@ -257,6 +285,7 @@ void SensorLogger::processCommand(const String& command) {
|
||||||
epoch = static_cast<unsigned long>(mktime(&date));
|
epoch = static_cast<unsigned long>(mktime(&date));
|
||||||
}
|
}
|
||||||
if (epoch != 0) {
|
if (epoch != 0) {
|
||||||
|
applyHistoricalTimeAdjustment(static_cast<int64_t>(epoch));
|
||||||
epochOffset_ = epoch - millis() / 1000UL;
|
epochOffset_ = epoch - millis() / 1000UL;
|
||||||
timeSynchronized_ = true;
|
timeSynchronized_ = true;
|
||||||
txCharacteristic_->setValue("OK_TIME\n");
|
txCharacteristic_->setValue("OK_TIME\n");
|
||||||
|
|
@ -269,17 +298,22 @@ void SensorLogger::processCommand(const String& command) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void SensorLogger::sendLog() {
|
void SensorLogger::sendLog() {
|
||||||
sendNotification("timestamp,temp,humid,light,soil,device\n");
|
sendNotification("タイムスタンプ,温度,湿度,土壌水分,光量,デバイス名\n");
|
||||||
for (size_t index = 0; index < logCount_; ++index) {
|
for (size_t index = 0; index < logCount_; ++index) {
|
||||||
size_t logIndex = (logStart_ + index) % kMaxLogEntries;
|
size_t logIndex = (logStart_ + index) % kMaxLogEntries;
|
||||||
const SensorReading& reading = log_[logIndex];
|
const SensorReading& reading = log_[logIndex];
|
||||||
char line[128];
|
char timestamp[32];
|
||||||
snprintf(line, sizeof(line), "%lld,%.2f,%.2f,%.2f,%u,%s\n",
|
char line[160];
|
||||||
reading.timestamp,
|
const bool timestampValid = formatTimestamp(reading.timestamp, timestamp, sizeof(timestamp));
|
||||||
|
if (!timestampValid) {
|
||||||
|
timestamp[0] = '\0';
|
||||||
|
}
|
||||||
|
snprintf(line, sizeof(line), "%s,%.2f,%.2f,%u,%.2f,%s\n",
|
||||||
|
timestamp,
|
||||||
reading.temperature,
|
reading.temperature,
|
||||||
reading.humidity,
|
reading.humidity,
|
||||||
reading.illuminance,
|
|
||||||
reading.soilMoisture,
|
reading.soilMoisture,
|
||||||
|
reading.illuminance,
|
||||||
bluetoothName_.c_str());
|
bluetoothName_.c_str());
|
||||||
sendNotification(line);
|
sendNotification(line);
|
||||||
delay(20);
|
delay(20);
|
||||||
|
|
|
||||||
|
|
@ -24,11 +24,13 @@ public:
|
||||||
bool hasReading() const;
|
bool hasReading() const;
|
||||||
size_t logCount() const;
|
size_t logCount() const;
|
||||||
bool getCurrentTime(char* buffer, size_t bufferSize) const;
|
bool getCurrentTime(char* buffer, size_t bufferSize) const;
|
||||||
|
bool formatTimestamp(int64_t timestamp, char* buffer, size_t bufferSize) const;
|
||||||
const char* bluetoothName() const;
|
const char* bluetoothName() const;
|
||||||
bool isBluetoothConnected() const;
|
bool isBluetoothConnected() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr unsigned long kMeasureIntervalMs = 3600000UL;
|
static constexpr unsigned long kMeasureIntervalMs = 10000UL;
|
||||||
|
static constexpr unsigned long kAdvertiseRetryIntervalMs = 500UL;
|
||||||
|
|
||||||
void readAndStore();
|
void readAndStore();
|
||||||
void processBluetooth();
|
void processBluetooth();
|
||||||
|
|
@ -37,6 +39,7 @@ private:
|
||||||
void sendNotification(const char* data);
|
void sendNotification(const char* data);
|
||||||
void sendLog();
|
void sendLog();
|
||||||
void clearLog();
|
void clearLog();
|
||||||
|
void applyHistoricalTimeAdjustment(int64_t epoch);
|
||||||
int64_t currentTimestamp() const;
|
int64_t currentTimestamp() const;
|
||||||
|
|
||||||
BLEServer* server_ = nullptr;
|
BLEServer* server_ = nullptr;
|
||||||
|
|
@ -47,8 +50,11 @@ private:
|
||||||
size_t logCount_ = 0;
|
size_t logCount_ = 0;
|
||||||
uint8_t soilPin_ = 0;
|
uint8_t soilPin_ = 0;
|
||||||
unsigned long lastMeasure_ = 0;
|
unsigned long lastMeasure_ = 0;
|
||||||
|
unsigned long lastAdvertise_ = 0;
|
||||||
int64_t epochOffset_ = 0;
|
int64_t epochOffset_ = 0;
|
||||||
|
int64_t historicalEpochOffset_ = 0;
|
||||||
bool timeSynchronized_ = false;
|
bool timeSynchronized_ = false;
|
||||||
|
bool historicalAdjustmentApplied_ = false;
|
||||||
bool bluetoothConnected_ = false;
|
bool bluetoothConnected_ = false;
|
||||||
String bluetoothName_;
|
String bluetoothName_;
|
||||||
String commandBuffer_;
|
String commandBuffer_;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue