Energy: Add generic smart meters (producer and consumer)

pull/2/head
Michael Zanetti 2022-03-02 12:19:06 +01:00
parent 1a3d25cefe
commit 0988ceeba5
2 changed files with 98 additions and 15 deletions

View File

@ -44,24 +44,24 @@ void IntegrationPluginGenericEnergy::setupThing(ThingSetupInfo *info)
{ {
Thing *thing = info->thing(); Thing *thing = info->thing();
if (thing->thingClassId() == smartMeterThingClassId) { if (thing->thingClassId() == impulseSmartMeterThingClassId) {
QTimer* smartMeterTimer = new QTimer(this); QTimer* smartMeterTimer = new QTimer(this);
int timeframe = thing->setting(smartMeterSettingsImpulseTimeframeParamTypeId).toInt(); int timeframe = thing->setting(impulseSmartMeterSettingsImpulseTimeframeParamTypeId).toInt();
smartMeterTimer->setInterval(timeframe * 1000); smartMeterTimer->setInterval(timeframe * 1000);
m_smartMeterTimer.insert(thing, smartMeterTimer); m_smartMeterTimer.insert(thing, smartMeterTimer);
smartMeterTimer->start(); smartMeterTimer->start();
connect(thing, &Thing::settingChanged, smartMeterTimer, [smartMeterTimer] (const ParamTypeId &paramTypeId, const QVariant &value) { connect(thing, &Thing::settingChanged, smartMeterTimer, [smartMeterTimer] (const ParamTypeId &paramTypeId, const QVariant &value) {
if (paramTypeId == smartMeterSettingsImpulseTimeframeParamTypeId) { if (paramTypeId == impulseSmartMeterSettingsImpulseTimeframeParamTypeId) {
smartMeterTimer->setInterval(value.toInt() * 1000); smartMeterTimer->setInterval(value.toInt() * 1000);
} }
}); });
connect(smartMeterTimer, &QTimer::timeout, thing, [this, smartMeterTimer, thing] { 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; int interval = smartMeterTimer->interval()/1000;
double power = (m_pulsesPerTimeframe.value(thing)/impulsePerKwh)/(interval/3600.00); // Power = Energy/Time; Energy = Impulses/ImpPerkWh 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); m_pulsesPerTimeframe.insert(thing, 0);
}); });
} else if (thing->thingClassId() == batteryThingClassId) { } else if (thing->thingClassId() == batteryThingClassId) {
@ -96,14 +96,14 @@ void IntegrationPluginGenericEnergy::executeAction(ThingActionInfo *info)
} else { } else {
Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8()); Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8());
} }
} else if (thing->thingClassId() == smartMeterThingClassId) { } else if (thing->thingClassId() == impulseSmartMeterThingClassId) {
if (action.actionTypeId() == smartMeterImpulseInputActionTypeId) { if (action.actionTypeId() == impulseSmartMeterImpulseInputActionTypeId) {
bool value = info->action().param(smartMeterImpulseInputActionImpulseInputParamTypeId).value().toBool(); bool value = info->action().param(impulseSmartMeterImpulseInputActionImpulseInputParamTypeId).value().toBool();
thing->setStateValue(smartMeterImpulseInputStateTypeId, value); thing->setStateValue(impulseSmartMeterImpulseInputStateTypeId, value);
int impulsePerKwh = info->thing()->setting(smartMeterSettingsImpulsePerKwhParamTypeId).toInt(); int impulsePerKwh = info->thing()->setting(impulseSmartMeterSettingsImpulsePerKwhParamTypeId).toInt();
if (value) { if (value) {
double currentEnergy = thing->stateValue(smartMeterTotalEnergyConsumedStateTypeId).toDouble(); double currentEnergy = thing->stateValue(impulseSmartMeterTotalEnergyConsumedStateTypeId).toDouble();
thing->setStateValue(smartMeterTotalEnergyConsumedStateTypeId ,currentEnergy + (1.00/impulsePerKwh)); thing->setStateValue(impulseSmartMeterTotalEnergyConsumedStateTypeId ,currentEnergy + (1.00/impulsePerKwh));
m_pulsesPerTimeframe[thing]++; m_pulsesPerTimeframe[thing]++;
} }
info->finish(Thing::ThingErrorNoError); 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()); 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) { } else if (thing->thingClassId() == batteryThingClassId) {
if (action.actionTypeId() == batteryBatteryLevelControlActionTypeId) { if (action.actionTypeId() == batteryBatteryLevelControlActionTypeId) {
int value = action.paramValue(batteryBatteryLevelControlActionBatteryLevelControlParamTypeId).toInt(); int value = action.paramValue(batteryBatteryLevelControlActionBatteryLevelControlParamTypeId).toInt();
@ -133,9 +154,9 @@ void IntegrationPluginGenericEnergy::executeAction(ThingActionInfo *info)
void IntegrationPluginGenericEnergy::thingRemoved(Thing *thing) void IntegrationPluginGenericEnergy::thingRemoved(Thing *thing)
{ {
if (thing->thingClassId() == smartMeterThingClassId) { if (thing->thingClassId() == impulseSmartMeterThingClassId) {
m_pulsesPerTimeframe.remove(thing); m_pulsesPerTimeframe.remove(thing);
} else if (thing->thingClassId() == smartMeterThingClassId) { } else if (thing->thingClassId() == impulseSmartMeterThingClassId) {
m_smartMeterTimer.take(thing)->deleteLater(); m_smartMeterTimer.take(thing)->deleteLater();
m_pulsesPerTimeframe.remove(thing); m_pulsesPerTimeframe.remove(thing);
} }

View File

@ -50,7 +50,7 @@
}, },
{ {
"id": "c3123967-f741-4fe1-a0d4-9a3e405d7e52", "id": "c3123967-f741-4fe1-a0d4-9a3e405d7e52",
"name": "smartMeter", "name": "impulseSmartMeter",
"displayName": "Impulse based energy meter", "displayName": "Impulse based energy meter",
"createMethods": ["user"], "createMethods": ["user"],
"interfaces": ["smartmeterconsumer"], "interfaces": ["smartmeterconsumer"],
@ -170,6 +170,68 @@
"defaultValue": false "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
}
]
} }
] ]
} }