From 0988ceeba5e4e55b7798c73eae29ed3cf235cee7 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Wed, 2 Mar 2022 12:19:06 +0100 Subject: [PATCH] Energy: Add generic smart meters (producer and consumer) --- .../integrationplugingenericenergy.cpp | 49 ++++++++++---- .../integrationplugingenericenergy.json | 64 ++++++++++++++++++- 2 files changed, 98 insertions(+), 15 deletions(-) diff --git a/genericenergy/integrationplugingenericenergy.cpp b/genericenergy/integrationplugingenericenergy.cpp index 5bec43c..5e5cf3e 100644 --- a/genericenergy/integrationplugingenericenergy.cpp +++ b/genericenergy/integrationplugingenericenergy.cpp @@ -44,24 +44,24 @@ void IntegrationPluginGenericEnergy::setupThing(ThingSetupInfo *info) { Thing *thing = info->thing(); - if (thing->thingClassId() == smartMeterThingClassId) { + if (thing->thingClassId() == impulseSmartMeterThingClassId) { QTimer* smartMeterTimer = new QTimer(this); - int timeframe = thing->setting(smartMeterSettingsImpulseTimeframeParamTypeId).toInt(); + int timeframe = thing->setting(impulseSmartMeterSettingsImpulseTimeframeParamTypeId).toInt(); smartMeterTimer->setInterval(timeframe * 1000); m_smartMeterTimer.insert(thing, smartMeterTimer); smartMeterTimer->start(); connect(thing, &Thing::settingChanged, smartMeterTimer, [smartMeterTimer] (const ParamTypeId ¶mTypeId, const QVariant &value) { - if (paramTypeId == smartMeterSettingsImpulseTimeframeParamTypeId) { + if (paramTypeId == impulseSmartMeterSettingsImpulseTimeframeParamTypeId) { smartMeterTimer->setInterval(value.toInt() * 1000); } }); connect(smartMeterTimer, &QTimer::timeout, thing, [this, smartMeterTimer, thing] { - double impulsePerKwh = thing->setting(smartMeterSettingsImpulsePerKwhParamTypeId).toDouble(); + double impulsePerKwh = thing->setting(impulseSmartMeterSettingsImpulsePerKwhParamTypeId).toDouble(); int interval = smartMeterTimer->interval()/1000; double power = (m_pulsesPerTimeframe.value(thing)/impulsePerKwh)/(interval/3600.00); // Power = Energy/Time; Energy = Impulses/ImpPerkWh - thing->setStateValue(smartMeterCurrentPowerStateTypeId, power*1000); + thing->setStateValue(impulseSmartMeterCurrentPowerStateTypeId, power*1000); m_pulsesPerTimeframe.insert(thing, 0); }); } else if (thing->thingClassId() == batteryThingClassId) { @@ -96,14 +96,14 @@ void IntegrationPluginGenericEnergy::executeAction(ThingActionInfo *info) } else { Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8()); } - } else if (thing->thingClassId() == smartMeterThingClassId) { - if (action.actionTypeId() == smartMeterImpulseInputActionTypeId) { - bool value = info->action().param(smartMeterImpulseInputActionImpulseInputParamTypeId).value().toBool(); - thing->setStateValue(smartMeterImpulseInputStateTypeId, value); - int impulsePerKwh = info->thing()->setting(smartMeterSettingsImpulsePerKwhParamTypeId).toInt(); + } else if (thing->thingClassId() == impulseSmartMeterThingClassId) { + if (action.actionTypeId() == impulseSmartMeterImpulseInputActionTypeId) { + bool value = info->action().param(impulseSmartMeterImpulseInputActionImpulseInputParamTypeId).value().toBool(); + thing->setStateValue(impulseSmartMeterImpulseInputStateTypeId, value); + int impulsePerKwh = info->thing()->setting(impulseSmartMeterSettingsImpulsePerKwhParamTypeId).toInt(); if (value) { - double currentEnergy = thing->stateValue(smartMeterTotalEnergyConsumedStateTypeId).toDouble(); - thing->setStateValue(smartMeterTotalEnergyConsumedStateTypeId ,currentEnergy + (1.00/impulsePerKwh)); + double currentEnergy = thing->stateValue(impulseSmartMeterTotalEnergyConsumedStateTypeId).toDouble(); + thing->setStateValue(impulseSmartMeterTotalEnergyConsumedStateTypeId ,currentEnergy + (1.00/impulsePerKwh)); m_pulsesPerTimeframe[thing]++; } info->finish(Thing::ThingErrorNoError); @@ -112,6 +112,27 @@ void IntegrationPluginGenericEnergy::executeAction(ThingActionInfo *info) Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8()); } + } else if (thing->thingClassId() == smartMeterConsumerThingClassId) { + if (action.actionTypeId() == smartMeterConsumerCurrentPowerActionTypeId) { + thing->setStateValue(smartMeterConsumerCurrentPowerStateTypeId, action.paramValue(smartMeterConsumerCurrentPowerActionCurrentPowerParamTypeId)); + info->finish(Thing::ThingErrorNoError); + } else if (action.actionTypeId() == smartMeterConsumerTotalEnergyConsumedActionTypeId) { + thing->setStateValue(smartMeterConsumerTotalEnergyConsumedStateTypeId, action.paramValue(smartMeterConsumerTotalEnergyConsumedActionTotalEnergyConsumedParamTypeId)); + info->finish(Thing::ThingErrorNoError); + } else { + Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8()); + } + + } else if (thing->thingClassId() == smartMeterProducerThingClassId) { + if (action.actionTypeId() == smartMeterProducerCurrentPowerActionTypeId) { + thing->setStateValue(smartMeterProducerCurrentPowerStateTypeId, action.paramValue(smartMeterProducerCurrentPowerActionCurrentPowerParamTypeId)); + info->finish(Thing::ThingErrorNoError); + } else if (action.actionTypeId() == smartMeterProducerTotalEnergyProducedActionTypeId) { + thing->setStateValue(smartMeterProducerTotalEnergyProducedStateTypeId, action.paramValue(smartMeterProducerTotalEnergyProducedActionTotalEnergyProducedParamTypeId)); + info->finish(Thing::ThingErrorNoError); + } else { + Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8()); + } } else if (thing->thingClassId() == batteryThingClassId) { if (action.actionTypeId() == batteryBatteryLevelControlActionTypeId) { int value = action.paramValue(batteryBatteryLevelControlActionBatteryLevelControlParamTypeId).toInt(); @@ -133,9 +154,9 @@ void IntegrationPluginGenericEnergy::executeAction(ThingActionInfo *info) void IntegrationPluginGenericEnergy::thingRemoved(Thing *thing) { - if (thing->thingClassId() == smartMeterThingClassId) { + if (thing->thingClassId() == impulseSmartMeterThingClassId) { m_pulsesPerTimeframe.remove(thing); - } else if (thing->thingClassId() == smartMeterThingClassId) { + } else if (thing->thingClassId() == impulseSmartMeterThingClassId) { m_smartMeterTimer.take(thing)->deleteLater(); m_pulsesPerTimeframe.remove(thing); } diff --git a/genericenergy/integrationplugingenericenergy.json b/genericenergy/integrationplugingenericenergy.json index 5201f90..946ad98 100644 --- a/genericenergy/integrationplugingenericenergy.json +++ b/genericenergy/integrationplugingenericenergy.json @@ -50,7 +50,7 @@ }, { "id": "c3123967-f741-4fe1-a0d4-9a3e405d7e52", - "name": "smartMeter", + "name": "impulseSmartMeter", "displayName": "Impulse based energy meter", "createMethods": ["user"], "interfaces": ["smartmeterconsumer"], @@ -170,6 +170,68 @@ "defaultValue": false } ] + }, + { + "id": "c26478b3-dd64-445f-9859-8ef816025cb9", + "name": "smartMeterConsumer", + "displayName": "Generic smart meter consumer", + "createMethods": ["user"], + "interfaces": ["smartmeterconsumer"], + "stateTypes": [ + { + "id": "5a083458-e476-487d-ab8f-75e92cf16489", + "name": "currentPower", + "displayName": "Current Power usage", + "displayNameEvent": "Current Power usage changed", + "displayNameAction": "Set current power usage", + "type": "double", + "defaultValue": 0.00, + "unit": "Watt", + "writable": true + }, + { + "id": "ba10ef2c-e41f-4de2-a620-dff2ec5f025a", + "name": "totalEnergyConsumed", + "displayName": "Consumed energy", + "displayNameEvent": "Consumed energy changed", + "displayNameAction": "Set total consumed energy", + "type": "double", + "defaultValue": 0.00, + "unit": "KiloWattHour", + "writable": true + } + ] + }, + { + "id": "986acf96-3cdd-4a8d-8f5c-2ffe12fef1c9", + "name": "smartMeterProducer", + "displayName": "Generic smart meter producer", + "createMethods": ["user"], + "interfaces": ["smartmeterproducer"], + "stateTypes": [ + { + "id": "8fd67ad4-322d-46d7-a0fd-20fdbfc12bc5", + "name": "currentPower", + "displayName": "Current Power production", + "displayNameEvent": "Current Power production changed", + "displayNameAction": "Set current power production", + "type": "double", + "defaultValue": 0.00, + "unit": "Watt", + "writable": true + }, + { + "id": "58a1212c-ab6e-4631-b08d-712cdde295c7", + "name": "totalEnergyProduced", + "displayName": "Produced energy", + "displayNameEvent": "Produced energy changed", + "displayNameAction": "Set total produced energy", + "type": "double", + "defaultValue": 0.00, + "unit": "KiloWattHour", + "writable": true + } + ] } ] }