mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-18 16:17:30 +00:00
Update Harmonhy connection handling on disconnect. Add Route Parse
This commit is contained in:
@@ -9,6 +9,7 @@ import net.whistlingfish.harmony.HarmonyClient;
|
||||
import net.whistlingfish.harmony.config.Activity;
|
||||
import net.whistlingfish.harmony.config.Device;
|
||||
import net.whistlingfish.harmony.config.HarmonyConfig;
|
||||
import com.bwssystems.HABridge.NamedIP;
|
||||
|
||||
public class HarmonyHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(HarmonyHandler.class);
|
||||
@@ -16,8 +17,9 @@ public class HarmonyHandler {
|
||||
private Boolean noopCalls;
|
||||
private Boolean devMode;
|
||||
private DevModeResponse devResponse;
|
||||
private NamedIP myNameAndIP;
|
||||
|
||||
public HarmonyHandler(HarmonyClient theClient, Boolean noopCallsSetting, DevModeResponse devResponseSetting) {
|
||||
public HarmonyHandler(HarmonyClient theClient, Boolean noopCallsSetting, DevModeResponse devResponseSetting, NamedIP aNameAndIP) {
|
||||
super();
|
||||
noopCalls = noopCallsSetting;
|
||||
devMode = Boolean.TRUE;
|
||||
@@ -27,6 +29,7 @@ public class HarmonyHandler {
|
||||
else
|
||||
devResponse = devResponseSetting;
|
||||
harmonyClient = theClient;
|
||||
myNameAndIP = aNameAndIP;
|
||||
}
|
||||
|
||||
public List<Activity> getActivities() {
|
||||
@@ -34,7 +37,15 @@ public class HarmonyHandler {
|
||||
if(devMode)
|
||||
return devResponse.getActivities();
|
||||
|
||||
return harmonyClient.getConfig().getActivities();
|
||||
List<Activity> listOfActivities = null;
|
||||
|
||||
try {
|
||||
listOfActivities = harmonyClient.getConfig().getActivities();
|
||||
} catch (RuntimeException e) {
|
||||
handleExceptionError(e);
|
||||
}
|
||||
|
||||
return listOfActivities;
|
||||
}
|
||||
|
||||
public List<Device> getDevices() {
|
||||
@@ -42,7 +53,14 @@ public class HarmonyHandler {
|
||||
if(devMode)
|
||||
return devResponse.getDevices();
|
||||
|
||||
return harmonyClient.getConfig().getDevices();
|
||||
List<Device> listOfDevices = null;
|
||||
|
||||
try {
|
||||
listOfDevices = harmonyClient.getConfig().getDevices();
|
||||
} catch (RuntimeException e) {
|
||||
handleExceptionError(e);
|
||||
}
|
||||
return listOfDevices;
|
||||
}
|
||||
|
||||
public HarmonyConfig getConfig() {
|
||||
@@ -50,15 +68,27 @@ public class HarmonyHandler {
|
||||
if(devMode)
|
||||
return devResponse.getConfig();
|
||||
|
||||
return harmonyClient.getConfig();
|
||||
HarmonyConfig aConfig = null;
|
||||
try {
|
||||
aConfig = harmonyClient.getConfig();
|
||||
} catch (RuntimeException e) {
|
||||
handleExceptionError(e);
|
||||
}
|
||||
return aConfig;
|
||||
}
|
||||
|
||||
public Activity getCurrentActivity() {
|
||||
log.debug("Harmony api current sctivity requested.");
|
||||
if(devMode)
|
||||
return devResponse.getCurrentActivity();
|
||||
|
||||
return harmonyClient.getCurrentActivity();
|
||||
|
||||
Activity anActivity = null;
|
||||
try {
|
||||
anActivity = harmonyClient.getCurrentActivity();
|
||||
} catch (RuntimeException e) {
|
||||
handleExceptionError(e);
|
||||
}
|
||||
return anActivity;
|
||||
}
|
||||
|
||||
public Boolean startActivity(RunActivity anActivity) {
|
||||
@@ -83,7 +113,11 @@ public class HarmonyHandler {
|
||||
} catch (IllegalArgumentException ei) {
|
||||
log.error("Error in finding activity: " + anActivity.getName() + " for a hub: " + anActivity.getHub());
|
||||
return false;
|
||||
} catch (RuntimeException e1) {
|
||||
handleExceptionError(e1);
|
||||
}
|
||||
} catch (RuntimeException e1) {
|
||||
handleExceptionError(e1);
|
||||
}
|
||||
} else {
|
||||
log.error("Error in finding activity: " + anActivity.getName() + " for a hub: " + anActivity.getHub());
|
||||
@@ -114,7 +148,11 @@ public class HarmonyHandler {
|
||||
} catch (IllegalArgumentException ei) {
|
||||
log.error("Error in finding device: " + aDeviceButton.getDevice() +" and a button: " + aDeviceButton.getButton() + " for a hub: " + aDeviceButton.getHub());
|
||||
return false;
|
||||
} catch (RuntimeException e1) {
|
||||
handleExceptionError(e1);
|
||||
}
|
||||
} catch (RuntimeException e1) {
|
||||
handleExceptionError(e1);
|
||||
}
|
||||
} else {
|
||||
log.error("Error in finding device: " + aDeviceButton.getDevice() +" and a button: " + aDeviceButton.getButton() + " for a hub: " + aDeviceButton.getHub());
|
||||
@@ -124,6 +162,18 @@ public class HarmonyHandler {
|
||||
return true;
|
||||
}
|
||||
|
||||
void handleExceptionError(Exception e) {
|
||||
if(e.getMessage().equalsIgnoreCase("Failed communicating with Harmony Hub")) {
|
||||
log.warn("Issue in communcicating with Harmony Hub, retrying connect....");
|
||||
try {
|
||||
harmonyClient.disconnect();
|
||||
} catch(Exception e1) {
|
||||
log.warn("Hub disconnect failed, continuing....");
|
||||
}
|
||||
harmonyClient.connect(myNameAndIP.getIp());
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
log.debug("Harmony api shutdown requested.");
|
||||
if(devMode)
|
||||
|
||||
Reference in New Issue
Block a user