added netlicensing plugin

This commit is contained in:
Boernsman 2019-11-10 21:09:02 +01:00
parent be230d9a3b
commit e8e664233e
8 changed files with 265 additions and 0 deletions

15
debian/control vendored
View File

@ -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},

View File

@ -0,0 +1 @@
usr/lib/@DEB_HOST_MULTIARCH@/nymea/plugins/libnymea_devicepluginnetlicensing.so

4
netlicensing/README.md Normal file
View File

@ -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 .

View File

@ -0,0 +1,105 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2019 Bernhard Trinnes <bernhard.trinnes@nymea.io> *
* *
* 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 *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "plugininfo.h"
#include "devicepluginnetlicensing.h"
#include "devices/device.h"
#include "network/networkaccessmanager.h"
#include <QDebug>
#include <QUrlQuery>
#include <QJsonDocument>
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<QNetworkReply *>(sender());
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
Q_UNUSED(status)
}

View File

@ -0,0 +1,60 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2019 Bernhard Trinnes <bernhard.trinnes@nymea.io> *
* *
* 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 *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef DEVICEPLUGINNETLICENSING_H
#define DEVICEPLUGINNETLICENSING_H
#include "plugintimer.h"
#include "devices/deviceplugin.h"
#include <QNetworkReply>
#include <QHostInfo>
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<QNetworkReply *, Device *> 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

View File

@ -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
}
]
}
]
}
]
}

View File

@ -0,0 +1,13 @@
include(../plugins.pri)
QT += network
TARGET = $$qtLibraryTarget(nymea_devicepluginnetlicensing)
SOURCES += \
devicepluginnetlicensing.cpp \
HEADERS += \
devicepluginnetlicensing.h \

View File

@ -29,6 +29,7 @@ PLUGIN_DIRS = \
mailnotification \
mqttclient \
netatmo \
netlicensing \
networkdetector \
onewire \
openweathermap \