package com.device.comm.mylibrary.NativeDevice.cgmadi.util;


import com.device.comm.mylibrary.NativeDevice.cgmadi.bean.ResponseBean;
import com.device.comm.mylibrary.NativeDevice.cgmadi.bean.THStatus;
import com.device.comm.mylibrary.NativeDevice.cgmadi.enums.OBJCode;
import com.device.comm.mylibrary.NativeDevice.cgmadi.enums.OPCode;

import java.util.ArrayList;
import java.util.List;

public class ResponseAnalysis
{
  public ResponseAnalysis() {}
  
  public static boolean booleanAnalysis(ResponseBean response, OPCode opCode, OBJCode objCode)
  {
    if ((response != null) && (opCode == response.getOpCode()) && (objCode == response.getObjCode())) {
      return Boolean.TRUE.booleanValue();
    }
    return Boolean.FALSE.booleanValue();
  }
  
  public static Integer[] intAnalysis(ResponseBean response, OPCode opCode, OBJCode objCode)
  {
    if (booleanAnalysis(response, opCode, objCode))
    {
      byte[] array = response.getData();
      byte[] temp = DataFormatUtil.changeHighAndLow(array);
      Integer[] intArray = DataFormatUtil.byteArray2Int(temp);
      return intArray;
    }
    return null;
  }
  
  public static double[] electricAnalysis(ResponseBean response, OPCode opCode, OBJCode objCode)
  {
    if (booleanAnalysis(response, opCode, objCode))
    {
      byte[] array = response.getData();
      double[] rtn = new double[array.length / 2];
      byte[] temp = null;
      for (int i = array.length - 1; i >= 0; i -= 2)
      {
        temp = new byte[] { array[i], array[(i - 1)] };
        rtn[((i - 1) / 2)] = (DataFormatUtil.wordBytes2int(temp) * 0.1D);
      }
      return rtn;
    }
    return null;
  }
  
  public static String stringASCIIAnalysis(ResponseBean response, OPCode opCode, OBJCode objCode)
  {
    if (booleanAnalysis(response, opCode, objCode)) {
      try
      {
        byte[] array = response.getData();
        if (!isEmpty(array)) {
          return new String(response.getData(), "US-ASCII").trim();
        }
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
    return null;
  }
  
  public static String stringASCIIAnalysis(byte[] bytes)
  {
    try
    {
      if (!isEmpty(bytes)) {
        return new String(bytes, "US-ASCII").trim();
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return null;
  }
  
  public static THStatus[] statusAnalysis(byte[] bytes)
  {
    try
    {
      if (!isEmpty(bytes))
      {
        List<THStatus> rtn = new ArrayList();
        String temp = null;
        byte[] arrayOfByte = bytes;int j = bytes.length;
        for (int i = 0; i < j; i++)
        {
          byte b = arrayOfByte[i];
          temp = Integer.toBinaryString(DataFormatUtil.byte2int(b));
          if (temp.length() < 8) {
            temp = DataFormatUtil.complement(temp, '0', 8 - temp.length());
          }
          rtn.add(new THStatus(temp.charAt(7) == '0' ? Boolean.FALSE : Boolean.TRUE, 
            temp.charAt(6) == '0' ? Boolean.FALSE : Boolean.TRUE, 
            temp.charAt(5) == '0' ? Boolean.FALSE : Boolean.TRUE, 
            temp.charAt(4) == '0' ? Boolean.FALSE : Boolean.TRUE, 
            temp.charAt(3) == '0' ? Boolean.FALSE : Boolean.TRUE, 
            temp.charAt(2) == '0' ? Boolean.FALSE : Boolean.TRUE, 
            temp.charAt(1) == '0' ? Boolean.FALSE : Boolean.TRUE, 
            temp.charAt(0) == '0' ? Boolean.FALSE : Boolean.TRUE));
        }
        return (THStatus[])rtn.toArray(new THStatus[0]);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return null;
  }
  
  public static THStatus[] statusAnalysis(ResponseBean response, OPCode opCode, OBJCode objCode)
  {
    if (booleanAnalysis(response, opCode, objCode)) {
      try
      {
        byte[] array = response.getData();
        if (!isEmpty(array))
        {
          List<THStatus> rtn = new ArrayList();
          String temp = null;
          byte[] arrayOfByte1;
          int j = (arrayOfByte1 = array).length;
          for (int i = 0; i < j; i++)
          {
            byte b = arrayOfByte1[i];
            temp = Integer.toBinaryString(DataFormatUtil.byte2int(b));
            if (temp.length() < 8) {
              temp = DataFormatUtil.complement(temp, '0', 8 - temp.length());
            }
            rtn.add(new THStatus(temp.charAt(7) == '0' ? Boolean.FALSE : Boolean.TRUE, 
              temp.charAt(6) == '0' ? Boolean.FALSE : Boolean.TRUE, 
              temp.charAt(5) == '0' ? Boolean.FALSE : Boolean.TRUE, 
              temp.charAt(4) == '0' ? Boolean.FALSE : Boolean.TRUE, 
              temp.charAt(3) == '0' ? Boolean.FALSE : Boolean.TRUE, 
              temp.charAt(2) == '0' ? Boolean.FALSE : Boolean.TRUE, 
              temp.charAt(1) == '0' ? Boolean.FALSE : Boolean.TRUE, 
              temp.charAt(0) == '0' ? Boolean.FALSE : Boolean.TRUE));
          }
          return (THStatus[])rtn.toArray(new THStatus[0]);
        }
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
    return null;
  }
  
  protected static boolean isEmpty(byte[] bytes)
  {
    for (int i = 0; i < bytes.length; i++) {
      if (bytes[i] != -1) {
        return Boolean.FALSE.booleanValue();
      }
    }
    return Boolean.TRUE.booleanValue();
  }
  
  public static Boolean intToBooleanAnalysis(ResponseBean response, OPCode opCode, OBJCode objCode)
  {
    if (booleanAnalysis(response, opCode, objCode)) {
      try
      {
        byte[] array = response.getData();
        if (!isEmpty(array))
        {
          if ((array.length > 0) && (array[0] == 1)) {
            return Boolean.valueOf(true);
          }
          return Boolean.valueOf(false);
        }
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
    return null;
  }
}


/* Location:              /Users/lvwang2002/Desktop/CodeSource/work/DY_GLUCOSE/thapi_test/libs/cgmthapi.jar!/com/sanme/cgmadi/bluetooth4/util/ResponseAnalysis.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */