use of com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4 in project xDrip by NightscoutFoundation.
the class MiBandService method sendSettings.
private void sendSettings() {
List<Pair<Integer, Boolean>> features = PrefBindingFactory.getInstance(getPrefBinder()).getStates("miband_feature_");
FeaturesControllMessage featureMessage = new FeaturesControllMessage();
for (Pair<Integer, Boolean> item : features) {
byte[] message = featureMessage.getMessage(item);
if (message.length != 0) {
new QueueMe().setBytes(message).setQueueWriteCharacterstic(featureMessage.getCharacteristicUUID()).setDescription("Set feature:" + item.first + ":" + item.second).expireInSeconds(QUEUE_EXPIRED_TIME).setDelayMs(QUEUE_DELAY).queue();
}
}
List<Integer> screenOpt = PrefBindingFactory.getInstance(getPrefBinder()).getEnabled("miband_screen");
DisplayControllMessage dispMessage;
MiBand.MiBandType type = MiBand.getMibandType();
if (type == MI_BAND2)
dispMessage = new DisplayControllMessageMiBand2();
else
dispMessage = new DisplayControllMessageMiband3_4();
new QueueMe().setBytes(dispMessage.getDisplayItemsCmd(screenOpt)).setQueueWriteCharacterstic(dispMessage.getCharacteristicUUID()).setDescription("Set screens").expireInSeconds(QUEUE_EXPIRED_TIME).setDelayMs(QUEUE_DELAY).queue();
setNightMode();
}
use of com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4 in project xDrip-plus by jamorham.
the class MiBandService method processFirmwareCommands.
@SuppressLint("CheckResult")
private void processFirmwareCommands(byte[] value, boolean isSeqCommand) {
RxBleConnection connection = I.connection;
FirmwareOperations.SequenceType seq = firmware.getSequence();
if (d)
UserError.Log.d(TAG, "processFirmwareCommands: " + bytesToHex(value) + ": seq:" + seq.toString());
if (isSeqCommand) {
switch(seq) {
case SET_NIGHTMODE:
{
if (true) {
isNeedToRestoreNightMode = true;
DisplayControllMessageMiband3_4 dispControl = new DisplayControllMessageMiband3_4();
Calendar sheduledCalendar = Calendar.getInstance();
sheduledCalendar.set(Calendar.HOUR_OF_DAY, 0);
sheduledCalendar.set(Calendar.MINUTE, 0);
Date sheduledDate = sheduledCalendar.getTime();
connection.writeCharacteristic(dispControl.getCharacteristicUUID(), dispControl.setNightModeCmd(Sheduled, sheduledDate, sheduledDate)).subscribe(valB -> {
UserError.Log.d(TAG, "Wrote nigntmode, got: " + JoH.bytesToHex(valB));
firmware.nextSequence();
processFirmwareCommands(null, true);
}, throwable -> {
UserError.Log.e(TAG, "Could not write nigntmode: " + throwable);
firmware.nextSequence();
processFirmwareCommands(null, true);
});
} else {
firmware.nextSequence();
processFirmwareCommands(null, true);
}
break;
}
case PREPARE_UPLOAD:
{
connection.writeCharacteristic(firmware.getFirmwareCharacteristicUUID(), firmware.prepareFWUploadInitCommand()).subscribe(valB -> {
UserError.Log.d(TAG, "Wrote prepareFWUploadInitCommand, got: " + JoH.bytesToHex(valB));
}, throwable -> {
UserError.Log.e(TAG, "Could not write prepareFWUploadInitCommand: " + throwable);
resetFirmwareState(false);
});
firmware.nextSequence();
break;
}
}
return;
} else {
if (value.length != 3 && value.length != 11) {
UserError.Log.e(TAG, "Notifications should be 3 or 11 bytes long.");
return;
}
boolean success = value[2] == OperationCodes.SUCCESS;
if (value[0] == OperationCodes.RESPONSE && success) {
try {
switch(value[1]) {
case OperationCodes.COMMAND_FIRMWARE_INIT:
{
if (seq == FirmwareOperations.SequenceType.TRANSFER_FW_START) {
connection.writeCharacteristic(firmware.getFirmwareCharacteristicUUID(), firmware.getFirmwareStartCommand()).subscribe(valB -> {
UserError.Log.d(TAG, "Wrote Start command, got: " + JoH.bytesToHex(valB));
}, throwable -> {
UserError.Log.e(TAG, "Could not write Start command: " + throwable);
resetFirmwareState(false);
});
firmware.nextSequence();
} else if (seq == FirmwareOperations.SequenceType.TRANSFER_SEND_WF_INFO) {
connection.writeCharacteristic(firmware.getFirmwareCharacteristicUUID(), firmware.sendFwInfo()).subscribe(valB -> {
UserError.Log.d(TAG, "Wrote sendFwInfo, got: " + JoH.bytesToHex(valB));
}, throwable -> {
UserError.Log.e(TAG, "Could not write firmware info: " + throwable);
resetFirmwareState(false);
});
firmware.nextSequence();
break;
}
break;
}
case OperationCodes.COMMAND_FIRMWARE_START_DATA:
{
sendFirmwareData();
break;
}
case OperationCodes.COMMAND_FIRMWARE_CHECKSUM:
{
firmware.nextSequence();
if (firmware.getFirmwareType() == FirmwareOperations.FirmwareType.FIRMWARE) {
// send reboot
} else {
UserError.Log.e(TAG, "Watch Face has been installed successfully");
resetFirmwareState(true);
}
break;
}
case OperationCodes.COMMAND_FIRMWARE_REBOOT:
{
UserError.Log.e(TAG, "Reboot command successfully sent.");
resetFirmwareState(true);
break;
}
default:
{
resetFirmwareState(false, "Unexpected response during firmware update");
}
}
} catch (Exception ex) {
resetFirmwareState(false);
}
} else {
String errorMessage = null;
Boolean sendBGNotification = false;
if (value[2] == OperationCodes.LOW_BATTERY_ERROR) {
errorMessage = "Cannot upload watchface, low battery, please charge device";
sendBGNotification = true;
} else if (value[2] == OperationCodes.TIMER_RUNNING) {
errorMessage = "Cannot upload watchface, timer running on band";
} else if (value[2] == OperationCodes.ON_CALL) {
errorMessage = "Cannot upload watchface, call in progress";
} else {
errorMessage = "Unexpected notification during firmware update:" + JoH.bytesToHex(value);
}
resetFirmwareState(false, errorMessage);
if (sendBGNotification) {
emptyQueue();
JoH.startService(MiBandService.class, "function", "update_bg_as_notification");
changeState(SLEEP);
}
}
}
}
use of com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4 in project xDrip-plus by jamorham.
the class MiBandService method sendSettings.
private void sendSettings() {
List<Pair<Integer, Boolean>> features = PrefBindingFactory.getInstance(getPrefBinder()).getStates("miband_feature_");
FeaturesControllMessage featureMessage = new FeaturesControllMessage();
for (Pair<Integer, Boolean> item : features) {
byte[] message = featureMessage.getMessage(item);
if (message.length != 0) {
new QueueMe().setBytes(message).setQueueWriteCharacterstic(featureMessage.getCharacteristicUUID()).setDescription("Set feature:" + item.first + ":" + item.second).expireInSeconds(QUEUE_EXPIRED_TIME).setDelayMs(QUEUE_DELAY).queue();
}
}
List<Integer> screenOpt = PrefBindingFactory.getInstance(getPrefBinder()).getEnabled("miband_screen");
DisplayControllMessage dispMessage;
MiBand.MiBandType type = MiBand.getMibandType();
if (type == MI_BAND2)
dispMessage = new DisplayControllMessageMiBand2();
else
dispMessage = new DisplayControllMessageMiband3_4();
new QueueMe().setBytes(dispMessage.getDisplayItemsCmd(screenOpt)).setQueueWriteCharacterstic(dispMessage.getCharacteristicUUID()).setDescription("Set screens").expireInSeconds(QUEUE_EXPIRED_TIME).setDelayMs(QUEUE_DELAY).queue();
setNightMode();
}
use of com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4 in project xDrip by NightscoutFoundation.
the class MiBandService method processFirmwareCommands.
@SuppressLint("CheckResult")
private void processFirmwareCommands(byte[] value, boolean isSeqCommand) {
RxBleConnection connection = I.connection;
FirmwareOperations.SequenceType seq = firmware.getSequence();
if (d)
UserError.Log.d(TAG, "processFirmwareCommands: " + bytesToHex(value) + ": seq:" + seq.toString());
if (isSeqCommand) {
switch(seq) {
case SET_NIGHTMODE:
{
if (true) {
isNeedToRestoreNightMode = true;
DisplayControllMessageMiband3_4 dispControl = new DisplayControllMessageMiband3_4();
Calendar sheduledCalendar = Calendar.getInstance();
sheduledCalendar.set(Calendar.HOUR_OF_DAY, 0);
sheduledCalendar.set(Calendar.MINUTE, 0);
Date sheduledDate = sheduledCalendar.getTime();
connection.writeCharacteristic(dispControl.getCharacteristicUUID(), dispControl.setNightModeCmd(Sheduled, sheduledDate, sheduledDate)).subscribe(valB -> {
UserError.Log.d(TAG, "Wrote nigntmode, got: " + JoH.bytesToHex(valB));
firmware.nextSequence();
processFirmwareCommands(null, true);
}, throwable -> {
UserError.Log.e(TAG, "Could not write nigntmode: " + throwable);
firmware.nextSequence();
processFirmwareCommands(null, true);
});
} else {
firmware.nextSequence();
processFirmwareCommands(null, true);
}
break;
}
case PREPARE_UPLOAD:
{
connection.writeCharacteristic(firmware.getFirmwareCharacteristicUUID(), firmware.prepareFWUploadInitCommand()).subscribe(valB -> {
UserError.Log.d(TAG, "Wrote prepareFWUploadInitCommand, got: " + JoH.bytesToHex(valB));
}, throwable -> {
UserError.Log.e(TAG, "Could not write prepareFWUploadInitCommand: " + throwable);
resetFirmwareState(false);
});
firmware.nextSequence();
break;
}
}
return;
} else {
if (value.length != 3 && value.length != 11) {
UserError.Log.e(TAG, "Notifications should be 3 or 11 bytes long.");
return;
}
boolean success = value[2] == OperationCodes.SUCCESS;
if (value[0] == OperationCodes.RESPONSE && success) {
try {
switch(value[1]) {
case OperationCodes.COMMAND_FIRMWARE_INIT:
{
if (seq == FirmwareOperations.SequenceType.TRANSFER_FW_START) {
connection.writeCharacteristic(firmware.getFirmwareCharacteristicUUID(), firmware.getFirmwareStartCommand()).subscribe(valB -> {
UserError.Log.d(TAG, "Wrote Start command, got: " + JoH.bytesToHex(valB));
}, throwable -> {
UserError.Log.e(TAG, "Could not write Start command: " + throwable);
resetFirmwareState(false);
});
firmware.nextSequence();
} else if (seq == FirmwareOperations.SequenceType.TRANSFER_SEND_WF_INFO) {
connection.writeCharacteristic(firmware.getFirmwareCharacteristicUUID(), firmware.sendFwInfo()).subscribe(valB -> {
UserError.Log.d(TAG, "Wrote sendFwInfo, got: " + JoH.bytesToHex(valB));
}, throwable -> {
UserError.Log.e(TAG, "Could not write firmware info: " + throwable);
resetFirmwareState(false);
});
firmware.nextSequence();
break;
}
break;
}
case OperationCodes.COMMAND_FIRMWARE_START_DATA:
{
sendFirmwareData();
break;
}
case OperationCodes.COMMAND_FIRMWARE_CHECKSUM:
{
firmware.nextSequence();
if (firmware.getFirmwareType() == FirmwareOperations.FirmwareType.FIRMWARE) {
// send reboot
} else {
UserError.Log.e(TAG, "Watch Face has been installed successfully");
resetFirmwareState(true);
}
break;
}
case OperationCodes.COMMAND_FIRMWARE_REBOOT:
{
UserError.Log.e(TAG, "Reboot command successfully sent.");
resetFirmwareState(true);
break;
}
default:
{
resetFirmwareState(false, "Unexpected response during firmware update");
}
}
} catch (Exception ex) {
resetFirmwareState(false);
}
} else {
String errorMessage = null;
Boolean sendBGNotification = false;
if (value[2] == OperationCodes.LOW_BATTERY_ERROR) {
errorMessage = "Cannot upload watchface, low battery, please charge device";
sendBGNotification = true;
} else if (value[2] == OperationCodes.TIMER_RUNNING) {
errorMessage = "Cannot upload watchface, timer running on band";
} else if (value[2] == OperationCodes.ON_CALL) {
errorMessage = "Cannot upload watchface, call in progress";
} else {
errorMessage = "Unexpected notification during firmware update:" + JoH.bytesToHex(value);
}
resetFirmwareState(false, errorMessage);
if (sendBGNotification) {
emptyQueue();
JoH.startService(MiBandService.class, "function", "update_bg_as_notification");
changeState(SLEEP);
}
}
}
}
use of com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4 in project xDrip by NightscoutFoundation.
the class MiBandService method setNightMode.
private void setNightMode() {
if (d)
UserError.Log.d(TAG, "Restore night mode");
Date start = null, end = null;
DisplayControllMessageMiband3_4.NightMode nightMode = DisplayControllMessageMiband3_4.NightMode.Off;
if (MiBandEntry.isNightModeEnabled()) {
nightMode = DisplayControllMessageMiband3_4.NightMode.Sheduled;
start = MiBandEntry.getNightModeStart();
end = MiBandEntry.getNightModeEnd();
}
RxBleConnection connection = I.connection;
DisplayControllMessageMiband3_4 dispControl = new DisplayControllMessageMiband3_4();
connection.writeCharacteristic(dispControl.getCharacteristicUUID(), dispControl.setNightModeCmd(nightMode, start, end)).subscribe(valB -> {
if (d)
UserError.Log.d(TAG, "Wrote nightmode");
isNeedToRestoreNightMode = false;
}, throwable -> {
if (d)
UserError.Log.e(TAG, "Could not write nightmode: " + throwable);
});
}
Aggregations