Skip to content

Instantly share code, notes, and snippets.

@halkeye
Created July 2, 2014 23:26
Show Gist options
  • Save halkeye/e442f7da5deec96016a2 to your computer and use it in GitHub Desktop.
Save halkeye/e442f7da5deec96016a2 to your computer and use it in GitHub Desktop.
import grails.converters.JSON
// Automatically generated. Make future change here.
definition(
name: "gavin_test_pushes",
namespace: "gavin",
author: "Gavin",
description: "Description goes here",
category: "Fun & Social",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png",
oauth: [displayName: "Gavin Test", displayLink: ""]
)
preferences {
section("Monitor") {
input "switches",
"capability.switch",
title: "Switches",
multiple: true,
description: "Which Switches to manage?"
input "sensors",
"capability.sensor",
title:"Sensors",
multiple:true,
description:"Which sensor?"
input "webPresence",
"capability.presenceSensor",
title:"Web Presence",
description:"Which sensor?"
}
}
mappings {
path("/location") {
action: [
GET: "showLocation"
]
}
path("/devices") {
action: [
GET: "listDevices"
]
}
path("/devices/:id") {
action: [
GET: "showDevice"
]
}
path("/switches/:id/:command/:attribute") {
action: [
GET: "updateSwitch"
]
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
// Helper Functions
def all_devices() {
return settings.switches + settings.sensors + settings.webPresence
}
private device_json(it) {
if (!it) { return null }
def values = [:]
def attrs = [:]
for (a in it.supportedAttributes) {
values[a.name] = it.currentState(a.name)
attrs[a.name] = [datatype: a.dataType, name: a.name, values: a.values]
}
return [capabilities: it.capabilities.collect { it.preferenceName },
label: it.displayName,
name: it.name,
id: it.id,
values: values,
attrs: attrs]
}
// locations
def showLocation() {
return [
"name": location.name,
"id": location.id,
"latitude": location.latitude,
"longitude": location.longitude,
"mode": location.mode,
"timezone": location.timeZone ? location.timeZone.ID : location.timeZone
]
}
// devices
def listDevices() {
return all_devices().collect{device_json(it)}
}
def showDevice() {
def device = all_devices().find { it.id == params.id }
if (!device) {
httpError(404, "Device not found")
return
}
return device_json(device)
}
// switches
def updateSwitch() {
update(all_devices())
}
def deviceChanged(evt) {
final PUSH_HOSTNAME = "-URL_GOES_HERE_"
def url = PUSH_HOSTNAME + "handler/smartthings"
log.debug "Pushing to ${url}"
def data = [:]
data['name'] = evt.name;
data['deviceId'] = evt.deviceId;
data['isStateChange'] = evt.isStateChange()
data['isPhysical'] = evt.isPhysical()
data['description'] = evt.description
data['descriptionText'] = evt.descriptionText
data['displayName'] = evt.displayName
data['date'] = evt.isoDate
// source
data['values[name]'] = evt.name
data['values[value'] = evt.value
data['values[unit'] = evt.unit
log.trace "temperatureChagne, evt: ${evt} ---, settings: ||$data||"
def successClosure = { response ->
log.debug "Request was successful, ${response.getData()}"
}
// TODO: subscribe to attributes, devices, locations, etc.
def params = [
uri: url,
success: successClosure,
body: data
]
httpPost(params)
}
def event(evt) {
log.trace "evt, evt: [${evt}], settings: [${settings}]"
}
def initialize() {
log.debug "all_devices: ${all_devices}"
for ( item in all_devices() ) {
log.debug "item: ${item.name}"
for (c in item.supportedAttributes) {
log.debug "Subscribing to ${item.name} => ${c.name}"
subscribe(item, c.name, deviceChanged )
}
}
}
private update(devices) {
log.debug "update, request: params: ${params}, devices: $devices.id"
//def attr = request.JSON?.attribute
def command = params.command
def attr = params.attribute
if (!attr) { attr = "switch"; }
//let's create a toggle option here
if (!command) {
httpError(404, "Command found")
return
}
def device = devices.find { it.id == params.id }
if (!device) {
httpError(404, "Device not found")
return
}
if(command == "toggle")
{
if(device.currentValue(attr) == "on") {
device.off();
command = "off";
} else {
device.on();
command = "on";
}
}
else
{
device."$command"()
}
return [(attr): command ]
// FIXME - state doesn't update quick enough
//return [ state: device.currentValue('switch') ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment