Parent function to return settings/state data to child app

Continuing the discussion from here: Parent/child app (post #59 - 63)


UPDATE -- Resolved :smiley: , see post 4 below for the working parent function.


I'm trying to get variable data from a parent app into a child app for the child app's logic to use. For the most part, I have the function below working, except I have a parent variable that is a date. The function returns it as a string. I'm trying to find a way to detect that the string should be a date and cast it/return it as such. Any help would be appreciated (or if you know a better way that is also appreciated).

Parent function:

def returnVar(var) {
    def dataType = "String"
    def returnValue
    if (!(settings."${var}" == null)) { returnValue = settings."${var}" }
    if (!(state."${var}" == null)) { returnValue = state."${var}" }
	if (!(atomicState."${var}" == null)) { returnValue = atomicState."${var}" }
    if (returnValue =~ "dddd-dd-ddTdd:.*") { dataType = "Date" }
    if (returnValue == "true") { dataType = "Boolean" }
    if (returnValue == "false") { dataType = "Boolean" }
    if (returnValue == true) { dataType = "Boolean" }
    if (returnValue == false) { dataType = "Boolean" }
    LOGDEBUG ("returnVar(${var}), DataType:${dataType}, Value: ${returnValue}")
    if (returnValue == null || returnValue == "") {}
    try {
	    returnValue = returnValue."to${dataType}" //becomes .toString , .toBoolean , etc
        // I cannot find a .toDate() or similar method to cast to date/datetime class
    } catch (e) {
    }
    return returnValue
}

Example calling from the child app:

def parentSpeechDeviceType = parent.returnVar("speechDeviceType")
// settings.speechDeviceType in the parent app = "capability.musicPlayer" so parentSpeechDeviceType will become a string:  "capability.musicPlayer"

The issue I have is with date/time strings being returned and not being able to evaluate them with timeOfDayIsBetween()

timeOfDayIsBetween(parent.returnVar("defaultStartTime"), parent.returnVar("defaultEndTime"), now, location.timeZone)

This ends up throwing the following error:

errorgroovy.lang.MissingMethodException: No signature of method: app1528955256796245835663.timeOfDayIsBetween() is applicable for argument types: (java.lang.String, java.lang.String, java.util.Date, sun.util.calendar.ZoneInfo) values: [2018-06-14T06:00:00.000-0500, 2018-06-14T23:50:00.000-0500, ...] 

I think the issue is that I need to somehow get parent.returnVar("defaultStartTime") and parent.returnVar("defaultEndTime") to be returned as a date/datetime data type.

How many of the Parent’s state variables do you need to access? Wouldn’t it be simpler to just write a method for each one of them that returns the correct data type?

If that isn’t practical, how about a generic method for each data type that you want returned?

There's probably 80 or so references to parent variables used in the child logic. I'm attempting to gather those as needed for processing in an effort to convert code to Hubitat. In the other platform I am able to just access the parent variable directly from the child with parent?.settings?.defaultStartTime but that's not working here and I was instructed that using a method would work, so I'm trying to convert with minimal modifications.

I'm just trying to call a single method in the parent app that would gather the data requested from the parents settings and/or states and return that for the child to use. So far it's all working except for the date being returned as a string.

Once I get this issue ironed out, I could see possibly returning a map for the child to use as needed containing the parent variable type (setting, state, atomicState), parent variable value, and parent variable data/class type (string, boolean, date, etc).

1 Like

Got it!!

def returnVar(var) {
    def dataType = "String"
    def returnValue
    if (!(settings."${var}" == null)) { returnValue = settings."${var}" }
    if (!(state."${var}" == null)) { returnValue = state."${var}" }
	if (!(atomicState."${var}" == null)) { returnValue = atomicState."${var}" }
    def dateTest = returnValue =~ /\d\d\d\d-\d\d-\d\dT\d\d:/
    if (dateTest) { dataType = "Date" }
    if (returnValue == "true") { dataType = "Boolean" }
    if (returnValue == "false") { dataType = "Boolean" }
    if (returnValue == true) { dataType = "Boolean" }
    if (returnValue == false) { dataType = "Boolean" }
    if (dataType == "Date") {returnValue = Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ", returnValue)}
    LOGDEBUG ("returnVar(${var}), DataType:${dataType}, Value: ${returnValue}")
    if (returnValue == null || returnValue == "") {}
    return returnValue
}
3 Likes