From f394a7a46b841fa577c1d421c049b96979da969c Mon Sep 17 00:00:00 2001 From: Konnichy Date: Mon, 1 Aug 2022 23:01:00 +0200 Subject: [PATCH 1/2] Add support for InfluxDB v2.x and Token authentication --- smartapps/influxdb-logger/README.md | 7 ++++++- .../influxdb-logger/influxdb-logger.groovy | 20 ++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/smartapps/influxdb-logger/README.md b/smartapps/influxdb-logger/README.md index a696a5d..44956b0 100644 --- a/smartapps/influxdb-logger/README.md +++ b/smartapps/influxdb-logger/README.md @@ -7,11 +7,13 @@ Copyright (c) [David Lomas](https://github.com/codersaur) This SmartApp logs SmartThings device attributes to an [InfluxDB](https://influxdata.com/) database. ### Key features: +* Supports InfluxDB v1.x and v2.x * Changes to device attributes are immediately logged to InfluxDB. * The _Soft-Polling_ feature forces attribute values to be written to the database periodically, even if values haven't changed. * Logs Location _Mode_ events. * Supports an InfluxDB instance on the local LAN, without needing to route traffic via the cloud. * Supports Basic Authentication to InfluxDB database. +* Supports Token Authentication to InfluxDB v2 database. ## Installation Follow [these instructions](https://github.com/codersaur/SmartThings#smartapp-installation-procedure) to install the SmartApp in the SmartThings IDE. However, before publishing the code in the IDE, edit the _getGroupName()_ command (at the bottom of the code) to add the Group IDs for your SmartThings instance. These can be found from the _'My Locations'_ tab in the SmartThings IDE. @@ -21,13 +23,16 @@ For more information about installing InfluxDB, Grafana, and this SmartApp, [see ## Usage SmartApp settings: -* **InfluxDB Database**: Specify your InfluxDB instance details in this section. +* **InfluxDB Database**: Specify your InfluxDB instance details in this section. Fill in "v1" or "v2"-marked parameters according to your InfluxDB version. * **Polling**: Configure the _Soft-Polling_ interval. All device attribute values will be written to the database at least once per interval. This is useful to ensure attribute values are written to the database, even when they have not changed. Set to zero to disable. * **System Monitoring**: Configure which location and hub attributes are logged. * **Devices to Monitor**: Specify which device attributes to monitor. ## Version History +#### 2022-08-01: v1.12 + * Supports InfluxDB v2 and Token Authentication. + #### 2017-04-03: v1.11 * Supports Basic HTTP Authentication. * logger(): Wrapper for all logging. diff --git a/smartapps/influxdb-logger/influxdb-logger.groovy b/smartapps/influxdb-logger/influxdb-logger.groovy index 3cba6e3..d971cec 100644 --- a/smartapps/influxdb-logger/influxdb-logger.groovy +++ b/smartapps/influxdb-logger/influxdb-logger.groovy @@ -64,9 +64,11 @@ preferences { section ("InfluxDB Database:") { input "prefDatabaseHost", "text", title: "Host", defaultValue: "10.10.10.10", required: true input "prefDatabasePort", "text", title: "Port", defaultValue: "8086", required: true - input "prefDatabaseName", "text", title: "Database Name", defaultValue: "", required: true - input "prefDatabaseUser", "text", title: "Username", required: false - input "prefDatabasePass", "text", title: "Password", required: false + input "prefDatabaseName", "text", title: "InfluxDB v1 Database Name or InfluxDB v2 Bucket", defaultValue: "", required: true + input "prefDatabaseUser", "text", title: "InfluxDB v1 Username", required: false + input "prefDatabasePass", "text", title: "InfluxDB v1 Password", required: false + input "prefDatabaseOrg", "text", title: "InfluxDB v2 Organization", required: false + input "prefDatabaseToken", "text", title: "InfluxDB v2 Token", required: false } section("Polling:") { @@ -171,12 +173,20 @@ def updated() { state.databaseName = settings.prefDatabaseName state.databaseUser = settings.prefDatabaseUser state.databasePass = settings.prefDatabasePass + state.databaseOrg = settings.prefDatabaseOrg + state.databaseToken = settings.prefDatabaseToken - state.path = "/write?db=${state.databaseName}" + if (state.databaseOrg) { + state.path = "/api/v2/write?org=${state.databaseOrg}&bucket=${state.databaseName}" + } else { + state.path = "/write?db=${state.databaseName}" + } state.headers = [:] state.headers.put("HOST", "${state.databaseHost}:${state.databasePort}") state.headers.put("Content-Type", "application/x-www-form-urlencoded") - if (state.databaseUser && state.databasePass) { + if (state.databaseOrg && state.databaseToken) { + state.headers.put("Authorization", "Token " + state.databaseToken) + } else if (state.databaseUser && state.databasePass) { state.headers.put("Authorization", encodeCredentialsBasic(state.databaseUser, state.databasePass)) } From 01b513f8f17bad0b01ea9288a0aa417a56c3b23b Mon Sep 17 00:00:00 2001 From: Konnichy Date: Mon, 1 Aug 2022 23:06:28 +0200 Subject: [PATCH 2/2] Workaround for multi-byte characters --- .../influxdb-logger/influxdb-logger.groovy | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/smartapps/influxdb-logger/influxdb-logger.groovy b/smartapps/influxdb-logger/influxdb-logger.groovy index d971cec..be6c2d4 100644 --- a/smartapps/influxdb-logger/influxdb-logger.groovy +++ b/smartapps/influxdb-logger/influxdb-logger.groovy @@ -607,7 +607,24 @@ def logSystemProperties() { **/ def postToInfluxDB(data) { logger("postToInfluxDB(): Posting data to InfluxDB: Host: ${state.databaseHost}, Port: ${state.databasePort}, Database: ${state.databaseName}, Data: [${data}]","debug") - + + /* Workaround for 2-byte characters counted as 1 */ + def number_of_accentued_characters = 0 + number_of_accentued_characters += data.count("à") + number_of_accentued_characters += data.count("â") + number_of_accentued_characters += data.count("é") + number_of_accentued_characters += data.count("è") + number_of_accentued_characters += data.count("ê") + number_of_accentued_characters += data.count("ë") + number_of_accentued_characters += data.count("î") + number_of_accentued_characters += data.count("ï") + number_of_accentued_characters += data.count("ô") + number_of_accentued_characters += data.count("ù") + number_of_accentued_characters += data.count("û") + number_of_accentued_characters += data.count("ç") + logger("postToInfluxDB(): Padding data with ${number_of_accentued_characters} extra space(s)", "debug") + data += " ".multiply(number_of_accentued_characters) + try { def hubAction = new physicalgraph.device.HubAction( [