Improve simulation plugin and add netatmo simulation
This commit is contained in:
parent
2072d4cccd
commit
d1e30bbb91
@ -22,38 +22,50 @@
|
||||
#include "devicepluginsimulation.h"
|
||||
#include "plugininfo.h"
|
||||
|
||||
#include <QColor>
|
||||
#include <QDateTime>
|
||||
|
||||
DevicePluginSimulation::DevicePluginSimulation()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
DevicePluginSimulation::~DevicePluginSimulation()
|
||||
{
|
||||
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer20Seconds);
|
||||
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer5Min);
|
||||
}
|
||||
|
||||
void DevicePluginSimulation::init()
|
||||
{
|
||||
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
|
||||
connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginSimulation::onPluginTimer);
|
||||
// Seed the random generator with current time
|
||||
qsrand(QDateTime::currentMSecsSinceEpoch() / 1000);
|
||||
|
||||
// Change some values every 20 seconds
|
||||
m_pluginTimer20Seconds = hardwareManager()->pluginTimerManager()->registerTimer(20);
|
||||
connect(m_pluginTimer20Seconds, &PluginTimer::timeout, this, &DevicePluginSimulation::onPluginTimer20Seconds);
|
||||
|
||||
// Change some values every 5 min
|
||||
m_pluginTimer5Min = hardwareManager()->pluginTimerManager()->registerTimer(300);
|
||||
connect(m_pluginTimer5Min, &PluginTimer::timeout, this, &DevicePluginSimulation::onPluginTimer5Minutes);
|
||||
}
|
||||
|
||||
DeviceManager::DeviceSetupStatus DevicePluginSimulation::setupDevice(Device *device)
|
||||
{
|
||||
Q_UNUSED(device)
|
||||
qCDebug(dcSimulation) << device->params();
|
||||
|
||||
qCDebug(dcSimulation()) << "Set up device" << device->name();
|
||||
return DeviceManager::DeviceSetupStatusSuccess;
|
||||
}
|
||||
|
||||
|
||||
DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device, const Action &action)
|
||||
{
|
||||
|
||||
// Check the DeviceClassId for "Simple Button"
|
||||
if (device->deviceClassId() == simpleButtonDeviceClassId ) {
|
||||
|
||||
// check if this is the "press" action
|
||||
if (action.actionTypeId() == simpleButtonTriggerActionTypeId) {
|
||||
|
||||
|
||||
// Emit the "button pressed" event
|
||||
qCDebug(dcSimulation()) << "Emit button pressed event for" << device->name();
|
||||
Event event(simpleButtonPressedEventTypeId, device->id());
|
||||
emit emitEvent(event);
|
||||
|
||||
@ -72,8 +84,7 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device,
|
||||
Param powerParam = action.param(alternativeButtonPowerStateParamTypeId);
|
||||
bool power = powerParam.value().toBool();
|
||||
|
||||
qCDebug(dcSimulation) << "ActionTypeId :" << action.actionTypeId().toString();
|
||||
qCDebug(dcSimulation) << "StateTypeId :" << alternativeButtonPowerStateTypeId.toString();
|
||||
qCDebug(dcSimulation()) << "Set power" << power << "for button" << device->name();
|
||||
|
||||
// Set the "power" state
|
||||
device->setStateValue(alternativeButtonPowerStateTypeId, power);
|
||||
@ -91,7 +102,7 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device,
|
||||
// get the param value
|
||||
Param powerParam = action.param(heatingPowerStateParamTypeId);
|
||||
bool power = powerParam.value().toBool();
|
||||
// Set the "power" state
|
||||
qCDebug(dcSimulation()) << "Set power" << power << "for heating device" << device->name();
|
||||
device->setStateValue(heatingPowerStateTypeId, power);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
|
||||
@ -101,7 +112,8 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device,
|
||||
Param temperatureParam = action.param(heatingTargetTemperatureStateParamTypeId);
|
||||
int temperature = temperatureParam.value().toInt();
|
||||
|
||||
// Set the "temperature" state
|
||||
qCDebug(dcSimulation()) << "Set target temperature" << temperature << "for heating device" << device->name();
|
||||
|
||||
device->setStateValue(heatingTargetTemperatureStateTypeId, temperature);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
}
|
||||
@ -114,7 +126,9 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device,
|
||||
// get the param value
|
||||
Param powerParam = action.param(evChargerPowerStateParamTypeId);
|
||||
bool power = powerParam.value().toBool();
|
||||
// Set the "power" state
|
||||
|
||||
qCDebug(dcSimulation()) << "Set power" << power << "for heating device" << device->name();
|
||||
|
||||
device->setStateValue(evChargerPowerStateTypeId, power);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
|
||||
@ -122,7 +136,7 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device,
|
||||
// get the param value
|
||||
Param currentParam = action.param(evChargerCurrentStateParamTypeId);
|
||||
int current = currentParam.value().toInt();
|
||||
// Set the "current" state
|
||||
qCDebug(dcSimulation()) << "Set current" << current << "for EV Charger device" << device->name();
|
||||
device->setStateValue(evChargerCurrentStateTypeId, current);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
}
|
||||
@ -136,6 +150,7 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device,
|
||||
Param powerParam = action.param(socketPowerStateParamTypeId);
|
||||
bool power = powerParam.value().toBool();
|
||||
// Set the "power" state
|
||||
qCDebug(dcSimulation()) << "Set power" << power << "for socket device" << device->name();
|
||||
device->setStateValue(socketPowerStateTypeId, power);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
}
|
||||
@ -146,21 +161,25 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device,
|
||||
|
||||
if(action.actionTypeId() == colorBulbBrightnessActionTypeId){
|
||||
int brightness = action.param(colorBulbBrightnessStateParamTypeId).value().toInt();
|
||||
qCDebug(dcSimulation()) << "Set brightness" << brightness << "for color bulb device" << device->name();
|
||||
device->setStateValue(colorBulbBrightnessStateTypeId, brightness);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
|
||||
} else if (action.actionTypeId() == colorBulbColorTemperatureActionTypeId){
|
||||
int temperature = action.param(colorBulbColorTemperatureStateParamTypeId).value().toInt();
|
||||
qCDebug(dcSimulation()) << "Set color temperature" << temperature << "for color bulb device" << device->name();
|
||||
device->setStateValue(colorBulbColorTemperatureStateTypeId, temperature);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
|
||||
} else if (action.actionTypeId() == colorBulbColorActionTypeId) {
|
||||
QVariant color = action.param(colorBulbColorStateParamTypeId).value();
|
||||
QColor color = action.param(colorBulbColorStateParamTypeId).value().value<QColor>();
|
||||
qCDebug(dcSimulation()) << "Set color" << color << "for color bulb device" << device->name();
|
||||
device->setStateValue(colorBulbColorStateTypeId, color);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
|
||||
} else if (action.actionTypeId() == colorBulbPowerActionTypeId) {
|
||||
bool power = action.param(colorBulbPowerStateParamTypeId).value().toBool();
|
||||
qCDebug(dcSimulation()) << "Set power" << power << "for color bulb device" << device->name();
|
||||
device->setStateValue(colorBulbPowerStateTypeId, power);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
}
|
||||
@ -172,14 +191,17 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device,
|
||||
|
||||
if (action.actionTypeId() == heatingRodPowerActionTypeId) {
|
||||
bool power = action.param(heatingRodPowerStateParamTypeId).value().toBool();
|
||||
qCDebug(dcSimulation()) << "Set power" << power << "for heating rod device" << device->name();
|
||||
device->setStateValue(heatingRodPowerStateTypeId, power);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
} else if (action.actionTypeId() == heatingRodWaterTemperatureActionTypeId) {
|
||||
int temperature = action.param(heatingRodWaterTemperatureStateParamTypeId).value().toInt();
|
||||
qCDebug(dcSimulation()) << "Set water temperature" << temperature << "for heating rod device" << device->name();
|
||||
device->setStateValue(heatingRodWaterTemperatureStateTypeId, temperature);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
} else if (action.actionTypeId() == heatingRodMaxPowerActionTypeId) {
|
||||
double maxPower = action.param(heatingRodMaxPowerStateParamTypeId).value().toDouble();
|
||||
qCDebug(dcSimulation()) << "Set max power" << maxPower << "for heating rod device" << device->name();
|
||||
device->setStateValue(heatingRodMaxPowerStateTypeId, maxPower);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
}
|
||||
@ -191,7 +213,8 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device,
|
||||
if (action.actionTypeId() == batteryMaxChargingActionTypeId) {
|
||||
int maxCharging = action.param(batteryMaxChargingStateParamTypeId).value().toInt();
|
||||
device->setStateValue(batteryMaxChargingStateTypeId, maxCharging);
|
||||
device->setStateValue(batteryChargingStateTypeId, ((double)maxCharging-10)/1000);
|
||||
qCDebug(dcSimulation()) << "Set max charging power" << maxCharging << "for battery device" << device->name();
|
||||
device->setStateValue(batteryChargingStateTypeId, maxCharging);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
}
|
||||
return DeviceManager::DeviceErrorActionTypeNotFound;
|
||||
@ -200,34 +223,70 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device,
|
||||
return DeviceManager::DeviceErrorDeviceClassNotFound;
|
||||
}
|
||||
|
||||
int DevicePluginSimulation::getRandomNumber(const int Min, const int Max)
|
||||
int DevicePluginSimulation::generateRandomIntValue(int min, int max)
|
||||
{
|
||||
return ((qrand() % ((Max + 1) - Min)) + Min);
|
||||
// Seed the random generator with current time
|
||||
qsrand(QDateTime::currentMSecsSinceEpoch() / 1000);
|
||||
int value = ((qrand() % ((max + 1) - min)) + min);
|
||||
// qCDebug(dcSimulation()) << "Generateed random int value: [" << min << ", " << max << "] -->" << value;
|
||||
return value;
|
||||
}
|
||||
|
||||
void DevicePluginSimulation::onPluginTimer()
|
||||
double DevicePluginSimulation::generateRandomDoubleValue(double min, double max)
|
||||
{
|
||||
double value = generateRandomIntValue(static_cast<int>(min * 10), static_cast<int>(max * 10)) / 10.0;
|
||||
// qCDebug(dcSimulation()) << "Generated random double value: [" << min << ", " << max << "] -->" << value;
|
||||
return value;
|
||||
}
|
||||
|
||||
bool DevicePluginSimulation::generateRandomBoolValue()
|
||||
{
|
||||
bool value = static_cast<bool>(generateRandomIntValue(0, 1));
|
||||
// qCDebug(dcSimulation()) << "Generated random bool value:" << value;
|
||||
return value;
|
||||
}
|
||||
|
||||
void DevicePluginSimulation::onPluginTimer20Seconds()
|
||||
{
|
||||
qCDebug(dcSimulation()) << "Update values for 20s timer";
|
||||
foreach (Device *device, myDevices()) {
|
||||
if (device->deviceClassId() == temperatureSensorDeviceClassId) {
|
||||
//generate Random Number
|
||||
double temperature = ((double)getRandomNumber(200, 230)/10.0);
|
||||
device->setStateValue(temperatureSensorTemperatureStateTypeId, temperature);
|
||||
device->setStateValue(temperatureSensorHumidityStateTypeId, getRandomNumber(40, 60));
|
||||
device->setStateValue(temperatureSensorBatteryLevelStateTypeId, getRandomNumber(10, 100));
|
||||
device->setStateValue(temperatureSensorBatteryCriticalStateTypeId, device->stateValue(temperatureSensorBatteryLevelStateTypeId).toDouble() <= 30);
|
||||
// Temperature sensor
|
||||
device->setStateValue(temperatureSensorTemperatureStateTypeId, generateRandomDoubleValue(18, 23));
|
||||
device->setStateValue(temperatureSensorHumidityStateTypeId, generateRandomIntValue(40, 55));
|
||||
device->setStateValue(temperatureSensorBatteryLevelStateTypeId, generateRandomIntValue(25, 40));
|
||||
device->setStateValue(temperatureSensorBatteryCriticalStateTypeId, device->stateValue(temperatureSensorBatteryLevelStateTypeId).toInt() <= 30);
|
||||
device->setStateValue(temperatureSensorConnectedStateTypeId, true);
|
||||
|
||||
} else if(device->deviceClassId() == motionDetectorDeviceClassId){
|
||||
bool active = true;
|
||||
if(getRandomNumber(0, 60)){
|
||||
active = false;
|
||||
}
|
||||
device->setStateValue(motionDetectorActiveStateTypeId, active);
|
||||
device->setStateValue(motionDetectorBatteryLevelStateTypeId, getRandomNumber(10, 100));
|
||||
device->setStateValue(motionDetectorBatteryCriticalStateTypeId, device->stateValue(motionDetectorBatteryLevelStateTypeId).toDouble() <= 30);
|
||||
} else if (device->deviceClassId() == motionDetectorDeviceClassId) {
|
||||
// Motion detector
|
||||
device->setStateValue(motionDetectorActiveStateTypeId, generateRandomBoolValue());
|
||||
device->setStateValue(motionDetectorBatteryLevelStateTypeId, generateRandomIntValue(25, 40));
|
||||
device->setStateValue(motionDetectorBatteryCriticalStateTypeId, device->stateValue(motionDetectorBatteryLevelStateTypeId).toInt() <= 30);
|
||||
device->setStateValue(motionDetectorConnectedStateTypeId, true);
|
||||
} else if(device->deviceClassId() == evChargerDeviceClassId){
|
||||
|
||||
} else if(device->deviceClassId() == netatmoIndoorDeviceClassId) {
|
||||
// Netatmo
|
||||
device->setStateValue(netatmoIndoorUpdateTimeStateTypeId, QDateTime::currentDateTime().toTime_t());
|
||||
device->setStateValue(netatmoIndoorHumidityStateTypeId, generateRandomIntValue(35, 45));
|
||||
device->setStateValue(netatmoIndoorTemperatureStateTypeId, generateRandomIntValue(20, 25));
|
||||
device->setStateValue(netatmoIndoorPressureStateTypeId, generateRandomIntValue(1003, 1008));
|
||||
device->setStateValue(netatmoIndoorNoiseStateTypeId, generateRandomIntValue(40, 80));
|
||||
device->setStateValue(netatmoIndoorWifiStrengthStateTypeId, generateRandomIntValue(85, 95));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DevicePluginSimulation::onPluginTimer5Minutes()
|
||||
{
|
||||
qCDebug(dcSimulation()) << "Update values for 5 minutes timer";
|
||||
foreach (Device *device, myDevices()) {
|
||||
if(device->deviceClassId() == netatmoIndoorDeviceClassId) {
|
||||
// Note: should change between > 1000 co2 < 1000 for showcase, please do not change this behaviour
|
||||
int currentValue = device->stateValue(netatmoIndoorCo2StateTypeId).toInt();
|
||||
if (currentValue < 1000) {
|
||||
device->setStateValue(netatmoIndoorCo2StateTypeId, generateRandomIntValue(1001, 1010));
|
||||
} else {
|
||||
device->setStateValue(netatmoIndoorCo2StateTypeId, generateRandomIntValue(950, 999));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,18 +36,23 @@ class DevicePluginSimulation : public DevicePlugin
|
||||
|
||||
public:
|
||||
explicit DevicePluginSimulation();
|
||||
~DevicePluginSimulation();
|
||||
|
||||
void init() override;
|
||||
DeviceManager::DeviceSetupStatus setupDevice(Device *device) override;
|
||||
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
|
||||
|
||||
private:
|
||||
PluginTimer *m_pluginTimer = nullptr;
|
||||
PluginTimer *m_pluginTimer20Seconds = nullptr;
|
||||
PluginTimer *m_pluginTimer5Min = nullptr;
|
||||
|
||||
int getRandomNumber(const int min, const int max);
|
||||
int generateRandomIntValue(int min, int max);
|
||||
double generateRandomDoubleValue(double min, double max);
|
||||
bool generateRandomBoolValue();
|
||||
|
||||
private slots:
|
||||
void onPluginTimer();
|
||||
void onPluginTimer20Seconds();
|
||||
void onPluginTimer5Minutes();
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -564,7 +564,7 @@
|
||||
"type": "int",
|
||||
"unit": "Watt",
|
||||
"defaultValue": 700,
|
||||
"minValue": 0,
|
||||
"minValue": 200,
|
||||
"maxValue": 2000,
|
||||
"writable": true
|
||||
},
|
||||
@ -643,6 +643,93 @@
|
||||
"defaultValue": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "7f1696e3-3145-421e-9a42-5bd3b2fd0e2c",
|
||||
"name": "netatmoIndoor",
|
||||
"displayName": "Netatmo Indoor Station",
|
||||
"deviceIcon": "Thermometer",
|
||||
"basicTags": [
|
||||
"Device",
|
||||
"Weather",
|
||||
"Sensor"
|
||||
],
|
||||
"createMethods": ["user"],
|
||||
"paramTypes": [ ],
|
||||
"stateTypes": [
|
||||
{
|
||||
"id": "29a95a72-f897-4027-99a8-ab1aee1ebe2b",
|
||||
"name": "updateTime",
|
||||
"displayName": "Last update",
|
||||
"displayNameEvent": "Last update changed",
|
||||
"unit": "UnixTime",
|
||||
"type": "int",
|
||||
"ruleRelevant": false,
|
||||
"eventRuleRelevant": false,
|
||||
"defaultValue": 0
|
||||
},
|
||||
{
|
||||
"id": "b5244e65-0811-4dc2-afd2-6bf3092d44c7",
|
||||
"name": "temperature",
|
||||
"displayName": "Temperature",
|
||||
"displayNameEvent": "Temperature changed",
|
||||
"unit": "DegreeCelsius",
|
||||
"graphRelevant": true,
|
||||
"type": "double",
|
||||
"defaultValue": 22.7
|
||||
},
|
||||
{
|
||||
"id": "b2225720-dfdc-40f8-a24a-20247e69e575",
|
||||
"name": "humidity",
|
||||
"displayName": "Humidity",
|
||||
"displayNameEvent": "Humidity changed",
|
||||
"graphRelevant": true,
|
||||
"unit": "Percentage",
|
||||
"type": "int",
|
||||
"defaultValue": 37
|
||||
},
|
||||
{
|
||||
"id": "b13fa34e-b63d-4fa4-a786-e82776ee9cbe",
|
||||
"name": "pressure",
|
||||
"displayName": "Pressure",
|
||||
"displayNameEvent": "Pressure changed",
|
||||
"unit": "MilliBar",
|
||||
"graphRelevant": true,
|
||||
"type": "int",
|
||||
"defaultValue": 1006
|
||||
},
|
||||
{
|
||||
"id": "b16ea43e-75ef-4bee-ade6-839682ec3068",
|
||||
"name": "noise",
|
||||
"displayName": "Noise",
|
||||
"displayNameEvent": "Noise changed",
|
||||
"graphRelevant": true,
|
||||
"unit": "Dezibel",
|
||||
"type": "int",
|
||||
"defaultValue": 68
|
||||
},
|
||||
{
|
||||
"id": "c5ebe5c0-b030-4eb6-a3f8-3a400061d09c",
|
||||
"name": "co2",
|
||||
"displayName": "CO2",
|
||||
"displayNameEvent": "CO2 changed",
|
||||
"graphRelevant": true,
|
||||
"unit": "PartsPerMillion",
|
||||
"type": "int",
|
||||
"defaultValue": 350
|
||||
},
|
||||
{
|
||||
"id": "3b8fb712-ff4c-4c0f-83bc-2edac54fede7",
|
||||
"name": "wifiStrength",
|
||||
"displayName": "Wifi signal strength",
|
||||
"displayNameEvent": "Wifi signal strength changed",
|
||||
"ruleRelevant": false,
|
||||
"eventRuleRelevant": false,
|
||||
"unit": "Percentage",
|
||||
"type": "int",
|
||||
"defaultValue": 87
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user