Skip to content

Commit

Permalink
Merge pull request #684 from SignalK/constant_sensor_config
Browse files Browse the repository at this point in the history
Fix constant sensor configuration schema typing
  • Loading branch information
mairas authored Mar 10, 2024
2 parents 050a81e + c8d7fb2 commit 398a696
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/sensesp/sensors/constant_sensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "constant_sensor.h"

#include <Arduino.h>

namespace sensesp {

template <>
const String ConstantSensor<String>::sensor_type_ = "string";
template <>
const String ConstantSensor<int>::sensor_type_ = "integer";
template <>
const String ConstantSensor<float>::sensor_type_ = "number";
template <>
const String ConstantSensor<bool>::sensor_type_ = "boolean";

} // namespace sensesp
23 changes: 19 additions & 4 deletions src/sensesp/sensors/constant_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ namespace sensesp {
* @param[in] config_path Configuration path for the sensor.
*/

static const char SCHEMA_CONSTANT_SENSOR[] PROGMEM = R"###({
static const char SCHEMA_CONSTANT_SENSOR[] = R"###({
"type": "object",
"properties": {
"value": { "title": "Constant Value", "type": "number", "description": "Constant value" }
"value": { "title": "Constant Value", "type": "%TYPE%", "description": "Constant value" }
}
})###";

Expand Down Expand Up @@ -72,22 +72,37 @@ class ConstantSensor : public Sensor<T> {
virtual bool set_configuration(const JsonObject &config) override {
// Neither of the configuration parameters are mandatory
if (config.containsKey("value")) {
value_ = config["value"];
value_ = config["value"].as<T>();
}
if (config.containsKey("interval")) {
send_interval_ = config["interval"];
}
return true;
}
virtual String get_config_schema() override {
return FPSTR(SCHEMA_CONSTANT_SENSOR);
String schema = SCHEMA_CONSTANT_SENSOR;
schema.replace("%TYPE%", sensor_type_);
return schema;
}
void update() { this->emit(value_); }

T value_;
int send_interval_; // seconds

static const String sensor_type_;
};

template <class T>
const String ConstantSensor<T>::sensor_type_ = "";
template <>
const String ConstantSensor<int>::sensor_type_;
template <>
const String ConstantSensor<float>::sensor_type_;
template <>
const String ConstantSensor<String>::sensor_type_;
template <>
const String ConstantSensor<bool>::sensor_type_;

// ..........................................
// constant value sensors
// ..........................................
Expand Down

0 comments on commit 398a696

Please sign in to comment.