这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,32 @@ public Object convert(Class type, Object value) {
/**
* used to store events that we have sent ourselves; we need to remember them for not reacting to them
*/
private List<String> ignoreEventList = Collections.synchronizedList(new ArrayList<String>());
private static class Update {
private String itemName;
private State state;

Update(final String itemName, final State state) {
this.itemName = itemName;
this.state = state;
}

@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof Update)) {
return false;
}
return (this.itemName == null ? ((Update) o).itemName == null : this.itemName.equals(((Update) o).itemName))
&& (this.state == null ? ((Update) o).state == null : this.state.equals(((Update) o).state));
}

@Override
public int hashCode() {
return (this.itemName == null ? 0 : this.itemName.hashCode())
^ (this.state == null ? 0 : this.state.hashCode());
}
}

private List<Update> ignoreEventList = Collections.synchronizedList(new ArrayList<Update>());

public EcobeeBinding() {
}
Expand Down Expand Up @@ -349,8 +374,9 @@ private void readEcobee(OAuthCredentials oauthCredentials, Selection selection)
* we need to make sure that we won't send out this event to Ecobee again, when receiving it on the
* openHAB bus
*/
ignoreEventList.add(itemName + newState.toString());
logger.trace("Added event (item='{}', newState='{}') to the ignore event list", itemName, newState);
ignoreEventList.add(new Update(itemName, newState));
logger.trace("Added event (item='{}', newState='{}') to the ignore event list (size={})", itemName,
newState, ignoreEventList.size());
this.eventPublisher.postUpdate(itemName, newState);
}
}
Expand Down Expand Up @@ -464,8 +490,7 @@ private void commandEcobee(final String itemName, final Command command) {
}

private boolean isEcho(String itemName, State state) {
String ignoreEventListKey = itemName + state.toString();
if (ignoreEventList.remove(ignoreEventListKey)) {
if (ignoreEventList.remove(new Update(itemName, state))) {
logger.trace(
"We received this event (item='{}', state='{}') from Ecobee, so we don't send it back again -> ignore!",
itemName, state.toString());
Expand Down Expand Up @@ -497,52 +522,57 @@ private void updateEcobee(final String itemName, final State newState) {
if (provider == null) {
logger.warn("no matching binding provider found [itemName={}, newState={}]", itemName, newState);
return;
} else {
final Selection selection = new Selection(selectionMatch);
List<AbstractFunction> functions = null;
logger.trace("Selection for update: {}", selection);
}

String property = provider.getProperty(itemName);
if (!provider.isOutBound(itemName)) {
logger.warn("attempt to update non-outbound item skipped [itemName={}, newState={}]", itemName, newState);
return;
}

try {
final Thermostat thermostat = new Thermostat(null);
final Selection selection = new Selection(selectionMatch);
List<AbstractFunction> functions = null;
logger.trace("Selection for update: {}", selection);

logger.debug("About to set property '{}' to '{}'", property, newState);
String property = provider.getProperty(itemName);

thermostat.setProperty(property, newState);
try {
final Thermostat thermostat = new Thermostat(null);

logger.trace("Thermostat for update: {}", thermostat);
logger.debug("About to set property '{}' to '{}'", property, newState);

OAuthCredentials oauthCredentials = getOAuthCredentials(provider.getUserid(itemName));
thermostat.setProperty(property, newState);

if (oauthCredentials == null) {
logger.warn("Unable to locate credentials for item {}; aborting update.", itemName);
return;
}
logger.trace("Thermostat for update: {}", thermostat);

if (oauthCredentials.noAccessToken()) {
if (!oauthCredentials.refreshTokens()) {
logger.warn("Sending update skipped.");
return;
}
OAuthCredentials oauthCredentials = getOAuthCredentials(provider.getUserid(itemName));

if (oauthCredentials == null) {
logger.warn("Unable to locate credentials for item {}; aborting update.", itemName);
return;
}

if (oauthCredentials.noAccessToken()) {
if (!oauthCredentials.refreshTokens()) {
logger.warn("Sending update skipped.");
return;
}
}

UpdateThermostatRequest request = new UpdateThermostatRequest(oauthCredentials.accessToken, selection,
functions, thermostat);
ApiResponse response = request.execute();
if (response.isError()) {
final Status status = response.getStatus();
if (status.isAccessTokenExpired()) {
if (oauthCredentials.refreshTokens()) {
updateEcobee(itemName, newState);
}
} else {
logger.error("Error updating thermostat(s): {}", response);
UpdateThermostatRequest request = new UpdateThermostatRequest(oauthCredentials.accessToken, selection,
functions, thermostat);
ApiResponse response = request.execute();
if (response.isError()) {
final Status status = response.getStatus();
if (status.isAccessTokenExpired()) {
if (oauthCredentials.refreshTokens()) {
updateEcobee(itemName, newState);
}
} else {
logger.error("Error updating thermostat(s): {}", response);
}
} catch (Exception e) {
logger.error("Unable to update thermostat(s)", e);
}
} catch (Exception e) {
logger.error("Unable to update thermostat(s)", e);
}
}

Expand Down