mirror of
https://github.com/mtan93/SmartThingsPublic.git
synced 2026-03-31 14:13:11 +01:00
Compare commits
1 Commits
MSA-1356-1
...
MSA-1358-1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
777f8f7e20 |
@@ -1,109 +0,0 @@
|
|||||||
/**
|
|
||||||
* Freeboard
|
|
||||||
*
|
|
||||||
* Copyright 2016 Justin Tung
|
|
||||||
*
|
|
||||||
* 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: "Freeboard",
|
|
||||||
namespace: "JTung1104",
|
|
||||||
author: "Justin Tung",
|
|
||||||
description: "See all the information from all your smart devices in one sexy dashboard.",
|
|
||||||
category: "Convenience",
|
|
||||||
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",
|
|
||||||
oauth: [displayName: "", displayLink: "www.freeboard.io"])
|
|
||||||
|
|
||||||
|
|
||||||
preferences {
|
|
||||||
section ("Allow external service to control these things...") {
|
|
||||||
input "switches", "capability.switch", multiple: true, required: true
|
|
||||||
input "motionSensors", "capability.motionSensor", required: true, multiple: true
|
|
||||||
input "contactSensors", "capability.contactSensor", required: true, multiple: true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def installed() {
|
|
||||||
log.debug "Installed with settings: ${settings}"
|
|
||||||
|
|
||||||
initialize()
|
|
||||||
}
|
|
||||||
|
|
||||||
def updated() {
|
|
||||||
log.debug "Updated with settings: ${settings}"
|
|
||||||
|
|
||||||
unsubscribe()
|
|
||||||
initialize()
|
|
||||||
}
|
|
||||||
|
|
||||||
def initialize() {
|
|
||||||
// TODO: subscribe to attributes, devices, locations, etc.
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: implement event handlers
|
|
||||||
|
|
||||||
mappings {
|
|
||||||
path("/switches") {
|
|
||||||
action: [
|
|
||||||
GET: "listSwitches"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def listSwitches() {
|
|
||||||
def resp = []
|
|
||||||
|
|
||||||
switches.each {
|
|
||||||
def result = []
|
|
||||||
|
|
||||||
it.supportedAttributes.each { attr ->
|
|
||||||
result << it["${attr}State"]
|
|
||||||
}
|
|
||||||
|
|
||||||
resp << [name: it.displayName, id: it.id, label: it.label, states: result]
|
|
||||||
}
|
|
||||||
|
|
||||||
motionSensors.each {
|
|
||||||
def result = []
|
|
||||||
|
|
||||||
it.supportedAttributes.each { attr ->
|
|
||||||
result << it["${attr}State"]
|
|
||||||
}
|
|
||||||
|
|
||||||
resp << [name: it.displayName, states: result, id: it.id, label: it.label]
|
|
||||||
}
|
|
||||||
|
|
||||||
temperatureSensors.each {
|
|
||||||
def result = []
|
|
||||||
|
|
||||||
it.supportedAttributes.each { attr ->
|
|
||||||
result << it["${attr}State"]
|
|
||||||
}
|
|
||||||
|
|
||||||
resp << [name: it.displayName, temperature: it.temperatureState, id: it.id, label: it.label]
|
|
||||||
}
|
|
||||||
|
|
||||||
contactSensors.each {
|
|
||||||
def result = []
|
|
||||||
|
|
||||||
it.supportedAttributes.each { attr ->
|
|
||||||
result << it["${attr}State"]
|
|
||||||
}
|
|
||||||
|
|
||||||
resp << [name: it.displayName, temperature: it.temperatureState, id: it.id, label: it.label]
|
|
||||||
}
|
|
||||||
|
|
||||||
resp
|
|
||||||
}
|
|
||||||
|
|
||||||
def updateSwitches() {}
|
|
||||||
@@ -658,29 +658,73 @@ def updateDevice() {
|
|||||||
def data = request.JSON
|
def data = request.JSON
|
||||||
def command = data.command
|
def command = data.command
|
||||||
def arguments = data.arguments
|
def arguments = data.arguments
|
||||||
|
|
||||||
log.debug "updateDevice, params: ${params}, request: ${data}"
|
log.debug "updateDevice, params: ${params}, request: ${data}"
|
||||||
if (!command) {
|
if (!command) {
|
||||||
render status: 400, data: '{"msg": "command is required"}'
|
render status: 400, data: '{"msg": "command is required"}'
|
||||||
} else {
|
} else {
|
||||||
def device = allDevices.find { it.id == params.id }
|
def device = allDevices.find { it.id == params.id }
|
||||||
if (device) {
|
if (device) {
|
||||||
if (device.hasCommand("$command")) {
|
if (validateCommand(device, command)) {
|
||||||
if (arguments) {
|
if (arguments) {
|
||||||
device."$command"(*arguments)
|
device."$command"(*arguments)
|
||||||
} else {
|
} else {
|
||||||
device."$command"()
|
device."$command"()
|
||||||
}
|
}
|
||||||
render status: 204, data: "{}"
|
render status: 204, data: "{}"
|
||||||
} else {
|
} else {
|
||||||
render status: 404, data: '{"msg": "Command not supported by this Device"}'
|
render status: 403, data: '{"msg": "Access denied. This command is not supported by current capability."}'
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
render status: 404, data: '{"msg": "Device not found"}'
|
render status: 404, data: '{"msg": "Device not found"}'
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validating the command passed by the user based on capability.
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
def validateCommand(device, command) {
|
||||||
|
def capabilityCommands = getDeviceCapabilityCommands(device.capabilities)
|
||||||
|
def currentDeviceCapability = getCapabilityName(device)
|
||||||
|
if (currentDeviceCapability != "" && capabilityCommands[currentDeviceCapability]) {
|
||||||
|
return command in capabilityCommands[currentDeviceCapability] ? true : false
|
||||||
|
} else {
|
||||||
|
// Handling other device types here, which don't accept commands
|
||||||
|
httpError(400, "Bad request.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Need to get the attribute name to do the lookup. Only
|
||||||
|
* doing it for the device types which accept commands
|
||||||
|
* @return attribute name of the device type
|
||||||
|
*/
|
||||||
|
def getCapabilityName(device) {
|
||||||
|
def capName = ""
|
||||||
|
if (switches.find{it.id == device.id})
|
||||||
|
capName = "Switch"
|
||||||
|
else if (alarms.find{it.id == device.id})
|
||||||
|
capName = "Alarm"
|
||||||
|
else if (locks.find{it.id == device.id})
|
||||||
|
capName = "Lock"
|
||||||
|
log.trace "Device: $device - Capability Name: $capName"
|
||||||
|
return capName
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructing the map over here of
|
||||||
|
* supported commands by device capability
|
||||||
|
* @return a map of device capability -> supported commands
|
||||||
|
*/
|
||||||
|
def getDeviceCapabilityCommands(deviceCapabilities) {
|
||||||
|
def map = [:]
|
||||||
|
deviceCapabilities.collect {
|
||||||
|
map[it.name] = it.commands.collect{ it.name.toString() }
|
||||||
|
}
|
||||||
|
return map
|
||||||
|
}
|
||||||
|
|
||||||
def listSubscriptions() {
|
def listSubscriptions() {
|
||||||
log.debug "listSubscriptions()"
|
log.debug "listSubscriptions()"
|
||||||
app.subscriptions?.findAll { it.device?.device && it.device.id }?.collect {
|
app.subscriptions?.findAll { it.device?.device && it.device.id }?.collect {
|
||||||
|
|||||||
Reference in New Issue
Block a user