Compare commits

...

5 Commits

Author SHA1 Message Date
Justin Tung
5e184322a0 MSA-1356: Allow users to see information about their devices in a sexy dashboard. 2016-06-15 17:16:49 -05:00
Vinay Rao
eac48382e8 Merge pull request #993 from SmartThingsCommunity/staging
Rolling down hotfix to master
2016-06-14 15:55:53 -07:00
Rohan Desai
8cc87f3858 Merge pull request #966 from rohandesai/PENG-160
PENG-160 Alfred workflow should not allow undefined commands
2016-06-13 14:17:10 -07:00
Vinay Rao
e818695947 Merge pull request #987 from SmartThingsCommunity/staging
Roll down hotfix
2016-06-13 10:50:29 -07:00
Rohan Desai
49d293e749 PENG-160 UBI should not allow undefined commands
- added some more changes

added changes to alfred workflow
2016-06-03 15:25:50 -07:00
2 changed files with 183 additions and 9 deletions

View File

@@ -0,0 +1,109 @@
/**
* 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() {}

View File

@@ -92,22 +92,87 @@ void updateLock() {
private void updateAll(devices) {
def command = request.JSON?.command
if (command) {
devices."$command"()
def type = params.param1
if (!devices) {
httpError(404, "Devices not found")
}
if (command){
devices.each { device ->
executeCommand(device, type, command)
}
}
}
private void update(devices) {
log.debug "update, request: ${request.JSON}, params: ${params}, devices: $devices.id"
def command = request.JSON?.command
if (command) {
def device = devices.find { it.id == params.id }
if (!device) {
httpError(404, "Device not found")
} else {
device."$command"()
}
def type = params.param1
def device = devices?.find { it.id == params.id }
if (!device) {
httpError(404, "Device not found")
}
if (command) {
executeCommand(device, type, 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) {