Clean Code Refactoring

Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
SirWayne
2016-02-09 10:16:07 +01:00
parent 9ae2ef0bc0
commit 1bb29338ac
35 changed files with 1614 additions and 1669 deletions

View File

@@ -10,7 +10,6 @@ package org.eclipse.hawkbit.simulator;
import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.hawkbit.simulator.DeviceSimulatorUpdater.UpdaterCallback;
import org.eclipse.hawkbit.simulator.http.ControllerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -62,48 +61,47 @@ public class DDISimulatedDevice extends AbstractSimulatedDevice {
@Override
public void clean() {
super.clean();
removed = true;
this.removed = true;
}
public int getPollDelaySec() {
return pollDelaySec;
return this.pollDelaySec;
}
/**
* Polls the base URL for the DDI API interface.
*/
public void poll() {
if (!removed) {
final String basePollJson = controllerResource.get(getTenant(), getId());
if (!this.removed) {
final String basePollJson = this.controllerResource.get(getTenant(), getId());
try {
final String href = JsonPath.parse(basePollJson).read("_links.deploymentBase.href");
final long actionId = Long.parseLong(href.substring(href.lastIndexOf("/") + 1, href.indexOf("?")));
if (currentActionId == null) {
final String deploymentJson = controllerResource.getDeployment(getTenant(), getId(), actionId);
if (this.currentActionId == null) {
final String deploymentJson = this.controllerResource.getDeployment(getTenant(), getId(), actionId);
final String swVersion = JsonPath.parse(deploymentJson).read("deployment.chunks[0].version");
currentActionId = actionId;
deviceUpdater.startUpdate(getTenant(), getId(), actionId, swVersion, new UpdaterCallback() {
@Override
public void updateFinished(final AbstractSimulatedDevice device, final Long actionId) {
switch (device.getResponseStatus()) {
case SUCCESSFUL:
controllerResource.postSuccessFeedback(getTenant(), getId(), actionId);
break;
case ERROR:
controllerResource.postErrorFeedback(getTenant(), getId(), actionId);
break;
default:
throw new IllegalStateException("simulated device has an unknown response status + "
+ device.getResponseStatus());
}
currentActionId = null;
this.currentActionId = actionId;
this.deviceUpdater.startUpdate(getTenant(), getId(), actionId, swVersion, (device, actionId1) -> {
switch (device.getResponseStatus()) {
case SUCCESSFUL:
DDISimulatedDevice.this.controllerResource.postSuccessFeedback(getTenant(), getId(),
actionId1);
break;
case ERROR:
DDISimulatedDevice.this.controllerResource.postErrorFeedback(getTenant(), getId(),
actionId1);
break;
default:
throw new IllegalStateException(
"simulated device has an unknown response status + " + device.getResponseStatus());
}
DDISimulatedDevice.this.currentActionId = null;
});
}
} catch (final PathNotFoundException e) {
// href might not be in the json response, so ignore
// exception here.
LOGGER.trace("Response does not contain a deploymentbase href link, ignoring.");
LOGGER.trace("Response does not contain a deploymentbase href link, ignoring.", e);
}
}

View File

@@ -8,6 +8,7 @@
*/
package org.eclipse.hawkbit.simulator;
import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -41,7 +42,7 @@ public class DeviceSimulatorUpdater {
/**
* Starting an simulated update process of an simulated device.
*
*
* @param tenant
* the tenant of the device
* @param id
@@ -57,16 +58,17 @@ public class DeviceSimulatorUpdater {
*/
public void startUpdate(final String tenant, final String id, final long actionId, final String swVersion,
final UpdaterCallback callback) {
final AbstractSimulatedDevice device = repository.get(tenant, id);
final AbstractSimulatedDevice device = this.repository.get(tenant, id);
device.setProgress(0.0);
device.setSwversion(swVersion);
eventbus.post(new InitUpdate(device));
threadPool.schedule(new DeviceSimulatorUpdateThread(device, spSenderService, actionId, eventbus, callback),
2000, TimeUnit.MILLISECONDS);
this.eventbus.post(new InitUpdate(device));
threadPool.schedule(
new DeviceSimulatorUpdateThread(device, this.spSenderService, actionId, this.eventbus, callback), 2000,
TimeUnit.MILLISECONDS);
}
private static final class DeviceSimulatorUpdateThread implements Runnable {
private static final Random rndSleep = new Random();
private static final Random rndSleep = new SecureRandom();
private final AbstractSimulatedDevice device;
private final SpSenderService spSenderService;
@@ -74,9 +76,8 @@ public class DeviceSimulatorUpdater {
private final EventBus eventbus;
private final UpdaterCallback callback;
private DeviceSimulatorUpdateThread(final AbstractSimulatedDevice device,
final SpSenderService spSenderService, final long actionId, final EventBus eventbus,
final UpdaterCallback callback) {
private DeviceSimulatorUpdateThread(final AbstractSimulatedDevice device, final SpSenderService spSenderService,
final long actionId, final EventBus eventbus, final UpdaterCallback callback) {
this.device = device;
this.spSenderService = spSenderService;
this.actionId = actionId;
@@ -86,15 +87,15 @@ public class DeviceSimulatorUpdater {
@Override
public void run() {
final double newProgress = device.getProgress() + 0.2;
device.setProgress(newProgress);
final double newProgress = this.device.getProgress() + 0.2;
this.device.setProgress(newProgress);
if (newProgress < 1.0) {
threadPool.schedule(new DeviceSimulatorUpdateThread(device, spSenderService, actionId, eventbus,
callback), rndSleep.nextInt(3000), TimeUnit.MILLISECONDS);
threadPool.schedule(new DeviceSimulatorUpdateThread(this.device, this.spSenderService, this.actionId,
this.eventbus, this.callback), rndSleep.nextInt(3000), TimeUnit.MILLISECONDS);
} else {
callback.updateFinished(device, actionId);
this.callback.updateFinished(this.device, this.actionId);
}
eventbus.post(new ProgressUpdate(device));
this.eventbus.post(new ProgressUpdate(this.device));
}
}
@@ -102,7 +103,7 @@ public class DeviceSimulatorUpdater {
* Callback interface which is called when the simulated update process has
* been finished and the caller of starting the simulated update process can
* send the result to the hawkbit update server back.
*
*
* @author Michael Hirsch
*
*/
@@ -111,7 +112,7 @@ public class DeviceSimulatorUpdater {
/**
* Callback method to indicate that the simulated update process has
* been finished.
*
*
* @param device
* the device which has been updated
* @param actionId

View File

@@ -16,6 +16,8 @@ import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.eclipse.hawkbit.simulator.event.NextPollCounterUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -24,13 +26,15 @@ import com.google.common.eventbus.EventBus;
/**
* Poll time trigger which executes the {@link DDISimulatedDevice#poll()} every
* second.
*
*
* @author Michael Hirsch
*
*/
@Component
public class NextPollTimeController {
private static final Logger LOGGER = LoggerFactory.getLogger(NextPollTimeController.class);
private static final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
private static final ExecutorService pollService = Executors.newFixedThreadPool(1);
@@ -50,7 +54,7 @@ public class NextPollTimeController {
private class NextPollUpdaterRunnable implements Runnable {
@Override
public void run() {
final List<AbstractSimulatedDevice> devices = repository.getAll().stream()
final List<AbstractSimulatedDevice> devices = NextPollTimeController.this.repository.getAll().stream()
.filter(device -> device instanceof DDISimulatedDevice).collect(Collectors.toList());
devices.forEach(device -> {
@@ -58,21 +62,16 @@ public class NextPollTimeController {
if (nextCounter < 0) {
if (device instanceof DDISimulatedDevice) {
try {
pollService.submit(new Runnable() {
@Override
public void run() {
((DDISimulatedDevice) device).poll();
}
});
} catch (final Exception e) {
pollService.submit(() -> ((DDISimulatedDevice) device).poll());
} catch (final IllegalStateException e) {
LOGGER.trace("Device could not be polled", e);
}
nextCounter = ((DDISimulatedDevice) device).getPollDelaySec();
}
}
device.setNextPollCounterSec(nextCounter);
});
eventBus.post(new NextPollCounterUpdate(devices));
NextPollTimeController.this.eventBus.post(new NextPollCounterUpdate(devices));
}
}
}