Search in sources :

Example 6 with JavascriptEvalProvider

use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider in project vorto by eclipse.

the class JsonMappingTest method testMapSingleFunctionblockOfInfomodel.

@Test
public void testMapSingleFunctionblockOfInfomodel() {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithCustomFunction()).registerConverterFunction(TypeFunctionFactory.createFunctions()).registerConverterFunction(StringFunctionFactory.createFunctions()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
    String json = "{\"clickType\" : \"DOUBLE\"}";
    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");
    assertNull(voltageFunctionblockData);
    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 7 with JavascriptEvalProvider

use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider in project vorto by eclipse.

the class JsonMappingTest method testMappingWithMalicousScript2.

@Test(expected = MappingException.class)
public void testMappingWithMalicousScript2() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithMaliciousFunction() {

        @Override
        protected String getMaliciousFunctionBody() {
            return "return exit();";
        }
    }).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
    String json = "{\"clickType\" : \"DOUBLE\", \"batteryVoltage\": \"2322mV\"}";
    mapper.mapSource(gson.fromJson(json, Object.class));
}
Also used : IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) Test(org.junit.Test)

Example 8 with JavascriptEvalProvider

use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider in project vorto by eclipse.

the class BinaryMappingTest method testMappingBinaryContaining2DataPoints.

@Test
public void testMappingBinaryContaining2DataPoints() {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecBinaryConverter()).registerConverterFunction(BinaryFunctionFactory.createFunctions()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
    // 2 byte temperature (Byte 1-2), 2 byte humidity (Byte 3-4)
    byte[] dest = new byte[4];
    byte[] value = Conversion.intToByteArray(2000, 0, dest, 0, 2);
    value = Conversion.intToByteArray(8819, 0, dest, 2, 2);
    BinaryData data = new BinaryData(value);
    InfomodelValue mapped = mapper.mapSource(data);
    assertEquals(20.00, mapped.get("temperature").getStatusProperty("value").get().getValue());
    assertEquals(88.19, mapped.get("humidity").getStatusProperty("value").get().getValue());
}
Also used : IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) BinaryData(org.eclipse.vorto.mapping.engine.model.binary.BinaryData) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) Test(org.junit.Test)

Example 9 with JavascriptEvalProvider

use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider 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 10 with JavascriptEvalProvider

use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider 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)

Aggregations

JavascriptEvalProvider (org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider)15 IDataMapper (org.eclipse.vorto.mapping.engine.IDataMapper)13 Test (org.junit.Test)13 InfomodelValue (org.eclipse.vorto.model.runtime.InfomodelValue)10 FunctionblockValue (org.eclipse.vorto.model.runtime.FunctionblockValue)7 BinaryData (org.eclipse.vorto.mapping.engine.model.binary.BinaryData)3 BeforeClass (org.junit.BeforeClass)2 Gson (com.google.gson.Gson)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 SpecBinaryConverter (org.eclipse.vorto.mapping.engine.converter.binary.SpecBinaryConverter)1 SpecWithBase64Converter (org.eclipse.vorto.mapping.engine.converter.binary.SpecWithBase64Converter)1 SpecWithByteArrayConverter (org.eclipse.vorto.mapping.engine.converter.binary.SpecWithByteArrayConverter)1 SpecWithTimestamp (org.eclipse.vorto.mapping.engine.converter.date.SpecWithTimestamp)1 SpecWithCustomFunction (org.eclipse.vorto.mapping.engine.converter.javascript.SpecWithCustomFunction)1 JSONDeserializer (org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer)1 GattCharacteristic (org.eclipse.vorto.mapping.engine.model.blegatt.GattCharacteristic)1 GattDevice (org.eclipse.vorto.mapping.engine.model.blegatt.GattDevice)1 GattService (org.eclipse.vorto.mapping.engine.model.blegatt.GattService)1 EntityPropertyValue (org.eclipse.vorto.model.runtime.EntityPropertyValue)1