Commit f4efea9a0bc62fecf7301d08deda40867ef67f75
1 parent
b24ea780
Improve connect event transformation
Showing
2 changed files
with
18 additions
and
10 deletions
... | ... | @@ -15,7 +15,6 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.service.state; |
17 | 17 | |
18 | -import com.fasterxml.jackson.databind.ObjectMapper; | |
19 | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
20 | 19 | import com.google.common.base.Function; |
21 | 20 | import com.google.common.util.concurrent.FutureCallback; |
... | ... | @@ -46,16 +45,17 @@ import org.thingsboard.server.common.data.page.TextPageLink; |
46 | 45 | import org.thingsboard.server.common.msg.TbMsg; |
47 | 46 | import org.thingsboard.server.common.msg.TbMsgDataType; |
48 | 47 | import org.thingsboard.server.common.msg.TbMsgMetaData; |
48 | +import org.thingsboard.server.common.msg.queue.ServiceType; | |
49 | +import org.thingsboard.server.common.msg.queue.TbCallback; | |
50 | +import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; | |
49 | 51 | import org.thingsboard.server.dao.attributes.AttributesService; |
50 | 52 | import org.thingsboard.server.dao.device.DeviceService; |
51 | 53 | import org.thingsboard.server.dao.tenant.TenantService; |
52 | 54 | import org.thingsboard.server.dao.timeseries.TimeseriesService; |
55 | +import org.thingsboard.server.dao.util.mapping.JacksonUtil; | |
56 | +import org.thingsboard.server.gen.transport.TransportProtos; | |
53 | 57 | import org.thingsboard.server.queue.discovery.PartitionChangeEvent; |
54 | 58 | import org.thingsboard.server.queue.discovery.PartitionService; |
55 | -import org.thingsboard.server.common.msg.queue.ServiceType; | |
56 | -import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; | |
57 | -import org.thingsboard.server.gen.transport.TransportProtos; | |
58 | -import org.thingsboard.server.common.msg.queue.TbCallback; | |
59 | 59 | import org.thingsboard.server.queue.util.TbCoreComponent; |
60 | 60 | import org.thingsboard.server.service.queue.TbClusterService; |
61 | 61 | import org.thingsboard.server.service.telemetry.TelemetrySubscriptionService; |
... | ... | @@ -91,7 +91,6 @@ import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE; |
91 | 91 | @Slf4j |
92 | 92 | public class DefaultDeviceStateService implements DeviceStateService { |
93 | 93 | |
94 | - private static final ObjectMapper json = new ObjectMapper(); | |
95 | 94 | public static final String ACTIVITY_STATE = "active"; |
96 | 95 | public static final String LAST_CONNECT_TIME = "lastConnectTime"; |
97 | 96 | public static final String LAST_DISCONNECT_TIME = "lastDisconnectTime"; |
... | ... | @@ -506,11 +505,11 @@ public class DefaultDeviceStateService implements DeviceStateService { |
506 | 505 | try { |
507 | 506 | String data; |
508 | 507 | if (msgType.equals(CONNECT_EVENT)) { |
509 | - ObjectNode stateNode = json.convertValue(state, ObjectNode.class); | |
508 | + ObjectNode stateNode = JacksonUtil.convertValue(state, ObjectNode.class); | |
510 | 509 | stateNode.remove(ACTIVITY_STATE); |
511 | - data = stateNode.toString(); | |
510 | + data = JacksonUtil.toString(stateNode); | |
512 | 511 | } else { |
513 | - data = json.writeValueAsString(state); | |
512 | + data = JacksonUtil.toString(state); | |
514 | 513 | } |
515 | 514 | TbMsg tbMsg = TbMsg.newMsg(msgType, stateData.getDeviceId(), stateData.getMetaData().copy(), TbMsgDataType.JSON, data); |
516 | 515 | clusterService.pushMsgToRuleEngine(stateData.getTenantId(), stateData.getDeviceId(), tbMsg, null); | ... | ... |
... | ... | @@ -28,6 +28,15 @@ public class JacksonUtil { |
28 | 28 | |
29 | 29 | public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
30 | 30 | |
31 | + public static <T> T convertValue(Object fromValue, Class<T> toValueType) { | |
32 | + try { | |
33 | + return OBJECT_MAPPER.convertValue(fromValue, toValueType); | |
34 | + } catch (IllegalArgumentException e) { | |
35 | + throw new IllegalArgumentException("The given object value: " | |
36 | + + fromValue + " cannot be converted to " + toValueType); | |
37 | + } | |
38 | + } | |
39 | + | |
31 | 40 | public static <T> T fromString(String string, Class<T> clazz) { |
32 | 41 | try { |
33 | 42 | return OBJECT_MAPPER.readValue(string, clazz); |
... | ... | @@ -60,4 +69,4 @@ public class JacksonUtil { |
60 | 69 | public static <T> T clone(T value) { |
61 | 70 | return fromString(toString(value), (Class<T>) value.getClass()); |
62 | 71 | } |
63 | -} | |
\ No newline at end of file | ||
72 | +} | ... | ... |