Return data from parent app to child driver

bingo ! thanks

def ArmDisRef(mode){
	log.trace "Incoming Mode CMD ${mode.value} "
    def respdata = ""
	def paramsMode = [
			uri: "https://mob.yalehomesystem.co.uk/yapi/api/panel/mode/",
			body: [area: 1, mode: "${mode.value}"],
			headers: ['Authorization' : "Bearer ${state.Token}"],
			requestContentType: "application/x-www-form-urlencoded", //in event of error// requestContentType: "application/json",
			contentType: "application/json"
	]
    try{
	httpPost(paramsMode) {	response ->
    	def respstatus = response?.status
        respdata = response?.data
		if (respstatus == 200){
            def respmsg = response?.data?.message
            if (state.errorCount != 0) {
				state.errorCount = 0
			}
            if (state.currentError != null) {
				state.currentError = null
            }
            log.info "Mode $mode - '$respstatus' - '$respmsg'" // $respdata" 
            
            
            if (respmsg != 'OK!'){
               	send("Alarm mode change to '$mode' issue, message $respmsg") //if door left open
            }   
		}
		else { //response status not 200
        	log.error "Error in MODE '$respstatus' to $mode, ${state.currentError} - $respdata"
			state.currentError = "error mode pannel $respstatus - $respdata"
			respdata = 'error'
            errorhand("Error Arm/Dis/Ref NOT 200")
		}
	}
    }
    catch (e){
    	log.error "Error arm/dis/Ref: ${e}, ${e?.message}"
        state.currentError = e?.message
        errorhand("Error Arm/Dis/Ref Catch")
    }
    return respdata
}
2 Likes

I was referring to the closure for the httpPost command, however the same principle applies to exception handler (try/catch) blocks. Here's a few examples...

httpPost(paramsMode) {	response ->
  def someVariable = "Test 123"
  log.trace someVariable // This outputs Test 123
}
log.trace someVariable // Outputs nothing because it was defined within the closure.

Here's an example, taking the definition up a level and adding an exception handler...

try {
  def someVariable = "Test 123"
  httpPost(paramsMode) {	response ->
    log.trace someVariable // This outputs Test 123 because it's in scope of the try block
  }
  log.trace someVariable // This outputs Test 123 because it's in scope of the try block
} catch (e) {
  log.trace someVariable  // Outputs nothing because it was defined within the try block.
}
log.trace someVariable // Outputs nothing because it was defined within the try block.

And finally where I suggested he define it...

def someVariable = "Test 123"
try {
  httpPost(paramsMode) {	response ->
    log.trace someVariable // This outputs Test 123 because it's in scope of the method
  }
  log.trace someVariable // This outputs Test 123 because it's in scope of the method
} catch (e) {
  log.trace someVariable // This outputs Test 123 because it's in scope of the method.
}
log.trace someVariable // This outputs Test 123 because it's in scope of the method

Hopefully this helps to clarify how scoping can raise unexpected issues like this..

1 Like

AH HA!!! That makes sense.

Having that in Camelcase wouldn't affect that, would it? (I'm still not 100% clear on when that is necessary and when it isn't. If one doesn't work I just try the other. )

Not in terms of scoping. But groovy is case sensitive, so for example;

def someVar
def SomeVar

Are two unique variables.

1 Like