import java.text.ParseException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
+import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
private @Nullable ScheduledFuture<?> refreshJob;
private @Nullable ScheduledFuture<?> logRefreshJob;
+ private @Nullable ScheduledFuture<?> clockSyncJob;
private int refreshPeriod = DEFAULT_REFRESH_PERIOD;
private int logRefreshPeriod = DEFAULT_LOG_REFRESH_PERIOD;
private boolean isCT80 = false;
private boolean disableLogs = false;
+ private boolean clockSync = false;
private String setpointCmdKeyPrefix = "t_";
public RadioThermostatHandler(Thing thing, RadioThermostatStateDescriptionProvider stateDescriptionProvider,
final Integer logRefresh = config.logRefresh;
this.isCT80 = config.isCT80;
this.disableLogs = config.disableLogs;
+ this.clockSync = config.clockSync;
- if (hostName == null || hostName.equals("")) {
+ if (hostName == null || "".equals(hostName)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Thermostat Host Name must be specified");
return;
channels.removeIf(c -> (c.getUID().getId().equals(PROGRAM_MODE)));
updateThing(editThing().withChannels(channels).build());
}
+
+ updateStatus(ThingStatus.UNKNOWN);
+
startAutomaticRefresh();
+
if (!this.disableLogs || this.isCT80) {
startAutomaticLogRefresh();
}
- updateStatus(ThingStatus.UNKNOWN);
+ if (this.clockSync) {
+ scheduleClockSyncJob();
+ }
}
@Override
}
}
+ /**
+ * Schedule the clock sync job
+ */
+ private void scheduleClockSyncJob() {
+ ScheduledFuture<?> clockSyncJob = this.clockSyncJob;
+ if (clockSyncJob == null || clockSyncJob.isCancelled()) {
+ clockSyncJob = null;
+ this.clockSyncJob = scheduler.scheduleWithFixedDelay(this::syncThermostatClock, 1, 60, TimeUnit.MINUTES);
+ }
+ }
+
+ /**
+ * Sync the thermostat's clock with the host system clock
+ */
+ private void syncThermostatClock() {
+ Calendar c = Calendar.getInstance();
+
+ // The thermostat week starts as Monday = 0, subtract 2 since in standard DoW Monday = 2
+ int thermDayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 2;
+ // Sunday will be -1, so add 7 to make it 6
+ if (thermDayOfWeek < 0) {
+ thermDayOfWeek += 7;
+ }
+
+ connector.sendCommand(null, null,
+ String.format(JSON_TIME, thermDayOfWeek, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE)),
+ TIME_RESOURCE);
+ }
+
/**
* Start the job to periodically update humidity and runtime date from the thermostat
*/
logRefreshJob.cancel(true);
this.logRefreshJob = null;
}
+
+ ScheduledFuture<?> clockSyncJob = this.clockSyncJob;
+ if (clockSyncJob != null) {
+ clockSyncJob.cancel(true);
+ this.clockSyncJob = null;
+ }
}
public void handleRawCommand(@Nullable String rawCommand) {