What is the best way to do input validation on incoming data from GET APIs etc? I’ve got a very rudimentary way at present which I’m sure is very clunky…
I’ve created a new Temperature Sensor and would like to update it based on the temperature and humidity values received by the API (sounds simple enough?)
Error Messages:
error java.lang.NullPointerException: Cannot invoke method setTemperature() on null object @ line 68
error error: groovy.lang.MissingMethodException: No signature of method: cript14342832688371907786814.$() is applicable for argument types: script14342832688371907786814$_makeJSONWeatherRequest_closure2_closure4) values: script14342832688371907786814$_makeJSONWeatherRequest_closure2_closure4@16a09c76] Possible solutions: is(java.lang.Object), now(), url(), run(), any(), app(java.util.Map)
Everything comes in as a string. Groovy mostly feels like java for string processing, and also has the same try/catch things
so, doing something like this should work
def dvtemp = -1.0
def ivhumi = -1
try {
dvtemp = vtemp.toDouble();
ivhumi = vhumi.toInteger();
// still here? seems plausible. validate dvtemp and ivhumi to be within whatever range
// you accept, and if so, call the set() methods
if ((dvtemp > 30.0) && (dvtemp < 90.0)) settings.target.setTemperature(dvtemp)
} catch (e) {
// invalid data passed in. Ignore it
// DON'T exit here
}
No need to exit, your SmartApp only runs when its needed
Thanks John and Sid. Appreciate the help, and I have been able to tighten up the error checking. However, I’m still getting the following error:
error: groovy.lang.MissingMethodException: No signature of method: script1434322929919539404578.$() is applicable for argument types: (script1434322929919539404578$_makeJSONWeatherRequest_closure2_closure4) values: [script1434322929919539404578$_makeJSONWeatherRequest_closure2_closure4@6565730]
Possible solutions: is(java.lang.Object), now(), url(), run(), any(), app(java.util.Map)
It would seem that you can’t set the temperature (or humidity) on a Thermostat Sensor manually created in the IDE… Not sure what is missing? Any clues?
Thanks very much for your help. So, lesson: I was trying to update the default device which couldn’t be updated.
I’ve been able to now get the SmartApp to work with the Custom Device. The data is flowing, I need to make some tweaks to the device code as I’m using C not F.
Last question on this topic: where do you get the code for the Devices? You used the Temperature Sensor template - are they all available?
tl;dr version:
Click Create New Device Type
Select From Template
Scroll down list of template device types - if you see the device type you want to modify, choose it and modify away!