Modifying 'Simple Control'

This commit is contained in:
Will Price
2016-05-20 12:49:23 -05:00
parent ee4747cbca
commit f29b73b5f1

View File

@@ -37,12 +37,17 @@ preferences()
mappings { mappings {
path("/devices") { path("/devices") {
action: [
GET: "getDevices"
]
}
path("/:deviceType/devices") {
action: [ action: [
GET: "getDevices", GET: "getDevices",
POST: "handleDevicesWithIDs" POST: "handleDevicesWithIDs"
] ]
} }
path("/device/:id") { path("/device/:deviceType/:id") {
action: [ action: [
GET: "getDevice", GET: "getDevice",
POST: "updateDevice" POST: "updateDevice"
@@ -99,53 +104,40 @@ def handleDevicesWithIDs()
//log.debug("ids: ${ids}") //log.debug("ids: ${ids}")
def command = data?.command def command = data?.command
def arguments = data?.arguments def arguments = data?.arguments
def type = params?.deviceType
//log.debug("device type: ${type}")
if (command) if (command)
{ {
def success = false def statusCode = 404
//log.debug("command ${command}, arguments ${arguments}") //log.debug("command ${command}, arguments ${arguments}")
for (devId in ids) for (devId in ids)
{ {
def device = allDevices.find { it.id == devId } def device = allDevices.find { it.id == devId }
//log.debug("device: ${device}")
// Check if we have a device that responds to the specified command // Check if we have a device that responds to the specified command
if (device && device.hasCommand(command)) { if (validateCommand(device, type, command)) {
switch (command) if (arguments) {
{ device."$command"(*arguments)
case "on": }
device.on() else {
break device."$command"()
case "off": }
device.off() statusCode = 200
break
case "setLevel":
device.setLevel(*arguments)
break
case "playText":
device.playText(*arguments)
break
case "lock":
device.lock()
break
case "unlock":
device.unlock()
break
default:
httpError(400, "Command ${command} cannot be sent to the target device")
break
}
success = true
} else { } else {
//log.debug("device not found ${devId}") statusCode = 403
} }
} }
def responseData = "{}"
if (success) switch (statusCode)
{ {
render status: 200, data: "{}" case 403:
} responseData = '{"msg": "Access denied. This command is not supported by current capability."}'
else break
{ case 404:
render status: 404, data: '{"msg": "Device not found"}' responseData = '{"msg": "Device not found"}'
break
} }
render status: statusCode, data: responseData
} }
else else
{ {
@@ -190,45 +182,101 @@ 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
def type = params?.deviceType
//log.debug("device type: ${type}")
//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 statusCode = 404
def device = allDevices.find { it.id == params.id } def device = allDevices.find { it.id == params.id }
// Check if we have a device that responds to the specified command if (device) {
if (device && device.hasCommand(command)) { // Check if we have a device that responds to the specified command
switch (command) if (validateCommand(device, type, command)) {
{ if (arguments) {
case "on": device."$command"(*arguments)
device.on() }
break else {
case "off": device."$command"()
device.off() }
break statusCode = 200
case "setLevel": } else {
device.setLevel(*arguments) statusCode = 403
break
case "playText":
device.playText(*arguments)
break
case "lock":
device.lock()
break
case "unlock":
device.unlock()
break
default:
httpError(400, "Command ${command} cannot be sent to the target device")
break
} }
render status: 204, data: "{}"
} else {
render status: 404, data: '{"msg": "Device not found"}'
} }
def responseData = "{}"
switch (statusCode)
{
case 403:
responseData = '{"msg": "Access denied. This command is not supported by current capability."}'
break
case 404:
responseData = '{"msg": "Device not found"}'
break
}
render status: statusCode, data: responseData
} }
} }
/**
* Validating the command passed by the user based on capability.
* @return boolean
*/
def validateCommand(device, deviceType, command) {
//log.debug("validateCommand ${command}")
def capabilityCommands = getDeviceCapabilityCommands(device.capabilities)
//log.debug("capabilityCommands: ${capabilityCommands}")
def currentDeviceCapability = getCapabilityName(deviceType)
//log.debug("currentDeviceCapability: ${currentDeviceCapability}")
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"
case "thermostats":
return "Thermostat"
case "doorControls":
return "Door Control"
case "colorControls":
return "Color Control"
case "musicPlayers":
return "Music Player"
case "switchLevels":
return "Switch Level"
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
}
def listSubscriptions() def listSubscriptions()
{ {
//log.debug "listSubscriptions()" //log.debug "listSubscriptions()"
@@ -724,5 +772,3 @@ def List getRealHubFirmwareVersions()
} }