This commit is contained in:
Milos Kozak 2017-09-15 13:52:28 +02:00
parent 08fca41b5f
commit 6662c9dd6c
5 changed files with 111 additions and 65 deletions

View file

@ -82,8 +82,8 @@ public class DanaRPump {
public double dailyTotalBasalUnits = 0; // RS only public double dailyTotalBasalUnits = 0; // RS only
public int maxDailyTotalUnits; public int maxDailyTotalUnits;
public double bolusStep; public double bolusStep = 0.1;
public double basalStep; public double basalStep = 0.1;
public double iob; public double iob;

View file

@ -218,8 +218,22 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface,
} }
public static void connect(String from) { public static void connect(String from) {
if (danaRSService != null && !mDeviceAddress.equals("") && !mDeviceName.equals(""))
danaRSService.connect(from, mDeviceAddress); if (danaRSService != null && !mDeviceAddress.equals("") && !mDeviceName.equals("")) {
final Object o = new Object();
danaRSService.connect(from, mDeviceAddress, o);
synchronized (o) {
try {
o.wait(20000);
} catch (InterruptedException e) {
log.error("InterruptedException " + e);
}
}
pumpDescription.basalStep = pump.basalStep;
pumpDescription.bolusStep = pump.bolusStep;
log.debug("RS connected");
}
} }
public static boolean isConnected() { public static boolean isConnected() {

View file

@ -52,7 +52,7 @@ public class DanaRS_Packet_Bolus_Set_Step_Bolus_Start extends DanaRS_Packet {
int dataSize = 1; int dataSize = 1;
int status = byteArrayToInt(getBytes(data, dataIndex, dataSize)); int status = byteArrayToInt(getBytes(data, dataIndex, dataSize));
failed = status != 0x01; failed = status != 0x00;
if (Config.logDanaMessageDetail) { if (Config.logDanaMessageDetail) {
log.debug("Result: " + status); log.debug("Result: " + status);

View file

@ -30,6 +30,7 @@ public class DanaRS_Packet_Bolus_Set_Step_Bolus_Stop extends DanaRS_Packet {
this.t = t; this.t = t;
this.amount = amount; this.amount = amount;
forced = false; forced = false;
stopped = false;
} }
@Override @Override

View file

@ -12,7 +12,6 @@ import android.bluetooth.BluetoothProfile;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Binder; import android.os.Binder;
import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.SystemClock; import android.os.SystemClock;
@ -34,21 +33,21 @@ import java.util.concurrent.TimeUnit;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
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.EventAppExit;
import info.nightscout.androidaps.events.EventInitializationChanged; import info.nightscout.androidaps.events.EventInitializationChanged;
import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
import info.nightscout.androidaps.plugins.PumpDanaR.events.EventDanaRNewStatus; import info.nightscout.androidaps.plugins.PumpDanaR.events.EventDanaRNewStatus;
import info.nightscout.androidaps.plugins.PumpDanaRS.DanaRSPlugin; import info.nightscout.androidaps.plugins.PumpDanaRS.DanaRSPlugin;
import info.nightscout.androidaps.plugins.PumpDanaRS.activities.PairingHelperActivity;
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.R;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
import info.nightscout.androidaps.plugins.PumpDanaRS.activities.PairingHelperActivity;
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Basal_Get_Basal_Rate; import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Basal_Get_Basal_Rate;
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Basal_Get_Profile_Number; import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Basal_Get_Profile_Number;
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Basal_Temporary_Basal_State; import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Basal_Temporary_Basal_State;
@ -61,6 +60,7 @@ import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Bolus_Se
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Bolus_Set_Step_Bolus_Stop; import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Bolus_Set_Step_Bolus_Stop;
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_General_Get_Shipping_Information; import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_General_Get_Shipping_Information;
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_General_Initial_Screen_Information; import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_General_Initial_Screen_Information;
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Notify_Delivery_Complete;
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Notify_Delivery_Rate_Display; import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Notify_Delivery_Rate_Display;
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Option_Get_Pump_Time; import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Option_Get_Pump_Time;
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Option_Set_Pump_Time; import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRS_Packet_Option_Set_Pump_Time;
@ -99,7 +99,7 @@ public class DanaRSService extends Service {
private DanaRPump danaRPump = DanaRPump.getInstance(); private DanaRPump danaRPump = DanaRPump.getInstance();
private Treatment bolusingTreatment = null; private Treatment bolusingTreatment = null;
private Handler mHandler = null; private Object mConfirmConnect = null;
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor(); private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
private static ScheduledFuture<?> scheduledDisconnection = null; private static ScheduledFuture<?> scheduledDisconnection = null;
@ -109,7 +109,6 @@ public class DanaRSService extends Service {
public DanaRSService() { public DanaRSService() {
mHandler = new Handler();
try { try {
MainApp.bus().unregister(this); MainApp.bus().unregister(this);
} catch (RuntimeException x) { } catch (RuntimeException x) {
@ -179,6 +178,7 @@ public class DanaRSService extends Service {
int speed = SP.getInt(R.string.key_danars_bolusspeed, 0); int speed = SP.getInt(R.string.key_danars_bolusspeed, 0);
DanaRS_Packet_Bolus_Set_Step_Bolus_Start start = new DanaRS_Packet_Bolus_Set_Step_Bolus_Start(insulin, speed); DanaRS_Packet_Bolus_Set_Step_Bolus_Start start = new DanaRS_Packet_Bolus_Set_Step_Bolus_Start(insulin, speed);
DanaRS_Packet_Bolus_Set_Step_Bolus_Stop stop = new DanaRS_Packet_Bolus_Set_Step_Bolus_Stop(insulin, t); DanaRS_Packet_Bolus_Set_Step_Bolus_Stop stop = new DanaRS_Packet_Bolus_Set_Step_Bolus_Stop(insulin, t);
DanaRS_Packet_Notify_Delivery_Complete complete = new DanaRS_Packet_Notify_Delivery_Complete(insulin, t);
if (!isConnected()) return false; if (!isConnected()) return false;
@ -297,7 +297,8 @@ public class DanaRSService extends Service {
return isConnecting; return isConnecting;
} }
public boolean connect(String from, String address) { public boolean connect(String from, String address, Object confirmConnect) {
mConfirmConnect = confirmConnect;
BluetoothManager tBluetoothManager = ((BluetoothManager) MainApp.instance().getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE)); BluetoothManager tBluetoothManager = ((BluetoothManager) MainApp.instance().getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE));
if (tBluetoothManager == null) { if (tBluetoothManager == null) {
return false; return false;
@ -412,16 +413,26 @@ public class DanaRSService extends Service {
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
log.debug("onCharacteristicRead" + (characteristic != null ? ":" + DanaRS_Packet.toHexString(characteristic.getValue()) : "")); log.debug("onCharacteristicRead" + (characteristic != null ? ":" + DanaRS_Packet.toHexString(characteristic.getValue()) : ""));
readDataParsing(characteristic.getValue()); addToReadBuffer(characteristic.getValue());
readDataParsing();
} }
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
log.debug("onCharacteristicChanged" + (characteristic != null ? ":" + DanaRS_Packet.toHexString(characteristic.getValue()) : "")); log.debug("onCharacteristicChanged" + (characteristic != null ? ":" + DanaRS_Packet.toHexString(characteristic.getValue()) : ""));
readDataParsing(characteristic.getValue()); addToReadBuffer(characteristic.getValue());
new Thread(new Runnable() {
@Override
public void run() {
readDataParsing();
}
}).start();
} }
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
log.debug("onCharacteristicWrite" + (characteristic != null ? ":" + DanaRS_Packet.toHexString(characteristic.getValue()) : "")); log.debug("onCharacteristicWrite" + (characteristic != null ? ":" + DanaRS_Packet.toHexString(characteristic.getValue()) : ""));
new Thread(new Runnable() {
@Override
public void run() {
synchronized (mSendQueue) { synchronized (mSendQueue) {
// after message sent, check if there is the rest of the message waiting and send it // after message sent, check if there is the rest of the message waiting and send it
if (mSendQueue.size() > 0) { if (mSendQueue.size() > 0) {
@ -431,6 +442,8 @@ public class DanaRSService extends Service {
} }
} }
} }
}).start();
}
}; };
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
@ -518,21 +531,27 @@ public class DanaRSService extends Service {
private byte[] readBuffer = new byte[1024]; private byte[] readBuffer = new byte[1024];
private int bufferLength = 0; private int bufferLength = 0;
private void readDataParsing(byte[] buffer) { private void addToReadBuffer(byte[] buffer) {
boolean startSignatureFound = false, packetIsValid = false; //log.debug("addToReadBuffer " + DanaRS_Packet.toHexString(buffer));
boolean isProcessing;
if (buffer == null || buffer.length == 0) { if (buffer == null || buffer.length == 0) {
return; return;
} }
synchronized (readBuffer) {
// Append incomming data to input buffer // Append incomming data to input buffer
System.arraycopy(buffer, 0, readBuffer, bufferLength, buffer.length); System.arraycopy(buffer, 0, readBuffer, bufferLength, buffer.length);
bufferLength += buffer.length; bufferLength += buffer.length;
}
}
private void readDataParsing() {
boolean startSignatureFound = false, packetIsValid = false;
boolean isProcessing;
isProcessing = true; isProcessing = true;
while (isProcessing) { while (isProcessing) {
int length = 0;
synchronized (readBuffer) {
// Find packet start [A5 A5] // Find packet start [A5 A5]
if (bufferLength >= 6) { if (bufferLength >= 6) {
for (int idxStartByte = 0; idxStartByte < bufferLength - 2; idxStartByte++) { for (int idxStartByte = 0; idxStartByte < bufferLength - 2; idxStartByte++) {
@ -551,7 +570,6 @@ public class DanaRSService extends Service {
// A5 A5 LEN TYPE CODE PARAMS CHECKSUM1 CHECKSUM2 5A 5A // A5 A5 LEN TYPE CODE PARAMS CHECKSUM1 CHECKSUM2 5A 5A
// ^---- LEN -----^ // ^---- LEN -----^
// total packet length 2 + 1 + readBuffer[2] + 2 + 2 // total packet length 2 + 1 + readBuffer[2] + 2 + 2
int length = 0;
if (startSignatureFound) { if (startSignatureFound) {
length = readBuffer[2]; length = readBuffer[2];
// test if there is enough data loaded // test if there is enough data loaded
@ -562,15 +580,26 @@ public class DanaRSService extends Service {
packetIsValid = true; packetIsValid = true;
} }
} }
}
if (packetIsValid) { if (packetIsValid) {
// copy packet to input buffer
byte[] inputBuffer = new byte[length + 7]; byte[] inputBuffer = new byte[length + 7];
synchronized (readBuffer) {
// copy packet to input buffer
System.arraycopy(readBuffer, 0, inputBuffer, 0, length + 7); System.arraycopy(readBuffer, 0, inputBuffer, 0, length + 7);
// Cut off the message from readBuffer
System.arraycopy(readBuffer, length + 7, readBuffer, 0, bufferLength - (length + 7));
bufferLength -= (length + 7);
}
// now we have encrypted packet in inputBuffer // now we have encrypted packet in inputBuffer
try { try {
// decrypt the packet // decrypt the packet
inputBuffer = BleCommandUtil.getInstance().getDecryptedPacket(inputBuffer); inputBuffer = BleCommandUtil.getInstance().getDecryptedPacket(inputBuffer);
if (inputBuffer == null) {
log.debug("Null decryptedInputBuffer");
return;
}
switch (inputBuffer[0]) { switch (inputBuffer[0]) {
// initial handshake packet // initial handshake packet
case (byte) BleCommandUtil.DANAR_PACKET__TYPE_ENCRYPTION_RESPONSE: case (byte) BleCommandUtil.DANAR_PACKET__TYPE_ENCRYPTION_RESPONSE:
@ -642,6 +671,14 @@ public class DanaRSService extends Service {
getPumpStatus(); getPumpStatus();
scheduleDisconnection(); scheduleDisconnection();
isConnected = true;
isConnecting = false;
if (mConfirmConnect != null) {
synchronized (mConfirmConnect) {
mConfirmConnect.notify();
mConfirmConnect = null;
}
}
break; break;
} }
break; break;
@ -677,9 +714,6 @@ public class DanaRSService extends Service {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
// Cut off the message from readBuffer
System.arraycopy(readBuffer, length + 7, readBuffer, 0, bufferLength - (length + 7));
bufferLength -= (length + 7);
startSignatureFound = false; startSignatureFound = false;
packetIsValid = false; packetIsValid = false;
if (bufferLength < 6) { if (bufferLength < 6) {
@ -765,10 +799,7 @@ public class DanaRSService extends Service {
} }
} }
try { SystemClock.sleep(200);
Thread.sleep(200);
} catch (InterruptedException e) {
}
if (!message.isReceived()) { if (!message.isReceived()) {
log.warn("Reply not received " + message.getFriendlyName()); log.warn("Reply not received " + message.getFriendlyName());
} }