package net.homeip.mleclerc.omnilinkbbclient.model;
import java.rmi.RemoteException;
import net.homeip.mleclerc.omnilinkbbclient.OmniLinkBBClient;
import net.homeip.mleclerc.omnilinkbbclient.stubs.AegisWebService;
import net.homeip.mleclerc.omnilinkbbclient.stubs.ArmingStatus;
import net.homeip.mleclerc.omnilinkbbclient.stubs.LatchedAlarmStatus;
import net.homeip.mleclerc.omnilinkbbclient.stubs.ZoneCondition;
import net.homeip.mleclerc.omnilinkbbclient.stubs.ZoneStatusResponse;
public class ZoneModel {
private AegisWebService client;
private ZoneStatusResponse[] cachedZoneStatuses;
public ZoneModel(AegisWebService client) {
this.client = client;
}
public int getZoneNumber(int index) {
return getZoneStatuses()[index].getZoneNumber();
}
public String getZoneName(int index) {
return getZoneStatuses()[index].getZoneName();
}
public boolean isZoneBypassed(int index) {
ArmingStatus armingStatus = getZoneStatuses()[index].getArmingStatus();
return armingStatus.equals(ArmingStatus.BYPASSED_BY_USER);
}
public ZoneCondition getZoneCondition(int index) {
return getZoneStatuses()[index].getCondition();
}
public LatchedAlarmStatus getLatchedAlarmStatus(int index) {
return getZoneStatuses()[index].getLatchedAlarmStatus();
}
public boolean bypassZone(final int ZoneNumber) {
try {
return OmniLinkBBClient.execute(new OmniLinkBBClient.BoolExecution() {
public boolean boolExecute() throws RemoteException {
return client.bypassZone(ZoneNumber);
}
});
} catch(OmniLinkBBClient.ExecutionException ex) {
return false;
}
}
public boolean restoreZone(final int ZoneNumber) {
try {
return OmniLinkBBClient.execute(new OmniLinkBBClient.BoolExecution() {
public boolean boolExecute() throws RemoteException {
return client.restoreZone(ZoneNumber);
}
});
} catch(OmniLinkBBClient.ExecutionException ex) {
return false;
}
}
public int getZoneCount() {
return getZoneStatuses().length;
}
public void reset() {
cachedZoneStatuses = null;
}
private ZoneStatusResponse[] getZoneStatuses() {
if (cachedZoneStatuses == null) {
try {
cachedZoneStatuses = (ZoneStatusResponse[]) OmniLinkBBClient.execute(new OmniLinkBBClient.Execution() {
public Object execute() throws RemoteException {
return client.getZoneStatuses();
}
});
} catch(OmniLinkBBClient.ExecutionException ex) {
cachedZoneStatuses = new ZoneStatusResponse[0];
}
}
return cachedZoneStatuses;
}
}