I'm working on a driver for a Samsung sound bar and I'm trying to use asynchronous HTTP GET calls to pull data from the device. My refresh() method fires off calls to retrieve the current volume level and current input back-to-back:
def refresh() {
sendCommand("getInput")
sendCommand("getVolume")
}
The method that translates these calls into the async requests is as follows:
private sendCommand(type, value = null) {
if (type == "setInput") {
command = "UIC?cmd=%3Cname%3ESetFunc%3C/name%3E%3Cp%20type=%22str%22%20name=%22function%22%20val=%22${value}%22/%3E"
} else if (type == "setVolume") {
command = "UIC?cmd=%3Cname%3ESetVolume%3C/name%3E%3Cp%20type=%22dec%22%20name=%22volume%22%20val=%22${value}%22/%3E"
} else if (type == "getVolume") {
command = "UIC?cmd=%3Cname%3EGetVolume%3C/name%3E"
} else if (type == "getInput") {
command = "UIC?cmd=%3Cname%3EGetFunc%3C/name%3E"
}
def params = [
uri: "http://${ipaddr}:56001/${command}",
]
log.debug("${type} - sendCommand: ${params['uri']}")
if (type == "getVolume") {
asynchttpGet('processGetVolumeResponse', params, [requestType: type])
} else if (type == "getInput") {
asynchttpGet('processGetInputResponse', params, [requestType: type])
} else {
asynchttpGet('processCommandResponse', params, [requestType: type])
}
}
And finally, the callback methods:
private processGetVolumeResponse(response, data) {
def xml = response.getXml()
log.debug(data['requestType'] + " - XML - " + xml)
soundbarVolume = xml.response.volume
hubitatVolume = device.currentValue("currentVolume")
log.debug(data['requestType'] + " - " + "Bar: ${soundbarVolume}")
log.debug(data['requestType'] + " - " + "Hub: ${hubitatVolume}")
if (soundbarVolume != hubitatVolume) {
log.debug(data['requestType'] + " - MISMATCH!")
}
}
private processGetInputResponse(response, data) {
def xml = response.getXml()
log.debug(data['requestType'] + " - XML - " + xml)
soundbarInput = xml.response.input
hubitatInput = device.currentValue("currentInput")
log.debug(data['requestType'] + " - " + "Bar: ${soundbarInput}")
log.debug(data['requestType'] + " - " + "Hub: ${hubitatInput}")
if (soundbarInput != hubitatInput) {
log.debug(data['requestType'] + " - MISMATCH!")
}
}
When the refresh() method is called, each time both callbacks seem to get the same XML data passed to them. Sometimes it's the volume data, other times it's the input data. Here are two runs of refresh() about 13 seconds apart.
The lines with "- XML -" in them show that for each call of refresh(), both callbacks are working with the same data. In the first screenshot they both get a copy of the input data. In the second, they're both given the volume data.
Is this a scoping issue on my part? Is there something else I need to be doing to separate these calls better?
Any insight would be much appreciated.