Skip to content
Snippets Groups Projects
ClientDataPackage.hpp 2.39 KiB
Newer Older
#pragma once

#include "SensorProtocol.hpp"
// having the data be a struct of basic types makes sending easier,
// otherwise we would have to serialize the data before sending
 private:
  MeasurementData measurementData;
  SensorInformation sensorInformation;
  unsigned long timestamp; // maybe make this array
  ClientDataPackage(MeasurementData value,
                    SensorInformation sensorInformation,
                    ErrorType errorType,
                    unsigned long timestamp)
      : measurementData(value),
        sensorInformation(sensorInformation),
        timestamp(timestamp),
        errorType(errorType) {
  }


  [[nodiscard]] ErrorType getErrorType() const {
    return errorType;
  }

  void setErrorType(ErrorType error) {
    ClientDataPackage::errorType = error;
  [[nodiscard]] const MeasurementData &
  getMeasurementData() const { return measurementData; }
  [[nodiscard]] const SensorInformation &
  getSensorInformation() const { return sensorInformation; }
  [[nodiscard]] unsigned long getTimestamp() const { return timestamp; }
  [[nodiscard]] std::string getDataPackageAsMinifiedJsonString() const {
    StaticJsonDocument<250> document; // 250 byte is the max send size of espnow
    document["hardwareName"] = sensorInformation.getHardwareNameString();
    document["timestamp"] = timestamp;
    document["sensorProtocol"] = sensorInformation.getProtocolString();
    document["value"] = measurementData.getValue();
    document["channel"] = measurementData.getChannel();


    document["i2cAddress"] = measurementData.getI2CAddress();
    document["measurementType"] = measurementData.getMeasurementTypeString();
    std::string jsonString;
    serializeJson(document, jsonString);
    return jsonString;
  }
  ClientDataPackage() : measurementData(MeasurementData(ERROR_VALUE,
                                                        MeasurementType::TEMPERATURE)),
                        sensorInformation(SensorInformation(HardwareName::NONE,
                                                            SensorProtocol::NULL_PROTOCOL)),
                        timestamp(0),
                        errorType(ErrorType::NO_DATA) {}