Skip to content

Commit

Permalink
fix(console): fixed error reporting for DeviceAssetChannel
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Jan 25, 2024
1 parent 66b99b0 commit a6d8ae0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
Expand Down Expand Up @@ -244,6 +245,7 @@ public void handleEvent(BaseEvent be) {
for (GwtDeviceAssetChannel channel : asset.getChannels()) {
field = paintChannel(channel);
field.setEnabled(currentSession.hasPermission(DeviceManagementSessionPermission.write()));

actionFieldSet.add(field, formData);
}
if (asset.getDescription() != null) {
Expand All @@ -261,34 +263,40 @@ public void handleEvent(BaseEvent be) {

private Field<?> paintChannel(GwtDeviceAssetChannel channel) {
Field<?> field;
switch (channel.getTypeEnum()) {
case LONG:
case DOUBLE:
case FLOAT:
case INT:
case INTEGER:
field = paintNumberChannel(channel);
break;
case BOOLEAN:
field = paintBooleanChannel(channel);
break;
case STRING:
default:
field = paintTextChannel(channel);
break;

if (!channel.hasError()) {
switch (channel.getTypeEnum()) {
case LONG:
case DOUBLE:
case FLOAT:
case INT:
case INTEGER:
field = paintNumberChannel(channel);
break;
case BOOLEAN:
field = paintBooleanChannel(channel);
break;
case STRING:
default:
field = paintTextChannel(channel);
break;
}
} else {
field = paintErrorChannel(channel);
}

field.setName(channel.getName());
field.setItemId(channel.getName());
field.setReadOnly(channel.getModeEnum().equals(GwtDeviceAssetChannelMode.READ));

return field;
}

private Field<?> paintTextChannel(GwtDeviceAssetChannel channel) {

TextField<String> field = new TextField<String>();
field.setName(channel.getName());
field.setAllowBlank(true);
field.setFieldLabel(channel.getName() + " (" + channel.getType() + " - " + channel.getMode() + ")");
field.setFieldLabel(createFieldLabelFromChannel(channel));
field.setLabelStyle(CssLiterals.WORD_BREAK_BREAK_ALL);
field.addPlugin(dirtyPlugin);

Expand All @@ -301,9 +309,8 @@ private Field<?> paintTextChannel(GwtDeviceAssetChannel channel) {

private Field<?> paintNumberChannel(GwtDeviceAssetChannel channel) {
NumberField field = new NumberField();
field.setName(channel.getName());
field.setAllowBlank(true);
field.setFieldLabel(channel.getName() + " (" + channel.getType() + " - " + channel.getMode() + ")");
field.setFieldLabel(createFieldLabelFromChannel(channel));
field.setLabelStyle(CssLiterals.WORD_BREAK_BREAK_ALL);
field.addPlugin(dirtyPlugin);
field.setMaxValue(MAX_SAFE_INTEGER);
Expand Down Expand Up @@ -356,9 +363,7 @@ private Field<?> paintBooleanChannel(GwtDeviceAssetChannel channel) {
radioFalse.setItemId("false");

RadioGroup radioGroup = new RadioGroup();
radioGroup.setName(channel.getName());
radioGroup.setItemId(channel.getName());
radioGroup.setFieldLabel(channel.getName() + " (" + channel.getType() + " - " + channel.getMode() + ")");
radioGroup.setFieldLabel(createFieldLabelFromChannel(channel));
radioGroup.setLabelStyle(CssLiterals.WORD_BREAK_BREAK_ALL);
radioGroup.add(radioTrue);
radioGroup.add(radioFalse);
Expand All @@ -383,4 +388,30 @@ private Field<?> paintBooleanChannel(GwtDeviceAssetChannel channel) {
return radioGroup;
}

/**
* Paints a {@link GwtDeviceAssetChannel} which when on error while reading the values form it.
*
* @param channel The {@link GwtDeviceAssetChannel} that when on error.
* @return A {@link LabelField} with the {@link GwtDeviceAssetChannel#getError()}
* @since 2.0.0
*/
private LabelField paintErrorChannel(GwtDeviceAssetChannel channel) {
LabelField labelField = new LabelField();
labelField.setFieldLabel(createFieldLabelFromChannel(channel));
labelField.setText(channel.getError());
labelField.setLabelStyle(CssLiterals.WORD_BREAK_BREAK_ALL);

return labelField;
}

/**
* Creates the {@link Field#getFieldLabel()} from the given {@link GwtDeviceAssetChannel}
*
* @param deviceAssetChannel The {@link GwtDeviceAssetChannel} from which to extract data.
* @return A formatted {@link String} with the laberl for the {@link Field}.
* @since 2.0.0
*/
private String createFieldLabelFromChannel(GwtDeviceAssetChannel deviceAssetChannel) {
return deviceAssetChannel.getName() + " (" + deviceAssetChannel.getType() + " - " + deviceAssetChannel.getMode() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -362,28 +362,23 @@ public void refresh() {
}
}

public void refreshAssetPanel(GwtDeviceAsset asset) {
public void refreshAssetPanel(final GwtDeviceAsset asset) {
apply.setEnabled(false);
reset.setEnabled(false);

if (assetValuesPanel != null) {
assetValuesPanel.removeFromParent();
}
if (asset != null) {

assetValuesPanel = new DeviceAssetsPanel(asset, currentSession);
assetValuesPanel.addListener(Events.Change, new Listener<BaseEvent>() {

@Override
public void handleEvent(BaseEvent be) {
apply.setEnabled(true);
reset.setEnabled(true);
}
});
assetValuesPanel = new DeviceAssetsPanel(asset, currentSession);
assetValuesPanel.addListener(Events.Change, new Listener<BaseEvent>() {
@Override
public void handleEvent(BaseEvent be) {
apply.setEnabled(asset != null);
reset.setEnabled(asset != null);
}
});

} else {
assetValuesPanel = new DeviceAssetsPanel(null, currentSession);
}
assetValuesContainer.add(assetValuesPanel, centerData);
assetValuesContainer.layout();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.service.device.management.asset.DeviceAsset;
import org.eclipse.kapua.service.device.management.asset.DeviceAssetChannel;
import org.eclipse.kapua.service.device.management.asset.DeviceAssetChannelMode;
import org.eclipse.kapua.service.device.management.asset.DeviceAssetManagementService;
import org.eclipse.kapua.service.device.management.asset.DeviceAssets;

Expand Down Expand Up @@ -80,20 +79,19 @@ public List<GwtDeviceAsset> get(PagingLoadConfig pagingLoadConfig, String scopeI
DeviceAsset assetMetadata = assetsMetadata.getAssets().get(assetIndex);
DeviceAsset assetValues = assetsValues.getAssets().get(assetIndex);
for (DeviceAssetChannel channelMetadata : assetMetadata.getChannels()) {
if (channelMetadata.getMode().equals(DeviceAssetChannelMode.READ) || channelMetadata.getMode().equals(DeviceAssetChannelMode.READ_WRITE)) {
for (DeviceAssetChannel channelValue : assetValues.getChannels()) {
if (channelValue.getName().equals(channelMetadata.getName())) {
channelMetadata.setValue(channelValue.getValue());
break;
}
for (DeviceAssetChannel channelValue : assetValues.getChannels()) {
if (channelValue.getName().equals(channelMetadata.getName())) {
channelMetadata.setValue(channelValue.getValue());
channelMetadata.setError(channelValue.getError());
break;
}
}
}
}

gwtAssets = KapuaGwtDeviceModelConverter.convertDeviceAssets(assetsMetadata);
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
throw KapuaExceptionHandler.buildExceptionFromError(t);
}
return gwtAssets.getAssets();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public void setError(String error) {
set("error", error);
}

public boolean hasError() {
return getError() != null;
}

public String getValue() {
return get("value");
}
Expand Down

0 comments on commit a6d8ae0

Please sign in to comment.