Search in sources :

Example 1 with CameraMessage

use of com.robo4j.socket.http.codec.CameraMessage in project robo4j by Robo4J.

the class HttpUnitTests method testHttpCameraMessage.

@Test
void testHttpCameraMessage() {
    final String jsonCammeraMessageCorrupted = "{ \"type\"  :  \"jpg\" ,  \"value\"   :  \"description\"  ,\"image\":\"12345\"}";
    final String jsonCammeraMessage = "{\"type\":\"jpg\",\"value\":\"description\",\"image\":\"12345\"}";
    CodecRegistry registry = new CodecRegistry("com.robo4j.socket.http.codec");
    SocketEncoder<CameraMessage, String> encoder = registry.getEncoder(CameraMessage.class);
    SocketDecoder<String, CameraMessage> decoder = registry.getDecoder(CameraMessage.class);
    assertNotNull(encoder);
    assertNotNull(decoder);
    CameraMessage cameraMessage = new CameraMessage("jpg", "description", "12345");
    String encoded = encoder.encode(cameraMessage);
    CameraMessage decoded = decoder.decode(jsonCammeraMessageCorrupted);
    assertEquals(jsonCammeraMessage, encoded);
    assertEquals(cameraMessage.getType(), decoded.getType());
    assertEquals(cameraMessage.getValue(), decoded.getValue());
}
Also used : CameraMessage(com.robo4j.socket.http.codec.CameraMessage) Test(org.junit.jupiter.api.Test)

Example 2 with CameraMessage

use of com.robo4j.socket.http.codec.CameraMessage in project robo4j by Robo4J.

the class ImageDecoratorUnit method onMessage.

// TODO: 12/10/17 (miro) : review header, try to simplify
@Override
public void onMessage(ImageDTO image) {
    final String imageBase64 = JsonUtil.toBase64String(image.getContent());
    final CameraMessage cameraMessage = new CameraMessage(image.getEncoding(), String.valueOf(imageNumber.incrementAndGet()), imageBase64);
    final ClientMessageWrapper resultMessage = new ClientMessageWrapper(HttpPathUtils.toPath(SystemPath.UNITS.getPath(), httpTarget), CameraMessage.class, cameraMessage);
    System.out.println(getClass().getSimpleName() + " image target: " + target + " resultMessage: " + resultMessage.getPath());
    getContext().getReference(target).sendMessage(resultMessage);
}
Also used : CameraMessage(com.robo4j.socket.http.codec.CameraMessage) ClientMessageWrapper(com.robo4j.socket.http.units.ClientMessageWrapper)

Example 3 with CameraMessage

use of com.robo4j.socket.http.codec.CameraMessage in project robo4j by Robo4J.

the class CameraImageProducerDesTestUnit method createImage.

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);
}
Also used : CameraMessage(com.robo4j.socket.http.codec.CameraMessage) ClientMessageWrapper(com.robo4j.socket.http.units.ClientMessageWrapper)

Example 4 with CameraMessage

use of com.robo4j.socket.http.codec.CameraMessage in project robo4j by Robo4J.

the class HttpUnitTests method testHttpCameraRealMessageImage.

@Test
void testHttpCameraRealMessageImage() throws Exception {
    String encodedImage = HttpUnitHelper.getExampleCamera();
    final String jsonCammeraMessageCorrupted = "{ \"type\"  :  \"jpg\" ,  \"value\"   :  \"description\"  ,\"image\":\"" + encodedImage + "\"}";
    final String jsonCammeraMessage = "{\"type\":\"jpg\",\"value\":\"description\",\"image\":\"" + encodedImage + "\"}";
    CodecRegistry registry = new CodecRegistry("com.robo4j.socket.http.codec");
    SocketEncoder<CameraMessage, String> encoder = registry.getEncoder(CameraMessage.class);
    SocketDecoder<String, CameraMessage> decoder = registry.getDecoder(CameraMessage.class);
    assertNotNull(encoder);
    assertNotNull(decoder);
    CameraMessage cameraMessage = new CameraMessage("jpg", "description", encodedImage);
    String encoded = encoder.encode(cameraMessage);
    CameraMessage decoded = decoder.decode(jsonCammeraMessageCorrupted);
    final byte[] imageDecoded = Base64.getDecoder().decode(decoded.getImage());
    assertEquals(jsonCammeraMessage, encoded);
    assertEquals(cameraMessage.getType(), decoded.getType());
    assertEquals(cameraMessage.getValue(), decoded.getValue());
    assertNotNull(imageDecoded);
    assertTrue(imageDecoded.length > 0);
}
Also used : CameraMessage(com.robo4j.socket.http.codec.CameraMessage) Test(org.junit.jupiter.api.Test)

Example 5 with CameraMessage

use of com.robo4j.socket.http.codec.CameraMessage in project robo4j by Robo4J.

the class HttpUnitTests method testHttpCameraMessageImage.

@Test
void testHttpCameraMessageImage() throws Exception {
    final InputStream imageData = new BufferedInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream("snapshot.png"));
    byte[] imageArray = StreamUtils.inputStreamToByteArray(imageData);
    String encodedImage = Base64.getEncoder().encodeToString(imageArray);
    final String jsonCameraMessageCorrupted = "{ \"type\"  :  \"jpg\" ,  \"value\"   :  \"description\"  ,\"image\":\"" + encodedImage + "\"}";
    final String jsonCameraMessage = "{\"type\":\"jpg\",\"value\":\"description\",\"image\":\"" + encodedImage + "\"}";
    CodecRegistry registry = new CodecRegistry("com.robo4j.socket.http.codec");
    SocketEncoder<CameraMessage, String> encoder = registry.getEncoder(CameraMessage.class);
    SocketDecoder<String, CameraMessage> decoder = registry.getDecoder(CameraMessage.class);
    assertNotNull(encoder);
    assertNotNull(decoder);
    CameraMessage cameraMessage = new CameraMessage("jpg", "description", encodedImage);
    String encoded = encoder.encode(cameraMessage);
    CameraMessage decoded = decoder.decode(jsonCameraMessageCorrupted);
    final byte[] imageDecoded = Base64.getDecoder().decode(decoded.getImage());
    assertEquals(jsonCameraMessage, encoded);
    assertEquals(cameraMessage.getType(), decoded.getType());
    assertEquals(cameraMessage.getValue(), decoded.getValue());
    assertEquals(imageArray.length, imageDecoded.length);
}
Also used : CameraMessage(com.robo4j.socket.http.codec.CameraMessage) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Test(org.junit.jupiter.api.Test)

Aggregations

CameraMessage (com.robo4j.socket.http.codec.CameraMessage)8 Test (org.junit.jupiter.api.Test)6 InputStream (java.io.InputStream)3 RoboBuilder (com.robo4j.RoboBuilder)2 RoboContext (com.robo4j.RoboContext)2 ClientMessageWrapper (com.robo4j.socket.http.units.ClientMessageWrapper)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 BufferedInputStream (java.io.BufferedInputStream)1