39 lines
include/heartbeat_store.h
Declares HeartbeatStore, kMaxBatchSize, and the flush contract.
// Heartbeat record storage and batch-flush interface.
#pragma once
#include <cstdint>
#include <functional>
#include <vector>
 
struct HeartbeatRecord {
  uint64_t deviceId;    // unique device identifier
  uint64_t timestampMs; // record timestamp in milliseconds since epoch
  int16_t  rssi;        // received signal strength in dBm
};
 
// Buffers device heartbeat records and flushes them to persistent storage in
// batches. Flush fires when the buffer reaches kMaxBatchSize, or when
// flushPending() is called by the periodic timer.
class HeartbeatStore {
public:
  static constexpr std::size_t kMaxBatchSize = 64; // maximum records per flush batch
 
  using FlushCallback = std::function<void(std::vector<HeartbeatRecord>)>;
 
  // Parameters:
  //   cb - callback invoked with the batch whenever a flush occurs
  // Returns: new HeartbeatStore ready to accept records
  explicit HeartbeatStore(FlushCallback cb) : onFlush_(std::move(cb)) {}
 
  // Parameters:
  //   rec - heartbeat record to append; triggers flush when buffer is at capacity
  // Returns: void
  void append(const HeartbeatRecord& rec);
 
  // Parameters: none
  // Returns: void; flushes any buffered records immediately (called by timer)
  void flushPending();
 
private:
  std::vector<HeartbeatRecord> records_; // in-flight record buffer
  FlushCallback onFlush_;                // caller-supplied flush sink
};