Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for InfluxDB v2.x and Token authentication #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion smartapps/influxdb-logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
39 changes: 33 additions & 6 deletions smartapps/influxdb-logger/influxdb-logger.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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:") {
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -597,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(
[
Expand Down