mirror of
https://github.com/mtan93/SmartThingsPublic.git
synced 2026-03-20 05:10:51 +00:00
Compare commits
1 Commits
MSA-1356-1
...
PROD_2016.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1a910f11f |
@@ -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() {}
|
|
||||||
@@ -92,89 +92,24 @@ void updateLock() {
|
|||||||
|
|
||||||
private void updateAll(devices) {
|
private void updateAll(devices) {
|
||||||
def command = request.JSON?.command
|
def command = request.JSON?.command
|
||||||
def type = params.param1
|
if (command) {
|
||||||
if (!devices) {
|
devices."$command"()
|
||||||
httpError(404, "Devices not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (command){
|
|
||||||
devices.each { device ->
|
|
||||||
executeCommand(device, type, command)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void update(devices) {
|
private void update(devices) {
|
||||||
log.debug "update, request: ${request.JSON}, params: ${params}, devices: $devices.id"
|
log.debug "update, request: ${request.JSON}, params: ${params}, devices: $devices.id"
|
||||||
def command = request.JSON?.command
|
def command = request.JSON?.command
|
||||||
def type = params.param1
|
|
||||||
def device = devices?.find { it.id == params.id }
|
|
||||||
|
|
||||||
if (!device) {
|
|
||||||
httpError(404, "Device not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (command) {
|
if (command) {
|
||||||
executeCommand(device, type, command)
|
def device = devices.find { it.id == params.id }
|
||||||
|
if (!device) {
|
||||||
|
httpError(404, "Device not found")
|
||||||
|
} else {
|
||||||
|
device."$command"()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Validating the command passed by the user based on capability.
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
def validateCommand(device, deviceType, command) {
|
|
||||||
def capabilityCommands = getDeviceCapabilityCommands(device.capabilities)
|
|
||||||
def currentDeviceCapability = getCapabilityName(deviceType)
|
|
||||||
if (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(type) {
|
|
||||||
switch(type) {
|
|
||||||
case "switches":
|
|
||||||
return "Switch"
|
|
||||||
case "locks":
|
|
||||||
return "Lock"
|
|
||||||
default:
|
|
||||||
return type
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates and executes the command
|
|
||||||
* on the device or devices
|
|
||||||
*/
|
|
||||||
def executeCommand(device, type, command) {
|
|
||||||
if (validateCommand(device, type, command)) {
|
|
||||||
device."$command"()
|
|
||||||
} else {
|
|
||||||
httpError(403, "Access denied. This command is not supported by current capability.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private show(devices, name) {
|
private show(devices, name) {
|
||||||
def device = devices.find { it.id == params.id }
|
def device = devices.find { it.id == params.id }
|
||||||
if (!device) {
|
if (!device) {
|
||||||
|
|||||||
Reference in New Issue
Block a user