diff --git a/debian/control b/debian/control index 8cedb1a3..96221816 100644 --- a/debian/control +++ b/debian/control @@ -434,6 +434,21 @@ Description: nymea.io plugin for netatmo This package will install the nymea.io plugin for netatmo +Package: nymea-plugin-netlicensing +Architecture: any +Depends: ${shlibs:Depends}, + ${misc:Depends}, + nymea-plugins-translations, +Description: nymea.io plugin for netlicensing + The nymea daemon is a plugin based IoT (Internet of Things) server. The + server works like a translator for devices, things and services and + allows them to interact. + With the powerful rule engine you are able to connect any device available + in the system and create individual scenes and behaviors for your environment. + . + This package will install the nymea.io plugin for netlicensing + + Package: nymea-plugin-networkdetector Architecture: any Depends: ${shlibs:Depends}, diff --git a/debian/nymea-plugin-netlicensing.install.in b/debian/nymea-plugin-netlicensing.install.in new file mode 100644 index 00000000..199eb418 --- /dev/null +++ b/debian/nymea-plugin-netlicensing.install.in @@ -0,0 +1 @@ +usr/lib/@DEB_HOST_MULTIARCH@/nymea/plugins/libnymea_devicepluginnetlicensing.so diff --git a/netlicensing/README.md b/netlicensing/README.md new file mode 100644 index 00000000..b4867444 --- /dev/null +++ b/netlicensing/README.md @@ -0,0 +1,4 @@ +# Netlicensing + +This plugin shows how to integrate Netlicensing as license manage plattform. +Netlicensing might be used to restrict access to plug-ins or features . diff --git a/netlicensing/devicepluginnetlicensing.cpp b/netlicensing/devicepluginnetlicensing.cpp new file mode 100644 index 00000000..8fe69696 --- /dev/null +++ b/netlicensing/devicepluginnetlicensing.cpp @@ -0,0 +1,105 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Bernhard Trinnes * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "plugininfo.h" +#include "devicepluginnetlicensing.h" +#include "devices/device.h" +#include "network/networkaccessmanager.h" + +#include +#include +#include + +DevicePluginNetlicensing::DevicePluginNetlicensing() +{ + +} + +void DevicePluginNetlicensing::init() +{ + +} + +void DevicePluginNetlicensing::setupDevice(DeviceSetupInfo *info) +{ + Device *device = info->device(); + Q_UNUSED(device) + QString licenseeNumber = QSysInfo::machineUniqueId(); + validateLicensee(licenseeNumber); + qCWarning(dcNetlicensing()) << "Unhandled device class in setupDevice"; +} + +void DevicePluginNetlicensing::deviceRemoved(Device *device) +{ + Q_UNUSED(device) + if (myDevices().isEmpty() && m_pluginTimer) { + m_pluginTimer->deleteLater(); + m_pluginTimer = nullptr; + } +} + +void DevicePluginNetlicensing::postSetupDevice(Device *device) +{ + Q_UNUSED(device) + if(!m_pluginTimer) { + m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(60); + connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginNetlicensing::onPluginTimer); + } +} + +void DevicePluginNetlicensing::validateLicensee(const QString &licenseeNumber) +{ + QUrl url = m_baseUrl + "/core/v2/rest/licensee/" + licenseeNumber; + url.setPort(443); + url.setUserName("apiKey"); + url.setPassword("4ec3e47c-7f1c-4de6-924d-ba502088162d"); + + QNetworkRequest request; + request.setUrl(url); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/x-www-form-urlencoded"); + request.setRawHeader("Accept", "application/json"); + + QNetworkReply *reply = hardwareManager()->networkManager()->get(request); + connect(reply, &QNetworkReply::finished, this, [reply, this]() { + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + // Check HTTP status code + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNetlicensing()) << "Request error:" << status << reply->errorString(); + } + }); +} + +void DevicePluginNetlicensing::onPluginTimer() +{ + +} + +void DevicePluginNetlicensing::onNetworkReplyFinished() +{ + QNetworkReply *reply = static_cast(sender()); + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + Q_UNUSED(status) +} diff --git a/netlicensing/devicepluginnetlicensing.h b/netlicensing/devicepluginnetlicensing.h new file mode 100644 index 00000000..d9e0f5a3 --- /dev/null +++ b/netlicensing/devicepluginnetlicensing.h @@ -0,0 +1,60 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Bernhard Trinnes * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef DEVICEPLUGINNETLICENSING_H +#define DEVICEPLUGINNETLICENSING_H + +#include "plugintimer.h" +#include "devices/deviceplugin.h" + +#include +#include + +class DevicePluginNetlicensing : public DevicePlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "io.nymea.DevicePlugin" FILE "devicepluginnetlicensing.json") + Q_INTERFACES(DevicePlugin) + +public: + explicit DevicePluginNetlicensing(); + + void init() override; + void setupDevice(DeviceSetupInfo *info) override; + void deviceRemoved(Device *device) override; + void postSetupDevice(Device *device) override; + +private: + PluginTimer *m_pluginTimer = nullptr; + QString m_baseUrl = "https://go.netlicensing.io/core/v2/rest/"; + QHash m_httpRequests; + + void validateLicensee(const QString &licenseeNumber); + void transferLicense(const QString &licenseeNumber, const QString &sourceLicenseeNumber); + +private slots: + void onPluginTimer(); + void onNetworkReplyFinished(); + void onAuthenticationChanged(); +}; + +#endif // DEVICEPLUGINNETLICENSING_H diff --git a/netlicensing/devicepluginnetlicensing.json b/netlicensing/devicepluginnetlicensing.json new file mode 100644 index 00000000..cbcceb11 --- /dev/null +++ b/netlicensing/devicepluginnetlicensing.json @@ -0,0 +1,66 @@ +{ + "displayName": "Netlicensing", + "name": "Netlicensing", + "id": "03f6ae2e-e1ac-4010-bd6f-c0338e58d7d9", + "vendors": [ + { + "displayName": "Netlicensing", + "name": "netlicensing", + "id": "6289b7c8-aaa2-4325-a8d1-07f9ac9ba954", + "deviceClasses": [ + { + "id": "9207594c-9a59-4902-9e9a-e8db22aa4777", + "name": "netlicenseConnection", + "displayName": "Netlicense Connection", + "interfaces": ["gateway"], + "createMethods": ["user"], + "paramTypes": [ + { + "id": "84f7e81a-62d8-4400-8b44-48b58c3a00ca", + "name": "apiKey", + "displayName": "API Key", + "type": "QString", + "inputType": "TextLine" + } + ], + "stateTypes": [ + { + "id": "8f9c4172-dc97-459f-ac9a-baaafd3c89cc", + "name": "connected", + "displayName": "Connected", + "displayNameEvent": "Connection changed", + "type": "bool", + "defaultValue": false + } + ] + }, + { + "id": "06573cda-e417-4cd2-9211-be0dafc2f094", + "name": "license", + "displayName": "License", + "createMethods": ["auto"], + "paramTypes": [ + { + "id": "72be35a3-440d-4c66-b7ca-4dd119bd1108", + "name": "name", + "displayName": "name", + "type": "QString", + "inputType": "TextLine" + } + ], + "stateTypes": [ + { + "id": "d70805c9-71a4-46c9-95cd-3835b041edc5", + "name": "updateTime", + "displayName": "last update", + "displayNameEvent": "last update changed", + "unit": "UnixTime", + "type": "int", + "defaultValue": 0 + } + ] + } + ] + } + ] +} diff --git a/netlicensing/netlicensing.pro b/netlicensing/netlicensing.pro new file mode 100644 index 00000000..5a087294 --- /dev/null +++ b/netlicensing/netlicensing.pro @@ -0,0 +1,13 @@ +include(../plugins.pri) + +QT += network + +TARGET = $$qtLibraryTarget(nymea_devicepluginnetlicensing) + +SOURCES += \ + devicepluginnetlicensing.cpp \ + +HEADERS += \ + devicepluginnetlicensing.h \ + + diff --git a/nymea-plugins.pro b/nymea-plugins.pro index 9e1177a0..bd0707d6 100644 --- a/nymea-plugins.pro +++ b/nymea-plugins.pro @@ -29,6 +29,7 @@ PLUGIN_DIRS = \ mailnotification \ mqttclient \ netatmo \ + netlicensing \ networkdetector \ onewire \ openweathermap \