Search in sources :

Example 31 with InfomodelValue

use of org.eclipse.vorto.model.runtime.InfomodelValue in project vorto by eclipse.

the class DateConvertTest method testMappingTimestamp.

@Test
public void testMappingTimestamp() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithTimestamp()).registerConverterFunction(DateFunctionFactory.createFunctions()).build();
    final Date timestamp = new Date();
    String json = "{\"time\" : " + timestamp.getTime() + "}";
    InfomodelValue mappedOutput = mapper.mapSource(gson.fromJson(json, Object.class));
    FunctionblockValue buttonFunctionblockData = mappedOutput.get("button");
    assertEquals(JSON_DATE_FORMAT.format(timestamp), buttonFunctionblockData.getStatusProperty("sensor_value").get().getValue());
    System.out.println(mappedOutput);
}
Also used : FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) Date(java.util.Date) Test(org.junit.Test)

Example 32 with InfomodelValue

use of org.eclipse.vorto.model.runtime.InfomodelValue in project vorto by eclipse.

the class JsonMappingTest method testMapping.

@Test
public void testMapping() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithCustomFunction()).registerConverterFunction(TypeFunctionFactory.createFunctions()).registerConverterFunction(StringFunctionFactory.createFunctions()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
    String json = "{\"clickType\" : \"DOUBLE\", \"batteryVoltage\": \"2322mV\"}";
    InfomodelValue mappedOutput = mapper.mapSource(gson.fromJson(json, Object.class));
    FunctionblockValue buttonFunctionblockData = mappedOutput.get("button");
    assertEquals(true, (Boolean) buttonFunctionblockData.getStatusProperty("digital_input_state").get().getValue());
    assertEquals(2, buttonFunctionblockData.getStatusProperty("digital_input_count").get().getValue());
    FunctionblockValue voltageFunctionblockData = mappedOutput.get("voltage");
    assertEquals(2322f, voltageFunctionblockData.getStatusProperty("sensor_value").get().getValue());
    assertEquals("mV", voltageFunctionblockData.getStatusProperty("sensor_units").get().getValue());
    System.out.println(gson.toJson(mappedOutput.serialize()));
}
Also used : FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) Test(org.junit.Test)

Example 33 with InfomodelValue

use of org.eclipse.vorto.model.runtime.InfomodelValue in project vorto by eclipse.

the class BinaryMappingTest method testMappingWithGattStructure.

@Test
public void testMappingWithGattStructure() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecGattConverter()).registerConverterFunction(BinaryFunctionFactory.createFunctions()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
    GattDevice gattDevice = new GattDevice();
    GattService gattService = new GattService();
    List<GattCharacteristic> characteristics = new ArrayList<GattCharacteristic>();
    byte[] dest = new byte[6];
    byte[] value = Conversion.intToByteArray(2000, 0, dest, 3, 3);
    characteristics.add(new GattCharacteristic("23-D1-13-EF-5F-78-23-15-DE-EF-12-12-0D-F0-00-00", value));
    gattService.setCharacteristics(characteristics);
    List<GattService> services = new ArrayList<GattService>();
    services.add(gattService);
    gattDevice.setModelNumber("23-23-23");
    gattDevice.setServices(services);
    gattDevice.setCharacteristics(characteristics);
    String json = new Gson().toJson(gattDevice);
    InfomodelValue mapped = mapper.mapSource(gson.fromJson(json, Object.class));
    assertEquals(20.00, mapped.get("button").getStatusProperty("sensor_value").get().getValue());
    System.out.println(mapped);
}
Also used : GattCharacteristic(org.eclipse.vorto.mapping.engine.model.blegatt.GattCharacteristic) GattDevice(org.eclipse.vorto.mapping.engine.model.blegatt.GattDevice) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) GattService(org.eclipse.vorto.mapping.engine.model.blegatt.GattService) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) Test(org.junit.Test)

Example 34 with InfomodelValue

use of org.eclipse.vorto.model.runtime.InfomodelValue in project vorto by eclipse.

the class BinaryMappingTest method testMapUsingBase64Converter.

@Test
public void testMapUsingBase64Converter() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithBase64Converter()).registerConverterFunction(BinaryFunctionFactory.createFunctions()).build();
    String json = "{\"data\" : \"" + Base64.encodeBase64String("20".getBytes()) + "\"}";
    InfomodelValue mappedOutput = mapper.mapSource(gson.fromJson(json, Object.class));
    assertEquals("20", new String((byte[]) mappedOutput.get("button").getStatusProperty("digital_input_state").get().getValue()));
}
Also used : IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) Test(org.junit.Test)

Example 35 with InfomodelValue

use of org.eclipse.vorto.model.runtime.InfomodelValue in project vorto by eclipse.

the class Main method main.

public static void main(String[] args) throws Exception {
    MappingEngine engine = MappingEngine.createFromInputStream(FileUtils.openInputStream(new File("src/test/resources/mappingspec.json")));
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("distance", "100m");
    InfomodelValue result = engine.mapSource(data);
    if (result.validate().isValid()) {
        JsonObject dittoPayloadToUpdateAllFeatures = TwinPayloadFactory.toDittoProtocol(result, "org.eclipse.vorto", "deviceid-123");
        System.out.println(gson.toJson(dittoPayloadToUpdateAllFeatures));
        JsonObject dittoPayloadToUpdateSingleFeature = TwinPayloadFactory.toDittoProtocol(result.get("distance"), "distance", "org.eclipse.vorto", "deviceid-123");
        System.out.println(gson.toJson(dittoPayloadToUpdateSingleFeature));
    } else {
        System.err.println("Mapped data is not valid to Vorto Model");
    }
}
Also used : HashMap(java.util.HashMap) MappingEngine(org.eclipse.vorto.mapping.engine.MappingEngine) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) File(java.io.File)

Aggregations

InfomodelValue (org.eclipse.vorto.model.runtime.InfomodelValue)58 Test (org.junit.Test)54 FunctionblockValue (org.eclipse.vorto.model.runtime.FunctionblockValue)29 IDataMapper (org.eclipse.vorto.mapping.engine.IDataMapper)25 JUnitPerfTest (com.github.noconnor.junitperf.JUnitPerfTest)24 JavascriptEvalProvider (org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider)10 IPayloadDeserializer (org.eclipse.vorto.mapping.engine.decoder.IPayloadDeserializer)10 JSONDeserializer (org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer)10 Random (java.util.Random)8 EntityPropertyValue (org.eclipse.vorto.model.runtime.EntityPropertyValue)8 Infomodel (org.eclipse.vorto.model.Infomodel)5 FunctionblockModel (org.eclipse.vorto.model.FunctionblockModel)4 HashMap (java.util.HashMap)3 IMappingSpecification (org.eclipse.vorto.mapping.engine.model.spec.IMappingSpecification)3 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 File (java.io.File)2 MappingEngine (org.eclipse.vorto.mapping.engine.MappingEngine)2 BinaryData (org.eclipse.vorto.mapping.engine.model.binary.BinaryData)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1