Skip to content

Commit

Permalink
feat: Added asset name in error logs
Browse files Browse the repository at this point in the history
Signed-off-by: MMaiero <[email protected]>
  • Loading branch information
MMaiero committed Sep 30, 2024
1 parent 6c5ba99 commit f333f7c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2023 Eurotech and/or its affiliates and others
* Copyright (c) 2016, 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -51,7 +51,6 @@
import org.eclipse.kura.channel.listener.ChannelEvent;
import org.eclipse.kura.channel.listener.ChannelListener;
import org.eclipse.kura.configuration.ComponentConfiguration;
import org.eclipse.kura.configuration.ConfigurationService;
import org.eclipse.kura.configuration.SelfConfiguringComponent;
import org.eclipse.kura.core.configuration.ComponentConfigurationImpl;
import org.eclipse.kura.core.configuration.metatype.Tad;
Expand Down Expand Up @@ -158,9 +157,9 @@ public class BaseAsset implements Asset, SelfConfiguringComponent {
* OSGi service component callback while activation.
*
* @param componentContext
* the component context
* the component context
* @param properties
* the service properties
* the service properties
*/
protected void activate(final ComponentContext componentContext, final Map<String, Object> properties) {
logger.info("activating...");
Expand All @@ -174,7 +173,7 @@ protected void activate(final ComponentContext componentContext, final Map<Strin
* OSGi service component update callback.
*
* @param properties
* the service properties
* the service properties
*/
public void updated(final Map<String, Object> properties) {

Expand All @@ -194,7 +193,7 @@ public void updated(final Map<String, Object> properties) {
* OSGi service component callback while deactivation.
*
* @param context
* the component context
* the component context
*/
protected void deactivate(final ComponentContext context) {
logger.debug("deactivating...");
Expand All @@ -213,9 +212,9 @@ protected void deactivate(final ComponentContext context) {
* PID.
*
* @param driverId
* the identifier of the driver
* the identifier of the driver
* @throws NullPointerException
* if driver id provided is null
* if driver id provided is null
*/
private void reopenDriverTracker(final String driverId) {
requireNonNull(driverId, "Driver PID cannot be null");
Expand Down Expand Up @@ -293,15 +292,13 @@ public ComponentConfiguration getConfiguration() throws KuraException {

final Map<String, Object> properties = this.config.getProperties();

final String componentName = properties.get(ConfigurationService.KURA_SERVICE_PID).toString();

Tocd ocd = this.config.getDefinition();

if (ocd == null) {
ocd = getOCD();
}

return new ComponentConfigurationImpl(componentName, ocd, new HashMap<>(properties));
return new ComponentConfigurationImpl(getKuraServicePid(), ocd, new HashMap<>(properties));
}

/**
Expand All @@ -313,7 +310,7 @@ protected String getFactoryPid() {
return CONF_PID;
}

protected String getKuraServicePid() throws KuraException {
protected String getKuraServicePid() {
return this.config.getKuraServicePid();
}

Expand Down Expand Up @@ -405,14 +402,13 @@ public List<ChannelRecord> read(final Set<String> channelNames) throws KuraExcep
}

protected List<ChannelRecord> getFinalRecords(List<ChannelRecord> channelRecords, Map<String, Channel> channels) {
channelRecords.stream()
.forEach(channelRecord -> {
Channel channel = channels.get(channelRecord.getChannelName());
channelRecords.stream().forEach(channelRecord -> {
Channel channel = channels.get(channelRecord.getChannelName());

if (shouldApplyScaleAndOffset(channelRecord, channel)) {
applyScaleAndOffset(channelRecord, channel);
}
});
if (shouldApplyScaleAndOffset(channelRecord, channel)) {
applyScaleAndOffset(channelRecord, channel);
}
});

return channelRecords;
}
Expand All @@ -427,19 +423,17 @@ private void applyScaleAndOffset(final ChannelRecord channelRecord, final Channe
final double channelOffset = channel.getValueOffset();

if (channelRecord.getValueType().equals(DataType.DOUBLE)) {
channelRecord.setValue(new DoubleValue(
(double) channelRecord.getValue().getValue() * channelScale + channelOffset));
} else if (channelRecord.getValueType().equals(DataType.FLOAT)) {
channelRecord.setValue(
new FloatValue((float) channelRecord.getValue().getValue() * (float) channelScale
+ (float) channelOffset));
new DoubleValue((double) channelRecord.getValue().getValue() * channelScale + channelOffset));
} else if (channelRecord.getValueType().equals(DataType.FLOAT)) {
channelRecord.setValue(new FloatValue(
(float) channelRecord.getValue().getValue() * (float) channelScale + (float) channelOffset));
} else if (channelRecord.getValueType().equals(DataType.INTEGER)) {
channelRecord.setValue(new IntegerValue(
(int) channelRecord.getValue().getValue() * (int) channelScale + (int) channelOffset));
} else if (channelRecord.getValueType().equals(DataType.LONG)) {
channelRecord
.setValue(new LongValue((long) channelRecord.getValue().getValue() * (long) channelScale
+ (long) channelOffset));
channelRecord.setValue(new LongValue(
(long) channelRecord.getValue().getValue() * (long) channelScale + (long) channelOffset));
}
}

Expand Down Expand Up @@ -619,8 +613,7 @@ protected class ChannelListenerHolder implements ChannelListener {
private final ChannelListener listener;
private final Channel channel;

public ChannelListenerHolder(Channel channel,
ChannelListener listener) {
public ChannelListenerHolder(Channel channel, ChannelListener listener) {
this.channel = channel;
this.listener = listener;
}
Expand All @@ -637,10 +630,10 @@ public ChannelListener getChannelListener() {
public void onChannelEvent(ChannelEvent event) {
final ChannelRecord originaRecord = event.getChannelRecord();

if (shouldApplyScaleAndOffset(originaRecord, channel)) {
if (shouldApplyScaleAndOffset(originaRecord, this.channel)) {
final ChannelRecord cloned = cloneRecord(originaRecord);

applyScaleAndOffset(cloned, channel);
applyScaleAndOffset(cloned, this.channel);

this.listener.onChannelEvent(new ChannelEvent(cloned));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2023 Eurotech and/or its affiliates and others
* Copyright (c) 2016, 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -27,7 +27,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.asset.AssetConfiguration;
import org.eclipse.kura.asset.provider.BaseAsset;
import org.eclipse.kura.channel.Channel;
Expand Down Expand Up @@ -116,7 +115,7 @@ public final class WireAsset extends BaseAsset implements WireEmitter, WireRecei
* Binds the Wire Helper Service.
*
* @param wireHelperService
* the new Wire Helper Service
* the new Wire Helper Service
*/
public void bindWireHelperService(final WireHelperService wireHelperService) {
if (isNull(this.wireHelperService)) {
Expand All @@ -128,7 +127,7 @@ public void bindWireHelperService(final WireHelperService wireHelperService) {
* Unbinds the Wire Helper Service.
*
* @param wireHelperService
* the new Wire Helper Service
* the new Wire Helper Service
*/
public void unbindWireHelperService(final WireHelperService wireHelperService) {
if (this.wireHelperService == wireHelperService) {
Expand All @@ -140,9 +139,9 @@ public void unbindWireHelperService(final WireHelperService wireHelperService) {
* OSGi service component activation callback.
*
* @param componentContext
* the component context
* the component context
* @param properties
* the service properties
* the service properties
*/
@Override
protected void activate(final ComponentContext componentContext, final Map<String, Object> properties) {
Expand All @@ -157,7 +156,7 @@ protected void activate(final ComponentContext componentContext, final Map<Strin
* OSGi service component update callback.
*
* @param properties
* the service properties
* the service properties
*/
@Override
public void updated(final Map<String, Object> properties) {
Expand All @@ -178,7 +177,7 @@ public void updated(final Map<String, Object> properties) {
* OSGi service component deactivate callback.
*
* @param context
* the context
* the context
*/
@Override
protected void deactivate(final ComponentContext context) {
Expand Down Expand Up @@ -217,9 +216,9 @@ protected String getFactoryPid() {
* Component(s).
*
* @param wireEnvelope
* the received {@link WireEnvelope}
* the received {@link WireEnvelope}
* @throws NullPointerException
* if {@link WireEnvelope} is null
* if {@link WireEnvelope} is null
*/
@Override
public void onWireReceive(final WireEnvelope wireEnvelope) {
Expand Down Expand Up @@ -249,7 +248,7 @@ private void emitAllReadChannels() {
try {
emitChannelRecords(readAllChannels());
} catch (final Exception e) {
logger.error("Error while performing read from the Wire Asset...", e);
logger.error("Error while performing read from the Wire Asset: {}", getKuraServicePid(), e);
}
}
}
Expand All @@ -258,13 +257,13 @@ private void emitAllReadChannels() {
* Determine the channels to write
*
* @param records
* the list of {@link WireRecord}s to parse
* the list of {@link WireRecord}s to parse
* @return list of Channel Records containing the values to be written
* @throws NullPointerException
* if argument is null
* if argument is null
*/
private List<ChannelRecord> determineWritingChannels(final WireRecord record) {
requireNonNull(record, "Wire Record cannot be null");
private List<ChannelRecord> determineWritingChannels(final WireRecord wireRecord) {
requireNonNull(wireRecord, "Wire Record cannot be null");

final List<ChannelRecord> channelRecordsToWrite = CollectionUtil.newArrayList();
final AssetConfiguration assetConfiguration = getAssetConfiguration();
Expand All @@ -278,7 +277,7 @@ private List<ChannelRecord> determineWritingChannels(final WireRecord record) {
continue;
}

Map<String, TypedValue<?>> wireRecordProperties = record.getProperties();
Map<String, TypedValue<?>> wireRecordProperties = wireRecord.getProperties();

if (wireRecordProperties.containsKey(channelName)) {
final TypedValue<?> value = wireRecordProperties.get(channelName);
Expand All @@ -294,13 +293,13 @@ private List<ChannelRecord> determineWritingChannels(final WireRecord record) {
* Emit the provided list of channel records to the associated wires.
*
* @param channelRecords
* the list of channel records conforming to the
* aforementioned
* specification
* the list of channel records conforming to the
* aforementioned
* specification
* @throws NullPointerException
* if provided records list is null
* if provided records list is null
* @throws IllegalArgumentException
* if provided records list is empty
* if provided records list is empty
*/
private void emitChannelRecords(final List<ChannelRecord> channelRecords) {
requireNonNull(channelRecords, "List of Channel Records cannot be null");
Expand All @@ -323,12 +322,8 @@ private void emitChannelRecords(final List<ChannelRecord> channelRecords) {
return;
}

try {
wireRecordProperties.put(WireAssetConstants.PROP_ASSET_NAME.value(),
TypedValues.newStringValue(getKuraServicePid()));
} catch (KuraException e) {
logger.error("Configurations cannot be null", e);
}
wireRecordProperties.put(WireAssetConstants.PROP_ASSET_NAME.value(),
TypedValues.newStringValue(getKuraServicePid()));

this.wireSupport.emit(Collections.singletonList(new WireRecord(wireRecordProperties)));
}
Expand All @@ -337,9 +332,9 @@ private void emitChannelRecords(final List<ChannelRecord> channelRecords) {
* Perform Channel Write operation
*
* @param channelRecordsToWrite
* the list of {@link ChannelRecord}s
* the list of {@link ChannelRecord}s
* @throws NullPointerException
* if the provided list is null
* if the provided list is null
*/
private void writeChannels(final List<ChannelRecord> channelRecordsToWrite) {
requireNonNull(channelRecordsToWrite, "List of Channel Records cannot be null");
Expand All @@ -350,15 +345,16 @@ private void writeChannels(final List<ChannelRecord> channelRecordsToWrite) {
try {
write(channelRecordsToWrite);
} catch (final Exception e) {
logger.error("Error while performing write from the Wire Asset...", e);
logger.error("Error while performing write from the Wire Asset: {}", getKuraServicePid(), e);
}
}

private boolean isListeningChannel(final Map<String, Object> properties) {
try {
return Boolean.parseBoolean(properties.get(WireAssetConstants.LISTEN_PROP_NAME.value()).toString());
} catch (Exception e) {
logger.warn("Failed to retreive \"listen\" property from channel configuration");
logger.warn("Failed to retreive \"listen\" property from channel configuration from the Wire Asset: {}",
getKuraServicePid());
return false;
}
}
Expand Down

0 comments on commit f333f7c

Please sign in to comment.