Skip to content

Commit

Permalink
Cleaned up return values by returning DTOs. Added Converters to conve…
Browse files Browse the repository at this point in the history
…rt DTOs to String values.

Signed-off-by: David Leangen <osgi@leangen.net>
  • Loading branch information
David Leangen committed Mar 11, 2024
1 parent aed98c6 commit aa24cd5
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 111 deletions.
2 changes: 0 additions & 2 deletions platform/sensinact-shell/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.sensinact.gateway.core</groupId>
<artifactId>impl</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,14 @@ public String get(
* @param resource the ID of the resource
* @param type type shorthand type of the resource to get (String, Integer, int)
*/
@SuppressWarnings("unchecked")
@Descriptor("Get the value of a resource.\n\n This variant accepts a simplified type for convenience.\n")
public <T>T get(
@Descriptor(" the provider ID") String provider,
@Descriptor(" the service ID") String service,
@Descriptor(" the resource ID") String resource,
@Descriptor("the simplified type of the resource value to get (one of: String, Integer, int)") ResourceType<T> type ) {

final T result = session.get().getResourceValue(provider, service, resource, type.type);
if (result == null)
return (T)"<NULL>";
return result;
return session.get().getResourceValue(provider, service, resource, type.type);
}

/**
Expand All @@ -77,17 +73,13 @@ public <T>T get(
* @param resource the ID of the resource
* @param type type fqn of the type of the resource to get
*/
@SuppressWarnings("unchecked")
@Descriptor("Get the value of a resource.\n\n This variant requires the FQN of the value type (i.e. java.lang.String).\n")
public <T>T get(
@Descriptor("the provider ID") String provider,
@Descriptor("the service ID") String service,
@Descriptor("the resource ID") String resource,
@Descriptor(" the type of the resource value to get") Class<T> type ) {

final T result = session.get().getResourceValue(provider, service, resource, type);
if (result == null)
return (T)"<NULL>";
return result;
return session.get().getResourceValue(provider, service, resource, type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
)
public class MetaCommands {

public static class MetaDTO {
public String provider;
public String service;
public String resource;
public Map<String, Object> metadata;
}

@Reference private SensiNactCommandSession session;

/**
Expand All @@ -40,25 +47,18 @@ public class MetaCommands {
* @param resource the ID of the resource
*/
@Descriptor("List all the metadata properties for a resource")
public String meta(
public MetaDTO meta(
@Descriptor("the provider ID") String provider,
@Descriptor("the service ID") String service,
@Descriptor("the resource ID") String resource) {

final Map<String, Object> description = session.get().getResourceMetadata(provider, service, resource);
if (description == null)
return "<NULL>";

final String data = description.entrySet().stream()
.map(e -> " " + e.getKey() + " = " + e.getValue())
.collect(Collectors.joining("\n "));

return "\n"
+ "Resource: " + provider + "/" + service + "/" + resource + "\n"
+ "\n"
+ " Metadata\n"
+ " --------\n"
+ " " + data + "\n";
final Map<String, Object> metadata = session.get().getResourceMetadata(provider, service, resource);
final MetaDTO dto = new MetaDTO();
dto.provider = provider;
dto.service = service;
dto.resource = resource;
dto.metadata = metadata;
return dto;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.osgi.service.component.annotations.Reference;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author David Leangen
Expand All @@ -37,10 +36,8 @@ public class ProviderCommands {
* List all providers.
*/
@Descriptor("List all providers")
public List<String> providers() {
return session.get().listProviders().stream()
.map(description -> description.provider)
.collect(Collectors.toList());
public List<ProviderDescription> providers() {
return session.get().listProviders();
}

/**
Expand All @@ -49,19 +46,7 @@ public List<String> providers() {
* @param provider the ID of the provider
*/
@Descriptor("Describe a provider")
public String provider(@Descriptor("the provider ID") String provider) {
final ProviderDescription description = session.get().describeProvider(provider);
if (description == null)
return "<NULL>";

final String services = description.services.stream()
.collect(Collectors.joining("\n "));

return "\n"
+ "Provider: " + description.provider + "\n"
+ "\n"
+ " Services\n"
+ " --------\n"
+ " " + services + "\n";
public ProviderDescription provider(@Descriptor("the provider ID") String provider) {
return session.get().describeProvider(provider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
**********************************************************************/
package org.eclipse.sensinact.gateway.commands.gogo;

import java.util.List;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.apache.felix.service.command.Descriptor;
import org.apache.felix.service.command.annotations.GogoCommand;
import org.eclipse.sensinact.core.session.ResourceDescription;
Expand All @@ -42,22 +38,10 @@ public class ResourceCommands {
* @param service the ID of the service
*/
@Descriptor("List all resources for a given service")
public String resources(
public ServiceDescription resources(
@Descriptor("the provider ID") String provider,
@Descriptor("the service ID") String service) {
final ServiceDescription description = session.get().describeService(provider, service);
if (description == null)
return "<NULL>";

final String resources = description.resources.stream()
.collect(Collectors.joining("\n "));

return "\n"
+ "Service: " + description.provider + "\n"
+ "\n"
+ " Resources\n"
+ " ---------\n"
+ " " + resources + "\n";
return session.get().describeService(provider, service);
}

/**
Expand All @@ -68,25 +52,10 @@ public String resources(
* @param resource the ID of the resource
*/
@Descriptor("Describe a resource")
public String resource(
public ResourceDescription resource(
@Descriptor("the provider ID") String provider,
@Descriptor("the service ID") String service,
@Descriptor("the resource ID") String resource) {
final ResourceDescription description = session.get().describeResource(provider, service, resource);
if (description == null)
return "<NULL>";

final List<Entry<String, Class<?>>> actTypes = description.actMethodArgumentsTypes;
final String params = actTypes == null ? "<NONE>" : actTypes.stream()
.map(e -> e.getKey() + " (" + e.getValue().toString() + ")")
.collect(Collectors.joining(", "));

return "\n"
+ "Resource: " + description.provider + "/" + description.service + "/" + description.resource + "\n"
+ "\n"
+ " Resource Type: " + description.resourceType + "\n"
+ " Value Type: " + description.valueType + "\n"
+ " Content Type: " + description.contentType + "\n"
+ " ACT params: " + params + "\n";
return session.get().describeResource(provider, service, resource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
**********************************************************************/
package org.eclipse.sensinact.gateway.commands.gogo;

import java.util.stream.Collectors;

import org.apache.felix.service.command.Descriptor;
import org.apache.felix.service.command.annotations.GogoCommand;
import org.eclipse.sensinact.core.session.ProviderDescription;
Expand All @@ -39,20 +37,8 @@ public class ServiceCommands {
* @param provider the ID of the provider
*/
@Descriptor("List all services for a given provider")
public String services(@Descriptor("the provider ID") String provider) {
final ProviderDescription description = session.get().describeProvider(provider);
if (description == null)
return "<NULL>";

final String services = description.services.stream()
.collect(Collectors.joining("\n "));

return "\n"
+ "Provider: " + description.provider + "\n"
+ "\n"
+ " Services\n"
+ " --------\n"
+ " " + services + "\n";
public ProviderDescription services(@Descriptor("the provider ID") String provider) {
return session.get().describeProvider(provider);
}

/**
Expand All @@ -62,21 +48,9 @@ public String services(@Descriptor("the provider ID") String provider) {
* @param service the ID of the service
*/
@Descriptor("Describe a service")
public String service(
public ServiceDescription service(
@Descriptor("the provider ID") String provider,
@Descriptor("the service ID") String service) {
final ServiceDescription description = session.get().describeService(provider, service);
if (description == null)
return "<NULL>";

final String services = description.resources.stream()
.collect(Collectors.joining("\n "));

return "\n"
+ "Service: " + description.provider + "\n"
+ "\n"
+ " Resources\n"
+ " ---------\n"
+ " " + services + "\n";
return session.get().describeService(provider, service);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*********************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Kentyou - initial implementation
**********************************************************************/
package org.eclipse.sensinact.gateway.commands.gogo.converter;

import java.util.stream.Collectors;

import org.apache.felix.service.command.Converter;
import org.eclipse.sensinact.gateway.commands.gogo.MetaCommands.MetaDTO;
import org.osgi.service.component.annotations.Component;

/**
* @author David Leangen
*/
@Component
public class MetadataConverter
implements Converter
{
@Override
public Object convert(Class<?> desiredType, Object in)
throws Exception
{
return null;
}

@Override
public CharSequence format(Object target, int level, Converter escape)
throws Exception
{
if (target instanceof MetaDTO) {
final MetaDTO dto = (MetaDTO) target;

if (level == INSPECT) {

if (dto.metadata == null)
return "<EMPTY>";

final String data = dto.metadata.entrySet().stream()
.map(e -> " " + e.getKey() + " = " + e.getValue())
.collect(Collectors.joining("\n "));

return "\n"
+ "Resource: " + dto.provider + "/" + dto.service + "/" + dto.resource + "\n"
+ "\n"
+ " Metadata\n"
+ " --------\n"
+ " " + data + "\n";
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*********************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Kentyou - initial implementation
**********************************************************************/
package org.eclipse.sensinact.gateway.commands.gogo.converter;

import java.util.stream.Collectors;

import org.apache.felix.service.command.Converter;
import org.eclipse.sensinact.core.session.ProviderDescription;
import org.osgi.service.component.annotations.Component;

/**
* @author David Leangen
*/
@Component
public class ProviderConverter
implements Converter
{
@Override
public Object convert(Class<?> desiredType, Object in)
throws Exception
{
return null;
}

@Override
public CharSequence format(Object target, int level, Converter escape)
throws Exception
{
if (target instanceof ProviderDescription) {
final ProviderDescription description = (ProviderDescription) target;

if (level == LINE) {
return description.provider;
}

if (level == INSPECT) {

final String services = description.services.stream()
.collect(Collectors.joining("\n "));

return "\n"
+ "Provider: " + description.provider + "\n"
+ "\n"
+ " Services\n"
+ " --------\n"
+ " " + services + "\n";
}
}

return null;
}
}

0 comments on commit aa24cd5

Please sign in to comment.