RBoy
(www.rboyapps.com - Making SmartThings Easy!)
1
I"m trying to figure out how to convert a hostname to an ipaddress in ST.
The groovy code would be:
InetAddress.getByName(hostname).address.collect { it & 0xFF }.join('.')
however with ST platform one cannot access the static object InetAddress. Anyone have any ideas?
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
2
Quick possibility ⌠this site has info on how to get a JSON response from their server; looks promising to me, but I havenât worked much with SmartThingsâs http calls yet. â Oops, this particular reference does the reverse of what you are looking for, I think. But Iâll be the opposite exists, of course, an ânslookupâ of some sort. Iâll add a new Reply⌠⌠( Convert hostname to IP Address )
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
3
How about this one? There probably is a frequency limit, but maybe it does the trick for a reasonable number of accesses? Basically it is a REST-API to the dig command, right?
RBoy
(www.rboyapps.com - Making SmartThings Easy!)
4
interesting but I donât think these will work with ST, they will have async reponses with ST where as Iâm trying to see if there is a synchronous option, the apps will convert the hostname into IP rather than deferring the action.
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
5
Forgive me; not sure I understand the difference in this context, but my experience is weak.
Arenât many SmartApps doing REST-API calls to servers that have similar, umm, âasync responsesâ; for example weather lookup, NEST status lookup, ?
RBoy
(www.rboyapps.com - Making SmartThings Easy!)
6
Thatâs correct the REST response are async which mean the response will be sent back by the server at a point, the smart app will âparseâ it and manage it. Where Iâm trying to see if this can be done real time because the app canât defer the action.
RBoy
(www.rboyapps.com - Making SmartThings Easy!)
7
@duncan, @tslagle13, @minollo - I guess itâs a limitation of the ST platform not being able to access static classes (unless Iâm wrong), is there anything else available you may be aware of?
1 Like
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
8
Ah⌠OK.
Can it be near real-time? i.e., busy-wait or block for a âshortâ time to wait for the reply? Even a synchronous call is not instantaneous, right?
RBoy
(www.rboyapps.com - Making SmartThings Easy!)
9
right but with sync - ie. the code waits until thereâs a response. In this case the smartapp canât defer the action and needs to wait until the âanswerâ is provided - hence the REST is not an option
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
10
BTW: Iâm wondering if this is this for local hostname resolution (hosts on the LAN, local hosts table, or local DNS), external, or both?
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
11
Hmmm⌠I still donât understand thatâs thereâs not an âeasyâ way to workaround this, but thatâs my own ignorance in play here.
Not urgent, but Iâd love to learn more about this sort of thing; so if you can share an actual code example in context or some other reference, Iâd love to learn more. No rush â hope you find what you need in the meantime!
Cheers,
âŚTerry.
RBoy
(www.rboyapps.com - Making SmartThings Easy!)
12
When calling hubAction from a device code one needs to use IPAddress and ports. this is what I understood from here:
so if the user enters a (public) hostname instead of an ipaddress when initializing the device, how does one convert it to an IPAddress. @urman looping you into the conversation incase youâve any insight into this and why InetAddress doesnât work with ST.
RBoy
(www.rboyapps.com - Making SmartThings Easy!)
13
@tgauchat interestingly to use REST in a device code youâll need the IPAddress for the website (in this case api.myiponline.com) to use hubAction to get the REST response unless Iâm missing something.
RBoy
(www.rboyapps.com - Making SmartThings Easy!)
14
@tgauchat I think I see what youâre getting at, according to the specs using httpGet we can get the code to âpauseâ for upto 20 seconds to get a response from the server without having to defer it. Thanks.
RBoy
(www.rboyapps.com - Making SmartThings Easy!)
15
Thanks for pointing me in the right direction, for those interested this worked:
private String convertHostnameToIPAddress(hostname) {
def params = [
uri: "http://api.myiponline.net/dig?url=" + hostname
]
def retVal = null
try { // thanks @CosmicPuppy
retVal = httpGet(params) { response ->
log.debug "Request was successful, data=$response.data, status=$response.status"
for(result in response.data) {
for(subresult in result) {
if (subresult.type == "A") {
log.debug("Hostname $subresult.host has IP Address $subresult.ip")
return subresult.ip
}
}
}
}
}
catch (Exception e) {
log.debug("Unable to convert hostname to IP Address, Error: $e")
}
return retVal
}
2 Likes
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
16