Skip to content

Commit afa310f

Browse files
committed
pooling refactor: gather/parse data in softpoll
refactor parsing code out into it's own function. in the softpoll build all the data that will be sent to influxdb and post it all at once.
1 parent edd060b commit afa310f

File tree

1 file changed

+20
-51
lines changed

1 file changed

+20
-51
lines changed

smartapps/influxdb-logger/influxdb-logger.groovy

+20-51
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ preferences {
6767
input "prefDatabaseName", "text", title: "Database Name", defaultValue: "", required: true
6868
input "prefDatabaseUser", "text", title: "Username", required: false
6969
input "prefDatabasePass", "text", title: "Password", required: false
70-
input "preDatabasePooling", "bool", title: "Enable Pooling", defaultValue: true, required: true
7170
}
7271

7372
section("Polling:") {
@@ -172,7 +171,6 @@ def updated() {
172171
state.databaseName = settings.prefDatabaseName
173172
state.databaseUser = settings.prefDatabaseUser
174173
state.databasePass = settings.prefDatabasePass
175-
state.databasePool = settings.prefDatabasePooling
176174

177175
state.path = "/write?db=${state.databaseName}"
178176
state.headers = [:]
@@ -231,9 +229,6 @@ def updated() {
231229
state.softPollingInterval = settings.prefSoftPollingInterval.toInteger()
232230
manageSchedules()
233231

234-
// Configure if we're pooling InfluxPooling
235-
state.poolData = ""
236-
237232
// Configure Subscriptions:
238233
manageSubscriptions()
239234
}
@@ -271,22 +266,29 @@ def handleModeEvent(evt) {
271266
/**
272267
* handleEvent(evt)
273268
*
274-
* Builds data to send to InfluxDB.
269+
* parseEvent() then post to InfluxDB.
270+
**/
271+
def handleEvent(evt) {
272+
logger("handleEvent(): $evt.displayName($evt.name:$evt.unit) $evt.value","info")
273+
data = parseEvent(evt)
274+
postToInfluxDB(data)
275+
}
276+
277+
/**
278+
* parseEvent(evt)
279+
*
280+
* Parses event data to send to InfluxDB.
275281
* - Escapes and quotes string values.
276282
* - Calculates logical binary values where string values can be
277283
* represented as binary values (e.g. contact: closed = 1, open = 0)
278284
*
279-
* Useful references:
280-
* - http://docs.smartthings.com/en/latest/capabilities-reference.html
281-
* - https://docs.influxdata.com/influxdb/v0.10/guides/writing_data/
282285
**/
283-
def handleEvent(evt) {
284-
logger("handleEvent(): $evt.displayName($evt.name:$evt.unit) $evt.value","info")
285-
286+
def parseEvent(evt) {
286287
// Build data string to send to InfluxDB:
287288
// Format: <measurement>[,<tag_name>=<tag_value>] field=<field_value>
288289
// If value is an integer, it must have a trailing "i"
289290
// If value is a string, it must be enclosed in double quotes.
291+
logger("handleEvent(): $evt.displayName($evt.name:$evt.unit) $evt.value","info")
290292
def measurement = evt.name
291293
// tags:
292294
def deviceId = escapeStringForInfluxDB(evt.deviceId)
@@ -487,15 +489,7 @@ def handleEvent(evt) {
487489
else {
488490
data += ",unit=${unit} value=${value}"
489491
}
490-
491-
// Post data to InfluxDB pool requests if enabled.
492-
if (settings.preDatabasePooling) {
493-
poolInfluxDB(data)
494-
}
495-
else {
496-
postToInfluxDB(data)
497-
}
498-
492+
return data
499493
}
500494

501495

@@ -520,15 +514,16 @@ def softPoll() {
520514

521515
// Iterate over each attribute for each device, in each device collection in deviceAttributes:
522516
def devs // temp variable to hold device collection.
517+
def eventsData = ""
523518
state.deviceAttributes.each { da ->
524519
devs = settings."${da.devices}"
525520
if (devs && (da.attributes)) {
526521
devs.each { d ->
527522
da.attributes.each { attr ->
528523
if (d.hasAttribute(attr) && d.latestState(attr)?.value != null) {
529524
logger("softPoll(): Softpolling device ${d} for attribute: ${attr}","info")
530-
// Send fake event to handleEvent():
531-
handleEvent([
525+
// Build multiple event string to sent to InfluxDB
526+
eventsData = eventsData + '\n' + parseEvent([
532527
name: attr,
533528
value: d.latestState(attr)?.value,
534529
unit: d.latestState(attr)?.unit,
@@ -541,7 +536,7 @@ def softPoll() {
541536
}
542537
}
543538
}
544-
539+
postToInfluxDB(eventsData)
545540
}
546541

547542
/**
@@ -598,31 +593,6 @@ def logSystemProperties() {
598593

599594
}
600595

601-
602-
/**
603-
* poolInfluxDB()
604-
**/
605-
def poolInfluxDB(data) {
606-
state.poolData = state.poolData + '\n' + "${data}"
607-
// If 80% of state data max commit immediately
608-
if (state.poolData.size() >= 80000) {
609-
commitPoolToInfluxDB()
610-
}
611-
else {
612-
runIn(5, commitPoolToInfluxDB)
613-
}
614-
}
615-
616-
/**
617-
* commit Pool
618-
**/
619-
def commitPoolToInfluxDB() {
620-
logger("commitPoolToInfluxDB(): ${state.poolData.size()} bytes", "info")
621-
logger("commitPoolToInfluxDB(): ${state.poolData}", "info")
622-
postToInfluxDB(state.poolData)
623-
state.poolData = ""
624-
}
625-
626596
/**
627597
* postToInfluxDB()
628598
*
@@ -829,5 +799,4 @@ private getGroupName(id) {
829799
else if (id == 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX') {return 'Lounge'}
830800
else if (id == 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX') {return 'Office'}
831801
else {return 'Unknown'}
832-
}
833-
802+
}

0 commit comments

Comments
 (0)