Browse Source

Merge pull request #14077 from thingsboard/lwm2m_type_Time_convert_to_Long

fix: Store LWM2M Time as raw Epoch for precision and UTC consistency
pull/14221/head
Andrew Shvayka 11 months ago
committed by GitHub
parent
commit
18b5e620d2
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 11
      common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/LwM2mTransportServerHelper.java
  2. 7
      common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/utils/LwM2mValueConverterImpl.java

11
common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/LwM2mTransportServerHelper.java

@ -38,6 +38,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
@ -180,8 +181,16 @@ public class LwM2mTransportServerHelper {
case BOOLEAN: case BOOLEAN:
kvProto.setType(BOOLEAN_V).setBoolV((Boolean) value).build(); kvProto.setType(BOOLEAN_V).setBoolV((Boolean) value).build();
break; break;
case STRING:
case TIME: case TIME:
if (value instanceof Date) {
kvProto.setType(TransportProtos.KeyValueType.LONG_V).setLongV(((Date) value).getTime());
} else if (value instanceof Integer || value instanceof Long) {
kvProto.setType(TransportProtos.KeyValueType.LONG_V).setLongV((long) (value));
} else {
kvProto.setType(TransportProtos.KeyValueType.STRING_V).setStringV(value.toString());
}
break;
case STRING:
case OPAQUE: case OPAQUE:
case OBJLNK: case OBJLNK:
kvProto.setType(TransportProtos.KeyValueType.STRING_V).setStringV((String) value); kvProto.setType(TransportProtos.KeyValueType.STRING_V).setStringV((String) value);

7
common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/utils/LwM2mValueConverterImpl.java

@ -33,6 +33,7 @@ import java.util.Date;
import static org.eclipse.leshan.core.model.ResourceModel.Type.NONE; import static org.eclipse.leshan.core.model.ResourceModel.Type.NONE;
import static org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE; import static org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE;
import static org.eclipse.leshan.core.model.ResourceModel.Type.TIME;
@Slf4j @Slf4j
public class LwM2mValueConverterImpl implements LwM2mValueConverter { public class LwM2mValueConverterImpl implements LwM2mValueConverter {
@ -58,7 +59,7 @@ public class LwM2mValueConverterImpl implements LwM2mValueConverter {
currentType = OPAQUE; currentType = OPAQUE;
} }
if (currentType == expectedType || currentType == NONE) { if (currentType == expectedType || currentType == NONE || currentType == TIME) {
/** expected type */ /** expected type */
return value; return value;
} }
@ -135,7 +136,7 @@ public class LwM2mValueConverterImpl implements LwM2mValueConverter {
**/ **/
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
log.debug("Unable to convert string to date", e); log.debug("Unable to convert string to date", e);
throw new CodecException("Unable to convert string (%s) to date for resource %s", value, throw new CodecException("Unable to convert string (%s) to %s for resource %s", value, TIME.name(),
resourcePath); resourcePath);
} }
default: default:
@ -149,7 +150,7 @@ public class LwM2mValueConverterImpl implements LwM2mValueConverter {
case FLOAT: case FLOAT:
return String.valueOf(value); return String.valueOf(value);
case TIME: case TIME:
String DATE_FORMAT = "MMM d, yyyy HH:mm a"; String DATE_FORMAT = "yyyy-MM-dd[[ ]['T']HH:mm[:ss[.SSS]][ ][XXX][Z][z][VV][O]]";
Long timeValue; Long timeValue;
try { try {
timeValue = ((Date) value).getTime(); timeValue = ((Date) value).getTime();

Loading…
Cancel
Save