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());
}
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);
}
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);
}
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);
}
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);
}
Aggregations