refactor connectivity notifications
This commit is contained in:
parent
ed6cabe7fa
commit
13fd6136c3
3 changed files with 42 additions and 33 deletions
|
@ -18,14 +18,24 @@ public class EventPumpStatusChanged {
|
||||||
public int sSecondsElapsed = 0;
|
public int sSecondsElapsed = 0;
|
||||||
public String sPerfomingAction = "";
|
public String sPerfomingAction = "";
|
||||||
|
|
||||||
|
public static String error = "";
|
||||||
|
|
||||||
public EventPumpStatusChanged(int status) {
|
public EventPumpStatusChanged(int status) {
|
||||||
sStatus = status;
|
sStatus = status;
|
||||||
sSecondsElapsed = 0;
|
sSecondsElapsed = 0;
|
||||||
|
error = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public EventPumpStatusChanged(int status, int secondsElapsed) {
|
public EventPumpStatusChanged(int status, int secondsElapsed) {
|
||||||
sStatus = status;
|
sStatus = status;
|
||||||
sSecondsElapsed = secondsElapsed;
|
sSecondsElapsed = secondsElapsed;
|
||||||
|
error = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public EventPumpStatusChanged(int status, String error) {
|
||||||
|
sStatus = status;
|
||||||
|
sSecondsElapsed = 0;
|
||||||
|
this.error = error;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EventPumpStatusChanged(String action) {
|
public EventPumpStatusChanged(String action) {
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
package info.nightscout.androidaps.plugins.PumpDanaRS.events;
|
|
||||||
|
|
||||||
import android.bluetooth.BluetoothDevice;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by mike on 01.09.2017.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class EventDanaRSConnection {
|
|
||||||
public EventDanaRSConnection(boolean isConnect, boolean isBusy, boolean isError, BluetoothDevice device) {
|
|
||||||
this.isConnect = isConnect;
|
|
||||||
this.isBusy = isBusy;
|
|
||||||
this.isError = isError;
|
|
||||||
this.device = device;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isConnect;
|
|
||||||
public boolean isBusy;
|
|
||||||
public boolean isError;
|
|
||||||
public BluetoothDevice device;
|
|
||||||
}
|
|
|
@ -25,10 +25,15 @@ import org.slf4j.LoggerFactory;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import info.nightscout.androidaps.Config;
|
import info.nightscout.androidaps.Config;
|
||||||
import info.nightscout.androidaps.data.Profile;
|
import info.nightscout.androidaps.data.Profile;
|
||||||
import info.nightscout.androidaps.db.Treatment;
|
import info.nightscout.androidaps.db.Treatment;
|
||||||
|
import info.nightscout.androidaps.events.EventPumpStatusChanged;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRSMessageHashTable;
|
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRSMessageHashTable;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet;
|
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet;
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
|
@ -36,7 +41,6 @@ import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.events.EventAppExit;
|
import info.nightscout.androidaps.events.EventAppExit;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
|
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaRS.activities.PairingHelperActivity;
|
import info.nightscout.androidaps.plugins.PumpDanaRS.activities.PairingHelperActivity;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaRS.events.EventDanaRSConnection;
|
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaRS.events.EventDanaRSPacket;
|
import info.nightscout.androidaps.plugins.PumpDanaRS.events.EventDanaRSPacket;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaRS.events.EventDanaRSPairingSuccess;
|
import info.nightscout.androidaps.plugins.PumpDanaRS.events.EventDanaRSPairingSuccess;
|
||||||
import info.nightscout.utils.SP;
|
import info.nightscout.utils.SP;
|
||||||
|
@ -70,6 +74,9 @@ public class DanaRSService extends Service {
|
||||||
|
|
||||||
private Handler mHandler = null;
|
private Handler mHandler = null;
|
||||||
|
|
||||||
|
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
private static ScheduledFuture<?> scheduledDisconnection = null;
|
||||||
|
|
||||||
private DanaRS_Packet processsedMessage = null;
|
private DanaRS_Packet processsedMessage = null;
|
||||||
private ArrayList<byte[]> mSendQueue = new ArrayList<>();
|
private ArrayList<byte[]> mSendQueue = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -195,6 +202,7 @@ public class DanaRSService extends Service {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTING));
|
||||||
isConnecting = true;
|
isConnecting = true;
|
||||||
if ((mBluetoothDeviceAddress != null) && (address.equals(mBluetoothDeviceAddress)) && (mBluetoothGatt != null)) {
|
if ((mBluetoothDeviceAddress != null) && (address.equals(mBluetoothDeviceAddress)) && (mBluetoothGatt != null)) {
|
||||||
log.debug("Trying to use an existing mBluetoothGatt for connection.");
|
log.debug("Trying to use an existing mBluetoothGatt for connection.");
|
||||||
|
@ -260,7 +268,7 @@ public class DanaRSService extends Service {
|
||||||
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
||||||
close();
|
close();
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
sendBTConnect(false, false, false, null);
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -466,10 +474,12 @@ public class DanaRSService extends Service {
|
||||||
|
|
||||||
} else if (inputBuffer.length == 6 && inputBuffer[2] == 'B' && inputBuffer[3] == 'U' && inputBuffer[4] == 'S' && inputBuffer[5] == 'Y') {
|
} else if (inputBuffer.length == 6 && inputBuffer[2] == 'B' && inputBuffer[3] == 'U' && inputBuffer[4] == 'S' && inputBuffer[5] == 'Y') {
|
||||||
log.debug("<<<<< " + "ENCRYPTION__PUMP_CHECK (BUSY)" + " " + DanaRS_Packet.toHexString(inputBuffer));
|
log.debug("<<<<< " + "ENCRYPTION__PUMP_CHECK (BUSY)" + " " + DanaRS_Packet.toHexString(inputBuffer));
|
||||||
sendBTConnect(false, true, false, null);
|
mSendQueue.clear();
|
||||||
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED, MainApp.sResources.getString(R.string.pumpbusy)));
|
||||||
} else {
|
} else {
|
||||||
log.debug("<<<<< " + "ENCRYPTION__PUMP_CHECK (ERROR)" + " " + DanaRS_Packet.toHexString(inputBuffer));
|
log.debug("<<<<< " + "ENCRYPTION__PUMP_CHECK (ERROR)" + " " + DanaRS_Packet.toHexString(inputBuffer));
|
||||||
sendBTConnect(false, false, true, null);
|
mSendQueue.clear();
|
||||||
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED, MainApp.sResources.getString(R.string.connectionerror)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
// 2nd packet, pairing key
|
// 2nd packet, pairing key
|
||||||
|
@ -508,7 +518,8 @@ public class DanaRSService extends Service {
|
||||||
pass = pass ^ 3463;
|
pass = pass ^ 3463;
|
||||||
DanaRPump.getInstance().rs_password = Integer.toHexString(pass);
|
DanaRPump.getInstance().rs_password = Integer.toHexString(pass);
|
||||||
log.debug("Pump user password: " + Integer.toHexString(pass));
|
log.debug("Pump user password: " + Integer.toHexString(pass));
|
||||||
sendBTConnect(true, false, false, getConnectDevice());
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTED));
|
||||||
|
scheduleDisconnection();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -538,6 +549,7 @@ public class DanaRSService extends Service {
|
||||||
} else {
|
} else {
|
||||||
log.error("Unknown message received " + DanaRS_Packet.toHexString(inputBuffer));
|
log.error("Unknown message received " + DanaRS_Packet.toHexString(inputBuffer));
|
||||||
}
|
}
|
||||||
|
scheduleDisconnection();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -638,6 +650,7 @@ public class DanaRSService extends Service {
|
||||||
if (!message.isReceived()) {
|
if (!message.isReceived()) {
|
||||||
log.warn("Reply not received " + message.getFriendlyName());
|
log.warn("Reply not received " + message.getFriendlyName());
|
||||||
}
|
}
|
||||||
|
scheduleDisconnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SendPairingRequest() {
|
private void SendPairingRequest() {
|
||||||
|
@ -659,13 +672,20 @@ public class DanaRSService extends Service {
|
||||||
writeCharacteristic_NO_RESPONSE(getUARTWriteBTGattChar(), bytes);
|
writeCharacteristic_NO_RESPONSE(getUARTWriteBTGattChar(), bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendBTConnect(boolean isConnected, boolean isBusy, boolean isError, BluetoothDevice device) {
|
public void scheduleDisconnection() {
|
||||||
if (isConnected) {
|
class DisconnectRunnable implements Runnable {
|
||||||
this.isConnected = true;
|
public void run() {
|
||||||
} else {
|
disconnect("scheduleDisconnection");
|
||||||
mSendQueue.clear();
|
scheduledDisconnection = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// prepare task for execution in 5 sec
|
||||||
MainApp.bus().post(new EventDanaRSConnection(isConnected, isBusy, isError, device));
|
// cancel waiting task to prevent sending multiple disconnections
|
||||||
|
if (scheduledDisconnection != null)
|
||||||
|
scheduledDisconnection.cancel(false);
|
||||||
|
Runnable task = new DisconnectRunnable();
|
||||||
|
final int sec = 5;
|
||||||
|
scheduledDisconnection = worker.schedule(task, sec, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue