added option for private api keys
parent
a89e442835
commit
602c4e6614
|
|
@ -29,9 +29,10 @@ Besides the air pollution level the plug-in also states a cautionary statement.
|
|||
Both states can be used to let nymea notify you about the pollution level and
|
||||
inform you what precautions should be taken.
|
||||
|
||||
## Requirments
|
||||
## Requirements
|
||||
|
||||
* Valid "Air Quality Index" API Key
|
||||
* This might be provided through the nymea community API Key provider or an own key in the nymead.conf file.
|
||||
* The package "nymea-plugin-airqualityindex" must be installed
|
||||
* Internet connection
|
||||
|
||||
|
|
@ -39,3 +40,8 @@ inform you what precautions should be taken.
|
|||
|
||||
More about the different Air Quality Levels: https://www.airnow.gov/index.cfm?action=aqibasics.aqi
|
||||
|
||||
Personal API Key settings in 'nymead.conf':
|
||||
'''
|
||||
[aqi]
|
||||
apiKey="123456789abcdfgh"
|
||||
'''
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@
|
|||
|
||||
#include "integrationpluginaqi.h"
|
||||
#include "plugininfo.h"
|
||||
#include "nymeasettings.h"
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
|
||||
|
|
@ -41,10 +42,9 @@ IntegrationPluginAqi::IntegrationPluginAqi()
|
|||
void IntegrationPluginAqi::discoverThings(ThingDiscoveryInfo *info)
|
||||
{
|
||||
if (!m_aqiConnection) {
|
||||
QString apiKey = apiKeyStorage()->requestKey("aqi").data("apiKey");
|
||||
if (apiKey.isEmpty()) {
|
||||
return info->finish(Thing::ThingErrorHardwareNotAvailable, tr("No API key is set."));
|
||||
}
|
||||
QString apiKey = getApiKey();
|
||||
if (apiKey.isEmpty())
|
||||
return info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("API key is not available."));
|
||||
m_aqiConnection = new AirQualityIndex(hardwareManager()->networkManager(), apiKey, this);
|
||||
connect(m_aqiConnection, &AirQualityIndex::requestExecuted, this, &IntegrationPluginAqi::onRequestExecuted);
|
||||
connect(m_aqiConnection, &AirQualityIndex::dataReceived, this, &IntegrationPluginAqi::onAirQualityDataReceived);
|
||||
|
|
@ -68,7 +68,9 @@ void IntegrationPluginAqi::setupThing(ThingSetupInfo *info)
|
|||
{
|
||||
if (info->thing()->thingClassId() == airQualityIndexThingClassId) {
|
||||
if (!m_aqiConnection) {
|
||||
QString apiKey = apiKeyStorage()->requestKey("aqi").data("apiKey");
|
||||
QString apiKey = getApiKey();
|
||||
if (apiKey.isEmpty())
|
||||
return info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("API key is not available."));
|
||||
m_aqiConnection = new AirQualityIndex(hardwareManager()->networkManager(), apiKey, this);
|
||||
connect(m_aqiConnection, &AirQualityIndex::requestExecuted, this, &IntegrationPluginAqi::onRequestExecuted);
|
||||
connect(m_aqiConnection, &AirQualityIndex::dataReceived, this, &IntegrationPluginAqi::onAirQualityDataReceived);
|
||||
|
|
@ -118,6 +120,27 @@ void IntegrationPluginAqi::postSetupThing(Thing *thing)
|
|||
}
|
||||
}
|
||||
|
||||
QString IntegrationPluginAqi::getApiKey()
|
||||
{
|
||||
QString apiKey;
|
||||
QSettings settings(NymeaSettings::settingsPath() + "/nymead.conf", QSettings::IniFormat);
|
||||
settings.beginGroup("aqi");
|
||||
if (settings.contains("apiKey")) {
|
||||
apiKey = settings.value("apiKey").toString();
|
||||
QString printedCopy = apiKey;
|
||||
qCDebug(dcAirQualityIndex()) << "Using custom API key:" << printedCopy.replace(printedCopy.length() - 10, 10, "**********");
|
||||
}
|
||||
settings.endGroup();
|
||||
|
||||
if (apiKey.isEmpty()) {
|
||||
apiKey = apiKeyStorage()->requestKey("aqi").data("apiKey");
|
||||
}
|
||||
if (apiKey.isEmpty()) {
|
||||
qCWarning(dcAirQualityIndex()) << "Could not find any API key for AQI";
|
||||
}
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
void IntegrationPluginAqi::thingRemoved(Thing *thing)
|
||||
{
|
||||
Q_UNUSED(thing)
|
||||
|
|
|
|||
|
|
@ -62,6 +62,7 @@ private:
|
|||
QHash<QUuid, ThingDiscoveryInfo *> m_asyncDiscovery;
|
||||
QHash<QUuid, ThingSetupInfo *> m_asyncSetups;
|
||||
QHash<QUuid, ThingId> m_asyncRequests;
|
||||
QString getApiKey();
|
||||
|
||||
private slots:
|
||||
void onPluginTimer();
|
||||
|
|
|
|||
Loading…
Reference in New Issue