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 https://www.grovestreams.com/developers/getting_started_smartthings.html
  4.  *
  5.  * Copyright 2015 Jason Steele
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  8.  * in compliance with the License. You may obtain a copy of the License at:
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
  13.  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
  14.  * for the specific language governing permissions and limitations under the License.
  15.  *
  16.  */
  17.  
  18. definition(
  19.         name: "GroveStreams QuickStart",
  20.         namespace: "JasonBSteele",
  21.         author: "Jason Steele",
  22.         description: "Log to GroveStreams",
  23.         category: "My Apps",
  24.         iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
  25.         iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
  26.         iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
  27.  
  28. preferences {
  29.     section("Log devices...") {
  30.         input "contacts", "capability.contactSensor", title: "Doors open/close", required: false, multiple: true
  31.         input "temperatures", "capability.temperatureMeasurement", title: "Temperatures", required:false, multiple: true
  32.     }
  33.  
  34.     section ("GroveStreams Feed PUT API key...") {
  35.         input "channelKey", "text", title: "API key"
  36.     }
  37. }
  38.  
  39. def installed() {
  40.     initialize()
  41. }
  42.  
  43. def updated() {
  44.     unsubscribe()
  45.     initialize()
  46. }
  47.  
  48. def initialize() {
  49.     subscribe(temperatures, "temperature", handleTemperatureEvent)
  50.     subscribe(contacts, "contact", handleContactEvent)
  51. }
  52.  
  53. def handleTemperatureEvent(evt) {
  54.     sendValue(evt) { it.toString() }
  55. }
  56.  
  57. def handleContactEvent(evt) {
  58.     sendValue(evt) { it == "open" ? "true" : "false" }
  59. }
  60.  
  61. private sendValue(evt, Closure convert) {
  62.     def compId = URLEncoder.encode(evt.displayName.trim())
  63.     def streamId = evt.name
  64.     def value = convert(evt.value)
  65.    
  66.     log.debug "Logging to GroveStreams ${compId}, ${streamId} = ${value}"
  67.    
  68.     def url = "https://grovestreams.com/api/feed?api_key=${channelKey}&compId=${compId}&${streamId}=${value}"
  69.     def putParams = [
  70.         uri: url,
  71.         body: []]
  72.  
  73.     httpPut(putParams) { response ->
  74.         if (response.status != 200 ) {
  75.             log.debug "GroveStreams logging failed, status = ${response.status}"
  76.         }
  77.     }
  78. }