设备标签增加类型转换

This commit is contained in:
zhou-hao 2021-01-28 15:35:03 +08:00
parent 18b25a05c1
commit d317f37ee4
2 changed files with 19 additions and 2 deletions

View File

@ -11,6 +11,8 @@ import org.hswebframework.web.api.crud.entity.GenericEntity;
import org.hswebframework.web.crud.annotation.EnableEntityEvent;
import org.hswebframework.web.crud.generator.Generators;
import org.hswebframework.web.validator.CreateGroup;
import org.jetlinks.core.metadata.Converter;
import org.jetlinks.core.metadata.DataType;
import org.jetlinks.core.metadata.PropertyMetadata;
import javax.persistence.Column;
@ -73,6 +75,21 @@ public class DeviceTagEntity extends GenericEntity<String> {
return entity;
}
public static DeviceTagEntity of(PropertyMetadata property, Object value) {
DeviceTagEntity tag = of(property);
DataType type = property.getValueType();
if (type instanceof Converter) {
Object newValue = ((Converter<?>) type).convert(value);
if (newValue != null) {
value = newValue;
}
}
tag.setValue(String.valueOf(value));
return tag;
}
public static String createTagId(String deviceId, String key) {
return DigestUtils.md5Hex(deviceId + ":" + key);
}

View File

@ -163,7 +163,7 @@ public class DeviceMessageBusinessHandler {
.map(e -> {
DeviceTagEntity tagEntity = metadata
.getTag(e.getKey())
.map(DeviceTagEntity::of)
.map(tagMeta -> DeviceTagEntity.of(tagMeta, e.getValue()))
.orElseGet(() -> {
DeviceTagEntity entity = new DeviceTagEntity();
entity.setKey(e.getKey());
@ -171,9 +171,9 @@ public class DeviceMessageBusinessHandler {
entity.setName(e.getKey());
entity.setCreateTime(new Date());
entity.setDescription("设备上报");
entity.setValue(String.valueOf(e.getValue()));
return entity;
});
tagEntity.setValue(String.valueOf(e.getValue()));
tagEntity.setDeviceId(deviceId);
tagEntity.setId(DeviceTagEntity.createTagId(deviceId, tagEntity.getKey()));
return tagEntity;