44import com .google .gson .JsonElement ;
55import com .google .gson .JsonParser ;
66import com .google .gson .JsonSyntaxException ;
7- import iot .technology .tsl .adaptor .AdaptorException ;
8- import iot .technology .tsl .adaptor .JsonConverter ;
9- import iot .technology .tsl .data .kv .AttributeKvEntry ;
10- import iot .technology .tsl .data .kv .KvEntry ;
11- import iot .technology .tsl .session .SessionMsgType ;
127import io .netty .buffer .ByteBuf ;
138import io .netty .buffer .ByteBufAllocator ;
149import io .netty .buffer .UnpooledByteBufAllocator ;
1510import io .netty .handler .codec .mqtt .MqttMessage ;
1611import io .netty .handler .codec .mqtt .MqttPublishMessage ;
12+ import iot .technology .tsl .adaptor .AdaptorException ;
13+ import iot .technology .tsl .adaptor .JsonConverter ;
14+ import iot .technology .tsl .data .kv .AttributeKvEntry ;
15+ import iot .technology .tsl .data .kv .KvEntry ;
16+ import iot .technology .tsl .session .SessionMsgType ;
1717import lombok .extern .slf4j .Slf4j ;
1818
1919import java .nio .charset .Charset ;
@@ -50,10 +50,11 @@ public static void convertToMsg(SessionMsgType type, MqttMessage inbound) throws
5050 private static void convertToTelemetryUploadRequest (MqttPublishMessage inbound ) throws AdaptorException {
5151 String payload = validatePayload (inbound .payload ());
5252 try {
53- Map <Long , List <KvEntry >> telemetryMaps = JsonConverter .convertToTelemetry (new JsonParser ().parse (payload ), inbound .variableHeader ().messageId ()).getData ();
54- for (Map .Entry <Long ,List <KvEntry >> entry : telemetryMaps .entrySet ()) {
55- log .info ("key: {}" ,entry .getKey ());
56- for (KvEntry kvEntry : entry .getValue ()) {
53+ Map <Long , List <KvEntry >> telemetryMaps =
54+ JsonConverter .convertToTelemetry (new JsonParser ().parse (payload ), inbound .variableHeader ().messageId ()).getData ();
55+ for (Map .Entry <Long , List <KvEntry >> entry : telemetryMaps .entrySet ()) {
56+ log .info ("key: {}" , entry .getKey ());
57+ for (KvEntry kvEntry : entry .getValue ()) {
5758 log .info ("属性名: {}. 属性值: {}" , kvEntry .getKey (), kvEntry .getValueAsString ());
5859 }
5960 }
@@ -65,9 +66,11 @@ private static void convertToTelemetryUploadRequest(MqttPublishMessage inbound)
6566 private static void convertToUpdateAttributesRequest (MqttPublishMessage inbound ) throws AdaptorException {
6667 String payload = validatePayload (inbound .payload ());
6768 try {
68- Set <AttributeKvEntry > attributeKvEntrySet = JsonConverter .convertToAttributes (new JsonParser ().parse (payload ), inbound .variableHeader ().messageId ()).getAttributes ();
69- for (AttributeKvEntry attributeKvEntry : attributeKvEntrySet ){
70- log .info ("属性名: {}. 属性值: {}" ,attributeKvEntry .getKey (), attributeKvEntry .getValueAsString ());
69+ Set <AttributeKvEntry > attributeKvEntrySet =
70+ JsonConverter .convertToAttributes (new JsonParser ().parse (payload ), inbound .variableHeader ().messageId ())
71+ .getAttributes ();
72+ for (AttributeKvEntry attributeKvEntry : attributeKvEntrySet ) {
73+ log .info ("属性名: {}. 属性值: {}" , attributeKvEntry .getKey (), attributeKvEntry .getValueAsString ());
7174 }
7275 } catch (IllegalStateException | JsonSyntaxException ex ) {
7376 throw new AdaptorException (ex );
@@ -89,7 +92,7 @@ private static void convertToGetAttributesRequest(MqttPublishMessage inbound) th
8992 log .info ("共享设备属性: {} " , sharedKey );
9093 }
9194 }
92- }catch (RuntimeException e ) {
95+ } catch (RuntimeException e ) {
9396 throw new AdaptorException (e );
9497 }
9598 }
@@ -104,14 +107,10 @@ private static Set<String> toStringSet(JsonElement requestBody, String name) {
104107 }
105108
106109 public static String validatePayload (ByteBuf payloadData ) throws AdaptorException {
107- try {
108- String payload = payloadData .toString (UTF8 );
109- if (payload == null ) {
110- throw new AdaptorException (new IllegalArgumentException ("Payload is empty!" ));
111- }
112- return payload ;
113- } finally {
114- payloadData .release ();
110+ String payload = payloadData .toString (UTF8 );
111+ if (payload == null ) {
112+ throw new AdaptorException (new IllegalArgumentException ("Payload is empty!" ));
115113 }
114+ return payload ;
116115 }
117116}
0 commit comments