-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #780 from SignalK/restore_threshold_transform
Restore Threshold transform
- Loading branch information
Showing
3 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "threshold.h" | ||
|
||
namespace sensesp { | ||
|
||
static const char kSchemaTemplate[] = | ||
R"({"type":"object","properties":{"min":{"title":"Minimum value","type":"!!TYPE!!","description":"Minimum value to be 'in range'"},"max":{"title":"Maximum value","type":"!!TYPE!!","description":"Maximum value to be 'in range'"},"in_range":{"title":"In range value","type":"boolean","description":"When checked, output true when input value is 'in range'"}}})"; | ||
|
||
const String ConfigSchema(const ThresholdTransform<float>& obj) { | ||
String schema = kSchemaTemplate; | ||
schema.replace("!!TYPE!!", "number"); | ||
return schema; | ||
} | ||
|
||
const String ConfigSchema(const ThresholdTransform<int>& obj) { | ||
String schema = kSchemaTemplate; | ||
schema.replace("!!TYPE!!", "integer"); | ||
return schema; | ||
} | ||
|
||
} // namespace sensesp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "transform.h" | ||
#ifndef _threshold_h | ||
#define _threshold_h | ||
|
||
namespace sensesp { | ||
|
||
/** | ||
* @brief A Transform base class that translates the value of type C into | ||
* boolean. Base class for classes FloatThreshold and IntThreshold. | ||
* | ||
* @param min_value Minimum value of input for output to be the value of | ||
* in_range. | ||
* | ||
* @param max_value Maximum value of input for output to be the value of | ||
* in_range. | ||
* | ||
* @param in_range Output value if input value is in range. | ||
* | ||
* @param out_range Output value if input value is out of the range. | ||
*/ | ||
template <typename C> | ||
class ThresholdTransform : public Transform<C, bool> { | ||
public: | ||
|
||
ThresholdTransform(C min_value, C max_value, bool in_range, | ||
String config_path = "") | ||
: Transform<C, bool>(config_path), | ||
min_value_{min_value}, | ||
max_value_{max_value}, | ||
in_range_{in_range} { | ||
this->load(); | ||
}; | ||
|
||
virtual void set(const C& new_value) override { | ||
if (new_value >= min_value_ && new_value <= max_value_) { | ||
this->output_ = in_range_; | ||
} else { | ||
this->output_ = !in_range_; | ||
} | ||
|
||
this->notify(); | ||
} | ||
|
||
bool from_json(const JsonObject& root) override { | ||
String expected[] = {"min", "max", "in_range", "out_range"}; | ||
for (auto str : expected) { | ||
|
||
if (!root[str].is<JsonVariant>()) { | ||
return false; | ||
} | ||
} | ||
min_value_ = root["min"]; | ||
max_value_ = root["max"]; | ||
in_range_ = root["in_range"]; | ||
return true; | ||
} | ||
|
||
bool to_json(JsonObject& root) override { | ||
root["min"] = min_value_; | ||
root["max"] = max_value_; | ||
root["in_range"] = in_range_; | ||
return true; | ||
} | ||
|
||
protected: | ||
C min_value_; | ||
C max_value_; | ||
bool in_range_; | ||
}; | ||
|
||
const String ConfigSchema(const ThresholdTransform<float>& obj); | ||
const String ConfigSchema(const ThresholdTransform<int>& obj); | ||
|
||
typedef ThresholdTransform<float> FloatThreshold; | ||
typedef ThresholdTransform<int> IntThreshold; | ||
|
||
} // namespace sensesp | ||
#endif |