diff --git a/genericthings/integrationplugingenericthings.cpp b/genericthings/integrationplugingenericthings.cpp index f224ee1..1c63477 100644 --- a/genericthings/integrationplugingenericthings.cpp +++ b/genericthings/integrationplugingenericthings.cpp @@ -47,6 +47,19 @@ void IntegrationPluginGenericThings::init() void IntegrationPluginGenericThings::setupThing(ThingSetupInfo *info) { + Thing *thing = info->thing(); + if (thing->thingClassId() == extendedSmartMeterConsumerThingClassId) { + if (!m_smartMeterTimer) { + m_smartMeterTimer = hardwareManager()->pluginTimerManager()->registerTimer(10); + connect(m_smartMeterTimer, &PluginTimer::timeout, thing, [this, thing] { + + int impulsePerKwh = thing->setting(extendedSmartMeterConsumerSettingsImpulsePerKwhParamTypeId).toInt(); + double power = (m_pulsesPerTimeframe.value(thing)/impulsePerKwh)/(m_smartMeterTimer->interval()/3600.00); // Power = Energy/Time; Energy = Impulses/ImpPerkWh + thing->setStateValue(extendedSmartMeterConsumerCurrentPowerStateTypeId, power); + m_pulsesPerTimeframe.insert(thing, 0); + }); + } + } info->finish(Thing::ThingErrorNoError); Thing *thing = info->thing(); if (thing->thingClassId() == extendedBlindThingClassId) { @@ -475,6 +488,20 @@ void IntegrationPluginGenericThings::executeAction(ThingActionInfo *info) } else { Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8()); } + } else if (thing->thingClassId() == extendedSmartMeterConsumerThingClassId) { + if (action.actionTypeId() == extendedSmartMeterConsumerS0InputActionTypeId) { + bool value = info->action().param(extendedSmartMeterConsumerS0InputActionS0InputParamTypeId).value().toBool(); + int impulsePerKwh = info->thing()->setting(extendedSmartMeterConsumerSettingsImpulsePerKwhParamTypeId).toInt(); + if (value) { + double currentEnergy = thing->stateValue(extendedSmartMeterConsumerTotalEnergyConsumedStateTypeId).toDouble(); + thing->setStateValue(extendedSmartMeterConsumerTotalEnergyConsumedStateTypeId ,currentEnergy + (1.00/impulsePerKwh)); + m_pulsesPerTimeframe.insert(thing, m_pulsesPerTimeframe.value(thing)+1); + } + info->finish(Thing::ThingErrorNoError); + return; + } else { + Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8()); + } } else { Q_ASSERT_X(false, "executeAction", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8()); } @@ -493,6 +520,18 @@ void IntegrationPluginGenericThings::thingRemoved(Thing *thing) } } +void IntegrationPluginGenericThings::thingRemoved(Thing *thing) +{ + if (thing->thingClassId() == extendedSmartMeterConsumerThingClassId) { + m_pulsesPerTimeframe.remove(thing); + } + + if (myThings().isEmpty()) { + hardwareManager()->pluginTimerManager()->unregisterTimer(m_smartMeterTimer); + m_smartMeterTimer = nullptr; + } +} + double IntegrationPluginGenericThings::mapDoubleValue(double value, double fromMin, double fromMax, double toMin, double toMax) { double percent = (value - fromMin) / (fromMax - fromMin); diff --git a/genericthings/integrationplugingenericthings.h b/genericthings/integrationplugingenericthings.h index a98c466..e8b5385 100644 --- a/genericthings/integrationplugingenericthings.h +++ b/genericthings/integrationplugingenericthings.h @@ -33,6 +33,7 @@ #include "integrations/integrationplugin.h" #include +#include "plugintimer.h" class IntegrationPluginGenericThings: public IntegrationPlugin { @@ -49,6 +50,9 @@ public: void thingRemoved(Thing *thing) override; private: + PluginTimer *m_smartMeterTimer = nullptr; + QHash m_pulsesPerTimeframe; + double mapDoubleValue(double value, double fromMin, double fromMax, double toMin, double toMax); QHash m_extendedBlindPercentageTimer; diff --git a/genericthings/integrationplugingenericthings.json b/genericthings/integrationplugingenericthings.json index fc4cfec..6cffcfc 100644 --- a/genericthings/integrationplugingenericthings.json +++ b/genericthings/integrationplugingenericthings.json @@ -693,6 +693,55 @@ "ioType": "analogOutput" } ] + }, + { + "id": "c3123967-f741-4fe1-a0d4-9a3e405d7e52", + "name": "extendedSmartMeterConsumer", + "displayName": "Extended smart meter consumer", + "createMethods": ["user"], + "interfaces": ["extendedsmartmeterconsumer"], + "settingsTypes": [ + { + "id": "c361732b-68eb-447e-a434-e84031231871", + "name": "impulsePerKwh", + "displayName": "Impulse/kWh", + "type": "int", + "minValue": 1, + "maxValue": 1000000, + "defaultValue": 1000 + } + ], + "stateTypes": [ + { + "id": "5983d714-5f80-42d8-bee2-9228b6382b3b", + "name": "currentPower", + "displayName": "Power", + "displayNameEvent": "Power changed", + "type": "double", + "defaultValue": 0.00, + "unit": "Watt" + }, + { + "id": "5821edb7-e6cb-4e5a-9d0b-3375126d3367", + "name": "totalEnergyConsumed", + "displayName": "Energy", + "displayNameEvent": "Energy changed", + "type": "double", + "defaultValue": 0.00, + "unit": "KiloWattHour" + }, + { + "id": "9cd7e5ca-f8f8-48d5-9785-911ae75158c3", + "name": "s0Input", + "displayName": "S0 input", + "displayNameEvent": "S0 impulse", + "displayNameAction": "S0 impulse", + "type": "bool", + "defaultValue": false, + "writable": true, + "ioType": "digitalInput" + } + ] } ] }