Skip to content

Commit

Permalink
[69] final cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mirage22 committed Oct 9, 2024
1 parent 24d7e21 commit c8bbe1d
Show file tree
Hide file tree
Showing 20 changed files with 1,557 additions and 1,628 deletions.

This file was deleted.

1 change: 0 additions & 1 deletion robo4j-core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
exports com.robo4j;
exports com.robo4j.util;
exports com.robo4j.configuration;
exports com.robo4j.logging;
exports com.robo4j.reflect;
exports com.robo4j.scheduler;
exports com.robo4j.net;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import com.robo4j.RoboContext;
import com.robo4j.RoboUnit;
import com.robo4j.configuration.Configuration;
import com.robo4j.logging.SimpleLoggingUtil;
import com.robo4j.socket.http.codec.CameraMessage;
import com.robo4j.socket.http.enums.SystemPath;
import com.robo4j.socket.http.units.ClientMessageWrapper;
import com.robo4j.socket.http.util.HttpPathUtils;
import com.robo4j.socket.http.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -41,6 +42,7 @@
*/
@CriticalSectionTrait
public class ImageDecoratorUnit extends RoboUnit<CameraImageDTO> {
private static final Logger LOGGER = LoggerFactory.getLogger(ImageDecoratorUnit.class);
private static final String PROPERTY_TARGET = "target";

private final AtomicInteger imageNumber = new AtomicInteger(0);
Expand Down Expand Up @@ -69,7 +71,7 @@ public void onMessage(CameraImageDTO image) {
String.valueOf(imageNumber.incrementAndGet()), imageBase64);
final ClientMessageWrapper resultMessage = new ClientMessageWrapper(
HttpPathUtils.toPath(SystemPath.UNITS.getPath(), httpTarget), CameraMessage.class, cameraMessage);
SimpleLoggingUtil.debug(ImageDecoratorUnit.class, "image target: " + target + " resultMessage: " + resultMessage.getPath());
LOGGER.info("image target:{},resultMessage:{}", target, resultMessage.getPath());
getContext().getReference(target).sendMessage(resultMessage);

}
Expand Down
1 change: 1 addition & 0 deletions robo4j-units-rpi-http/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module robo4j.units.rpi.http {
requires robo4j.http;
requires org.slf4j;

exports com.robo4j.units.rpi.http.camera to robo4j.core;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
import com.robo4j.RoboContext;
import com.robo4j.RoboUnit;
import com.robo4j.configuration.Configuration;
import com.robo4j.logging.SimpleLoggingUtil;
import com.robo4j.socket.http.codec.CameraMessage;
import com.robo4j.socket.http.enums.SystemPath;
import com.robo4j.socket.http.units.ClientMessageWrapper;
import com.robo4j.socket.http.util.HttpPathUtils;
import com.robo4j.socket.http.util.JsonUtil;
import com.robo4j.util.StreamUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.EnumSet;
import java.util.concurrent.CountDownLatch;
Expand All @@ -43,85 +44,85 @@
public class CameraImageProducerDesTestUnit extends RoboUnit<Boolean> {


public static final String ATTRIBUTE_NUMBER_OF_SENT_IMAGES_NAME = "numberOfSentImages";
public static final String ATTR_TOTAL_IMAGES = "numberOfImages";
public static final String ATTR_GENERATED_IMAGES_LATCH = "generatedImagesLatch";
public static final DefaultAttributeDescriptor<CountDownLatch> DESCRIPTOR_GENERATED_IMAGES_LATCH = DefaultAttributeDescriptor
.create(CountDownLatch.class, ATTR_GENERATED_IMAGES_LATCH);
public static final AttributeDescriptor<Integer> DESCRIPTOR_TOTAL_IMAGES = new DefaultAttributeDescriptor<>(Integer.class,
CameraImageProducerDesTestUnit.ATTR_TOTAL_IMAGES);
protected static final String IMAGE_ENCODING = "jpg";
public static final String ATTRIBUTE_NUMBER_OF_SENT_IMAGES_NAME = "numberOfSentImages";
public static final String ATTR_TOTAL_IMAGES = "numberOfImages";
public static final String ATTR_GENERATED_IMAGES_LATCH = "generatedImagesLatch";
public static final DefaultAttributeDescriptor<CountDownLatch> DESCRIPTOR_GENERATED_IMAGES_LATCH = DefaultAttributeDescriptor
.create(CountDownLatch.class, ATTR_GENERATED_IMAGES_LATCH);
public static final AttributeDescriptor<Integer> DESCRIPTOR_TOTAL_IMAGES = new DefaultAttributeDescriptor<>(Integer.class,
CameraImageProducerDesTestUnit.ATTR_TOTAL_IMAGES);
protected static final String IMAGE_ENCODING = "jpg";
private static final Logger LOGGER = LoggerFactory.getLogger(CameraImageProducerDesTestUnit.class);
protected final AtomicBoolean progress = new AtomicBoolean(false);
protected String target;
protected String httpTarget;
protected String fileName;
private volatile AtomicInteger counter = new AtomicInteger(0);
protected volatile CountDownLatch generatedImagesLatch;
private Integer numberOfImages;

protected final AtomicBoolean progress = new AtomicBoolean(false);
protected String target;
protected String httpTarget;
protected String fileName;
private volatile AtomicInteger counter = new AtomicInteger(0);
protected volatile CountDownLatch generatedImagesLatch;
private Integer numberOfImages;
public CameraImageProducerDesTestUnit(RoboContext context, String id) {
super(Boolean.class, context, id);
}

public CameraImageProducerDesTestUnit(RoboContext context, String id) {
super(Boolean.class, context, id);
}
@Override
protected void onInitialization(Configuration configuration) throws ConfigurationException {
target = configuration.getString("target", null);
httpTarget = configuration.getString("httpTarget", null);
fileName = configuration.getString("fileName", null);
numberOfImages = configuration.getInteger(ATTR_TOTAL_IMAGES, null);
generatedImagesLatch = new CountDownLatch(numberOfImages);
}

@Override
protected void onInitialization(Configuration configuration) throws ConfigurationException {
target = configuration.getString("target", null);
httpTarget = configuration.getString("httpTarget", null);
fileName = configuration.getString("fileName", null);
numberOfImages = configuration.getInteger(ATTR_TOTAL_IMAGES, null);
generatedImagesLatch = new CountDownLatch(numberOfImages);
}
@Override
public void onMessage(Boolean message) {
if (message) {
LOGGER.info("message:{}", message);
createImage(counter.get());
}
}

@Override
public void onMessage(Boolean message) {
if (message) {
SimpleLoggingUtil.debug(getClass(), "message: " + message);
createImage(counter.get());
}
}
@Override
public void start() {
EnumSet<LifecycleState> acceptedStates = EnumSet.of(LifecycleState.STARTING, LifecycleState.STARTED);
getContext().getScheduler().execute(() -> {
while (acceptedStates.contains(getState())) {
if (progress.compareAndSet(false, true) && counter.getAndIncrement() < numberOfImages) {
createImage(counter.get());
}
}
});
}

@Override
public void start() {
EnumSet<LifecycleState> acceptedStates = EnumSet.of(LifecycleState.STARTING, LifecycleState.STARTED);
getContext().getScheduler().execute(() -> {
while (acceptedStates.contains(getState())) {
if (progress.compareAndSet(false, true) && counter.getAndIncrement() < numberOfImages) {
createImage(counter.get());
}
}
});
}
@SuppressWarnings("unchecked")
@Override
protected synchronized <R> R onGetAttribute(AttributeDescriptor<R> descriptor) {
if (descriptor.getAttributeType() == Integer.class) {
if (descriptor.getAttributeName().equals(ATTR_TOTAL_IMAGES)) {
return (R) numberOfImages;
} else if (descriptor.getAttributeName().equals(ATTRIBUTE_NUMBER_OF_SENT_IMAGES_NAME)) {
return (R) Integer.valueOf(counter.get());
}
}
if (descriptor.getAttributeName().equals(ATTR_GENERATED_IMAGES_LATCH) && descriptor.getAttributeType() == CountDownLatch.class) {
return (R) generatedImagesLatch;
}
return super.onGetAttribute(descriptor);
}

@SuppressWarnings("unchecked")
@Override
protected synchronized <R> R onGetAttribute(AttributeDescriptor<R> descriptor) {
if (descriptor.getAttributeType() == Integer.class) {
if (descriptor.getAttributeName().equals(ATTR_TOTAL_IMAGES)) {
return (R) numberOfImages;
} else if (descriptor.getAttributeName().equals(ATTRIBUTE_NUMBER_OF_SENT_IMAGES_NAME)) {
return (R) Integer.valueOf(counter.get());
}
}
if(descriptor.getAttributeName().equals(ATTR_GENERATED_IMAGES_LATCH) && descriptor.getAttributeType() == CountDownLatch.class ){
return (R) generatedImagesLatch;
}
return super.onGetAttribute(descriptor);
}
protected void countDonwSentImage() {

protected void countDonwSentImage(){
}

}

protected void createImage(int imageNumber) {
final byte[] image = StreamUtils
.inputStreamToByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
final CameraMessage cameraMessage = new CameraMessage(IMAGE_ENCODING, String.valueOf(imageNumber),
JsonUtil.toBase64String(image));
final ClientMessageWrapper resultMessage = new ClientMessageWrapper(
HttpPathUtils.toPath(SystemPath.UNITS.getPath(), httpTarget), CameraMessage.class, cameraMessage);
getContext().getReference(target).sendMessage(resultMessage);
generatedImagesLatch.countDown();
progress.set(false);
}
protected void createImage(int imageNumber) {
final byte[] image = StreamUtils
.inputStreamToByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
final CameraMessage cameraMessage = new CameraMessage(IMAGE_ENCODING, String.valueOf(imageNumber),
JsonUtil.toBase64String(image));
final ClientMessageWrapper resultMessage = new ClientMessageWrapper(
HttpPathUtils.toPath(SystemPath.UNITS.getPath(), httpTarget), CameraMessage.class, cameraMessage);
getContext().getReference(target).sendMessage(resultMessage);
generatedImagesLatch.countDown();
progress.set(false);
}
}
Loading

0 comments on commit c8bbe1d

Please sign in to comment.