mirror of
https://github.com/mtan93/SmartThingsPublic.git
synced 2026-03-18 05:10:52 +00:00
Compare commits
6 Commits
PROD_2016.
...
MSA-1370-1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b285ec93b | ||
|
|
777f8f7e20 | ||
|
|
eac48382e8 | ||
|
|
8cc87f3858 | ||
|
|
e818695947 | ||
|
|
49d293e749 |
@@ -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 {
|
||||||
@@ -780,17 +824,51 @@ def deviceHandler(evt) {
|
|||||||
|
|
||||||
def sendToHarmony(evt, String callbackUrl) {
|
def sendToHarmony(evt, String callbackUrl) {
|
||||||
def callback = new URI(callbackUrl)
|
def callback = new URI(callbackUrl)
|
||||||
def host = callback.port != -1 ? "${callback.host}:${callback.port}" : callback.host
|
if(isIP(callback.host)){
|
||||||
def path = callback.query ? "${callback.path}?${callback.query}".toString() : callback.path
|
def host = callback.port != -1 ? "${callback.host}:${callback.port}" : callback.host
|
||||||
sendHubCommand(new physicalgraph.device.HubAction(
|
def path = callback.query ? "${callback.path}?${callback.query}".toString() : callback.path
|
||||||
method: "POST",
|
sendHubCommand(new physicalgraph.device.HubAction(
|
||||||
path: path,
|
method: "POST",
|
||||||
headers: [
|
path: path,
|
||||||
"Host": host,
|
headers: [
|
||||||
"Content-Type": "application/json"
|
"Host": host,
|
||||||
],
|
"Content-Type": "application/json"
|
||||||
body: [evt: [deviceId: evt.deviceId, name: evt.name, value: evt.value]]
|
],
|
||||||
))
|
body: [evt: [deviceId: evt.deviceId, name: evt.name, value: evt.value]]
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
def params = [
|
||||||
|
uri: callbackUrl,
|
||||||
|
body: [evt: [deviceId: evt.deviceId, name: evt.name, value: evt.value]]
|
||||||
|
]
|
||||||
|
try {
|
||||||
|
log.debug "Sending data to Harmony Cloud: $params"
|
||||||
|
httpPostJson(params) { resp ->
|
||||||
|
log.debug "Harmony Cloud - Response: ${resp.status}"
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log.error "Harmony Cloud - Something went wrong: $e"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isIP(String str) {
|
||||||
|
try {
|
||||||
|
String[] parts = str.split("\\.");
|
||||||
|
if (parts.length != 4) return false;
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
int p
|
||||||
|
try {
|
||||||
|
p = Integer.parseInt(parts[i]);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (p > 255 || p < 0) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def listHubs() {
|
def listHubs() {
|
||||||
|
|||||||
@@ -92,21 +92,86 @@ void updateLock() {
|
|||||||
|
|
||||||
private void updateAll(devices) {
|
private void updateAll(devices) {
|
||||||
def command = request.JSON?.command
|
def command = request.JSON?.command
|
||||||
if (command) {
|
def type = params.param1
|
||||||
devices."$command"()
|
if (!devices) {
|
||||||
|
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) {
|
||||||
def device = devices.find { it.id == params.id }
|
executeCommand(device, type, command)
|
||||||
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.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user