added first version of datetime plugin
This commit is contained in:
parent
35e4796941
commit
b8616dc53a
12
plugins/deviceplugins/datetime/datetime.pro
Normal file
12
plugins/deviceplugins/datetime/datetime.pro
Normal file
@ -0,0 +1,12 @@
|
||||
include(../../plugins.pri)
|
||||
|
||||
TARGET = $$qtLibraryTarget(guh_deviceplugindatetime)
|
||||
|
||||
QT+= network
|
||||
|
||||
SOURCES += \
|
||||
deviceplugindatetime.cpp
|
||||
|
||||
HEADERS += \
|
||||
deviceplugindatetime.h
|
||||
|
||||
132
plugins/deviceplugins/datetime/deviceplugindatetime.cpp
Normal file
132
plugins/deviceplugins/datetime/deviceplugindatetime.cpp
Normal file
@ -0,0 +1,132 @@
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* *
|
||||
* This file is part of guh. *
|
||||
* *
|
||||
* Guh is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, version 2 of the License. *
|
||||
* *
|
||||
* Guh is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
|
||||
#include "deviceplugindatetime.h"
|
||||
|
||||
#include "plugin/device.h"
|
||||
#include "devicemanager.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
#include <QTimeZone>
|
||||
|
||||
DeviceClassId dateTimeDeviceClassId = DeviceClassId("fbf665fb-9aca-423f-a5f2-924e50ebe6ca");
|
||||
|
||||
StateTypeId minuteStateTypeId = StateTypeId("4f867051-bc3c-4b55-8493-10ab74c98a49");
|
||||
StateTypeId hourStateTypeId = StateTypeId("5b19d9de-a533-4b6f-b42c-bf8069e31adc");
|
||||
StateTypeId dayStateTypeId = StateTypeId("eb5231ea-6a1b-4d7e-a95f-d49e7b25122e");
|
||||
StateTypeId monthStateTypeId = StateTypeId("fcd8ec96-4488-438a-8b30-58bfe2a7fae2");
|
||||
StateTypeId yearStateTypeId = StateTypeId("79d4ae9b-ea27-4346-8229-1d90f1ddfc9d");
|
||||
StateTypeId weekdayStateTypeId = StateTypeId("f627d052-cee6-4727-b9c6-0e935d41e04a");
|
||||
StateTypeId weekendStateTypeId = StateTypeId("4de5b57b-bb1a-4d66-9ce3-22bb280b075d");
|
||||
|
||||
|
||||
DevicePluginDateTime::DevicePluginDateTime()
|
||||
{
|
||||
m_timer = new QTimer(this);
|
||||
m_timer->setInterval(1000);
|
||||
|
||||
connect(m_timer, &QTimer::timeout, this, &DevicePluginDateTime::timeout);
|
||||
}
|
||||
|
||||
DeviceManager::DeviceSetupStatus DevicePluginDateTime::setupDevice(Device *device)
|
||||
{
|
||||
// check the DeviceClassId
|
||||
if(device->deviceClassId() != dateTimeDeviceClassId){
|
||||
return DeviceManager::DeviceSetupStatusFailure;
|
||||
}
|
||||
|
||||
// make shore there is just one date/time
|
||||
if(deviceManager()->findConfiguredDevices(dateTimeDeviceClassId).count() != 0){
|
||||
return DeviceManager::DeviceSetupStatusFailure;
|
||||
}
|
||||
|
||||
device->setName("Time (" + device->paramValue("timezone").toString() + ")");
|
||||
m_timeZone = QTimeZone(device->paramValue("timezone").toByteArray());
|
||||
|
||||
if(m_timeZone.isValid()){
|
||||
QDateTime zoneTime = QDateTime(QDate::currentDate(), QTime::currentTime(), m_timeZone).toLocalTime();
|
||||
qDebug() << zoneTime.toLocalTime().date() << zoneTime.toLocalTime().time() << QLocale::countryToString(m_timeZone.country());
|
||||
m_timer->start();
|
||||
|
||||
return DeviceManager::DeviceSetupStatusSuccess;
|
||||
}
|
||||
return DeviceManager::DeviceSetupStatusFailure;
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginDateTime::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms)
|
||||
{
|
||||
Q_UNUSED(deviceClassId);
|
||||
|
||||
QList<DeviceDescriptor> deviceDescriptors;
|
||||
foreach (QByteArray timeZone, QTimeZone::availableTimeZoneIds()) {
|
||||
QByteArray continent = params.paramValue("continent").toByteArray();
|
||||
if(timeZone.contains(continent)){
|
||||
DeviceDescriptor descriptor(dateTimeDeviceClassId, timeZone.right(timeZone.length() - (continent.length() + 1)), continent);
|
||||
ParamList params;
|
||||
params.append(Param("timezone", timeZone));
|
||||
descriptor.setParams(params);
|
||||
deviceDescriptors.append(descriptor);
|
||||
}
|
||||
}
|
||||
emit devicesDiscovered(dateTimeDeviceClassId, deviceDescriptors);
|
||||
|
||||
return DeviceManager::DeviceErrorAsync;
|
||||
}
|
||||
|
||||
DeviceManager::HardwareResources DevicePluginDateTime::requiredHardware() const
|
||||
{
|
||||
return DeviceManager::HardwareResourceNone;
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginDateTime::executeAction(Device *device, const Action &action)
|
||||
{
|
||||
Q_UNUSED(device);
|
||||
Q_UNUSED(action);
|
||||
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
}
|
||||
|
||||
void DevicePluginDateTime::deviceRemoved(Device *device)
|
||||
{
|
||||
Q_UNUSED(device);
|
||||
m_timer->stop();
|
||||
}
|
||||
|
||||
void DevicePluginDateTime::timeout()
|
||||
{
|
||||
QDateTime zoneTime = QDateTime(QDate::currentDate(), QTime::currentTime(), m_timeZone).toLocalTime();
|
||||
|
||||
if(deviceManager()->findConfiguredDevices(dateTimeDeviceClassId).count() == 1){
|
||||
Device *device = deviceManager()->findConfiguredDevices(dateTimeDeviceClassId).first();
|
||||
device->setStateValue(minuteStateTypeId, zoneTime.time().minute());
|
||||
device->setStateValue(hourStateTypeId, zoneTime.time().hour());
|
||||
device->setStateValue(dayStateTypeId, zoneTime.date().day());
|
||||
device->setStateValue(monthStateTypeId, zoneTime.date().month());
|
||||
device->setStateValue(yearStateTypeId, zoneTime.date().year());
|
||||
device->setStateValue(weekdayStateTypeId, zoneTime.date().longDayName(zoneTime.date().dayOfWeek()));
|
||||
|
||||
if(zoneTime.date().dayOfWeek() == 6 || zoneTime.date().dayOfWeek() == 7){
|
||||
device->setStateValue(weekendStateTypeId, true);
|
||||
}else{
|
||||
device->setStateValue(weekendStateTypeId, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
55
plugins/deviceplugins/datetime/deviceplugindatetime.h
Normal file
55
plugins/deviceplugins/datetime/deviceplugindatetime.h
Normal file
@ -0,0 +1,55 @@
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* *
|
||||
* This file is part of guh. *
|
||||
* *
|
||||
* Guh is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, version 2 of the License. *
|
||||
* *
|
||||
* Guh is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#ifndef DEVICEPLUGINDATETIME_H
|
||||
#define DEVICEPLUGINDATETIME_H
|
||||
|
||||
#include "plugin/deviceplugin.h"
|
||||
#include <QDateTime>
|
||||
#include <QTimeZone>
|
||||
#include <QTimer>
|
||||
|
||||
class DevicePluginDateTime : public DevicePlugin
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PLUGIN_METADATA(IID "guru.guh.DevicePlugin" FILE "deviceplugindatetime.json")
|
||||
Q_INTERFACES(DevicePlugin)
|
||||
|
||||
public:
|
||||
explicit DevicePluginDateTime();
|
||||
|
||||
DeviceManager::DeviceSetupStatus setupDevice(Device *device) override;
|
||||
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override;
|
||||
DeviceManager::HardwareResources requiredHardware() const override;
|
||||
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
|
||||
|
||||
void deviceRemoved(Device *device) override;
|
||||
|
||||
private:
|
||||
QTimer *m_timer;
|
||||
QTimeZone m_timeZone;
|
||||
|
||||
private slots:
|
||||
void timeout();
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // DEVICEPLUGINDATETIME_H
|
||||
74
plugins/deviceplugins/datetime/deviceplugindatetime.json
Normal file
74
plugins/deviceplugins/datetime/deviceplugindatetime.json
Normal file
@ -0,0 +1,74 @@
|
||||
{
|
||||
"name": "Time",
|
||||
"id": "c26014c6-87fb-4233-85ed-01d18625018d",
|
||||
"vendors": [
|
||||
{
|
||||
"name": "guh",
|
||||
"id": "2062d64d-3232-433c-88bc-0d33c0ba2ba6",
|
||||
"deviceClasses": [
|
||||
{
|
||||
"deviceClassId": "fbf665fb-9aca-423f-a5f2-924e50ebe6ca",
|
||||
"name": "Time",
|
||||
"createMethods": ["discovery"],
|
||||
"discoveryParamTypes": [
|
||||
{
|
||||
"name": "continent",
|
||||
"type": "QString",
|
||||
"allowedValues": ["Africa", "America", "Antarctica", "Asia", "Atlantic", "Australia", "Europe", "Indian", "Pacific"]
|
||||
}
|
||||
],
|
||||
"paramTypes": [
|
||||
{
|
||||
"name": "timezone",
|
||||
"type": "QString"
|
||||
}
|
||||
],
|
||||
"stateTypes": [
|
||||
{
|
||||
"id": "4f867051-bc3c-4b55-8493-10ab74c98a49",
|
||||
"name": "minute",
|
||||
"type": "uint",
|
||||
"defaultValue": "0"
|
||||
},
|
||||
{
|
||||
"id": "5b19d9de-a533-4b6f-b42c-bf8069e31adc",
|
||||
"name": "hour",
|
||||
"type": "uint",
|
||||
"defaultValue": "0"
|
||||
},
|
||||
{
|
||||
"id": "eb5231ea-6a1b-4d7e-a95f-d49e7b25122e",
|
||||
"name": "day",
|
||||
"type": "uint",
|
||||
"defaultValue": "1"
|
||||
},
|
||||
{
|
||||
"id": "fcd8ec96-4488-438a-8b30-58bfe2a7fae2",
|
||||
"name": "month",
|
||||
"type": "uint",
|
||||
"defaultValue": "1"
|
||||
},
|
||||
{
|
||||
"id": "79d4ae9b-ea27-4346-8229-1d90f1ddfc9d",
|
||||
"name": "year",
|
||||
"type": "uint",
|
||||
"defaultValue": "1970"
|
||||
},
|
||||
{
|
||||
"id": "f627d052-cee6-4727-b9c6-0e935d41e04a",
|
||||
"name": "weekday",
|
||||
"type": "QString",
|
||||
"defaultValue": "-"
|
||||
},
|
||||
{
|
||||
"id": "4de5b57b-bb1a-4d66-9ce3-22bb280b075d",
|
||||
"name": "weekend",
|
||||
"type": "bool",
|
||||
"defaultValue": "false"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,7 +1,6 @@
|
||||
TEMPLATE = subdirs
|
||||
SUBDIRS += elro \
|
||||
intertechno \
|
||||
# meisteranker \
|
||||
wifidetector \
|
||||
conrad \
|
||||
mock \
|
||||
@ -11,7 +10,7 @@ SUBDIRS += elro \
|
||||
mailnotification \
|
||||
philipshue \
|
||||
lgsmarttv \
|
||||
genericelements \
|
||||
datetime \
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user