From acef4163d22a187602b9e22dc37deb9ab6f3201e Mon Sep 17 00:00:00 2001 From: Carl Leiby Date: Wed, 10 Jan 2018 11:05:19 -0500 Subject: [PATCH] Updated to use sendHubCommand for in-home network communication Also cleaned up the handlers and added some value parsing to improve graphing in kibana. --- .../LogstashEventLogger.groovy | 262 +++++++----------- 1 file changed, 103 insertions(+), 159 deletions(-) diff --git a/Logstash Event Logger/LogstashEventLogger.groovy b/Logstash Event Logger/LogstashEventLogger.groovy index 760927c..4995798 100644 --- a/Logstash Event Logger/LogstashEventLogger.groovy +++ b/Logstash Event Logger/LogstashEventLogger.groovy @@ -28,41 +28,41 @@ preferences { section("Log these presence sensors:") { input "presences", "capability.presenceSensor", multiple: true, required: false } - section("Log these switches:") { - input "switches", "capability.switch", multiple: true, required: false + section("Log these switches:") { + input "switches", "capability.switch", multiple: true, required: false } - section("Log these switch levels:") { - input "levels", "capability.switchLevel", multiple: true, required: false + section("Log these switch levels:") { + input "levels", "capability.switchLevel", multiple: true, required: false } - section("Log these motion sensors:") { - input "motions", "capability.motionSensor", multiple: true, required: false + section("Log these motion sensors:") { + input "motions", "capability.motionSensor", multiple: true, required: false } - section("Log these temperature sensors:") { - input "temperatures", "capability.temperatureMeasurement", multiple: true, required: false + section("Log these temperature sensors:") { + input "temperatures", "capability.temperatureMeasurement", multiple: true, required: false } section("Log these humidity sensors:") { - input "humidities", "capability.relativeHumidityMeasurement", multiple: true, required: false + input "humidities", "capability.relativeHumidityMeasurement", multiple: true, required: false } section("Log these contact sensors:") { - input "contacts", "capability.contactSensor", multiple: true, required: false + input "contacts", "capability.contactSensor", multiple: true, required: false } section("Log these alarms:") { - input "alarms", "capability.alarm", multiple: true, required: false - } + input "alarms", "capability.alarm", multiple: true, required: false + } section("Log these indicators:") { - input "indicators", "capability.indicator", multiple: true, required: false + input "indicators", "capability.indicator", multiple: true, required: false } section("Log these CO detectors:") { - input "codetectors", "capability.carbonMonoxideDetector", multiple: true, required: false + input "codetectors", "capability.carbonMonoxideDetector", multiple: true, required: false } section("Log these smoke detectors:") { - input "smokedetectors", "capability.smokeDetector", multiple: true, required: false + input "smokedetectors", "capability.smokeDetector", multiple: true, required: false } section("Log these water detectors:") { - input "waterdetectors", "capability.waterSensor", multiple: true, required: false + input "waterdetectors", "capability.waterSensor", multiple: true, required: false } section("Log these acceleration sensors:") { - input "accelerations", "capability.accelerationSensor", multiple: true, required: false + input "accelerations", "capability.accelerationSensor", multiple: true, required: false } section("Log these energy meters:") { input "energymeters", "capability.energyMeter", multiple: true, required: false @@ -76,158 +76,102 @@ preferences { } def installed() { - log.debug "Installed with settings: ${settings}" + log.debug "Installed with settings: ${settings}" - initialize() + initialize() } def updated() { - log.debug "Updated with settings: ${settings}" + log.debug "Updated with settings: ${settings}" - unsubscribe() - initialize() + unsubscribe() + initialize() } def initialize() { - // TODO: subscribe to attributes, devices, locations, etc. - doSubscriptions() + // TODO: subscribe to attributes, devices, locations, etc. + doSubscriptions() } def doSubscriptions() { - subscribe(alarms, "alarm", alarmHandler) - subscribe(codetectors, "carbonMonoxideDetector", coHandler) - subscribe(contacts, "contact", contactHandler) - subscribe(indicators, "indicator", indicatorHandler) - subscribe(modes, "locationMode", modeHandler) - subscribe(motions, "motion", motionHandler) - subscribe(presences, "presence", presenceHandler) - subscribe(relays, "relaySwitch", relayHandler) - subscribe(smokedetectors, "smokeDetector", smokeHandler) - subscribe(switches, "switch", switchHandler) - subscribe(levels, "level", levelHandler) - subscribe(temperatures, "temperature", temperatureHandler) - subscribe(waterdetectors, "water", waterHandler) - subscribe(location, "location", locationHandler) - subscribe(accelerations, "acceleration", accelerationHandler) - subscribe(energymeters, "power", powerHandler) -} - -def genericHandler(evt) { - /* - log.debug("------------------------------") - log.debug("date: ${evt.date}") - log.debug("name: ${evt.name}") - log.debug("displayName: ${evt.displayName}") - log.debug("device: ${evt.device}") - log.debug("deviceId: ${evt.deviceId}") - log.debug("value: ${evt.value}") - log.debug("isStateChange: ${evt.isStateChange()}") - log.debug("id: ${evt.id}") - log.debug("description: ${evt.description}") - log.debug("descriptionText: ${evt.descriptionText}") - log.debug("installedSmartAppId: ${evt.installedSmartAppId}") - log.debug("isoDate: ${evt.isoDate}") - log.debug("isDigital: ${evt.isDigital()}") - log.debug("isPhysical: ${evt.isPhysical()}") - log.debug("location: ${evt.location}") - log.debug("locationId: ${evt.locationId}") - log.debug("source: ${evt.source}") - log.debug("unit: ${evt.unit}") - */ - def json = "{" - json += "\"date\":\"${evt.date}\"," - json += "\"name\":\"${evt.name}\"," - json += "\"displayName\":\"${evt.displayName}\"," - json += "\"device\":\"${evt.device}\"," - json += "\"deviceId\":\"${evt.deviceId}\"," - json += "\"value\":\"${evt.value}\"," - json += "\"isStateChange\":\"${evt.isStateChange()}\"," - json += "\"id\":\"${evt.id}\"," - json += "\"description\":\"${evt.description}\"," - json += "\"descriptionText\":\"${evt.descriptionText}\"," - json += "\"installedSmartAppId\":\"${evt.installedSmartAppId}\"," - json += "\"isoDate\":\"${evt.isoDate}\"," - json += "\"isDigital\":\"${evt.isDigital()}\"," - json += "\"isPhysical\":\"${evt.isPhysical()}\"," - json += "\"location\":\"${evt.location}\"," - json += "\"locationId\":\"${evt.locationId}\"," - json += "\"unit\":\"${evt.unit}\"," - json += "\"source\":\"${evt.source}\"," - json += "\"program\":\"SmartThings\"" - json += "}" - log.debug("JSON: ${json}") - - def params = [ - uri: "http://${logstash_host}:${logstash_port}", - body: json - ] - try { - httpPostJson(params) - } catch ( groovyx.net.http.HttpResponseException ex ) { - log.debug "Unexpected response error: ${ex.statusCode}" + subscribe(alarms, "alarm", eventHandler) + subscribe(codetectors, "carbonMonoxideDetector", eventHandler) + subscribe(contacts, "contact", eventHandler) + subscribe(indicators, "indicator", eventHandler) + subscribe(modes, "locationMode", eventHandler) + subscribe(motions, "motion", eventHandler) + subscribe(presences, "presence", eventHandler) + subscribe(relays, "relaySwitch", eventHandler) + subscribe(smokedetectors, "smokeDetector", eventHandler) + subscribe(switches, "switch", eventHandler) + subscribe(levels, "level", eventHandler) + subscribe(temperatures, "temperature", eventHandler) + subscribe(waterdetectors, "water", eventHandler) + subscribe(location, "location", eventHandler) + subscribe(accelerations, "acceleration", eventHandler) + subscribe(energymeters, "power", eventHandler) +} + +def genericHandler(evt, specificsMap) { + def body = [ + "date": evt.date, + "eventName": evt.name, + "displayName": evt.displayName, + "device": "${evt.device}", + "deviceId": "${evt.deviceId}", + "value": evt.value, + "isStateChange": evt.isStateChange(), + "id": "${evt.id}", + "description": "${evt.description}", + "descriptionText": "${evt.descriptionText}", + "installedSmartAppId": "${evt.installedSmartAppId}", + "isoDate": evt.isoDate, + "isDigital": evt.isDigital(), + "isPhysical": evt.isPhysical(), + "location": "${evt.location}", + "locationId": "${evt.locationId}", + "unit": "${evt.unit}", + "source": "${evt.source}", + "program": "SmartThings" + ] + specificsMap + + def result = new physicalgraph.device.HubAction( + method: "POST", + path: "/", + headers: [ + "HOST" : "${logstash_host}:${logstash_port}", + "Content-Type": "application/json"], + "body": body, + null, + [callback: calledBackHandler] + ) + sendHubCommand(result); +} + +void calledBackHandler(physicalgraph.device.HubResponse hubResponse) { + log.debug "Entered calledBackHandler($hubResponse)..." + log.debug "body in calledBackHandler() is: ${hubResponse.body}" +} + +def eventHandler(evt) { + def specificsMap = [:] + switch(evt.name) { + case "temperature": + case "level": + specificsMap[evt.name+"Value"] = Integer.parseInt(evt.value); + break + case "power": + specificsMap[evt.name+"Value"] = Float.parseFloat(evt.value) + break + case "switch": + specificsMap[evt.name+"Value"] = evt.value == "on"?true:false + break + case "contact": + specificsMap[evt.name+"Value"] = evt.value == "closed"?true:false + break + case "motion": + specificsMap[evt.name+"Value"] = evt.value == "active"?true:false + break } -} - -def alarmHandler(evt) { - genericHandler(evt) -} - -def coHandler(evt) { - genericHandler(evt) -} - -def indicatorHandler(evt) { - genericHandler(evt) -} - -def presenceHandler(evt) { - genericHandler(evt) -} - -def switchHandler(evt) { - genericHandler(evt) -} - -def smokeHandler(evt) { - genericHandler(evt) -} - -def levelHandler(evt) { - genericHandler(evt) -} - -def contactHandler(evt) { - genericHandler(evt) -} - -def temperatureHandler(evt) { - genericHandler(evt) -} - -def motionHandler(evt) { - genericHandler(evt) -} - -def modeHandler(evt) { - genericHandler(evt) -} - -def relayHandler(evt) { - genericHandler(evt) -} - -def waterHandler(evt) { - genericHandler(evt) -} - -def locationHandler(evt) { - genericHandler(evt) -} - -def accelerationHandler(evt) { - genericHandler(evt) -} - -def powerHandler(evt) { - genericHandler(evt) + genericHandler(evt, specificsMap) }