Centralize the calendar conversion to/from byte array take 2.

Move to an independent class.
This commit is contained in:
Daniele Gobbetti 2015-08-28 10:40:25 +02:00
parent 3a6e433fb3
commit cbea0feb9e
5 changed files with 49 additions and 41 deletions

View File

@ -132,39 +132,4 @@ public class MiBandCoordinator implements DeviceCoordinator {
return Integer.parseInt(prefs.getString(MiBandConst.PREF_MIBAND_FITNESS_GOAL, "10000")); return Integer.parseInt(prefs.getString(MiBandConst.PREF_MIBAND_FITNESS_GOAL, "10000"));
} }
/**
* uses the standard algorithm to convert bytes received from the MiBand to a Calendar object
* @param value
* @return
*/
public static GregorianCalendar rawBytesToCalendar(byte[] value) {
GregorianCalendar timestamp = new GregorianCalendar();
if (value.length == 6) {
timestamp.set(Calendar.YEAR, (2000 + value[0]));
timestamp.set(Calendar.MONTH, value[1]);
timestamp.set(Calendar.DATE, value[2]);
timestamp.set(Calendar.HOUR_OF_DAY, value[3]);
timestamp.set(Calendar.MINUTE, value[4]);
timestamp.set(Calendar.SECOND, value[5]);
}
return timestamp;
}
/**
* uses the standard algorithm to convert a Calendar object to a byte array to send to MiBand
* @param timestamp
* @return
*/
public static byte[] calendarToRawBytes(Calendar timestamp) {
return new byte[]{
(byte) (timestamp.get(Calendar.YEAR) - 2000),
(byte) timestamp.get(Calendar.MONTH),
(byte) timestamp.get(Calendar.DATE),
(byte) timestamp.get(Calendar.HOUR_OF_DAY),
(byte) timestamp.get(Calendar.MINUTE),
(byte) timestamp.get(Calendar.SECOND)
};
}
} }

View File

@ -0,0 +1,42 @@
package nodomain.freeyourgadget.gadgetbridge.devices.miband;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MiBandDateConverter {
/**
* uses the standard algorithm to convert bytes received from the MiBand to a Calendar object
* @param value
* @return
*/
public static GregorianCalendar rawBytesToCalendar(byte[] value) {
GregorianCalendar timestamp = new GregorianCalendar();
if (value.length == 6) {
timestamp.set(Calendar.YEAR, (2000 + value[0]));
timestamp.set(Calendar.MONTH, value[1]);
timestamp.set(Calendar.DATE, value[2]);
timestamp.set(Calendar.HOUR_OF_DAY, value[3]);
timestamp.set(Calendar.MINUTE, value[4]);
timestamp.set(Calendar.SECOND, value[5]);
}
return timestamp;
}
/**
* uses the standard algorithm to convert a Calendar object to a byte array to send to MiBand
* @param timestamp
* @return
*/
public static byte[] calendarToRawBytes(Calendar timestamp) {
return new byte[]{
(byte) (timestamp.get(Calendar.YEAR) - 2000),
(byte) timestamp.get(Calendar.MONTH),
(byte) timestamp.get(Calendar.DATE),
(byte) timestamp.get(Calendar.HOUR_OF_DAY),
(byte) timestamp.get(Calendar.MINUTE),
(byte) timestamp.get(Calendar.SECOND)
};
}
}

View File

@ -2,7 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.miband;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandDateConverter;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState; import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
public class BatteryInfo extends AbstractInfo { public class BatteryInfo extends AbstractInfo {
@ -46,7 +46,7 @@ public class BatteryInfo extends AbstractInfo {
GregorianCalendar lastCharge = new GregorianCalendar(); GregorianCalendar lastCharge = new GregorianCalendar();
if (mData.length >= 10) { if (mData.length >= 10) {
lastCharge = MiBandCoordinator.rawBytesToCalendar(new byte[]{ lastCharge = MiBandDateConverter.rawBytesToCalendar(new byte[]{
mData[1], mData[2], mData[3], mData[4], mData[5], mData[6] mData[1], mData[2], mData[3], mData[4], mData[5], mData[6]
}); });
} }

View File

@ -21,6 +21,7 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInf
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandDateConverter;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile; import nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State;
@ -428,7 +429,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
* @param builder * @param builder
*/ */
private MiBandSupport setCurrentTime(TransactionBuilder builder) { private MiBandSupport setCurrentTime(TransactionBuilder builder) {
byte[] nowBytes = MiBandCoordinator.calendarToRawBytes(GregorianCalendar.getInstance()); byte[] nowBytes = MiBandDateConverter.calendarToRawBytes(GregorianCalendar.getInstance());
byte[] time = new byte[]{ byte[] time = new byte[]{
nowBytes[0], nowBytes[0],
nowBytes[1], nowBytes[1],
@ -684,7 +685,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
* @param characteristic * @param characteristic
*/ */
private void queueAlarm(Alarm alarm, TransactionBuilder builder, BluetoothGattCharacteristic characteristic) { private void queueAlarm(Alarm alarm, TransactionBuilder builder, BluetoothGattCharacteristic characteristic) {
byte[] alarmCalBytes = MiBandCoordinator.calendarToRawBytes(alarm.getAlarmCal()); byte[] alarmCalBytes = MiBandDateConverter.calendarToRawBytes(alarm.getAlarmCal());
byte[] alarmMessage = new byte[]{ byte[] alarmMessage = new byte[]{
(byte) MiBandService.COMMAND_SET_TIMER, (byte) MiBandService.COMMAND_SET_TIMER,

View File

@ -19,7 +19,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandDateConverter;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService;
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEOperation; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEOperation;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
@ -225,7 +225,7 @@ public class FetchActivityOperation extends AbstractBTLEOperation<MiBandSupport>
* @param bytesTransferred * @param bytesTransferred
*/ */
private void sendAckDataTransfer(Calendar time, int bytesTransferred) { private void sendAckDataTransfer(Calendar time, int bytesTransferred) {
byte[] ackTime = MiBandCoordinator.calendarToRawBytes(time); byte[] ackTime = MiBandDateConverter.calendarToRawBytes(time);
byte[] ack = new byte[]{ byte[] ack = new byte[]{
MiBandService.COMMAND_CONFIRM_ACTIVITY_DATA_TRANSFER_COMPLETE, MiBandService.COMMAND_CONFIRM_ACTIVITY_DATA_TRANSFER_COMPLETE,
ackTime[0], ackTime[0],