Dana: catch wrong history date after DST change

This commit is contained in:
Milos Kozak 2022-03-28 11:23:51 +02:00
parent d7313c5f79
commit aeb0d0cf2d
2 changed files with 57 additions and 27 deletions

View file

@ -4,6 +4,7 @@ import android.annotation.TargetApi;
import android.os.Build; import android.os.Build;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.IllegalInstantException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
@ -144,8 +145,7 @@ public class MessageBase {
} }
public int getCommand() { public int getCommand() {
int command = byteFromRawBuff(buffer, 5) | (byteFromRawBuff(buffer, 4) << 8); return byteFromRawBuff(buffer, 5) | (byteFromRawBuff(buffer, 4) << 8);
return command;
} }
public int byteFromRawBuff(byte[] buff, int offset) { public int byteFromRawBuff(byte[] buff, int offset) {
@ -180,8 +180,9 @@ public class MessageBase {
} }
public synchronized long dateTimeSecFromBuff(byte[] buff, int offset) { public synchronized long dateTimeSecFromBuff(byte[] buff, int offset) {
return
new DateTime( try {
return new DateTime(
2000 + intFromBuff(buff, offset, 1), 2000 + intFromBuff(buff, offset, 1),
intFromBuff(buff, offset + 1, 1), intFromBuff(buff, offset + 1, 1),
intFromBuff(buff, offset + 2, 1), intFromBuff(buff, offset + 2, 1),
@ -189,6 +190,19 @@ public class MessageBase {
intFromBuff(buff, offset + 4, 1), intFromBuff(buff, offset + 4, 1),
intFromBuff(buff, offset + 5, 1) intFromBuff(buff, offset + 5, 1)
).getMillis(); ).getMillis();
} catch (IllegalInstantException e) {
// expect
// org.joda.time.IllegalInstantException: Illegal instant due to time zone offset transition (daylight savings time 'gap')
// add 1 hour
return new DateTime(
2000 + intFromBuff(buff, offset, 1),
intFromBuff(buff, offset + 1, 1),
intFromBuff(buff, offset + 2, 1),
intFromBuff(buff, offset + 3, 1) + 1,
intFromBuff(buff, offset + 4, 1),
intFromBuff(buff, offset + 5, 1)
).getMillis();
}
} }
public long dateFromBuff(byte[] buff, int offset) { public long dateFromBuff(byte[] buff, int offset) {
@ -204,18 +218,18 @@ public class MessageBase {
@TargetApi(Build.VERSION_CODES.KITKAT) @TargetApi(Build.VERSION_CODES.KITKAT)
public static String stringFromBuff(byte[] buff, int offset, int length) { public static String stringFromBuff(byte[] buff, int offset, int length) {
byte[] strbuff = new byte[length]; byte[] strBuff = new byte[length];
System.arraycopy(buff, offset + 6, strbuff, 0, length); System.arraycopy(buff, offset + 6, strBuff, 0, length);
return new String(strbuff, StandardCharsets.UTF_8); return new String(strBuff, StandardCharsets.UTF_8);
} }
@TargetApi(Build.VERSION_CODES.KITKAT) @TargetApi(Build.VERSION_CODES.KITKAT)
public static String asciiStringFromBuff(byte[] buff, int offset, int length) { public static String asciiStringFromBuff(byte[] buff, int offset, int length) {
byte[] strbuff = new byte[length]; byte[] strBuff = new byte[length];
System.arraycopy(buff, offset + 6, strbuff, 0, length); System.arraycopy(buff, offset + 6, strBuff, 0, length);
for (int pos = 0; pos < length; pos++) for (int pos = 0; pos < length; pos++)
strbuff[pos] += 65; // "A" strBuff[pos] += 65; // "A"
return new String(strbuff, StandardCharsets.UTF_8); return new String(strBuff, StandardCharsets.UTF_8);
} }
public static String toHexString(byte[] buff) { public static String toHexString(byte[] buff) {

View file

@ -5,6 +5,7 @@ import info.nightscout.androidaps.danars.encryption.BleEncryption
import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import org.joda.time.DateTime import org.joda.time.DateTime
import org.joda.time.IllegalInstantException
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import javax.inject.Inject import javax.inject.Inject
@ -69,7 +70,9 @@ open class DanaRSPacket(protected var injector: HasAndroidInjector) {
else -> -1 else -> -1
} }
@Synchronized fun dateTimeSecFromBuff(buff: ByteArray, offset: Int): Long = @Synchronized
fun dateTimeSecFromBuff(buff: ByteArray, offset: Int): Long =
try {
DateTime( DateTime(
2000 + intFromBuff(buff, offset, 1), 2000 + intFromBuff(buff, offset, 1),
intFromBuff(buff, offset + 1, 1), intFromBuff(buff, offset + 1, 1),
@ -78,6 +81,19 @@ open class DanaRSPacket(protected var injector: HasAndroidInjector) {
intFromBuff(buff, offset + 4, 1), intFromBuff(buff, offset + 4, 1),
intFromBuff(buff, offset + 5, 1) intFromBuff(buff, offset + 5, 1)
).millis ).millis
} catch (e: IllegalInstantException) {
// expect
// org.joda.time.IllegalInstantException: Illegal instant due to time zone offset transition (daylight savings time 'gap')
// add 1 hour
DateTime(
2000 + intFromBuff(buff, offset, 1),
intFromBuff(buff, offset + 1, 1),
intFromBuff(buff, offset + 2, 1),
intFromBuff(buff, offset + 3, 1) + 1,
intFromBuff(buff, offset + 4, 1),
intFromBuff(buff, offset + 5, 1)
).millis
}
protected fun intFromBuff(b: ByteArray, srcStart: Int, srcLength: Int): Int = protected fun intFromBuff(b: ByteArray, srcStart: Int, srcLength: Int): Int =
when (srcLength) { when (srcLength) {