R: handle not received time, refactor to kotlin, unify SerialIOThread

This commit is contained in:
Milos Kozak 2019-09-08 14:17:41 +02:00
parent 118b4bc0d1
commit a3bc942cd7
17 changed files with 277 additions and 691 deletions

View file

@ -12,7 +12,7 @@ import java.io.OutputStream;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageBase;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageHashTable;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageHashTableBase;
import info.nightscout.androidaps.plugins.pump.danaR.services.AbstractSerialIOThread;
import info.nightscout.androidaps.utils.CRC;
@ -30,9 +30,11 @@ public class SerialIOThread extends AbstractSerialIOThread {
private byte[] mReadBuff = new byte[0];
private MessageBase processedMessage;
private MessageHashTableBase hashTable;
public SerialIOThread(BluetoothSocket rfcommSocket) {
public SerialIOThread(BluetoothSocket rfcommSocket, MessageHashTableBase hashTable) {
super();
this.hashTable = hashTable;
mRfCommSocket = rfcommSocket;
try {
@ -68,11 +70,11 @@ public class SerialIOThread extends AbstractSerialIOThread {
message = processedMessage;
} else {
// get it from hash table
message = MessageHashTable.findMessage(command);
message = hashTable.findMessage(command);
}
if (L.isEnabled(L.PUMPBTCOMM))
log.debug("<<<<< " + message.getMessageName() + " " + message.toHexString(extractedBuff));
log.debug("<<<<< " + message.getMessageName() + " " + MessageBase.toHexString(extractedBuff));
// process the message content
message.received = true;
@ -83,14 +85,14 @@ public class SerialIOThread extends AbstractSerialIOThread {
}
}
} catch (Exception e) {
if (e.getMessage().indexOf("bt socket closed") < 0)
if (!e.getMessage().contains("bt socket closed"))
log.error("Thread exception: ", e);
mKeepRunning = false;
}
disconnect("EndOfLoop");
}
void appendToBuffer(byte[] newData, int gotBytes) {
private void appendToBuffer(byte[] newData, int gotBytes) {
// add newData to mReadBuff
byte[] newReadBuff = new byte[mReadBuff.length + gotBytes];
System.arraycopy(mReadBuff, 0, newReadBuff, 0, mReadBuff.length);
@ -98,7 +100,7 @@ public class SerialIOThread extends AbstractSerialIOThread {
mReadBuff = newReadBuff;
}
byte[] cutMessageFromBuffer() {
private byte[] cutMessageFromBuffer() {
if (mReadBuff[0] == (byte) 0x7E && mReadBuff[1] == (byte) 0x7E) {
int length = (mReadBuff[2] & 0xFF) + 7;
// Check if we have enough data
@ -148,7 +150,7 @@ public class SerialIOThread extends AbstractSerialIOThread {
byte[] messageBytes = message.getRawMessageBytes();
if (L.isEnabled(L.PUMPBTCOMM))
log.debug(">>>>> " + message.getMessageName() + " " + message.toHexString(messageBytes));
log.debug(">>>>> " + message.getMessageName() + " " + MessageBase.toHexString(messageBytes));
try {
mOutputStream.write(messageBytes);
@ -165,8 +167,10 @@ public class SerialIOThread extends AbstractSerialIOThread {
}
SystemClock.sleep(200);
if (!message.received) {
log.warn("Reply not received " + message.getMessageName());
if (!message.isReceived()) {
message.handleMessageNotReceived();
if (L.isEnabled(L.PUMPBTCOMM))
log.error("Reply not received " + message.getMessageName());
if (message.getCommand() == 0xF0F1) {
DanaRPump.getInstance().isNewPump = false;
if (L.isEnabled(L.PUMPCOMM))

View file

@ -105,6 +105,9 @@ public class MessageBase {
}
}
public void handleMessageNotReceived() {
}
public int getCommand() {
int command = byteFromRawBuff(buffer, 5) | (byteFromRawBuff(buffer, 4) << 8);
return command;
@ -189,4 +192,8 @@ public class MessageBase {
return sb.toString();
}
public boolean isReceived() {
return received;
}
}

View file

@ -1,82 +0,0 @@
package info.nightscout.androidaps.plugins.pump.danaR.comm;
import java.util.HashMap;
/**
* Created by mike on 28.05.2016.
*/
public class MessageHashTable {
public static HashMap<Integer, MessageBase> messages = null;
static {
if (messages == null) {
messages = new HashMap<Integer, MessageBase>();
put(new MsgBolusStop()); // 0x0101 CMD_MEALINS_STOP
put(new MsgBolusStart()); // 0x0102 CMD_MEALINS_START_DATA
put(new MsgBolusStartWithSpeed()); // 0x0104 CMD_MEALINS_START_DATA_SPEED
put(new MsgBolusProgress()); // 0x0202 CMD_PUMP_THIS_REMAINDER_MEAL_INS
put(new MsgStatusProfile()); // 0x0204 CMD_PUMP_CALCULATION_SETTING
put(new MsgStatusTempBasal()); // 0x0205 CMD_PUMP_EXERCISE_MODE
put(new MsgStatusBolusExtended()); // 0x0207 CMD_PUMP_EXPANS_INS_I
put(new MsgStatusBasic()); // 0x020A CMD_PUMP_INITVIEW_I
put(new MsgStatus()); // 0x020B CMD_PUMP_STATUS
put(new MsgInitConnStatusTime()); // 0x0301 CMD_PUMPINIT_TIME_INFO
put(new MsgInitConnStatusBolus()); // 0x0302 CMD_PUMPINIT_BOLUS_INFO
put(new MsgInitConnStatusBasic()); // 0x0303 CMD_PUMPINIT_INIT_INFO
put(new MsgInitConnStatusOption()); // 0x0304 CMD_PUMPINIT_OPTION
put(new MsgSetTempBasalStart()); // 0x0401 CMD_PUMPSET_EXERCISE_S
put(new MsgSetCarbsEntry()); // 0x0402 CMD_PUMPSET_HIS_S
put(new MsgSetTempBasalStop()); // 0x0403 CMD_PUMPSET_EXERCISE_STOP
put(new MsgSetExtendedBolusStop()); // 0x0406 CMD_PUMPSET_EXPANS_INS_STOP
put(new MsgSetExtendedBolusStart()); // 0x0407 CMD_PUMPSET_EXPANS_INS_S
put(new MsgError()); // 0x0601 CMD_PUMPOWAY_SYSTEM_STATUS
put(new MsgPCCommStart()); // 0x3001 CMD_CONNECT
put(new MsgPCCommStop()); // 0x3002 CMD_DISCONNECT
put(new MsgHistoryBolus()); // 0x3101 CMD_HISTORY_MEAL_INS
put(new MsgHistoryDailyInsulin()); // 0x3102 CMD_HISTORY_DAY_INS
put(new MsgHistoryGlucose()); // 0x3104 CMD_HISTORY_GLUCOSE
put(new MsgHistoryAlarm()); // 0x3105 CMD_HISTORY_ALARM
put(new MsgHistoryError()); // 0x3106 CMD_HISTORY_ERROR
put(new MsgHistoryCarbo()); // 0x3107 CMD_HISTORY_CARBOHY
put(new MsgHistoryRefill()); // 0x3108 CMD_HISTORY_REFILL
put(new MsgHistorySuspend()); // 0x3109 CMD_HISTORY_SUSPEND
put(new MsgHistoryBasalHour()); // 0x310A CMD_HISTORY_BASAL_HOUR
put(new MsgHistoryDone()); // 0x31F1 CMD_HISTORY_DONT_USED
put(new MsgSettingBasal()); // 0x3202 CMD_SETTING_V_BASAL_INS_I
put(new MsgSettingMeal()); // 0x3203 CMD_SETTING_V_MEAL_SETTING_I
put(new MsgSettingProfileRatios()); // 0x3204 CMD_SETTING_V_CCC_I
put(new MsgSettingMaxValues()); // 0x3205 CMD_SETTING_V_MAX_VALUE_I
put(new MsgSettingBasalProfileAll()); // 0x3206 CMD_SETTING_V_BASAL_PROFILE_ALL
put(new MsgSettingShippingInfo()); // 0x3207 CMD_SETTING_V_SHIPPING_I
put(new MsgSettingGlucose()); // 0x3209 CMD_SETTING_V_GLUCOSEandEASY
put(new MsgSettingPumpTime()); // 0x320A CMD_SETTING_V_TIME_I
put(new MsgSettingUserOptions()); // 0x320B CMD_SETTING_V_USER_OPTIONS
put(new MsgSettingActiveProfile()); // 0x320C CMD_SETTING_V_PROFILE_NUMBER
put(new MsgSettingProfileRatiosAll()); // 0x320D CMD_SETTING_V_CIR_CF_VALUE
put(new MsgSetSingleBasalProfile()); // 0x3302 CMD_SETTING_BASAL_INS_S
put(new MsgSetBasalProfile()); // 0x3306 CMD_SETTING_BASAL_PROFILE_S
put(new MsgSetUserOptions()); // 0x330B CMD_SETTING_USER_OPTIONS_S
put(new MsgSetActivateBasalProfile()); // 0x330C CMD_SETTING_PROFILE_NUMBER_S
put(new MsgHistoryAllDone()); // 0x41F1 CMD_HISTORY_ALL_DONE
put(new MsgHistoryAll()); // 0x41F2 CMD_HISTORY_ALL
put(new MsgHistoryNewDone()); // 0x42F1 CMD_HISTORY_NEW_DONE
put(new MsgHistoryNew()); // 0x42F2 CMD_HISTORY_NEW
put(new MsgCheckValue()); // 0xF0F1 CMD_PUMP_CHECK_VALUE
}
}
public static void put(MessageBase message) {
int command = message.getCommand();
//String name = MessageOriginalNames.getName(command);
messages.put(command, message);
//log.debug(String.format("%04x ", command) + " " + name);
}
public static MessageBase findMessage(Integer command) {
if (messages.containsKey(command)) {
return messages.get(command);
} else {
return new MessageBase();
}
}
}

View file

@ -0,0 +1,6 @@
package info.nightscout.androidaps.plugins.pump.danaR.comm
interface MessageHashTableBase {
fun put(message: MessageBase)
fun findMessage(command: Int): MessageBase
}

View file

@ -0,0 +1,69 @@
package info.nightscout.androidaps.plugins.pump.danaR.comm
import java.util.*
object MessageHashTableR : MessageHashTableBase {
var messages: HashMap<Int, MessageBase> = HashMap()
init {
put(MsgBolusStop()) // 0x0101 CMD_MEALINS_STOP
put(MsgBolusStart()) // 0x0102 CMD_MEALINS_START_DATA
put(MsgBolusStartWithSpeed()) // 0x0104 CMD_MEALINS_START_DATA_SPEED
put(MsgBolusProgress()) // 0x0202 CMD_PUMP_THIS_REMAINDER_MEAL_INS
put(MsgStatusProfile()) // 0x0204 CMD_PUMP_CALCULATION_SETTING
put(MsgStatusTempBasal()) // 0x0205 CMD_PUMP_EXERCISE_MODE
put(MsgStatusBolusExtended()) // 0x0207 CMD_PUMP_EXPANS_INS_I
put(MsgStatusBasic()) // 0x020A CMD_PUMP_INITVIEW_I
put(MsgStatus()) // 0x020B CMD_PUMP_STATUS
put(MsgInitConnStatusTime()) // 0x0301 CMD_PUMPINIT_TIME_INFO
put(MsgInitConnStatusBolus()) // 0x0302 CMD_PUMPINIT_BOLUS_INFO
put(MsgInitConnStatusBasic()) // 0x0303 CMD_PUMPINIT_INIT_INFO
put(MsgInitConnStatusOption()) // 0x0304 CMD_PUMPINIT_OPTION
put(MsgSetTempBasalStart()) // 0x0401 CMD_PUMPSET_EXERCISE_S
put(MsgSetCarbsEntry()) // 0x0402 CMD_PUMPSET_HIS_S
put(MsgSetTempBasalStop()) // 0x0403 CMD_PUMPSET_EXERCISE_STOP
put(MsgSetExtendedBolusStop()) // 0x0406 CMD_PUMPSET_EXPANS_INS_STOP
put(MsgSetExtendedBolusStart()) // 0x0407 CMD_PUMPSET_EXPANS_INS_S
put(MsgError()) // 0x0601 CMD_PUMPOWAY_SYSTEM_STATUS
put(MsgPCCommStart()) // 0x3001 CMD_CONNECT
put(MsgPCCommStop()) // 0x3002 CMD_DISCONNECT
put(MsgHistoryBolus()) // 0x3101 CMD_HISTORY_MEAL_INS
put(MsgHistoryDailyInsulin()) // 0x3102 CMD_HISTORY_DAY_INS
put(MsgHistoryGlucose()) // 0x3104 CMD_HISTORY_GLUCOSE
put(MsgHistoryAlarm()) // 0x3105 CMD_HISTORY_ALARM
put(MsgHistoryError()) // 0x3106 CMD_HISTORY_ERROR
put(MsgHistoryCarbo()) // 0x3107 CMD_HISTORY_CARBOHY
put(MsgHistoryRefill()) // 0x3108 CMD_HISTORY_REFILL
put(MsgHistorySuspend()) // 0x3109 CMD_HISTORY_SUSPEND
put(MsgHistoryBasalHour()) // 0x310A CMD_HISTORY_BASAL_HOUR
put(MsgHistoryDone()) // 0x31F1 CMD_HISTORY_DONT_USED
put(MsgSettingBasal()) // 0x3202 CMD_SETTING_V_BASAL_INS_I
put(MsgSettingMeal()) // 0x3203 CMD_SETTING_V_MEAL_SETTING_I
put(MsgSettingProfileRatios()) // 0x3204 CMD_SETTING_V_CCC_I
put(MsgSettingMaxValues()) // 0x3205 CMD_SETTING_V_MAX_VALUE_I
put(MsgSettingBasalProfileAll()) // 0x3206 CMD_SETTING_V_BASAL_PROFILE_ALL
put(MsgSettingShippingInfo()) // 0x3207 CMD_SETTING_V_SHIPPING_I
put(MsgSettingGlucose()) // 0x3209 CMD_SETTING_V_GLUCOSEandEASY
put(MsgSettingPumpTime()) // 0x320A CMD_SETTING_V_TIME_I
put(MsgSettingUserOptions()) // 0x320B CMD_SETTING_V_USER_OPTIONS
put(MsgSettingActiveProfile()) // 0x320C CMD_SETTING_V_PROFILE_NUMBER
put(MsgSettingProfileRatiosAll()) // 0x320D CMD_SETTING_V_CIR_CF_VALUE
put(MsgSetSingleBasalProfile()) // 0x3302 CMD_SETTING_BASAL_INS_S
put(MsgSetBasalProfile()) // 0x3306 CMD_SETTING_BASAL_PROFILE_S
put(MsgSetUserOptions()) // 0x330B CMD_SETTING_USER_OPTIONS_S
put(MsgSetActivateBasalProfile()) // 0x330C CMD_SETTING_PROFILE_NUMBER_S
put(MsgHistoryAllDone()) // 0x41F1 CMD_HISTORY_ALL_DONE
put(MsgHistoryAll()) // 0x41F2 CMD_HISTORY_ALL
put(MsgHistoryNewDone()) // 0x42F1 CMD_HISTORY_NEW_DONE
put(MsgHistoryNew()) // 0x42F2 CMD_HISTORY_NEW
put(MsgCheckValue()) // 0xF0F1 CMD_PUMP_CHECK_VALUE
}
override fun put(message: MessageBase) {
messages[message.command] = message
}
override fun findMessage(command: Int): MessageBase {
return messages[command] ?: MessageBase()
}
}

View file

@ -34,4 +34,10 @@ public class MsgSettingPumpTime extends MessageBase {
DanaRPump.getInstance().pumpTime = time;
}
@Override
public void handleMessageNotReceived() {
DanaRPump.getInstance().pumpTime = 0;
}
}

View file

@ -33,6 +33,7 @@ import info.nightscout.androidaps.plugins.general.overview.notifications.Notific
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump;
import info.nightscout.androidaps.plugins.pump.danaR.SerialIOThread;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageBase;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageHashTableR;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusProgress;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusStart;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusStartWithSpeed;
@ -124,7 +125,7 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService {
if (mSerialIOThread != null) {
mSerialIOThread.disconnect("Recreate SerialIOThread");
}
mSerialIOThread = new SerialIOThread(mRfcommSocket);
mSerialIOThread = new SerialIOThread(mRfcommSocket, MessageHashTableR.INSTANCE);
mHandshakeInProgress = true;
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, 0));
}
@ -186,6 +187,15 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService {
mSerialIOThread.sendMessage(new MsgSettingUserOptions());
MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumptime)));
mSerialIOThread.sendMessage(new MsgSettingPumpTime());
if (danaRPump.pumpTime == 0) {
// initial handshake was not successfull
// deinitialize pump
danaRPump.lastConnection = 0;
danaRPump.lastSettingsRead = 0;
RxBus.INSTANCE.send(new EventDanaRNewStatus());
MainApp.bus().post(new EventInitializationChanged());
return;
}
long timeDiff = (danaRPump.pumpTime - System.currentTimeMillis()) / 1000L;
if (L.isEnabled(L.PUMP))
log.debug("Pump time difference: " + timeDiff + " seconds");

View file

@ -1,210 +0,0 @@
package info.nightscout.androidaps.plugins.pump.danaRKorean;
import android.bluetooth.BluetoothSocket;
import android.os.SystemClock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageBase;
import info.nightscout.androidaps.plugins.pump.danaR.services.AbstractSerialIOThread;
import info.nightscout.androidaps.plugins.pump.danaRKorean.comm.MessageHashTable_k;
import info.nightscout.androidaps.utils.CRC;
/**
* Created by mike on 17.07.2016.
*/
public class SerialIOThread extends AbstractSerialIOThread {
private static Logger log = LoggerFactory.getLogger(L.PUMPBTCOMM);
private InputStream mInputStream = null;
private OutputStream mOutputStream = null;
private BluetoothSocket mRfCommSocket;
private boolean mKeepRunning = true;
private byte[] mReadBuff = new byte[0];
private MessageBase processedMessage;
public SerialIOThread(BluetoothSocket rfcommSocket) {
super();
mRfCommSocket = rfcommSocket;
try {
mOutputStream = mRfCommSocket.getOutputStream();
mInputStream = mRfCommSocket.getInputStream();
} catch (IOException e) {
log.error("Unhandled exception", e);
}
this.start();
}
@Override
public final void run() {
try {
while (mKeepRunning) {
int availableBytes = mInputStream.available();
// Ask for 1024 byte (or more if available)
byte[] newData = new byte[Math.max(1024, availableBytes)];
int gotBytes = mInputStream.read(newData);
// When we are here there is some new data available
appendToBuffer(newData, gotBytes);
// process all messages we already got
while (mReadBuff.length > 3) { // 3rd byte is packet size. continue only if we an determine packet size
byte[] extractedBuff = cutMessageFromBuffer();
if (extractedBuff == null)
break; // message is not complete in buffer (wrong packet calls disconnection)
int command = (extractedBuff[5] & 0xFF) | ((extractedBuff[4] << 8) & 0xFF00);
MessageBase message;
if (processedMessage != null && processedMessage.getCommand() == command) {
message = processedMessage;
} else {
// get it from hash table
message = MessageHashTable_k.findMessage(command);
}
if (L.isEnabled(L.PUMPBTCOMM))
log.debug("<<<<< " + message.getMessageName() + " " + message.toHexString(extractedBuff));
// process the message content
message.received = true;
message.handleMessage(extractedBuff);
synchronized (message) {
message.notify();
}
}
}
} catch (Exception e) {
if (e.getMessage().indexOf("bt socket closed") < 0)
log.error("Thread exception: ", e);
mKeepRunning = false;
}
disconnect("EndOfLoop");
}
void appendToBuffer(byte[] newData, int gotBytes) {
// add newData to mReadBuff
byte[] newReadBuff = new byte[mReadBuff.length + gotBytes];
System.arraycopy(mReadBuff, 0, newReadBuff, 0, mReadBuff.length);
System.arraycopy(newData, 0, newReadBuff, mReadBuff.length, gotBytes);
mReadBuff = newReadBuff;
}
byte[] cutMessageFromBuffer() {
if (mReadBuff[0] == (byte) 0x7E && mReadBuff[1] == (byte) 0x7E) {
int length = (mReadBuff[2] & 0xFF) + 7;
// Check if we have enough data
if (mReadBuff.length < length) {
return null;
}
if (mReadBuff[length - 2] != (byte) 0x2E || mReadBuff[length - 1] != (byte) 0x2E) {
log.error("wrong packet lenght=" + length + " data " + MessageBase.toHexString(mReadBuff));
disconnect("wrong packet");
return null;
}
short crc = CRC.getCrc16(mReadBuff, 3, length - 7);
byte crcByte0 = (byte) (crc >> 8 & 0xFF);
byte crcByte1 = (byte) (crc & 0xFF);
byte crcByte0received = mReadBuff[length - 4];
byte crcByte1received = mReadBuff[length - 3];
if (crcByte0 != crcByte0received || crcByte1 != crcByte1received) {
log.error("CRC Error" + String.format("%02x ", crcByte0) + String.format("%02x ", crcByte1) + String.format("%02x ", crcByte0received) + String.format("%02x ", crcByte1received));
disconnect("crc error");
return null;
}
// Packet is verified here. extract data
byte[] extractedBuff = new byte[length];
System.arraycopy(mReadBuff, 0, extractedBuff, 0, length);
// remove extracted data from read buffer
byte[] unprocessedData = new byte[mReadBuff.length - length];
System.arraycopy(mReadBuff, length, unprocessedData, 0, unprocessedData.length);
mReadBuff = unprocessedData;
return extractedBuff;
} else {
log.error("Wrong beginning of packet len=" + mReadBuff.length + " " + MessageBase.toHexString(mReadBuff));
disconnect("Wrong beginning of packet");
return null;
}
}
@Override
public synchronized void sendMessage(MessageBase message) {
if (!mRfCommSocket.isConnected()) {
log.error("Socket not connected on sendMessage");
return;
}
processedMessage = message;
byte[] messageBytes = message.getRawMessageBytes();
if (L.isEnabled(L.PUMPBTCOMM))
log.debug(">>>>> " + message.getMessageName() + " " + message.toHexString(messageBytes));
try {
mOutputStream.write(messageBytes);
} catch (Exception e) {
log.error("sendMessage write exception: ", e);
}
synchronized (message) {
try {
message.wait(5000);
} catch (InterruptedException e) {
log.error("sendMessage InterruptedException", e);
}
}
SystemClock.sleep(200);
if (!message.received) {
if (L.isEnabled(L.PUMPBTCOMM))
log.warn("Reply not received " + message.getMessageName());
if (message.getCommand() == 0xF0F1) {
DanaRPump.getInstance().isNewPump = false;
log.error("Old firmware detected");
}
}
}
@Override
public void disconnect(String reason) {
mKeepRunning = false;
try {
mInputStream.close();
} catch (Exception e) {
if (L.isEnabled(L.PUMPBTCOMM))
log.debug(e.getMessage());
}
try {
mOutputStream.close();
} catch (Exception e) {
if (L.isEnabled(L.PUMPBTCOMM))
log.debug(e.getMessage());
}
try {
mRfCommSocket.close();
} catch (Exception e) {
if (L.isEnabled(L.PUMPBTCOMM))
log.debug(e.getMessage());
}
try {
System.runFinalization();
} catch (Exception e) {
if (L.isEnabled(L.PUMPBTCOMM))
log.debug(e.getMessage());
}
if (L.isEnabled(L.PUMPBTCOMM))
log.debug("Disconnected: " + reason);
}
}

View file

@ -0,0 +1,56 @@
package info.nightscout.androidaps.plugins.pump.danaRKorean.comm
import info.nightscout.androidaps.plugins.pump.danaR.comm.*
import java.util.*
object MessageHashTableRkorean : MessageHashTableBase {
var messages: HashMap<Int, MessageBase> = HashMap()
init {
put(MsgBolusStop()) // 0x0101 CMD_MEALINS_STOP
put(MsgBolusStart()) // 0x0102 CMD_MEALINS_START_DATA
put(MsgBolusProgress()) // 0x0202 CMD_PUMP_THIS_REMAINDER_MEAL_INS
put(MsgStatusProfile()) // 0x0204 CMD_PUMP_CALCULATION_SETTING
put(MsgStatusTempBasal()) // 0x0205 CMD_PUMP_EXERCISE_MODE
put(MsgStatusBolusExtended()) // 0x0207 CMD_PUMP_EXPANS_INS_I
put(MsgStatusBasic_k()) // 0x020A CMD_PUMP_INITVIEW_I
put(MsgStatus_k()) // 0x020B CMD_PUMP_STATUS
put(MsgInitConnStatusTime_k()) // 0x0301 CMD_PUMPINIT_TIME_INFO
put(MsgInitConnStatusBolus_k()) // 0x0302 CMD_PUMPINIT_BOLUS_INFO
put(MsgInitConnStatusBasic_k()) // 0x0303 CMD_PUMPINIT_INIT_INFO
put(MsgSetTempBasalStart()) // 0x0401 CMD_PUMPSET_EXERCISE_S
put(MsgSetCarbsEntry()) // 0x0402 CMD_PUMPSET_HIS_S
put(MsgSetTempBasalStop()) // 0x0403 CMD_PUMPSET_EXERCISE_STOP
put(MsgSetExtendedBolusStop()) // 0x0406 CMD_PUMPSET_EXPANS_INS_STOP
put(MsgSetExtendedBolusStart()) // 0x0407 CMD_PUMPSET_EXPANS_INS_S
put(MsgError()) // 0x0601 CMD_PUMPOWAY_SYSTEM_STATUS
put(MsgPCCommStart()) // 0x3001 CMD_CONNECT
put(MsgPCCommStop()) // 0x3002 CMD_DISCONNECT
put(MsgHistoryBolus()) // 0x3101 CMD_HISTORY_MEAL_INS
put(MsgHistoryDailyInsulin()) // 0x3102 CMD_HISTORY_DAY_INS
put(MsgHistoryGlucose()) // 0x3104 CMD_HISTORY_GLUCOSE
put(MsgHistoryAlarm()) // 0x3105 CMD_HISTORY_ALARM
put(MsgHistoryCarbo()) // 0x3107 CMD_HISTORY_CARBOHY
put(MsgSettingBasal_k()) // 0x3202 CMD_SETTING_V_BASAL_INS_I
put(MsgSettingMeal()) // 0x3203 CMD_SETTING_V_MEAL_SETTING_I
put(MsgSettingProfileRatios()) // 0x3204 CMD_SETTING_V_CCC_I
put(MsgSettingMaxValues()) // 0x3205 CMD_SETTING_V_MAX_VALUE_I
put(MsgSettingBasalProfileAll_k()) // 0x3206 CMD_SETTING_V_BASAL_PROFILE_ALL
put(MsgSettingShippingInfo()) // 0x3207 CMD_SETTING_V_SHIPPING_I
put(MsgSettingGlucose()) // 0x3209 CMD_SETTING_V_GLUCOSEandEASY
put(MsgSettingPumpTime()) // 0x320A CMD_SETTING_V_TIME_I
put(MsgSetSingleBasalProfile()) // 0x3302 CMD_SETTING_BASAL_INS_S
put(MsgHistoryAll()) // 0x41F2 CMD_HISTORY_ALL
put(MsgHistoryNewDone()) // 0x42F1 CMD_HISTORY_NEW_DONE
put(MsgHistoryNew()) // 0x42F2 CMD_HISTORY_NEW
put(MsgCheckValue_k()) // 0xF0F1 CMD_PUMP_CHECK_VALUE
}
override fun put(message: MessageBase) {
messages[message.command] = message
}
override fun findMessage(command: Int): MessageBase {
return messages[command] ?: MessageBase()
}
}

View file

@ -1,76 +0,0 @@
package info.nightscout.androidaps.plugins.pump.danaRKorean.comm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageBase;
import info.nightscout.androidaps.plugins.pump.danaR.comm.*;
/**
* Created by mike on 28.05.2016.
*/
public class MessageHashTable_k {
private static Logger log = LoggerFactory.getLogger(MessageHashTable_k.class);
public static HashMap<Integer, MessageBase> messages = null;
static {
if (messages == null) {
messages = new HashMap<Integer, MessageBase>();
put(new MsgBolusStop()); // 0x0101 CMD_MEALINS_STOP
put(new MsgBolusStart()); // 0x0102 CMD_MEALINS_START_DATA
put(new MsgBolusProgress()); // 0x0202 CMD_PUMP_THIS_REMAINDER_MEAL_INS
put(new MsgStatusProfile()); // 0x0204 CMD_PUMP_CALCULATION_SETTING
put(new MsgStatusTempBasal()); // 0x0205 CMD_PUMP_EXERCISE_MODE
put(new MsgStatusBolusExtended()); // 0x0207 CMD_PUMP_EXPANS_INS_I
put(new MsgStatusBasic_k()); // 0x020A CMD_PUMP_INITVIEW_I
put(new MsgStatus_k()); // 0x020B CMD_PUMP_STATUS
put(new MsgInitConnStatusTime_k()); // 0x0301 CMD_PUMPINIT_TIME_INFO
put(new MsgInitConnStatusBolus_k()); // 0x0302 CMD_PUMPINIT_BOLUS_INFO
put(new MsgInitConnStatusBasic_k()); // 0x0303 CMD_PUMPINIT_INIT_INFO
put(new MsgSetTempBasalStart()); // 0x0401 CMD_PUMPSET_EXERCISE_S
put(new MsgSetCarbsEntry()); // 0x0402 CMD_PUMPSET_HIS_S
put(new MsgSetTempBasalStop()); // 0x0403 CMD_PUMPSET_EXERCISE_STOP
put(new MsgSetExtendedBolusStop()); // 0x0406 CMD_PUMPSET_EXPANS_INS_STOP
put(new MsgSetExtendedBolusStart()); // 0x0407 CMD_PUMPSET_EXPANS_INS_S
put(new MsgError()); // 0x0601 CMD_PUMPOWAY_SYSTEM_STATUS
put(new MsgPCCommStart()); // 0x3001 CMD_CONNECT
put(new MsgPCCommStop()); // 0x3002 CMD_DISCONNECT
put(new MsgHistoryBolus()); // 0x3101 CMD_HISTORY_MEAL_INS
put(new MsgHistoryDailyInsulin()); // 0x3102 CMD_HISTORY_DAY_INS
put(new MsgHistoryGlucose()); // 0x3104 CMD_HISTORY_GLUCOSE
put(new MsgHistoryAlarm()); // 0x3105 CMD_HISTORY_ALARM
put(new MsgHistoryCarbo()); // 0x3107 CMD_HISTORY_CARBOHY
put(new MsgSettingBasal_k()); // 0x3202 CMD_SETTING_V_BASAL_INS_I
put(new MsgSettingMeal()); // 0x3203 CMD_SETTING_V_MEAL_SETTING_I
put(new MsgSettingProfileRatios()); // 0x3204 CMD_SETTING_V_CCC_I
put(new MsgSettingMaxValues()); // 0x3205 CMD_SETTING_V_MAX_VALUE_I
put(new MsgSettingBasalProfileAll_k()); // 0x3206 CMD_SETTING_V_BASAL_PROFILE_ALL
put(new MsgSettingShippingInfo()); // 0x3207 CMD_SETTING_V_SHIPPING_I
put(new MsgSettingGlucose()); // 0x3209 CMD_SETTING_V_GLUCOSEandEASY
put(new MsgSettingPumpTime()); // 0x320A CMD_SETTING_V_TIME_I
put(new MsgSetSingleBasalProfile()); // 0x3302 CMD_SETTING_BASAL_INS_S
put(new MsgHistoryAll()); // 0x41F2 CMD_HISTORY_ALL
put(new MsgHistoryNewDone()); // 0x42F1 CMD_HISTORY_NEW_DONE
put(new MsgHistoryNew()); // 0x42F2 CMD_HISTORY_NEW
put(new MsgCheckValue_k()); // 0xF0F1 CMD_PUMP_CHECK_VALUE
}
}
public static void put(MessageBase message) {
int command = message.getCommand();
//String name = MessageOriginalNames.getName(command);
messages.put(command, message);
//log.debug(String.format("%04x ", command) + " " + name);
}
public static MessageBase findMessage(Integer command) {
if (messages.containsKey(command)) {
return messages.get(command);
} else {
return new MessageBase();
}
}
}

View file

@ -30,6 +30,7 @@ import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgress
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump;
import info.nightscout.androidaps.plugins.pump.danaR.SerialIOThread;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusProgress;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusStart;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusStop;
@ -51,7 +52,7 @@ import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgStatusBolusExtended
import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgStatusTempBasal;
import info.nightscout.androidaps.plugins.pump.danaR.events.EventDanaRNewStatus;
import info.nightscout.androidaps.plugins.pump.danaR.services.AbstractDanaRExecutionService;
import info.nightscout.androidaps.plugins.pump.danaRKorean.SerialIOThread;
import info.nightscout.androidaps.plugins.pump.danaRKorean.comm.MessageHashTableRkorean;
import info.nightscout.androidaps.plugins.pump.danaRKorean.comm.MsgCheckValue_k;
import info.nightscout.androidaps.plugins.pump.danaRKorean.comm.MsgSettingBasal_k;
import info.nightscout.androidaps.plugins.pump.danaRKorean.comm.MsgStatusBasic_k;
@ -129,7 +130,7 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService {
if (mSerialIOThread != null) {
mSerialIOThread.disconnect("Recreate SerialIOThread");
}
mSerialIOThread = new SerialIOThread(mRfcommSocket);
mSerialIOThread = new SerialIOThread(mRfcommSocket, MessageHashTableRkorean.INSTANCE);
mHandshakeInProgress = true;
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, 0));
}
@ -187,6 +188,15 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService {
mSerialIOThread.sendMessage(new MsgSettingProfileRatios());
MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumptime)));
mSerialIOThread.sendMessage(new MsgSettingPumpTime());
if (danaRPump.pumpTime == 0) {
// initial handshake was not successfull
// deinitialize pump
danaRPump.lastConnection = 0;
danaRPump.lastSettingsRead = 0;
RxBus.INSTANCE.send(new EventDanaRNewStatus());
MainApp.bus().post(new EventInitializationChanged());
return;
}
long timeDiff = (danaRPump.pumpTime - System.currentTimeMillis()) / 1000L;
if (L.isEnabled(L.PUMP))
log.debug("Pump time difference: " + timeDiff + " seconds");

View file

@ -1,209 +0,0 @@
package info.nightscout.androidaps.plugins.pump.danaRv2;
import android.bluetooth.BluetoothSocket;
import android.os.SystemClock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageBase;
import info.nightscout.androidaps.plugins.pump.danaR.services.AbstractSerialIOThread;
import info.nightscout.androidaps.plugins.pump.danaRv2.comm.MessageHashTable_v2;
import info.nightscout.androidaps.utils.CRC;
/**
* Created by mike on 17.07.2016.
*/
public class SerialIOThread extends AbstractSerialIOThread {
private static Logger log = LoggerFactory.getLogger(L.PUMPBTCOMM);
private InputStream mInputStream = null;
private OutputStream mOutputStream = null;
private BluetoothSocket mRfCommSocket;
private boolean mKeepRunning = true;
private byte[] mReadBuff = new byte[0];
private MessageBase processedMessage;
public SerialIOThread(BluetoothSocket rfcommSocket) {
super();
mRfCommSocket = rfcommSocket;
try {
mOutputStream = mRfCommSocket.getOutputStream();
mInputStream = mRfCommSocket.getInputStream();
} catch (IOException e) {
log.error("Unhandled exception", e);
}
this.start();
}
@Override
public final void run() {
try {
while (mKeepRunning) {
int availableBytes = mInputStream.available();
// Ask for 1024 byte (or more if available)
byte[] newData = new byte[Math.max(1024, availableBytes)];
int gotBytes = mInputStream.read(newData);
// When we are here there is some new data available
appendToBuffer(newData, gotBytes);
// process all messages we already got
while (mReadBuff.length > 3) { // 3rd byte is packet size. continue only if we an determine packet size
byte[] extractedBuff = cutMessageFromBuffer();
if (extractedBuff == null)
break; // message is not complete in buffer (wrong packet calls disconnection)
int command = (extractedBuff[5] & 0xFF) | ((extractedBuff[4] << 8) & 0xFF00);
MessageBase message;
if (processedMessage != null && processedMessage.getCommand() == command) {
message = processedMessage;
} else {
// get it from hash table
message = MessageHashTable_v2.findMessage(command);
}
if (L.isEnabled(L.PUMPBTCOMM))
log.debug("<<<<< " + message.getMessageName() + " " + message.toHexString(extractedBuff));
// process the message content
message.received = true;
message.handleMessage(extractedBuff);
synchronized (message) {
message.notify();
}
}
}
} catch (Exception e) {
if (e.getMessage().indexOf("bt socket closed") < 0)
log.error("Thread exception: ", e);
mKeepRunning = false;
}
disconnect("EndOfLoop");
}
void appendToBuffer(byte[] newData, int gotBytes) {
// add newData to mReadBuff
byte[] newReadBuff = new byte[mReadBuff.length + gotBytes];
System.arraycopy(mReadBuff, 0, newReadBuff, 0, mReadBuff.length);
System.arraycopy(newData, 0, newReadBuff, mReadBuff.length, gotBytes);
mReadBuff = newReadBuff;
}
byte[] cutMessageFromBuffer() {
if (mReadBuff[0] == (byte) 0x7E && mReadBuff[1] == (byte) 0x7E) {
int length = (mReadBuff[2] & 0xFF) + 7;
// Check if we have enough data
if (mReadBuff.length < length) {
return null;
}
if (mReadBuff[length - 2] != (byte) 0x2E || mReadBuff[length - 1] != (byte) 0x2E) {
log.error("wrong packet lenght=" + length + " data " + MessageBase.toHexString(mReadBuff));
disconnect("wrong packet");
return null;
}
short crc = CRC.getCrc16(mReadBuff, 3, length - 7);
byte crcByte0 = (byte) (crc >> 8 & 0xFF);
byte crcByte1 = (byte) (crc & 0xFF);
byte crcByte0received = mReadBuff[length - 4];
byte crcByte1received = mReadBuff[length - 3];
if (crcByte0 != crcByte0received || crcByte1 != crcByte1received) {
log.error("CRC Error" + String.format("%02x ", crcByte0) + String.format("%02x ", crcByte1) + String.format("%02x ", crcByte0received) + String.format("%02x ", crcByte1received));
disconnect("crc error");
return null;
}
// Packet is verified here. extract data
byte[] extractedBuff = new byte[length];
System.arraycopy(mReadBuff, 0, extractedBuff, 0, length);
// remove extracted data from read buffer
byte[] unprocessedData = new byte[mReadBuff.length - length];
System.arraycopy(mReadBuff, length, unprocessedData, 0, unprocessedData.length);
mReadBuff = unprocessedData;
return extractedBuff;
} else {
log.error("Wrong beginning of packet len=" + mReadBuff.length + " " + MessageBase.toHexString(mReadBuff));
disconnect("Wrong beginning of packet");
return null;
}
}
@Override
public synchronized void sendMessage(MessageBase message) {
if (!mRfCommSocket.isConnected()) {
log.error("Socket not connected on sendMessage");
return;
}
processedMessage = message;
byte[] messageBytes = message.getRawMessageBytes();
if (L.isEnabled(L.PUMPBTCOMM))
log.debug(">>>>> " + message.getMessageName() + " " + message.toHexString(messageBytes));
try {
mOutputStream.write(messageBytes);
} catch (Exception e) {
log.error("sendMessage write exception: ", e);
}
synchronized (message) {
try {
message.wait(5000);
} catch (InterruptedException e) {
log.error("sendMessage InterruptedException", e);
}
}
SystemClock.sleep(200);
if (!message.received) {
log.error("Reply not received " + message.getMessageName());
if (message.getCommand() == 0xF0F1) {
DanaRPump.getInstance().isNewPump = false;
log.error("Old firmware detected");
}
}
}
@Override
public void disconnect(String reason) {
mKeepRunning = false;
try {
mInputStream.close();
} catch (Exception e) {
if (L.isEnabled(L.PUMPBTCOMM))
log.debug(e.getMessage());
}
try {
mOutputStream.close();
} catch (Exception e) {
if (L.isEnabled(L.PUMPBTCOMM))
log.debug(e.getMessage());
}
try {
mRfCommSocket.close();
} catch (Exception e) {
if (L.isEnabled(L.PUMPBTCOMM))
log.debug(e.getMessage());
}
try {
System.runFinalization();
} catch (Exception e) {
if (L.isEnabled(L.PUMPBTCOMM))
log.debug(e.getMessage());
}
if (L.isEnabled(L.PUMPBTCOMM))
log.debug("Disconnected: " + reason);
}
}

View file

@ -0,0 +1,77 @@
package info.nightscout.androidaps.plugins.pump.danaRv2.comm
import info.nightscout.androidaps.plugins.pump.danaR.comm.*
import java.util.*
object MessageHashTableRv2 : MessageHashTableBase {
var messages: HashMap<Int, MessageBase> = HashMap()
init {
put(MsgBolusStop()) // 0x0101 CMD_MEALINS_STOP
put(MsgBolusStart()) // 0x0102 CMD_MEALINS_START_DATA
put(MsgBolusStartWithSpeed()) // 0x0104 CMD_MEALINS_START_DATA_SPEED
put(MsgBolusProgress()) // 0x0202 CMD_PUMP_THIS_REMAINDER_MEAL_INS
put(MsgStatusProfile()) // 0x0204 CMD_PUMP_CALCULATION_SETTING
put(MsgStatusTempBasal_v2()) // 0x0205 CMD_PUMP_EXERCISE_MODE
put(MsgStatusBolusExtended_v2()) // 0x0207 CMD_PUMP_EXPANS_INS_I
put(MsgStatusBasic()) // 0x020A CMD_PUMP_INITVIEW_I
put(MsgStatus()) // 0x020B CMD_PUMP_STATUS
put(MsgInitConnStatusTime()) // 0x0301 CMD_PUMPINIT_TIME_INFO
put(MsgInitConnStatusBolus()) // 0x0302 CMD_PUMPINIT_BOLUS_INFO
put(MsgInitConnStatusBasic()) // 0x0303 CMD_PUMPINIT_INIT_INFO
put(MsgInitConnStatusOption()) // 0x0304 CMD_PUMPINIT_OPTION
put(MsgSetTempBasalStart()) // 0x0401 CMD_PUMPSET_EXERCISE_S
put(MsgSetCarbsEntry()) // 0x0402 CMD_PUMPSET_HIS_S
put(MsgSetTempBasalStop()) // 0x0403 CMD_PUMPSET_EXERCISE_STOP
put(MsgSetExtendedBolusStop()) // 0x0406 CMD_PUMPSET_EXPANS_INS_STOP
put(MsgSetExtendedBolusStart()) // 0x0407 CMD_PUMPSET_EXPANS_INS_S
put(MsgError()) // 0x0601 CMD_PUMPOWAY_SYSTEM_STATUS
put(MsgPCCommStart()) // 0x3001 CMD_CONNECT
put(MsgPCCommStop()) // 0x3002 CMD_DISCONNECT
put(MsgHistoryBolus()) // 0x3101 CMD_HISTORY_MEAL_INS
put(MsgHistoryDailyInsulin()) // 0x3102 CMD_HISTORY_DAY_INS
put(MsgHistoryGlucose()) // 0x3104 CMD_HISTORY_GLUCOSE
put(MsgHistoryAlarm()) // 0x3105 CMD_HISTORY_ALARM
put(MsgHistoryError()) // 0x3106 CMD_HISTORY_ERROR
put(MsgHistoryCarbo()) // 0x3107 CMD_HISTORY_CARBOHY
put(MsgHistoryRefill()) // 0x3108 CMD_HISTORY_REFILL
put(MsgHistorySuspend()) // 0x3109 CMD_HISTORY_SUSPEND
put(MsgHistoryBasalHour()) // 0x310A CMD_HISTORY_BASAL_HOUR
put(MsgHistoryDone()) // 0x31F1 CMD_HISTORY_DONT_USED
put(MsgSettingBasal()) // 0x3202 CMD_SETTING_V_BASAL_INS_I
put(MsgSettingMeal()) // 0x3203 CMD_SETTING_V_MEAL_SETTING_I
put(MsgSettingProfileRatios()) // 0x3204 CMD_SETTING_V_CCC_I
put(MsgSettingMaxValues()) // 0x3205 CMD_SETTING_V_MAX_VALUE_I
put(MsgSettingBasalProfileAll()) // 0x3206 CMD_SETTING_V_BASAL_PROFILE_ALL
put(MsgSettingShippingInfo()) // 0x3207 CMD_SETTING_V_SHIPPING_I
put(MsgSettingGlucose()) // 0x3209 CMD_SETTING_V_GLUCOSEandEASY
put(MsgSettingPumpTime()) // 0x320A CMD_SETTING_V_TIME_I
put(MsgSettingUserOptions()) // 0x320B CMD_SETTING_V_USER_OPTIONS
put(MsgSettingActiveProfile()) // 0x320C CMD_SETTING_V_PROFILE_NUMBER
put(MsgSettingProfileRatiosAll()) // 0x320D CMD_SETTING_V_CIR_CF_VALUE
put(MsgSetSingleBasalProfile()) // 0x3302 CMD_SETTING_BASAL_INS_S
put(MsgSetBasalProfile()) // 0x3306 CMD_SETTING_BASAL_PROFILE_S
put(MsgSetUserOptions()) // 0x330B CMD_SETTING_USER_OPTIONS_S
put(MsgSetActivateBasalProfile()) // 0x330C CMD_SETTING_PROFILE_NUMBER_S
put(MsgHistoryAllDone()) // 0x41F1 CMD_HISTORY_ALL_DONE
put(MsgHistoryAll()) // 0x41F2 CMD_HISTORY_ALL
put(MsgHistoryNewDone()) // 0x42F1 CMD_HISTORY_NEW_DONE
put(MsgHistoryNew()) // 0x42F2 CMD_HISTORY_NEW
put(MsgCheckValue_v2()) // 0xF0F1 CMD_PUMP_CHECK_VALUE
put(MsgStatusAPS_v2()) // 0xE001 CMD_PUMPSTATUS_APS
put(MsgSetAPSTempBasalStart_v2()) // 0xE002 CMD_PUMPSET_APSTEMP
put(MsgHistoryEvents_v2()) // 0xE003 CMD_GET_HISTORY
put(MsgSetHistoryEntry_v2()) // 0xE004 CMD_SET_HISTORY_ENTRY
}
override fun put(message: MessageBase) {
messages[message.command] = message
}
override fun findMessage(command: Int): MessageBase {
return messages[command] ?: MessageBase()
}
}

View file

@ -1,92 +0,0 @@
package info.nightscout.androidaps.plugins.pump.danaRv2.comm;
import java.util.HashMap;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageBase;
import info.nightscout.androidaps.plugins.pump.danaR.comm.*;
/**
* Created by mike on 28.05.2016.
*/
public class MessageHashTable_v2 {
public static HashMap<Integer, MessageBase> messages = null;
static {
if (messages == null) {
messages = new HashMap<Integer, MessageBase>();
put(new MsgBolusStop()); // 0x0101 CMD_MEALINS_STOP
put(new MsgBolusStart()); // 0x0102 CMD_MEALINS_START_DATA
put(new MsgBolusStartWithSpeed()); // 0x0104 CMD_MEALINS_START_DATA_SPEED
put(new MsgBolusProgress()); // 0x0202 CMD_PUMP_THIS_REMAINDER_MEAL_INS
put(new MsgStatusProfile()); // 0x0204 CMD_PUMP_CALCULATION_SETTING
put(new MsgStatusTempBasal_v2()); // 0x0205 CMD_PUMP_EXERCISE_MODE
put(new MsgStatusBolusExtended_v2()); // 0x0207 CMD_PUMP_EXPANS_INS_I
put(new MsgStatusBasic()); // 0x020A CMD_PUMP_INITVIEW_I
put(new MsgStatus()); // 0x020B CMD_PUMP_STATUS
put(new MsgInitConnStatusTime()); // 0x0301 CMD_PUMPINIT_TIME_INFO
put(new MsgInitConnStatusBolus()); // 0x0302 CMD_PUMPINIT_BOLUS_INFO
put(new MsgInitConnStatusBasic()); // 0x0303 CMD_PUMPINIT_INIT_INFO
put(new MsgInitConnStatusOption()); // 0x0304 CMD_PUMPINIT_OPTION
put(new MsgSetTempBasalStart()); // 0x0401 CMD_PUMPSET_EXERCISE_S
put(new MsgSetCarbsEntry()); // 0x0402 CMD_PUMPSET_HIS_S
put(new MsgSetTempBasalStop()); // 0x0403 CMD_PUMPSET_EXERCISE_STOP
put(new MsgSetExtendedBolusStop()); // 0x0406 CMD_PUMPSET_EXPANS_INS_STOP
put(new MsgSetExtendedBolusStart()); // 0x0407 CMD_PUMPSET_EXPANS_INS_S
put(new MsgError()); // 0x0601 CMD_PUMPOWAY_SYSTEM_STATUS
put(new MsgPCCommStart()); // 0x3001 CMD_CONNECT
put(new MsgPCCommStop()); // 0x3002 CMD_DISCONNECT
put(new MsgHistoryBolus()); // 0x3101 CMD_HISTORY_MEAL_INS
put(new MsgHistoryDailyInsulin()); // 0x3102 CMD_HISTORY_DAY_INS
put(new MsgHistoryGlucose()); // 0x3104 CMD_HISTORY_GLUCOSE
put(new MsgHistoryAlarm()); // 0x3105 CMD_HISTORY_ALARM
put(new MsgHistoryError()); // 0x3106 CMD_HISTORY_ERROR
put(new MsgHistoryCarbo()); // 0x3107 CMD_HISTORY_CARBOHY
put(new MsgHistoryRefill()); // 0x3108 CMD_HISTORY_REFILL
put(new MsgHistorySuspend()); // 0x3109 CMD_HISTORY_SUSPEND
put(new MsgHistoryBasalHour()); // 0x310A CMD_HISTORY_BASAL_HOUR
put(new MsgHistoryDone()); // 0x31F1 CMD_HISTORY_DONT_USED
put(new MsgSettingBasal()); // 0x3202 CMD_SETTING_V_BASAL_INS_I
put(new MsgSettingMeal()); // 0x3203 CMD_SETTING_V_MEAL_SETTING_I
put(new MsgSettingProfileRatios()); // 0x3204 CMD_SETTING_V_CCC_I
put(new MsgSettingMaxValues()); // 0x3205 CMD_SETTING_V_MAX_VALUE_I
put(new MsgSettingBasalProfileAll()); // 0x3206 CMD_SETTING_V_BASAL_PROFILE_ALL
put(new MsgSettingShippingInfo()); // 0x3207 CMD_SETTING_V_SHIPPING_I
put(new MsgSettingGlucose()); // 0x3209 CMD_SETTING_V_GLUCOSEandEASY
put(new MsgSettingPumpTime()); // 0x320A CMD_SETTING_V_TIME_I
put(new MsgSettingUserOptions()); // 0x320B CMD_SETTING_V_USER_OPTIONS
put(new MsgSettingActiveProfile()); // 0x320C CMD_SETTING_V_PROFILE_NUMBER
put(new MsgSettingProfileRatiosAll()); // 0x320D CMD_SETTING_V_CIR_CF_VALUE
put(new MsgSetSingleBasalProfile()); // 0x3302 CMD_SETTING_BASAL_INS_S
put(new MsgSetBasalProfile()); // 0x3306 CMD_SETTING_BASAL_PROFILE_S
put(new MsgSetUserOptions()); // 0x330B CMD_SETTING_USER_OPTIONS_S
put(new MsgSetActivateBasalProfile()); // 0x330C CMD_SETTING_PROFILE_NUMBER_S
put(new MsgHistoryAllDone()); // 0x41F1 CMD_HISTORY_ALL_DONE
put(new MsgHistoryAll()); // 0x41F2 CMD_HISTORY_ALL
put(new MsgHistoryNewDone()); // 0x42F1 CMD_HISTORY_NEW_DONE
put(new MsgHistoryNew()); // 0x42F2 CMD_HISTORY_NEW
put(new MsgCheckValue_v2()); // 0xF0F1 CMD_PUMP_CHECK_VALUE
put(new MsgStatusAPS_v2()); // 0xE001 CMD_PUMPSTATUS_APS
put(new MsgSetAPSTempBasalStart_v2()); // 0xE002 CMD_PUMPSET_APSTEMP
put(new MsgHistoryEvents_v2()); // 0xE003 CMD_GET_HISTORY
put(new MsgSetHistoryEntry_v2()); // 0xE004 CMD_SET_HISTORY_ENTRY
}
}
public static void put(MessageBase message) {
int command = message.getCommand();
//String name = MessageOriginalNames.getName(command);
messages.put(command, message);
//log.debug(String.format("%04x ", command) + " " + name);
}
public static MessageBase findMessage(Integer command) {
if (messages.containsKey(command)) {
return messages.get(command);
} else {
return new MessageBase();
}
}
}

View file

@ -33,6 +33,7 @@ import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotifi
import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress;
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump;
import info.nightscout.androidaps.plugins.pump.danaR.SerialIOThread;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageBase;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusProgress;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusStart;
@ -62,7 +63,7 @@ import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgStatusBasic;
import info.nightscout.androidaps.plugins.pump.danaR.events.EventDanaRNewStatus;
import info.nightscout.androidaps.plugins.pump.danaR.services.AbstractDanaRExecutionService;
import info.nightscout.androidaps.plugins.pump.danaRv2.DanaRv2Plugin;
import info.nightscout.androidaps.plugins.pump.danaRv2.SerialIOThread;
import info.nightscout.androidaps.plugins.pump.danaRv2.comm.MessageHashTableRv2;
import info.nightscout.androidaps.plugins.pump.danaRv2.comm.MsgCheckValue_v2;
import info.nightscout.androidaps.plugins.pump.danaRv2.comm.MsgHistoryEvents_v2;
import info.nightscout.androidaps.plugins.pump.danaRv2.comm.MsgSetAPSTempBasalStart_v2;
@ -147,7 +148,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService {
if (mSerialIOThread != null) {
mSerialIOThread.disconnect("Recreate SerialIOThread");
}
mSerialIOThread = new SerialIOThread(mRfcommSocket);
mSerialIOThread = new SerialIOThread(mRfcommSocket, MessageHashTableRv2.INSTANCE);
mHandshakeInProgress = true;
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, 0));
}
@ -195,6 +196,15 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService {
MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumptime)));
mSerialIOThread.sendMessage(new MsgSettingPumpTime());
if (danaRPump.pumpTime == 0) {
// initial handshake was not successfull
// deinitialize pump
danaRPump.lastConnection = 0;
danaRPump.lastSettingsRead = 0;
RxBus.INSTANCE.send(new EventDanaRNewStatus());
MainApp.bus().post(new EventInitializationChanged());
return;
}
long timeDiff = (danaRPump.pumpTime - System.currentTimeMillis()) / 1000L;
if (L.isEnabled(L.PUMP))
log.debug("Pump time difference: " + timeDiff + " seconds");

View file

@ -18,14 +18,14 @@ import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, SP.class, L.class})
public class MessageHashTableTest {
public class MessageHashTableRTest {
@Test
public void runTest() {
AAPSMocker.mockMainApp();
AAPSMocker.mockSP();
AAPSMocker.mockL();
MessageHashTable messageHashTable = new MessageHashTable();
MessageHashTableR messageHashTable = MessageHashTableR.INSTANCE;
MessageBase testMessage = messageHashTable.findMessage(0x41f2);
assertEquals("CMD_HISTORY_ALL", testMessage.getMessageName());

View file

@ -18,7 +18,7 @@ import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, SP.class, L.class})
public class MessageHashTable_v2Test {
public class MessageHashTable_rv2Test {
@Test
public void runTest() {
AAPSMocker.mockMainApp();
@ -27,16 +27,16 @@ public class MessageHashTable_v2Test {
AAPSMocker.mockL();
AAPSMocker.mockBus();
MessageHashTable_v2 packet = new MessageHashTable_v2();
MessageHashTableRv2 hashTableRv2 = MessageHashTableRv2.INSTANCE;
MessageBase forTesting = new MsgStatusAPS_v2();
MessageBase testPacket = MessageHashTable_v2.findMessage(forTesting.getCommand());
MessageBase testPacket = MessageHashTableRv2.INSTANCE.findMessage(forTesting.getCommand());
assertEquals(0xE001, testPacket.getCommand());
// try putting another command
MessageBase testMessage = new MessageBase();
testMessage.SetCommand(0xE005);
packet.put(testMessage);
assertEquals(0xE005, packet.findMessage(0xE005).getCommand());
hashTableRv2.put(testMessage);
assertEquals(0xE005, hashTableRv2.findMessage(0xE005).getCommand());
}
byte[] createArray(int length, byte fillWith){