Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * SmartThings example Code for GroveStreams
  3.  * A full "how to" guide for this example can be found at
  4.  *   https://www.grovestreams.com/developers/getting_started_smartthings.html
  5.  *
  6.  * Copyright 2015 Jason Steele
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  9.  * in compliance with the License. You may obtain a copy of the License at:
  10.  *
  11.  * http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
  14.  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
  15.  * for the specific language governing permissions and limitations under the License.
  16.  *
  17.  */
  18.  
  19. definition(
  20.         name: "GroveStreams",
  21.         namespace: "JasonBSteele",
  22.         author: "Jason Steele",
  23.         description: "Log to GroveStreams",
  24.         category: "My Apps",
  25.         iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
  26.         iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
  27.         iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
  28.  
  29. preferences {
  30.  
  31.     section("Log devices...") {
  32.         input "temperatures", "capability.temperatureMeasurement", title: "Temperatures", required:false, multiple: true
  33.         input "humidities", "capability.relativeHumidityMeasurement", title: "Humidities", required: false, multiple: true
  34.         input "contacts", "capability.contactSensor", title: "Doors open/close", required: false, multiple: true
  35.         input "accelerations", "capability.accelerationSensor", title: "Accelerations", required: false, multiple: true
  36.         input "motions", "capability.motionSensor", title: "Motions", required: false, multiple: true
  37.         input "presence", "capability.presenceSensor", title: "Presence", required: false, multiple: true
  38.         input "switches", "capability.switch", title: "Switches", required: false, multiple: true
  39.         input "waterSensors", "capability.waterSensor", title: "Water sensors", required: false, multiple: true
  40.         input "batteries", "capability.battery", title: "Batteries", required:false, multiple: true
  41.         input "powers", "capability.powerMeter", title: "Power Meters", required:false, multiple: true
  42.         input "energies", "capability.energyMeter", title: "Energy Meters", required:false, multiple: true
  43.     }
  44.  
  45.     section ("GroveStreams Feed PUT API key...") {
  46.         input "apiKey", "text", title: "API key"
  47.     }
  48. }
  49.  
  50. def installed() {
  51.     initialize()
  52. }
  53.  
  54. def updated() {
  55.     unsubscribe()
  56.     initialize()
  57. }
  58.  
  59. def initialize() {
  60.  
  61.     subscribe(temperatures, "temperature", handleTemperatureEvent)
  62.     subscribe(waterSensors, "water", handleWaterEvent)
  63.     subscribe(humidities, "humidity", handleHumidityEvent)
  64.     subscribe(contacts, "contact", handleContactEvent)
  65.     subscribe(accelerations, "acceleration", handleAccelerationEvent)
  66.     subscribe(motions, "motion", handleMotionEvent)
  67.     subscribe(presence, "presence", handlePresenceEvent)
  68.     subscribe(switches, "switch", handleSwitchEvent)
  69.     subscribe(batteries, "battery", handleBatteryEvent)
  70.     subscribe(powers, "power", handlePowerEvent)
  71.     subscribe(energies, "energy", handleEnergyEvent)
  72. }
  73.  
  74. def handleTemperatureEvent(evt) {
  75.     sendValue(evt) { it.toString() }
  76. }
  77.  
  78. def handleWaterEvent(evt) {
  79.     sendValue(evt) { it == "wet" ? "true" : "false" }
  80. }
  81.  
  82. def handleHumidityEvent(evt) {
  83.     sendValue(evt) { it.toString() }
  84. }
  85.  
  86. def handleContactEvent(evt) {
  87.     sendValue(evt) { it == "open" ? "true" : "false" }
  88. }
  89.  
  90. def handleAccelerationEvent(evt) {
  91.     sendValue(evt) { it == "active" ? "true" : "false" }
  92. }
  93.  
  94. def handleMotionEvent(evt) {
  95.     sendValue(evt) { it == "active" ? "true" : "false" }
  96. }
  97.  
  98. def handlePresenceEvent(evt) {
  99.     sendValue(evt) { it == "present" ? "true" : "false" }
  100. }
  101.  
  102. def handleSwitchEvent(evt) {
  103.     sendValue(evt) { it == "on" ? "true" : "false" }
  104. }
  105.  
  106. def handleBatteryEvent(evt) {
  107.     sendValue(evt) { it.toString() }
  108. }
  109.  
  110. def handlePowerEvent(evt) {
  111.     sendValue(evt) { it.toString() }
  112. }
  113.  
  114. def handleEnergyEvent(evt) {
  115.     sendValue(evt) { it.toString() }
  116. }
  117.  
  118. private sendValue(evt, Closure convert) {
  119.     def compId = URLEncoder.encode(evt.displayName.trim())
  120.     def streamId = evt.name
  121.     def value = convert(evt.value)
  122.    
  123.     log.debug "Logging to GroveStreams ${compId}, ${streamId} = ${value}"
  124.  
  125.     def url = "https://grovestreams.com/api/feed?api_key=${apiKey}&compId=${compId}&${streamId}=${value}"
  126.  
  127.     //Make the actual device the origin of the message to avoid exceeding 12 calls within 2 minutes rule:
  128.     //http://forum.grovestreams.com/topic/155/10-second-feed-put-limit-algorithm-change/
  129.     def header = ["X-Forwarded-For": evt.deviceId]
  130.  
  131.     def putParams = [
  132.         uri: url,
  133.         header: header,
  134.         body: []]
  135.  
  136.     httpPut(putParams) { response ->
  137.         if (response.status != 200 ) {
  138.             log.debug "GroveStreams logging failed, status = ${response.status}"
  139.         }
  140.  
  141.     }
  142.  
  143. }