1#ifndef SENSESP_TRANSFORMS_LAMBDA_TRANSFORM_H_
2#define SENSESP_TRANSFORMS_LAMBDA_TRANSFORM_H_
40template <
class IN,
class OUT,
class P1 = bool,
class P2 = bool,
41 class P3 = bool,
class P4 = bool,
class P5 = bool,
class P6 =
bool>
45 const String& config_path =
"")
46 :
Transform<IN, OUT>(config_path), num_params_{0}, function0_{function} {
51 const String& config_path =
"")
55 param_info_{param_info} {
60 const ParamInfo* param_info,
const String& config_path =
"")
65 param_info_{param_info} {
83 const ParamInfo* param_info,
const String& config_path =
"")
89 param_info_{param_info} {
94 P2 param2, P3 param3,
const ParamInfo* param_info,
95 const String& config_path =
"")
102 param_info_{param_info} {
107 P2 param2, P3 param3, P4 param4,
const ParamInfo* param_info,
108 const String& config_path =
"")
111 function4_{function},
116 param_info_{param_info} {
121 P1 param1, P2 param2, P3 param3, P4 param4, P5 param5,
122 const ParamInfo* param_info,
const String& config_path =
"")
125 function5_{function},
131 param_info_{param_info} {
136 P1 param1, P2 param2, P3 param3, P4 param4, P5 param5,
138 const String& config_path =
"")
141 function6_{function},
148 param_info_{param_info} {
152 void set(
const IN& input)
override {
153 switch (num_params_) {
155 this->
output_ = function0_(input);
184 switch (num_params_) {
188 doc[param_info_[4].key] =
param5_;
190 doc[param_info_[3].key] =
param4_;
192 doc[param_info_[2].key] =
param3_;
194 doc[param_info_[1].key] =
param2_;
196 doc[param_info_[0].key] =
param1_;
206 ESP_LOGD(__FILENAME__,
"Preparing to restore configuration from FS.");
207 for (
int i = 0; i < num_params_; i++) {
208 const char* expected = param_info_[i].
key;
209 if (!config[expected].is<JsonVariant>()) {
210 ESP_LOGD(__FILENAME__,
"Didn't find all keys.");
214 switch (num_params_) {
218 param5_ = config[param_info_[4].key];
220 param4_ = config[param_info_[3].key];
222 param3_ = config[param_info_[2].key];
224 param2_ = config[param_info_[1].key];
226 param1_ = config[param_info_[0].key];
230 ESP_LOGD(__FILENAME__,
"Restored configuration");
246 std::function<OUT(IN)> function0_;
247 std::function<OUT(IN, P1)> function1_;
248 std::function<OUT(IN, P1, P2)> function2_;
249 std::function<OUT(IN, P1, P2, P3)> function3_;
250 std::function<OUT(IN, P1, P2, P3, P4)> function4_;
251 std::function<OUT(IN, P1, P2, P3, P4, P5)> function5_;
252 std::function<OUT(IN, P1, P2, P3, P4, P5, P6)> function6_;
257static const char kLambdaTransformSchemaHead[] = R
"({
262static const char kLambdaTransformSchemaTail[] = R
"(
266static const String format_schema_row(
const char key[],
const char title[],
267 const char type[],
const bool read_only) {
269 const char* read_only_str = read_only ?
"true" :
"false";
271 static const char schema_row[] = R
"(
272 "%s": { "title": "%s", "type": "%s", "readOnly": %s }
275 sprintf(row, schema_row, key, title, type, read_only_str);
280template <
class IN,
class OUT>
287template <
class IN,
class OUT,
class P1>
289 String output = kLambdaTransformSchemaHead;
291 output.concat(format_schema_row(param_info[0].key, param_info[0].description,
293 output.concat(kLambdaTransformSchemaTail);
299template <
class IN,
class OUT,
class P1,
class P2>
301 String output = kLambdaTransformSchemaHead;
303 output.concat(format_schema_row(param_info[0].key,
304 param_info[0].description,
307 output.concat(format_schema_row(param_info[1].key,
308 param_info[1].description,
310 output.concat(kLambdaTransformSchemaTail);
316template <
class IN,
class OUT,
class P1,
class P2,
class P3>
318 String output = kLambdaTransformSchemaHead;
321 output.concat(format_schema_row(param_info[0].key,
322 param_info[0].description,
325 output.concat(format_schema_row(param_info[1].key,
326 param_info[1].description,
329 output.concat(format_schema_row(param_info[2].key,
330 param_info[2].description,
332 output.concat(kLambdaTransformSchemaTail);
338template <
class IN,
class OUT,
class P1,
class P2,
class P3,
class P4>
340 String output = kLambdaTransformSchemaHead;
343 output.concat(format_schema_row(param_info[0].key,
344 param_info[0].description,
347 output.concat(format_schema_row(param_info[1].key,
348 param_info[1].description,
351 output.concat(format_schema_row(param_info[2].key,
352 param_info[2].description,
355 output.concat(format_schema_row(param_info[3].key,
356 param_info[3].description,
358 output.concat(kLambdaTransformSchemaTail);
364template <
class IN,
class OUT,
class P1,
class P2,
class P3,
class P4,
class P5>
367 String output = kLambdaTransformSchemaHead;
370 output.concat(format_schema_row(param_info[0].key,
371 param_info[0].description,
374 output.concat(format_schema_row(param_info[1].key,
375 param_info[1].description,
378 output.concat(format_schema_row(param_info[2].key,
379 param_info[2].description,
382 output.concat(format_schema_row(param_info[3].key,
383 param_info[3].description,
386 output.concat(format_schema_row(param_info[4].key,
387 param_info[4].description,
389 output.concat(kLambdaTransformSchemaTail);
395template <
class IN,
class OUT,
class P1,
class P2,
class P3,
class P4,
class P5,
399 String output = kLambdaTransformSchemaHead;
402 output.concat(format_schema_row(param_info[0].key,
403 param_info[0].description,
406 output.concat(format_schema_row(param_info[1].key,
407 param_info[1].description,
410 output.concat(format_schema_row(param_info[2].key,
411 param_info[2].description,
414 output.concat(format_schema_row(param_info[3].key,
415 param_info[3].description,
418 output.concat(format_schema_row(param_info[4].key,
419 param_info[4].description,
422 output.concat(format_schema_row(param_info[5].key,
423 param_info[5].description,
425 output.concat(kLambdaTransformSchemaTail);
virtual bool load() override
Load and populate the object from a persistent storage.
const String ConfigSchema(const SmartSwitchController &obj)
const char * get_schema_type_string(const int)