From 67d5f85c931ed72c9277744e7ac9c0d63ed8d45e Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 12 Oct 2021 23:49:52 +0200 Subject: [PATCH] Generic things: Add a generic water level sensor --- .../integrationplugingenericthings.cpp | 25 ++++++++ .../integrationplugingenericthings.json | 58 +++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/genericthings/integrationplugingenericthings.cpp b/genericthings/integrationplugingenericthings.cpp index 8e93f0d0e..6739ed99f 100644 --- a/genericthings/integrationplugingenericthings.cpp +++ b/genericthings/integrationplugingenericthings.cpp @@ -251,6 +251,19 @@ void IntegrationPluginGenericThings::setupThing(ThingSetupInfo *info) thermostatCheckPowerOutputState(thing); } }); + } else if (thing->thingClassId() == waterLevelSensorThingClassId) { + connect(thing, &Thing::settingChanged, thing, [ thing](const ParamTypeId &settingTypeId, const QVariant &value){ + if (settingTypeId == waterLevelSensorSettingsCapacityParamTypeId) { + double capacity = value.toDouble(); + double input = thing->stateValue(waterLevelSensorInputStateTypeId).toDouble(); + double minInputValue = thing->setting(waterLevelSensorSettingsMinInputValueParamTypeId).toDouble(); + double maxInputValue = thing->setting(waterLevelSensorSettingsMaxInputValueParamTypeId).toDouble(); + double normalizedInput = (input - minInputValue) / (maxInputValue - minInputValue); + double waterLevel = normalizedInput * capacity; + thing->setStateMaxValue(waterLevelSensorWaterLevelStateTypeId, capacity); + thing->setStateValue(waterLevelSensorWaterLevelStateTypeId, waterLevel); + } + }); } else if (thing->thingClassId() == sgReadyThingClassId) { bool relay1 = thing->stateValue(sgReadyRelay1StateTypeId).toBool(); bool relay2 = thing->stateValue(sgReadyRelay2StateTypeId).toBool(); @@ -866,6 +879,18 @@ void IntegrationPluginGenericThings::executeAction(ThingActionInfo *info) info->finish(Thing::ThingErrorNoError); return; } + } else if (thing->thingClassId() == waterLevelSensorThingClassId) { + if (action.actionTypeId() == waterLevelSensorInputActionTypeId) { + double capacity = thing->setting(waterLevelSensorSettingsCapacityParamTypeId).toDouble(); + double input = action.paramValue(waterLevelSensorInputActionInputParamTypeId).toDouble(); + double minInputValue = thing->setting(waterLevelSensorSettingsMinInputValueParamTypeId).toDouble(); + double maxInputValue = thing->setting(waterLevelSensorSettingsMaxInputValueParamTypeId).toDouble(); + double normalizedInput = (input - minInputValue) / (maxInputValue - minInputValue); + double waterLevel = normalizedInput * capacity; + thing->setStateValue(waterLevelSensorWaterLevelStateTypeId, waterLevel); + info->finish(Thing::ThingErrorNoError); + return; + } } else if (thing->thingClassId() == presenceSensorThingClassId) { if (action.actionTypeId() == presenceSensorIsPresentActionTypeId) { bool isPresent = action.paramValue(presenceSensorIsPresentActionIsPresentParamTypeId).toBool(); diff --git a/genericthings/integrationplugingenericthings.json b/genericthings/integrationplugingenericthings.json index 763809917..50ab6dcd4 100644 --- a/genericthings/integrationplugingenericthings.json +++ b/genericthings/integrationplugingenericthings.json @@ -1682,6 +1682,64 @@ } ] }, + { + "id": "f1576df0-fb45-4bf0-89fa-a83c4118c326", + "name": "waterLevelSensor", + "displayName": "Generic water level sensor", + "createMethods": ["user"], + "interfaces": ["waterlevelsensor"], + "settingsTypes": [ + { + "id": "5e98e8d2-d849-46c5-b25a-d54f184ea4c7", + "name": "capacity", + "displayName": "Tank capactity", + "type": "double", + "unit": "Liter", + "minValue": 0, + "defaultValue": 100 + }, + { + "id": "16ea3cf2-46fd-40a3-88bf-21a2bb7cbabe", + "name": "minInputValue", + "displayName": "Minimum input value", + "type": "double", + "defaultValue": 0 + }, + { + "id": "4e228f9b-8631-4643-8375-3d8d76d12e9c", + "name": "maxInputValue", + "displayName": "Maximum input value", + "type": "double", + "defaultValue": 1 + } + ], + "stateTypes": [ + { + "id": "07563165-e42d-4d0f-ac60-31cdd19170f2", + "name": "waterLevel", + "displayName": "Water level", + "displayNameEvent": "Water level changed", + "type": "double", + "unit": "Liter", + "defaultValue": 0, + "minValue": 0, + "maxValue": 100 + }, + { + "id": "d344887d-da5d-4742-83bd-608754b2d0aa", + "name": "input", + "displayName": "Input value", + "displayNameEvent": "Input value changed", + "displayNameAction": "Set input value", + "type": "double", + "defaultValue": 0, + "minValue": 0, + "maxValue": 1, + "writable": true, + "ioType": "analogOutput" + } + ] + }, { "id": "339a0c54-4086-404f-8d36-bcf20621b785", "name": "presenceSensor",