Improved code readability.

Signed-off-by: Kai Zimmermann <kai.zimmermann@bosch-si.com>
This commit is contained in:
Kai Zimmermann
2016-05-09 10:50:14 +02:00
parent 0014dc6302
commit 5144f78c2b
4 changed files with 104 additions and 51 deletions

View File

@@ -138,7 +138,7 @@ public class DeviceSimulatorUpdater {
public void run() { public void run() {
if (device.getProgress() <= 0 && modules != null) { if (device.getProgress() <= 0 && modules != null) {
device.setUpdateStatus(simulateDownloads(device.getTargetSecurityToken())); device.setUpdateStatus(simulateDownloads(device.getTargetSecurityToken()));
if (device.getUpdateStatus().getResponseStatus().equals(ResponseStatus.ERROR)) { if (isErrorResponse(device.getUpdateStatus())) {
callback.updateFinished(device, actionId); callback.updateFinished(device, actionId);
eventbus.post(new ProgressUpdate(device)); eventbus.post(new ProgressUpdate(device));
return; return;
@@ -169,7 +169,7 @@ public class DeviceSimulatorUpdater {
result.getStatusMessages().add("Simulation complete!"); result.getStatusMessages().add("Simulation complete!");
status.forEach(download -> { status.forEach(download -> {
result.getStatusMessages().addAll(download.getStatusMessages()); result.getStatusMessages().addAll(download.getStatusMessages());
if (download.getResponseStatus().equals(ResponseStatus.ERROR)) { if (isErrorResponse(download)) {
result.setResponseStatus(ResponseStatus.ERROR); result.setResponseStatus(ResponseStatus.ERROR);
} }
}); });
@@ -179,6 +179,14 @@ public class DeviceSimulatorUpdater {
return result; return result;
} }
private boolean isErrorResponse(final UpdateStatus status) {
if (status == null) {
return false;
}
return ResponseStatus.ERROR.equals(status.getResponseStatus());
}
private static void handleArtifacts(final String targetToken, final List<UpdateStatus> status, private static void handleArtifacts(final String targetToken, final List<UpdateStatus> status,
final Artifact artifact) { final Artifact artifact) {
artifact.getUrls().entrySet().forEach(entry -> { artifact.getUrls().entrySet().forEach(entry -> {
@@ -195,7 +203,7 @@ public class DeviceSimulatorUpdater {
} }
private static UpdateStatus downloadUrl(final String url, final String targetToken, final String sha1Hash) { private static UpdateStatus downloadUrl(final String url, final String targetToken, final String sha1Hash) {
LOGGER.debug("Downloading " + url); LOGGER.debug("Downloading {}", url);
long overallread = 0; long overallread = 0;
try { try {
@@ -245,7 +253,7 @@ public class DeviceSimulatorUpdater {
/** /**
* Callback interface which is called when the simulated update process has * Callback interface which is called when the simulated update process has
* been finished and the caller of starting the simulated update process can * been finished and the caller of starting the simulated update process can
* send the result to the hawkBit update server back. * * send the result back to the hawkBit update server.
*/ */
@FunctionalInterface @FunctionalInterface
public interface UpdaterCallback { public interface UpdaterCallback {

View File

@@ -16,15 +16,30 @@ import java.util.List;
* *
*/ */
public class UpdateStatus { public class UpdateStatus {
private ResponseStatus responseStatus = ResponseStatus.SUCCESSFUL; private ResponseStatus responseStatus;
private final List<String> statusMessages = new ArrayList<>(); private List<String> statusMessages;
/**
* Constructor.
*
* @param responseStatus
* of the update
*/
public UpdateStatus(final ResponseStatus responseStatus) { public UpdateStatus(final ResponseStatus responseStatus) {
this.responseStatus = responseStatus; this.responseStatus = responseStatus;
} }
/**
* Constructor including status message.
*
* @param responseStatus
* of the update
* @param message
* of the update status
*/
public UpdateStatus(final ResponseStatus responseStatus, final String message) { public UpdateStatus(final ResponseStatus responseStatus, final String message) {
this(responseStatus); this(responseStatus);
statusMessages = new ArrayList<>();
statusMessages.add(message); statusMessages.add(message);
} }
@@ -37,6 +52,10 @@ public class UpdateStatus {
} }
public List<String> getStatusMessages() { public List<String> getStatusMessages() {
if (statusMessages == null) {
statusMessages = new ArrayList<>();
}
return statusMessages; return statusMessages;
} }
@@ -46,11 +65,12 @@ public class UpdateStatus {
*/ */
public enum ResponseStatus { public enum ResponseStatus {
/** /**
* updated has been successful and response the successful update. * Update has been successful and response the successful update.
*/ */
SUCCESSFUL, SUCCESSFUL,
/** /**
* updated has been not successful and response the error update. * Update has been not successful and response the error update.
*/ */
ERROR; ERROR;
} }

View File

@@ -55,6 +55,22 @@ import com.vaadin.ui.renderers.ProgressBarRenderer;
@SpringView(name = "") @SpringView(name = "")
public class SimulatorView extends VerticalLayout implements View { public class SimulatorView extends VerticalLayout implements View {
private static final String NEXT_POLL_COUNTER_SEC_COL = "nextPollCounterSec";
private static final String RESPONSE_STATUS_COL = "responseStatus";
private static final String PROTOCOL_COL = "protocol";
private static final String TENANT_COL = "tenant";
private static final String PROGRESS_COL = "progress";
private static final String SWVERSION_COL = "swversion";
private static final String STATUS_COL = "status";
private static final String ID_COL = "id";
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Autowired @Autowired
@@ -75,6 +91,7 @@ public class SimulatorView extends VerticalLayout implements View {
private BeanContainer<String, AbstractSimulatedDevice> beanContainer; private BeanContainer<String, AbstractSimulatedDevice> beanContainer;
@SuppressWarnings("unchecked")
@Override @Override
public void enter(final ViewChangeEvent event) { public void enter(final ViewChangeEvent event) {
eventbus.register(this); eventbus.register(this);
@@ -87,7 +104,7 @@ public class SimulatorView extends VerticalLayout implements View {
createToolbar(); createToolbar();
beanContainer = new BeanContainer<>(AbstractSimulatedDevice.class); beanContainer = new BeanContainer<>(AbstractSimulatedDevice.class);
beanContainer.setBeanIdProperty("id"); beanContainer.setBeanIdProperty(ID_COL);
grid.setSizeFull(); grid.setSizeFull();
grid.setCellStyleGenerator(new CellStyleGenerator() { grid.setCellStyleGenerator(new CellStyleGenerator() {
@@ -96,28 +113,28 @@ public class SimulatorView extends VerticalLayout implements View {
@Override @Override
public String getStyle(final CellReference cellReference) { public String getStyle(final CellReference cellReference) {
return cellReference.getPropertyId().equals("status") ? "centeralign" : null; return cellReference.getPropertyId().equals(STATUS_COL) ? "centeralign" : null;
} }
}); });
grid.setSelectionMode(SelectionMode.NONE); grid.setSelectionMode(SelectionMode.NONE);
grid.setContainerDataSource(beanContainer); grid.setContainerDataSource(beanContainer);
grid.appendHeaderRow().getCell("responseStatus").setComponent(responseComboBox); grid.appendHeaderRow().getCell(RESPONSE_STATUS_COL).setComponent(responseComboBox);
grid.setColumnOrder("id", "status", "swversion", "progress", "tenant", "protocol", "responseStatus", grid.setColumnOrder(ID_COL, STATUS_COL, SWVERSION_COL, PROGRESS_COL, TENANT_COL, PROTOCOL_COL,
"nextPollCounterSec"); RESPONSE_STATUS_COL, NEXT_POLL_COUNTER_SEC_COL);
// header widths // header widths
grid.getColumn("status").setMaximumWidth(80); grid.getColumn(STATUS_COL).setMaximumWidth(80);
grid.getColumn("protocol").setMaximumWidth(180); grid.getColumn(PROTOCOL_COL).setMaximumWidth(180);
grid.getColumn("responseStatus").setMaximumWidth(240); grid.getColumn(RESPONSE_STATUS_COL).setMaximumWidth(240);
grid.getColumn("nextPollCounterSec").setMaximumWidth(210); grid.getColumn(NEXT_POLL_COUNTER_SEC_COL).setMaximumWidth(210);
grid.getColumn("nextPollCounterSec").setHeaderCaption("Next Poll in (sec)"); grid.getColumn(NEXT_POLL_COUNTER_SEC_COL).setHeaderCaption("Next Poll in (sec)");
grid.getColumn("swversion").setHeaderCaption("SW Version"); grid.getColumn(SWVERSION_COL).setHeaderCaption("SW Version");
grid.getColumn("responseStatus").setHeaderCaption("Response Update Status"); grid.getColumn(RESPONSE_STATUS_COL).setHeaderCaption("Response Update Status");
grid.getColumn("progress").setRenderer(new ProgressBarRenderer()); grid.getColumn(PROGRESS_COL).setRenderer(new ProgressBarRenderer());
grid.getColumn("protocol").setConverter(createProtocolConverter()); grid.getColumn(PROTOCOL_COL).setConverter(createProtocolConverter());
grid.getColumn("status").setRenderer(new HtmlRenderer(), createStatusConverter()); grid.getColumn(STATUS_COL).setRenderer(new HtmlRenderer(), createStatusConverter());
grid.removeColumn("tenant"); grid.removeColumn(TENANT_COL);
// grid combobox // grid combobox
responseComboBox.setItemIcon(ResponseStatus.SUCCESSFUL, FontAwesome.CHECK_CIRCLE); responseComboBox.setItemIcon(ResponseStatus.SUCCESSFUL, FontAwesome.CHECK_CIRCLE);
@@ -125,8 +142,8 @@ public class SimulatorView extends VerticalLayout implements View {
responseComboBox.setNullSelectionAllowed(false); responseComboBox.setNullSelectionAllowed(false);
responseComboBox.setValue(ResponseStatus.SUCCESSFUL); responseComboBox.setValue(ResponseStatus.SUCCESSFUL);
responseComboBox.addValueChangeListener(valueChangeEvent -> { responseComboBox.addValueChangeListener(valueChangeEvent -> {
beanContainer.getItemIds().forEach(itemId -> beanContainer.getItem(itemId).getItemProperty("responseStatus") beanContainer.getItemIds().forEach(itemId -> beanContainer.getItem(itemId)
.setValue(valueChangeEvent.getProperty().getValue())); .getItemProperty(RESPONSE_STATUS_COL).setValue(valueChangeEvent.getProperty().getValue()));
}); });
// add all components // add all components
@@ -137,7 +154,7 @@ public class SimulatorView extends VerticalLayout implements View {
setExpandRatio(grid, 1.0F); setExpandRatio(grid, 1.0F);
// load beans // load beans
repository.getAll().forEach(device -> beanContainer.addBean(device)); repository.getAll().forEach(beanContainer::addBean);
} }
@Override @Override
@@ -146,13 +163,14 @@ public class SimulatorView extends VerticalLayout implements View {
eventbus.unregister(this); eventbus.unregister(this);
} }
@SuppressWarnings("unchecked")
@Subscribe @Subscribe
public void pollCounterUpdate(final NextPollCounterUpdate update) { public void pollCounterUpdate(final NextPollCounterUpdate update) {
final List<AbstractSimulatedDevice> devices = update.getDevices(); final List<AbstractSimulatedDevice> devices = update.getDevices();
this.getUI().access(() -> devices.forEach(device -> { this.getUI().access(() -> devices.forEach(device -> {
final BeanItem<AbstractSimulatedDevice> item = beanContainer.getItem(device.getId()); final BeanItem<AbstractSimulatedDevice> item = beanContainer.getItem(device.getId());
if (item != null) { if (item != null) {
item.getItemProperty("nextPollCounterSec").setValue(device.getNextPollCounterSec()); item.getItemProperty(NEXT_POLL_COUNTER_SEC_COL).setValue(device.getNextPollCounterSec());
} }
})); }));
} }
@@ -163,17 +181,19 @@ public class SimulatorView extends VerticalLayout implements View {
* @param update * @param update
* the update event posted on the event bus * the update event posted on the event bus
*/ */
@SuppressWarnings("unchecked")
@Subscribe @Subscribe
public void initUpdate(final InitUpdate update) { public void initUpdate(final InitUpdate update) {
final AbstractSimulatedDevice device = update.getDevice(); final AbstractSimulatedDevice device = update.getDevice();
this.getUI().access(() -> { this.getUI().access(() -> {
final BeanItem<AbstractSimulatedDevice> item = beanContainer.getItem(device.getId()); final BeanItem<AbstractSimulatedDevice> item = beanContainer.getItem(device.getId());
if (item != null) { if (item == null) {
item.getItemProperty("progress").setValue(device.getProgress()); return;
item.getItemProperty("status").setValue(Status.PEDNING);
item.getItemProperty("swversion").setValue(device.getSwversion());
} }
item.getItemProperty(PROGRESS_COL).setValue(device.getProgress());
item.getItemProperty(STATUS_COL).setValue(Status.PEDNING);
item.getItemProperty(SWVERSION_COL).setValue(device.getSwversion());
}); });
} }
@@ -183,32 +203,37 @@ public class SimulatorView extends VerticalLayout implements View {
* @param update * @param update
* the update event posted on the event bus * the update event posted on the event bus
*/ */
@SuppressWarnings("unchecked")
@Subscribe @Subscribe
public void progessUpdate(final ProgressUpdate update) { public void progessUpdate(final ProgressUpdate update) {
final AbstractSimulatedDevice device = update.getDevice(); final AbstractSimulatedDevice device = update.getDevice();
this.getUI().access(() -> { this.getUI().access(() -> {
final BeanItem<AbstractSimulatedDevice> item = beanContainer.getItem(device.getId()); final BeanItem<AbstractSimulatedDevice> item = beanContainer.getItem(device.getId());
if (item != null) { if (item != null) {
item.getItemProperty("progress").setValue(device.getProgress()); item.getItemProperty(PROGRESS_COL).setValue(device.getProgress());
if (device.getProgress() >= 1) { setStatusColumn(device, item);
switch (device.getUpdateStatus().getResponseStatus()) {
case SUCCESSFUL:
item.getItemProperty("status").setValue(Status.FINISH);
break;
case ERROR:
item.getItemProperty("status").setValue(Status.ERROR);
break;
default:
item.getItemProperty("status").setValue(Status.UNKNWON);
}
} else {
item.getItemProperty("status").setValue(Status.PEDNING);
}
} }
}); });
} }
@SuppressWarnings("unchecked")
private void setStatusColumn(final AbstractSimulatedDevice device, final BeanItem<AbstractSimulatedDevice> item) {
if (device.getProgress() >= 1) {
switch (device.getUpdateStatus().getResponseStatus()) {
case SUCCESSFUL:
item.getItemProperty(STATUS_COL).setValue(Status.FINISH);
break;
case ERROR:
item.getItemProperty(STATUS_COL).setValue(Status.ERROR);
break;
default:
item.getItemProperty(STATUS_COL).setValue(Status.UNKNWON);
}
} else {
item.getItemProperty(STATUS_COL).setValue(Status.PEDNING);
}
}
private void createToolbar() { private void createToolbar() {
final Button createDevicesButton = new Button("generate..."); final Button createDevicesButton = new Button("generate...");
createDevicesButton.setIcon(FontAwesome.GEARS); createDevicesButton.setIcon(FontAwesome.GEARS);

View File

@@ -220,7 +220,7 @@ public class RootController {
+ " of: " + request.getRequestURI()); + " of: " + request.getRequestURI());
} else { } else {
statusMessage.addMessage( statusMessage.addMessage(
ControllerManagement.SERVER_MESSAGE_PREFIX + "Target downloads: " + request.getRequestURI()); ControllerManagement.SERVER_MESSAGE_PREFIX + "Target downloads " + request.getRequestURI());
} }
controllerManagement.addActionStatusMessage(statusMessage); controllerManagement.addActionStatusMessage(statusMessage);
return action; return action;
@@ -390,7 +390,7 @@ public class RootController {
LOG.debug("Controller confirmed cancel (actionid: {}, targetid: {}) as we got {} report.", actionid, LOG.debug("Controller confirmed cancel (actionid: {}, targetid: {}) as we got {} report.", actionid,
targetid, feedback.getStatus().getExecution()); targetid, feedback.getStatus().getExecution());
actionStatus.setStatus(Status.CANCELED); actionStatus.setStatus(Status.CANCELED);
actionStatus.addMessage(ControllerManagement.SERVER_MESSAGE_PREFIX + "Target confirmed cancelation"); actionStatus.addMessage(ControllerManagement.SERVER_MESSAGE_PREFIX + "Target confirmed cancelation.");
break; break;
case REJECTED: case REJECTED:
LOG.info("Controller reported internal error (actionid: {}, targetid: {}) as we got {} report.", actionid, LOG.info("Controller reported internal error (actionid: {}, targetid: {}) as we got {} report.", actionid,
@@ -424,7 +424,7 @@ public class RootController {
targetid, feedback.getStatus().getExecution()); targetid, feedback.getStatus().getExecution());
actionStatus.setStatus(Status.RUNNING); actionStatus.setStatus(Status.RUNNING);
actionStatus.addMessage( actionStatus.addMessage(
ControllerManagement.SERVER_MESSAGE_PREFIX + "Target reported: " + feedback.getStatus().getExecution()); ControllerManagement.SERVER_MESSAGE_PREFIX + "Target reported " + feedback.getStatus().getExecution());
} }
private static void handleClosedUpdateStatus(final ActionFeedback feedback, final String targetid, private static void handleClosedUpdateStatus(final ActionFeedback feedback, final String targetid,