Commit 6b90c7ce21233a8c29c47566d5ebf15b32d9227d
1 parent
8c9aca91
Improve connect event transformation
Showing
2 changed files
with
20 additions
and
11 deletions
... | ... | @@ -36,26 +36,27 @@ import org.thingsboard.server.common.data.Tenant; |
36 | 36 | import org.thingsboard.server.common.data.id.DeviceId; |
37 | 37 | import org.thingsboard.server.common.data.id.TenantId; |
38 | 38 | import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
39 | -import org.thingsboard.server.common.data.page.PageData; | |
40 | -import org.thingsboard.server.common.data.page.PageLink; | |
41 | 39 | import org.thingsboard.server.common.data.kv.BasicTsKvEntry; |
42 | 40 | import org.thingsboard.server.common.data.kv.BooleanDataEntry; |
43 | 41 | import org.thingsboard.server.common.data.kv.KvEntry; |
44 | 42 | import org.thingsboard.server.common.data.kv.LongDataEntry; |
45 | 43 | import org.thingsboard.server.common.data.kv.TsKvEntry; |
44 | +import org.thingsboard.server.common.data.page.PageData; | |
45 | +import org.thingsboard.server.common.data.page.PageLink; | |
46 | 46 | import org.thingsboard.server.common.msg.TbMsg; |
47 | 47 | import org.thingsboard.server.common.msg.TbMsgDataType; |
48 | 48 | import org.thingsboard.server.common.msg.TbMsgMetaData; |
49 | +import org.thingsboard.server.common.msg.queue.ServiceType; | |
50 | +import org.thingsboard.server.common.msg.queue.TbCallback; | |
51 | +import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; | |
49 | 52 | import org.thingsboard.server.dao.attributes.AttributesService; |
50 | 53 | import org.thingsboard.server.dao.device.DeviceService; |
51 | 54 | import org.thingsboard.server.dao.tenant.TenantService; |
52 | 55 | import org.thingsboard.server.dao.timeseries.TimeseriesService; |
56 | +import org.thingsboard.server.dao.util.mapping.JacksonUtil; | |
57 | +import org.thingsboard.server.gen.transport.TransportProtos; | |
53 | 58 | import org.thingsboard.server.queue.discovery.PartitionChangeEvent; |
54 | 59 | 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 | 60 | import org.thingsboard.server.queue.util.TbCoreComponent; |
60 | 61 | import org.thingsboard.server.service.queue.TbClusterService; |
61 | 62 | import org.thingsboard.server.service.telemetry.TelemetrySubscriptionService; |
... | ... | @@ -91,7 +92,6 @@ import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE; |
91 | 92 | @Slf4j |
92 | 93 | public class DefaultDeviceStateService implements DeviceStateService { |
93 | 94 | |
94 | - private static final ObjectMapper json = new ObjectMapper(); | |
95 | 95 | public static final String ACTIVITY_STATE = "active"; |
96 | 96 | public static final String LAST_CONNECT_TIME = "lastConnectTime"; |
97 | 97 | public static final String LAST_DISCONNECT_TIME = "lastDisconnectTime"; |
... | ... | @@ -506,11 +506,11 @@ public class DefaultDeviceStateService implements DeviceStateService { |
506 | 506 | try { |
507 | 507 | String data; |
508 | 508 | if (msgType.equals(CONNECT_EVENT)) { |
509 | - ObjectNode stateNode = json.convertValue(state, ObjectNode.class); | |
509 | + ObjectNode stateNode = JacksonUtil.convertValue(state, ObjectNode.class); | |
510 | 510 | stateNode.remove(ACTIVITY_STATE); |
511 | - data = stateNode.toString(); | |
511 | + data = JacksonUtil.toString(stateNode); | |
512 | 512 | } else { |
513 | - data = json.writeValueAsString(state); | |
513 | + data = JacksonUtil.toString(state); | |
514 | 514 | } |
515 | 515 | TbMsg tbMsg = TbMsg.newMsg(msgType, stateData.getDeviceId(), stateData.getMetaData().copy(), TbMsgDataType.JSON, data); |
516 | 516 | 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 | +} | ... | ... |