Skip to content

Instantly share code, notes, and snippets.

@nonoo
Created March 23, 2018 12:47
Show Gist options
  • Save nonoo/c0210411d5a79096207f3208c5ca7782 to your computer and use it in GitHub Desktop.
Save nonoo/c0210411d5a79096207f3208c5ca7782 to your computer and use it in GitHub Desktop.
/**
* HTTP GET on button press
*
* Copyright 2018 Norbert Varga <nonoo@nonoo.hu>
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
definition(
name: "HTTP GET on button press",
namespace: "nonoo",
author: "Norbert Varga",
description: "Run HTTP GET when a button is pressed.",
category: "My Apps",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png") {}
preferences {
section("Which buttons to use?") {
input "buttons", "capability.button", multiple: true
}
input("host", "string", title: "HTTP GET host", description: "", required: true, displayDuringSetup: true)
input("port", "number", title: "HTTP GET port", description: "", required: false, displayDuringSetup: true)
input("path", "string", title: "HTTP GET path", description: "ex. /index.php?a=1", required: false, displayDuringSetup: true)
input("localcall", "bool", title: "Run HTTP GET locally?", description: "", required: false, displayDuringSetup: true)
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
subscribe(buttons, "buttonClicks", buttonclick)
}
def buttonclick(evt) {
log.debug "evt.value: $evt.value"
def useport
if (port != null && port != "") {
useport = port
} else {
useport = 80
}
def uselocalcall
if (localcall != null) {
uselocalcall = localcall
} else {
uselocalcall = true
}
log.debug "calling host: $host port: $useport path: $path locally: $uselocalcall"
def hubAction = new physicalgraph.device.HubAction(
method: "GET",
path: "http://$host:$useport$path",
headers: [HOST:"$host:$useport"]
)
if (!uselocalcall) {
hubAction.options = [outputMsgToS3:true]
}
sendHubCommand(hubAction)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment