@ -24,6 +24,8 @@ import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException ;
import com.google.gson.JsonSyntaxException ;
import org.apache.commons.lang3.math.NumberUtils ;
import org.apache.commons.lang3.math.NumberUtils ;
import org.springframework.util.StringUtils ;
import org.springframework.util.StringUtils ;
import org.thingsboard.server.common.data.DataConstants ;
import org.thingsboard.server.common.data.id.DeviceId ;
import org.thingsboard.server.common.data.kv.AttributeKey ;
import org.thingsboard.server.common.data.kv.AttributeKey ;
import org.thingsboard.server.common.data.kv.AttributeKvEntry ;
import org.thingsboard.server.common.data.kv.AttributeKvEntry ;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry ;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry ;
@ -35,6 +37,7 @@ import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.common.msg.kv.AttributesKVMsg ;
import org.thingsboard.server.common.msg.kv.AttributesKVMsg ;
import org.thingsboard.server.gen.transport.TransportProtos ;
import org.thingsboard.server.gen.transport.TransportProtos ;
import org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg ;
import org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg ;
import org.thingsboard.server.gen.transport.TransportProtos.ClaimDeviceMsg ;
import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg ;
import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg ;
import org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto ;
import org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto ;
import org.thingsboard.server.gen.transport.TransportProtos.KeyValueType ;
import org.thingsboard.server.gen.transport.TransportProtos.KeyValueType ;
@ -63,23 +66,69 @@ public class JsonConverter {
private static int maxStringValueLength = 0 ;
private static int maxStringValueLength = 0 ;
public static PostTelemetryMsg convertToTelemetryProto ( JsonElement jsonObject ) throws JsonSyntaxException {
public static PostTelemetryMsg convertToTelemetryProto ( JsonElement jsonElement ) throws JsonSyntaxException {
long systemTs = System . currentTimeMillis ( ) ;
PostTelemetryMsg . Builder builder = PostTelemetryMsg . newBuilder ( ) ;
PostTelemetryMsg . Builder builder = PostTelemetryMsg . newBuilder ( ) ;
if ( jsonObject . isJsonObject ( ) ) {
convertToTelemetry ( jsonElement , System . currentTimeMillis ( ) , null , builder ) ;
parseObject ( builder , systemTs , jsonObject ) ;
return builder . build ( ) ;
} else if ( jsonObject . isJsonArray ( ) ) {
}
jsonObject . getAsJsonArray ( ) . forEach ( je - > {
private static void convertToTelemetry ( JsonElement jsonElement , long systemTs , Map < Long , List < KvEntry > > result , PostTelemetryMsg . Builder builder ) {
if ( jsonElement . isJsonObject ( ) ) {
parseObject ( systemTs , result , builder , jsonElement . getAsJsonObject ( ) ) ;
} else if ( jsonElement . isJsonArray ( ) ) {
jsonElement . getAsJsonArray ( ) . forEach ( je - > {
if ( je . isJsonObject ( ) ) {
if ( je . isJsonObject ( ) ) {
parseObject ( builder , systemTs , je . getAsJsonObject ( ) ) ;
parseObject ( systemTs , result , builder , je . getAsJsonObject ( ) ) ;
} else {
} else {
throw new JsonSyntaxException ( CAN_T_PARSE_VALUE + je ) ;
throw new JsonSyntaxException ( CAN_T_PARSE_VALUE + je ) ;
}
}
} ) ;
} ) ;
} else {
} else {
throw new JsonSyntaxException ( CAN_T_PARSE_VALUE + jsonObjec t ) ;
throw new JsonSyntaxException ( CAN_T_PARSE_VALUE + jsonElemen t ) ;
}
}
return builder . build ( ) ;
}
private static void parseObject ( long systemTs , Map < Long , List < KvEntry > > result , PostTelemetryMsg . Builder builder , JsonObject jo ) {
if ( result ! = null ) {
parseObject ( result , systemTs , jo ) ;
} else {
parseObject ( builder , systemTs , jo ) ;
}
}
public static ClaimDeviceMsg convertToClaimDeviceProto ( DeviceId deviceId , String json ) {
long durationMs = 0L ;
if ( json ! = null & & ! json . isEmpty ( ) ) {
return convertToClaimDeviceProto ( deviceId , new JsonParser ( ) . parse ( json ) ) ;
}
return buildClaimDeviceMsg ( deviceId , DataConstants . DEFAULT_SECRET_KEY , durationMs ) ;
}
public static ClaimDeviceMsg convertToClaimDeviceProto ( DeviceId deviceId , JsonElement jsonElement ) {
String secretKey = DataConstants . DEFAULT_SECRET_KEY ;
long durationMs = 0L ;
if ( jsonElement . isJsonObject ( ) ) {
JsonObject jo = jsonElement . getAsJsonObject ( ) ;
if ( jo . has ( DataConstants . SECRET_KEY_FIELD_NAME ) ) {
secretKey = jo . get ( DataConstants . SECRET_KEY_FIELD_NAME ) . getAsString ( ) ;
}
if ( jo . has ( DataConstants . DURATION_MS_FIELD_NAME ) ) {
durationMs = jo . get ( DataConstants . DURATION_MS_FIELD_NAME ) . getAsLong ( ) ;
}
} else {
throw new JsonSyntaxException ( CAN_T_PARSE_VALUE + jsonElement ) ;
}
return buildClaimDeviceMsg ( deviceId , secretKey , durationMs ) ;
}
private static ClaimDeviceMsg buildClaimDeviceMsg ( DeviceId deviceId , String secretKey , long durationMs ) {
ClaimDeviceMsg . Builder result = ClaimDeviceMsg . newBuilder ( ) ;
return result
. setDeviceIdMSB ( deviceId . getId ( ) . getMostSignificantBits ( ) )
. setDeviceIdLSB ( deviceId . getId ( ) . getLeastSignificantBits ( ) )
. setSecretKey ( secretKey )
. setDurationMs ( durationMs )
. build ( ) ;
}
}
public static PostAttributeMsg convertToAttributesProto ( JsonElement jsonObject ) throws JsonSyntaxException {
public static PostAttributeMsg convertToAttributesProto ( JsonElement jsonObject ) throws JsonSyntaxException {
@ -103,8 +152,7 @@ public class JsonConverter {
return result ;
return result ;
}
}
private static void parseObject ( PostTelemetryMsg . Builder builder , long systemTs , JsonElement jsonObject ) {
private static void parseObject ( PostTelemetryMsg . Builder builder , long systemTs , JsonObject jo ) {
JsonObject jo = jsonObject . getAsJsonObject ( ) ;
if ( jo . has ( "ts" ) & & jo . has ( "values" ) ) {
if ( jo . has ( "ts" ) & & jo . has ( "values" ) ) {
parseWithTs ( builder , jo ) ;
parseWithTs ( builder , jo ) ;
} else {
} else {
@ -137,7 +185,7 @@ public class JsonConverter {
String message = String . format ( "String value length [%d] for key [%s] is greater than maximum allowed [%d]" , value . getAsString ( ) . length ( ) , valueEntry . getKey ( ) , maxStringValueLength ) ;
String message = String . format ( "String value length [%d] for key [%s] is greater than maximum allowed [%d]" , value . getAsString ( ) . length ( ) , valueEntry . getKey ( ) , maxStringValueLength ) ;
throw new JsonSyntaxException ( message ) ;
throw new JsonSyntaxException ( message ) ;
}
}
if ( isTypeCastEnabled & & NumberUtils . isParsable ( value . getAsString ( ) ) ) {
if ( isTypeCastEnabled & & NumberUtils . isParsable ( value . getAsString ( ) ) ) {
try {
try {
result . add ( buildNumericKeyValueProto ( value , valueEntry . getKey ( ) ) ) ;
result . add ( buildNumericKeyValueProto ( value , valueEntry . getKey ( ) ) ) ;
} catch ( RuntimeException th ) {
} catch ( RuntimeException th ) {
@ -400,7 +448,7 @@ public class JsonConverter {
String message = String . format ( "String value length [%d] for key [%s] is greater than maximum allowed [%d]" , value . getAsString ( ) . length ( ) , valueEntry . getKey ( ) , maxStringValueLength ) ;
String message = String . format ( "String value length [%d] for key [%s] is greater than maximum allowed [%d]" , value . getAsString ( ) . length ( ) , valueEntry . getKey ( ) , maxStringValueLength ) ;
throw new JsonSyntaxException ( message ) ;
throw new JsonSyntaxException ( message ) ;
}
}
if ( isTypeCastEnabled & & NumberUtils . isParsable ( value . getAsString ( ) ) ) {
if ( isTypeCastEnabled & & NumberUtils . isParsable ( value . getAsString ( ) ) ) {
try {
try {
parseNumericValue ( result , valueEntry , value ) ;
parseNumericValue ( result , valueEntry , value ) ;
} catch ( RuntimeException th ) {
} catch ( RuntimeException th ) {
@ -423,26 +471,13 @@ public class JsonConverter {
return result ;
return result ;
}
}
public static Map < Long , List < KvEntry > > convertToTelemetry ( JsonElement jsonObjec t , long systemTs ) throws JsonSyntaxException {
public static Map < Long , List < KvEntry > > convertToTelemetry ( JsonElement jsonElemen t , long systemTs ) throws JsonSyntaxException {
Map < Long , List < KvEntry > > result = new HashMap < > ( ) ;
Map < Long , List < KvEntry > > result = new HashMap < > ( ) ;
if ( jsonObject . isJsonObject ( ) ) {
convertToTelemetry ( jsonElement , systemTs , result , null ) ;
parseObject ( result , systemTs , jsonObject ) ;
} else if ( jsonObject . isJsonArray ( ) ) {
jsonObject . getAsJsonArray ( ) . forEach ( je - > {
if ( je . isJsonObject ( ) ) {
parseObject ( result , systemTs , je . getAsJsonObject ( ) ) ;
} else {
throw new JsonSyntaxException ( CAN_T_PARSE_VALUE + je ) ;
}
} ) ;
} else {
throw new JsonSyntaxException ( CAN_T_PARSE_VALUE + jsonObject ) ;
}
return result ;
return result ;
}
}
private static void parseObject ( Map < Long , List < KvEntry > > result , long systemTs , JsonElement jsonObject ) {
private static void parseObject ( Map < Long , List < KvEntry > > result , long systemTs , JsonObject jo ) {
JsonObject jo = jsonObject . getAsJsonObject ( ) ;
if ( jo . has ( "ts" ) & & jo . has ( "values" ) ) {
if ( jo . has ( "ts" ) & & jo . has ( "values" ) ) {
parseWithTs ( result , jo ) ;
parseWithTs ( result , jo ) ;
} else {
} else {