From 9b285ec93b80e0d98349e8907244377f5031c60f Mon Sep 17 00:00:00 2001 From: Juan Pablo Risso Date: Tue, 21 Jun 2016 13:19:53 -0400 Subject: [PATCH] PRP-151 - Harmony Cloud (#1003) - Added isIP() to check if a local or cloud callback URL - Added try around Integer.parseInt --- .../logitech-harmony-connect.groovy | 56 +++++++++++++++---- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/smartapps/smartthings/logitech-harmony-connect.src/logitech-harmony-connect.groovy b/smartapps/smartthings/logitech-harmony-connect.src/logitech-harmony-connect.groovy index 7731c80..e3dda08 100644 --- a/smartapps/smartthings/logitech-harmony-connect.src/logitech-harmony-connect.groovy +++ b/smartapps/smartthings/logitech-harmony-connect.src/logitech-harmony-connect.groovy @@ -824,17 +824,51 @@ def deviceHandler(evt) { def sendToHarmony(evt, String callbackUrl) { def callback = new URI(callbackUrl) - def host = callback.port != -1 ? "${callback.host}:${callback.port}" : callback.host - def path = callback.query ? "${callback.path}?${callback.query}".toString() : callback.path - sendHubCommand(new physicalgraph.device.HubAction( - method: "POST", - path: path, - headers: [ - "Host": host, - "Content-Type": "application/json" - ], - body: [evt: [deviceId: evt.deviceId, name: evt.name, value: evt.value]] - )) + if(isIP(callback.host)){ + def host = callback.port != -1 ? "${callback.host}:${callback.port}" : callback.host + def path = callback.query ? "${callback.path}?${callback.query}".toString() : callback.path + sendHubCommand(new physicalgraph.device.HubAction( + method: "POST", + path: path, + headers: [ + "Host": host, + "Content-Type": "application/json" + ], + body: [evt: [deviceId: evt.deviceId, name: evt.name, value: evt.value]] + )) + } else { + def params = [ + uri: callbackUrl, + body: [evt: [deviceId: evt.deviceId, name: evt.name, value: evt.value]] + ] + try { + log.debug "Sending data to Harmony Cloud: $params" + httpPostJson(params) { resp -> + log.debug "Harmony Cloud - Response: ${resp.status}" + } + } catch (e) { + log.error "Harmony Cloud - Something went wrong: $e" + } + } +} + +public static boolean isIP(String str) { + try { + String[] parts = str.split("\\."); + if (parts.length != 4) return false; + for (int i = 0; i < 4; ++i) { + int p + try { + p = Integer.parseInt(parts[i]); + } catch (Exception e) { + return false; + } + if (p > 255 || p < 0) return false; + } + return true; + } catch (Exception e) { + return false; + } } def listHubs() {