Skip to content

Commit

Permalink
rework some mapping rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ai91 committed Sep 22, 2024
1 parent dcb8d30 commit d82a75f
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ public class ValueMapRegex extends ValueMap {

@Override
public boolean isApplicable(Object value) {
return value != null && Pattern.matches(search, value.toString());
if (value != null)
{
Pattern p = Pattern.compile(search, Pattern.DOTALL);
return p.matcher(value.toString()).find();
}
return false;
}

@Override
public String map(Object value) {
return value.toString().replaceAll(search, replace);
Pattern p = Pattern.compile(search, Pattern.DOTALL);
return p.matcher(value.toString()).replaceAll(replace);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Device getDeviceState(String deviceId) {
Device deviceStateReport = new Device();
deviceStateReport.id = deviceId;

Optional<Device> deviceOpt = deviceRepository.getDeviceById(deviceId);
Optional<Device> deviceOpt = findDevice(deviceId);
if (deviceOpt.isPresent()) {
Device device = deviceOpt.get();

Expand Down Expand Up @@ -102,13 +102,17 @@ public Device getDeviceState(String deviceId) {
return deviceStateReport;
}

Optional<Device> findDevice(String deviceId) {
return deviceRepository.getDeviceById(deviceId);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Object getAlisaValue(Object alisaValue, DeviceBridgingRule rule) {

if (rule.mqttState != null) {

String mqttValue = rule.mqttState.state;
String mappedValue = mqttValue;
String mappedValue = null;

// try to match first mapper. If none matches - simply pass value as it is.
for (ValueMap valueMap: rule.valueMapsToAlisa) {
Expand Down Expand Up @@ -137,8 +141,17 @@ Object getAlisaValue(Object alisaValue, DeviceBridgingRule rule) {
Object castValue(String value) {

if (value == null) {
return "";
return null;
}

if (StringUtils.equalsIgnoreCase(value, "null")) {
return Boolean.valueOf(value);
}

if (StringUtils.startsWith(value, "\"") && StringUtils.endsWith(value, "\"")) {
return StringUtils.substring(value, 1, -1);
}

if (StringUtils.equalsAnyIgnoreCase(value, "true", "false")) {
return Boolean.valueOf(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,46 @@ void test() throws JsonMappingException, JsonProcessingException {

}

@Test
void test2() throws JsonMappingException, JsonProcessingException {

String jsonString = TestUtil.readResource("/valuemapstest/regex2.json");

ValueMapRegex valueMap = new ObjectMapper().readerFor(ValueMapRegex.class).readValue(jsonString);

assertFalse(valueMap.isApplicable(null));
assertFalse(valueMap.isApplicable("1"));
assertFalse(valueMap.isApplicable("on"));
assertTrue(valueMap.isApplicable(
"""
{
"power":1,
"swing": 0,
"mode": "heat",
"targetTemp": 27,
"currentTemp": 26,
"pipeTemp": 26,
"speed": 0,
"settingsCelcius": 1,
"mute": 0
}
"""));

assertEquals("true", valueMap.map(
"""
{
"power":1,
"swing": 0,
"mode": "heat",
"targetTemp": 27,
"currentTemp": 26,
"pipeTemp": 26,
"speed": 0,
"settingsCelcius": 1,
"mute": 0
}
"""));

}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
package by.ibn.alisamqttbridge.service;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import by.ibn.alisamqttbridge.TestUtil;
import by.ibn.alisamqttbridge.model.DevicesConfig;
import by.ibn.alisamqttbridge.resources.Device;
import by.ibn.alisamqttbridge.resources.DeviceBridgingRule;
import by.ibn.alisamqttbridge.resources.MQTTState;

class DeviceStateServiceTest {

@Test
void castValue() {

DeviceStateService testee = new DeviceStateService();

assertEquals("", testee.castValue(null));
assertEquals(null, testee.castValue(null));
assertEquals("test", testee.castValue("test"));
assertEquals(Long.valueOf(1), testee.castValue("1"));
assertEquals("1", testee.castValue("\"1\""));
assertEquals(new BigDecimal(1).setScale(1), testee.castValue("1.0"));
assertEquals(Boolean.TRUE, testee.castValue("true"));

Expand All @@ -28,4 +41,40 @@ void castValue() {

}

@Test
void testConfig() throws JsonMappingException, JsonProcessingException {

String jsonString = TestUtil.readResource("/valuemapstest/device.json");

DevicesConfig devicesConfig = new ObjectMapper().readerFor(DevicesConfig.class).readValue(jsonString);


DeviceStateService testee = new DeviceStateService() {
@Override
Optional<Device> findDevice(String deviceId) {
return Optional.of(devicesConfig.devices.get(0));
}
};

MQTTState state = new MQTTState();

List<DeviceBridgingRule> rules = devicesConfig.devices.get(0).capabilities.get(0).rules;
for(DeviceBridgingRule rule: rules)
{
rule.mqttState = state;
}

state.state = "val12";
Device deviceState = testee.getDeviceState("abc-123");
assertEquals(Long.valueOf(12), deviceState.capabilities.get(0).state.value);

state.state = "str12";
deviceState = testee.getDeviceState("abc-123");
assertEquals("12", deviceState.capabilities.get(0).state.value);

state.state = "poooh12";
deviceState = testee.getDeviceState("abc-123");
assertEquals(0, deviceState.capabilities.size());
}

}
30 changes: 30 additions & 0 deletions alisamqttbridge/src/test/resources/valuemapstest/device.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"devices": [
{
"id": "abc-123",
"capabilities": [
{
"type": "sometype",
"parameters": {
"instance": "someinstance"
},
"rules": [
{
"alisa": { "instance": "someinstance" },
"mqtt": { "state": "statetopic" },
"valueMapsToAlisa": [ { "type": "regex", "search": "^val([0-9]+)$", "replace": "$1" } ]
},
{
"alisa": { "instance": "someinstance" },
"mqtt": { "state": "statetopic" },
"valueMapsToAlisa": [ { "type": "regex", "search": "^str([0-9]+)$", "replace": "\"$1\"" } ]
}
]

}
],
"properties": [
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "regex", "search": "^.*\"power\":\\s*1.*$", "replace": "true" }

0 comments on commit d82a75f

Please sign in to comment.