Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freebox binding: fix issue #1170 #1180

Merged
merged 1 commit into from
Aug 6, 2016
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 @@ -182,69 +182,76 @@ private boolean authorize() {
public void initialize() {
logger.debug("initializing Freebox Server handler.");

String apiBaseUrl = null;
String apiVersion = null;
String hardwareVersion = null;
FreeboxServerConfiguration configuration = getConfigAs(FreeboxServerConfiguration.class);
String result = HttpUtil.executeUrl("GET", "http://" + configuration.fqdn + "/api_version", 5000);
if (result != null) {
apiBaseUrl = StringUtils.trim(
StringUtils.replace(StringUtils.substringBetween(result, "\"api_base_url\":\"", "\""), "\\/", "/"));
apiVersion = StringUtils.trim(StringUtils.substringBetween(result, "\"api_version\":\"", "\""));
hardwareVersion = StringUtils.trim(StringUtils.substringBetween(result, "\"device_type\":\"", "\""));
}

if ((apiBaseUrl != null) && (apiVersion != null) && (hardwareVersion != null)) {
if ((configuration != null) && (configuration.fqdn != null) && !configuration.fqdn.isEmpty()) {
updateStatus(ThingStatus.OFFLINE);

logger.debug("Server OK, binding will schedule a job to establish a connection...");
logger.debug("Binding will schedule a job to establish a connection...");
if (authorizeJob == null || authorizeJob.isCancelled()) {
authorizeJob = scheduler.schedule(authorizeRunnable, 1, TimeUnit.SECONDS);
}
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

Map<String, String> properties = editProperties();
boolean update = false;
if ((apiBaseUrl != null) && !apiBaseUrl.isEmpty()
&& ((properties.get(FreeboxBindingConstants.API_BASE_URL) == null)
|| !properties.get(FreeboxBindingConstants.API_BASE_URL).equals(apiBaseUrl))) {
update = true;
properties.put(FreeboxBindingConstants.API_BASE_URL, apiBaseUrl);
}
if ((apiVersion != null) && !apiVersion.isEmpty()
&& ((properties.get(FreeboxBindingConstants.API_VERSION) == null)
|| !properties.get(FreeboxBindingConstants.API_VERSION).equals(apiVersion))) {
update = true;
properties.put(FreeboxBindingConstants.API_VERSION, apiVersion);
}
if ((hardwareVersion != null) && !hardwareVersion.isEmpty()
&& ((properties.get(Thing.PROPERTY_HARDWARE_VERSION) == null)
|| !properties.get(Thing.PROPERTY_HARDWARE_VERSION).equals(hardwareVersion))) {
update = true;
properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareVersion);
}
if (update) {
updateProperties(properties);
}
}

private Runnable authorizeRunnable = new Runnable() {
@Override
public void run() {
logger.debug("Authorize job...");
if (authorize()) {
updateStatus(ThingStatus.ONLINE);

if (globalJob == null || globalJob.isCancelled()) {
long polling_interval = getConfigAs(FreeboxServerConfiguration.class).refreshInterval;
logger.debug("Scheduling server state update every {} seconds...", polling_interval);
globalJob = scheduler.scheduleAtFixedRate(globalRunnable, 1, polling_interval, TimeUnit.SECONDS);
String apiBaseUrl = null;
String apiVersion = null;
String hardwareVersion = null;
FreeboxServerConfiguration configuration = getConfigAs(FreeboxServerConfiguration.class);
String result = HttpUtil.executeUrl("GET", "http://" + configuration.fqdn + "/api_version", 5000);
if (result != null) {
apiBaseUrl = StringUtils.trim(StringUtils
.replace(StringUtils.substringBetween(result, "\"api_base_url\":\"", "\""), "\\/", "/"));
apiVersion = StringUtils.trim(StringUtils.substringBetween(result, "\"api_version\":\"", "\""));
hardwareVersion = StringUtils.trim(StringUtils.substringBetween(result, "\"device_type\":\"", "\""));
}

if ((apiBaseUrl != null) && (apiVersion != null) && (hardwareVersion != null)) {
if (authorize()) {
updateStatus(ThingStatus.ONLINE);

if (globalJob == null || globalJob.isCancelled()) {
long polling_interval = getConfigAs(FreeboxServerConfiguration.class).refreshInterval;
logger.debug("Scheduling server state update every {} seconds...", polling_interval);
globalJob = scheduler.scheduleAtFixedRate(globalRunnable, 1, polling_interval,
TimeUnit.SECONDS);
}
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

Map<String, String> properties = editProperties();
boolean update = false;
if ((apiBaseUrl != null) && !apiBaseUrl.isEmpty()
&& ((properties.get(FreeboxBindingConstants.API_BASE_URL) == null)
|| !properties.get(FreeboxBindingConstants.API_BASE_URL).equals(apiBaseUrl))) {
update = true;
properties.put(FreeboxBindingConstants.API_BASE_URL, apiBaseUrl);
}
if ((apiVersion != null) && !apiVersion.isEmpty()
&& ((properties.get(FreeboxBindingConstants.API_VERSION) == null)
|| !properties.get(FreeboxBindingConstants.API_VERSION).equals(apiVersion))) {
update = true;
properties.put(FreeboxBindingConstants.API_VERSION, apiVersion);
}
if ((hardwareVersion != null) && !hardwareVersion.isEmpty()
&& ((properties.get(Thing.PROPERTY_HARDWARE_VERSION) == null)
|| !properties.get(Thing.PROPERTY_HARDWARE_VERSION).equals(hardwareVersion))) {
update = true;
properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareVersion);
}
if (update) {
updateProperties(properties);
}
}
};

Expand Down