package com.eveningoutpost.dexdrip.G5Model; import com.chrispr.bluetooth.BluetoothScanner; import com.eveningoutpost.dexdrip.Models.JoH; import com.eveningoutpost.dexdrip.Models.UserError; import com.google.gson.annotations.Expose; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.Marker; import org.slf4j.MarkerFactory; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Locale; // jamorham public class BaseMessage { protected static final String TAG = G5CollectionService.TAG; // meh static final int INVALID_TIME = 0xFFFFFFFF; @Expose long postExecuteGuardTime = 50; @Expose public volatile byte[] byteSequence; public ByteBuffer data; void init(final byte opcode, final int length) { data = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN); data.put(opcode); if (length == 1) { getByteSequence(); } else if (length == 3) { appendCRC(); } } byte[] appendCRC() { data.put(FastCRC16.calculate(getByteSequence(), byteSequence.length - 2)); return getByteSequence(); } boolean checkCRC(byte[] data) { if ((data == null) || (data.length < 3)) return false; final byte[] crc = FastCRC16.calculate(data, data.length - 2); return crc[0] == data[data.length - 2] && crc[1] == data[data.length - 1]; } byte[] getByteSequence() { return byteSequence = data.array(); } long guardTime() { return postExecuteGuardTime; } static long getUnsignedInt(ByteBuffer data) { return ((data.get() & 0xff) + ((data.get() & 0xff) << 8) + ((data.get() & 0xff) << 16) + ((data.get() & 0xff) << 24)); } static int getUnsignedShort(ByteBuffer data) { return ((data.get() & 0xff) + ((data.get() & 0xff) << 8)); } static int getUnsignedByte(ByteBuffer data) { return ((data.get() & 0xff)); } static String dottedStringFromData(ByteBuffer data, int length) { final byte[] bytes = new byte[length]; data.get(bytes); final StringBuilder sb = new StringBuilder(100); for (byte x : bytes) { if (sb.length() > 0) sb.append("."); sb.append(String.format(Locale.US, "%d", (x & 0xff))); } return sb.toString(); } static int getUnixTime() { return (int) (JoH.tsl() / 1000); } public static class G5CollectionService { public static String TAG = "Message"; } public static class UserError { public static class Log { protected static final Logger logger = LoggerFactory.getLogger("Message"); public static Marker getMarker(String markerName) { Marker m = MarkerFactory.getMarker(markerName); return m; } public static void e(String a, String b){ //android.util.Log.e(a, b); logger.error(getMarker(a), b); //new com.eveningoutpost.dexdrip.Models.UserError(a, b); } public static void e(String tag, String b, Exception e){ //android.util.Log.e(tag, b, e); logger.error(getMarker(tag), b, e); //new com.eveningoutpost.dexdrip.Models.UserError(tag, b + "\n" + e.toString()); } public static void w(String tag, String b){ //android.util.Log.w(tag, b); logger.warn(getMarker(tag), b); //com.eveningoutpost.dexdrip.Models.UserError.UserErrorLow(tag, b); } public static void w(String tag, String b, Exception e){ //android.util.Log.w(tag, b, e); logger.warn(getMarker(tag), b, e); //com.eveningoutpost.dexdrip.Models.UserError.UserErrorLow(tag, b + "\n" + e.toString()); } public static void wtf(String tag, String b){ //android.util.Log.wtf(tag, b); logger.error(getMarker(tag), b); //com.eveningoutpost.dexdrip.Models.UserError.UserErrorHigh(tag, b); } public static void wtf(String tag, String b, Exception e){ //android.util.Log.wtf(tag, b, e); logger.error(getMarker(tag), b, e); //com.eveningoutpost.dexdrip.Models.UserError.UserErrorHigh(tag, b + "\n" + e.toString()); } public static void wtf(String tag, Exception e){ //android.util.Log.wtf(tag, e); logger.error(getMarker(tag), "Error", e); //com.eveningoutpost.dexdrip.Models.UserError.UserErrorHigh(tag, e.toString()); } public static void uel(String tag, String b) { //android.util.Log.i(tag, b); logger.info(getMarker(tag), b); //com.eveningoutpost.dexdrip.Models.UserError.UserEventLow(tag, b); } public static void ueh(String tag, String b) { //android.util.Log.i(tag, b); logger.info(getMarker(tag), b); //com.eveningoutpost.dexdrip.Models.UserError.UserEventHigh(tag, b); } public static void d(String tag, String b){ //android.util.Log.d(tag, b); logger.debug(getMarker(tag), b); //if(com.eveningoutpost.dexdrip.Models.UserError.ExtraLogTags.shouldLogTag(tag, android.util.Log.DEBUG)) { // UserErrorLow(tag, b); //} } public static void v(String tag, String b){ //android.util.Log.v(tag, b); logger.debug(getMarker(tag), b); //if(com.eveningoutpost.dexdrip.Models.UserError.ExtraLogTags.shouldLogTag(tag, android.util.Log.VERBOSE)) { // UserErrorLow(tag, b); //} } public static void i(String tag, String b){ //android.util.Log.i(tag, b); logger.info(getMarker(tag), b); //if(com.eveningoutpost.dexdrip.Models.UserError.ExtraLogTags.shouldLogTag(tag, android.util.Log.INFO)) { // UserErrorLow(tag, b); //} } } } }