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

Use InfluxDb 2 write api #12

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2.0b 2022 May 01, Sun
* Refactor for influxdb2

1.07 2019 Apr 26, Fri
* New module icon
* Tranform string to float
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Zway-InfluxDbStats
# Zway-InfluxDb2Stats

Store sensor data in InfluxDB instances. Sensor and device data is transfered
periodically, as well as every time a change occurs.
Expand Down
68 changes: 30 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*** InfluxDbStats Z-Way HA module *******************************************
/*** InfluxDb2Stats Z-Way HA module *******************************************

Version: 1.07
(c) Maroš Kollár, 2015-2017
Expand All @@ -13,41 +13,36 @@ Description:
// --- Class definition, inheritance and setup
// ----------------------------------------------------------------------------

function InfluxDbStats (id, controller) {
function InfluxDb2Stats (id, controller) {
// Call superconstructor first (AutomationModule)
InfluxDbStats.super_.call(this, id, controller);
InfluxDb2Stats.super_.call(this, id, controller);

this.interval = undefined;
this.url = undefined;
this.token = '';
this.langfile = undefined;
this.commandClass = 0x80;
}

inherits(InfluxDbStats, BaseModule);
inherits(InfluxDb2Stats, BaseModule);

_module = InfluxDbStats;
_module = InfluxDb2Stats;

// ----------------------------------------------------------------------------
// --- Module instance initialized
// ----------------------------------------------------------------------------

InfluxDbStats.prototype.init = function (config) {
InfluxDbStats.super_.prototype.init.call(this, config);
InfluxDb2Stats.prototype.init = function (config) {
InfluxDb2Stats.super_.prototype.init.call(this, config);
var self = this;

self.url = self.config.server
+ ':'
+ self.config.port
+ '/write'
+ '?db='
+ encodeURIComponent(self.config.database);
self.url = self.config.server + ':' + self.config.port +
'/api/v2/write' +
'?org=' + encodeURIComponent(self.config.organisation) +
'&bucket=' + encodeURIComponent(self.config.bucket) +
'&precision=ns';

if (typeof(self.config.username) !== 'undefined') {
self.url = self.url + '&u=' + encodeURIComponent(self.config.username);
}
if (typeof(self.config.password) !== 'undefined') {
self.url = self.url + '&p=' + encodeURIComponent(self.config.password);
}
self.token = self.config.token;

if (typeof(self.config.interval) !== 'undefined') {
var interval = parseInt(self.config.interval,10) * 60 * 1000;
Expand All @@ -59,7 +54,7 @@ InfluxDbStats.prototype.init = function (config) {
self.controller.devices.on("modify:metrics:level",self.handleUpdate);
};

InfluxDbStats.prototype.stop = function () {
InfluxDb2Stats.prototype.stop = function () {
var self = this;

// Remove interval
Expand All @@ -71,14 +66,14 @@ InfluxDbStats.prototype.stop = function () {
self.controller.devices.off("modify:metrics:level",self.handleUpdate);
self.handleUpdate = undefined;

InfluxDbStats.super_.prototype.stop.call(this);
InfluxDb2Stats.super_.prototype.stop.call(this);
};

// ----------------------------------------------------------------------------
// --- Module methods
// ----------------------------------------------------------------------------

InfluxDbStats.prototype.updateDevice = function (vDev) {
InfluxDb2Stats.prototype.updateDevice = function (vDev) {
var self = this;

if (typeof(vDev) === 'undefined') {
Expand All @@ -98,7 +93,7 @@ InfluxDbStats.prototype.updateDevice = function (vDev) {
}
};

InfluxDbStats.prototype.escapeValue = function (value) {
InfluxDb2Stats.prototype.escapeValue = function (value) {
var self = this;

switch(typeof(value)) {
Expand All @@ -112,7 +107,7 @@ InfluxDbStats.prototype.escapeValue = function (value) {
};


InfluxDbStats.prototype.collectVirtualDevice = function (deviceObject) {
InfluxDb2Stats.prototype.collectVirtualDevice = function (deviceObject) {
var self = this;

var level = deviceObject.get('metrics:level');
Expand Down Expand Up @@ -153,7 +148,7 @@ InfluxDbStats.prototype.collectVirtualDevice = function (deviceObject) {
' level=' + self.escapeValue(level);
};

InfluxDbStats.prototype.collectZwaveDevice = function (deviceIndex,device) {
InfluxDb2Stats.prototype.collectZwaveDevice = function (deviceIndex,device) {
var self = this;
if (typeof(device) === 'undefined') {
return;
Expand All @@ -172,7 +167,7 @@ InfluxDbStats.prototype.collectZwaveDevice = function (deviceIndex,device) {
(typeof(batteryData) !== 'undefined' ? ',battery=' + self.escapeValue(batteryData.data.last.value) : '');
};

InfluxDbStats.prototype.getValue = function (object,fallback) {
InfluxDb2Stats.prototype.getValue = function (object,fallback) {
var self = this;
if (typeof(object) !== 'undefined'
&& typeof(object.value) !== 'undefined') {
Expand All @@ -185,7 +180,7 @@ InfluxDbStats.prototype.getValue = function (object,fallback) {
}
};

InfluxDbStats.prototype.updateAll = function () {
InfluxDb2Stats.prototype.updateAll = function () {
var self = this;

self.log('Update all');
Expand Down Expand Up @@ -216,7 +211,7 @@ InfluxDbStats.prototype.updateAll = function () {
self.sendStats(lines);
};

InfluxDbStats.prototype.sendStats = function (lines) {
InfluxDb2Stats.prototype.sendStats = function (lines) {
var self = this;

if (lines.length === 0) {
Expand All @@ -229,26 +224,23 @@ InfluxDbStats.prototype.sendStats = function (lines) {
method: 'POST',
async: true,
data: lines.join("\n"),
headers: {
'Accept': 'application/json',
'Authorization': 'Token ' + self.config.token,
'Content-Type': 'text/plain; charset=utf-8',
'Content-Type':'application/json',
},
error: function(response) {
self.error("Could not post stats: " + response.statusText + "\nPOST " + self.url + "\nBody: " + lines.join("\n") + "\nResponse: " + response.data);
console.logJS(response.data);
self.controller.addNotification(
"error",
self.langFile.error,
"module",
"InfluxDbStats"
"InfluxDb2Stats"
);
}
};
/*
if (typeof(self.config.username) !== 'undefined'
&& typeof(self.config.password) !== 'undefined') {
request.auth = {
login: self.config.username,
password: self.config.password
};
}
*/

http.request(request);
};
10 changes: 5 additions & 5 deletions lang/de.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"database_label" : "Datenbank Name",
"bucket_label" : "Influxdb Bucket",
"error" : "Fehler beim Senden von Statistiken an InfluxDB.",
"exclude_tags_helper" : "Alle Module mit diesen Tags werden nicht an InfluxDb geschickt, auch wenn andere Tags übereinstimmen.",
"exclude_tags_label" : "Ausschließende Tags",
"interval_helper" : "Spezifiziert das Aktualisierungs Interval in Minuten in dem alle Statistiken an InfluxDB geschickt werden. Wenn dieser Wert leer ist, dann werden die Device Statistiken nur geschickt wenn sich Werte ändern. (Nicht empfohlen).",
"interval_label" : "Aktualisierungs Interval",
"m_descr" : "Speichert Sensoren Messwerte und Zwave Netzwerke Statistiken in InfluxDB Instanzen. InfluxDB ist eine Zeitserien Datenbank die Zeit Messwerte speichern und auswerten kann. Um dieses Modul zu betreiben benötigt man eine externe InfluxDB Installtion.<br>Siehe <a href=https://github.com/maros/Zway-InfluxDbStats/blob/master/README.md>github.com/maros/Zway-InfluxDbStats</a> für detailliertere Dokumentation.",
"m_title" : "InfluxDb Statistiken",
"password_label" : "Database Passwort",
"m_title" : "InfluxDb2 Statistiken",
"token_label" : "Influxdb Access Token",
"port_label" : "Database Server Port",
"server_helper" : "Die tatsächliche benutzte URL wird im Frotmat $URL:$PORT/write sein. Das Protokoll (zB. https:// ) muss angegeben werden",
"server_helper" : "Die tatsächliche benutzte URL wird im Format $URL:$PORT/write sein. Das Protokoll (zB. https:// ) muss angegeben werden",
"server_label" : "Database Server URL",
"tags_helper" : "Alle Module mit diesen Tags werden erfasst und an InfluxDb geschickt.",
"tags_label" : "Tags",
"username_label" : "Datenbank Benutzername"
"organisation_label" : "Influxdb Organisation"
}
8 changes: 4 additions & 4 deletions lang/en.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"database_label" : "Database name",
"bucket_label" : "Influxdb bucket",
"error" : "Could not post InfluxDb statistics. Please check log files for details",
"exclude_tags_helper" : "All devices tagged with this label will be excluded from statistics, even if other tags match",
"exclude_tags_label" : "Exclude tags",
"interval_helper" : "Sets the interval (in minutes) for statistics updates. If left empty, then the statistics will only be updated if values change (not recommended).",
"interval_label" : "Update interval",
"m_descr" : "Store sensor data and ZWave Network data in InfluxDB instances. InfluxDB is a time series database that can be used for storing and analyzing measurements and statistics over time. You need an external InfluxDB instance to operate this module. <br>Check <a href=https://github.com/maros/Zway-InfluxDbStats/blob/master/README.md>github.com/maros/Zway-InfluxDbStats</a> for detailed documentation.",
"m_title" : "InfluxDb Statistics Collector",
"password_label" : "Database password",
"m_title" : "InfluxDb2 Statistics Collector",
"token_label" : "Influxdb access token",
"port_label" : "Database Server Port",
"server_helper" : "The final URL used for writing will be $URL:$PORT/write. You have to include the protocol (ie. https:// )",
"server_label" : "Database Server URL",
"tags_helper" : "All devices tagged with this label will be handled by this module and sent to InfluxDB",
"tags_label" : "Tags",
"username_label" : "Database username"
"organisation_label" : "Influxdb Organisation"
}
Loading