b4aafa9aae538f6b16f65da767f144b77d74694b.svn-base 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package eVVM.apk.ui.first;
  2. import android.content.Intent;
  3. import android.location.Location;
  4. import android.nfc.NdefMessage;
  5. import android.nfc.NdefRecord;
  6. import android.nfc.NfcAdapter;
  7. import android.os.Parcelable;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import java.math.BigInteger;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.LinkedList;
  14. import java.util.List;
  15. import eVVM.apk.R;
  16. import eVVM.apk.helper.GPS.GPSLocationListener;
  17. import eVVM.apk.helper.GPS.GPSLocationManager;
  18. import eVVM.apk.helper.SPUtils;
  19. import eVVM.apk.helper.SoundPoolHelper;
  20. import eVVM.apk.ui.bean.UploadChipBean;
  21. import eVVM.apk.ui.home.BaseNfcActivity;
  22. import eVVM.apk.helper.ActivityStackManager;
  23. import eVVM.apk.helper.DoubleClickHelper;
  24. import eVVM.apk.ui.home.uploadChip.UploadChipContract;
  25. public class ReadingActivity extends BaseNfcActivity implements UploadChipContract.View {
  26. private GPSLocationManager gpsLocationManager;
  27. private Location location;
  28. @Override
  29. protected int getLayoutId() {
  30. return R.layout.activity_reading;
  31. }
  32. @Override
  33. protected int getTitleId() {
  34. return R.id.reading_title;
  35. }
  36. @Override
  37. protected void initView() {
  38. soundPoolHelper = new SoundPoolHelper(4,SoundPoolHelper.TYPE_MUSIC)
  39. .setRingtoneType(SoundPoolHelper.RING_TYPE_MUSIC)
  40. .loadDefault(ReadingActivity.this)
  41. .load(ReadingActivity.this,"factory_operator_success",R.raw.factory_operator_success)
  42. .load(ReadingActivity.this,"factory_operator_error",R.raw.factory_operator_error);
  43. }
  44. @Override
  45. protected void initData() {
  46. mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  47. gpsLocationManager = GPSLocationManager.getInstances(ReadingActivity.this);
  48. }
  49. private SoundPoolHelper soundPoolHelper;
  50. @Override
  51. public void uploadChipError(String msg) {
  52. if(msg.equals("芯片不存在")){
  53. soundPoolHelper.play("factory_operator_error",false);
  54. }
  55. toast(msg);
  56. }
  57. @Override
  58. public void uploadChipSuccess(UploadChipBean data) {
  59. soundPoolHelper.play("factory_operator_success",false);
  60. Log.e("FactoryOperatorActivity",data.getCode()+"");
  61. }
  62. /**
  63. * 读取NFC标签文本数据
  64. */
  65. private String readNfcTag(Intent intent) {
  66. if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
  67. Parcelable[] rawMsgs = intent.getParcelableArrayExtra( NfcAdapter.EXTRA_NDEF_MESSAGES);
  68. NdefMessage msgs[] = null;
  69. if (rawMsgs != null) {
  70. msgs = new NdefMessage[rawMsgs.length];
  71. for (int i = 0; i < rawMsgs.length; i++) {
  72. msgs[i] = (NdefMessage) rawMsgs[i];
  73. }
  74. }
  75. try {
  76. if (msgs != null) {
  77. NdefRecord record = msgs[0].getRecords()[0];
  78. return parseTextRecord(record);
  79. }
  80. } catch (Exception e) {
  81. }
  82. }
  83. return null;
  84. }
  85. public String parseTextRecord(NdefRecord ndefRecord) {
  86. if (ndefRecord.getTnf() != NdefRecord.TNF_WELL_KNOWN) {
  87. return null;
  88. }
  89. if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
  90. return null;
  91. }
  92. try {
  93. byte[] bArray = ndefRecord.getPayload();
  94. StringBuffer sb = new StringBuffer(bArray.length);
  95. String sTemp;
  96. for (int i = 0; i < bArray.length; i++) {
  97. sTemp = Integer.toHexString(0xFF & bArray[i]);
  98. if (sTemp.length() < 2)
  99. sb.append(0);
  100. sb.append(sTemp.toUpperCase());
  101. }
  102. alarmDetection(bArray,sb.substring(6,56 * 2));
  103. return sb.toString();
  104. } catch (Exception e) {
  105. throw new IllegalArgumentException();
  106. }
  107. }
  108. @Override
  109. public void onNewIntent(Intent intent) {
  110. String textRecord = readNfcTag(intent);
  111. if(textRecord != null && textRecord.length() > 56 * 2){
  112. if(location != null){
  113. getPresenter().forDoctor(textRecord.substring(6,56 * 2),textRecord,location.getLongitude(),location.getLatitude());
  114. }else{
  115. toast("位置信息获取失败");
  116. }
  117. }else{
  118. toast("读取失败,请重试");
  119. }
  120. }
  121. public void alarmDetection(byte[] bytes,String chipNumber){
  122. try{
  123. //将byte[]转为各种进制的字符串
  124. String binaryStr = new BigInteger(1, bytes).toString(2);
  125. int startIndex = (3 + 16 + 16 + 1 + 4 + 7 ) * 4 + 1 * 4 ; //减去前边非温度部分和启用后的一小时
  126. int marginRight = 2 * 4; //减去疫苗使用前的两小时
  127. String binaryArr[] = new String[(binaryStr.length() - marginRight) / 2 - startIndex];
  128. for (int i = startIndex; i < (binaryStr.length() - marginRight) / 2; i++) {
  129. binaryArr[i - startIndex] = String.valueOf(binaryStr.charAt(i * 2)) + binaryStr.charAt(i * 2 + 1);
  130. }
  131. LinkedList<Integer> exceedingIndexs = new LinkedList<Integer>();
  132. exceedingIndexs.add(-1);
  133. for (int i = 0; i < binaryArr.length; i++) { //不计算开始的一小时和最后两小时
  134. if(!binaryArr[i].equals("01")){
  135. if(i != 0 && !exceedingIndexs.get(exceedingIndexs.size() - 1).equals(i)){
  136. exceedingIndexs.add(i);
  137. }
  138. if(i != binaryArr.length - 1 && !exceedingIndexs.get(exceedingIndexs.size() - 1).equals(i + 1)){
  139. exceedingIndexs.add(i + 1);
  140. }
  141. }
  142. }
  143. // 48小时
  144. int type = Integer.parseInt(chipNumber.substring(1,2), 16);
  145. int category = Integer.parseInt(SPUtils.get("categorytimes_" + type,48).toString());
  146. if((exceedingIndexs.size() - 1) / 2.f > category){
  147. toast("报警");
  148. }else{
  149. toast("正常");
  150. }
  151. }catch (Exception e){
  152. e.printStackTrace();
  153. }
  154. }
  155. @Override
  156. protected void onStart() {
  157. super.onStart();
  158. //开启定位
  159. gpsLocationManager.start(new ReadingActivity.MyListener());
  160. }
  161. @Override
  162. protected void onStop() {
  163. super.onStop();
  164. //终止定位
  165. gpsLocationManager.stop();
  166. }
  167. @Override
  168. protected void onDestroy() {
  169. super.onDestroy();
  170. }
  171. class MyListener implements GPSLocationListener {
  172. @Override
  173. public void UpdateLocation(Location _location) {
  174. if (_location != null) {
  175. location = _location;
  176. }
  177. }
  178. @Override
  179. public void UpdateStatus(String provider, int status, Bundle extras) {
  180. if ("gps" == provider) {
  181. //Toast.makeText(FactoryOperatorActivity.this, "定位类型:" + provider, Toast.LENGTH_SHORT).show();
  182. }
  183. }
  184. @Override
  185. public void UpdateGPSProviderStatus(int gpsStatus) {
  186. }
  187. }
  188. //
  189. // @Override
  190. // public void onBackPressed() {
  191. // if (DoubleClickHelper.isOnDoubleClick()) {
  192. // //移动到上一个任务栈,避免侧滑引起的不良反应
  193. // moveTaskToBack(false);
  194. // postDelayed(new Runnable() {
  195. //
  196. // @Override
  197. // public void run() {
  198. // // 进行内存优化,销毁掉所有的界面
  199. // ActivityStackManager.getInstance().finishAllActivities();
  200. // // 销毁进程
  201. // System.exit(0);
  202. // }
  203. // }, 300);
  204. // } else {
  205. // toast(getResources().getString(R.string.home_exit_hint));
  206. // }
  207. // }
  208. @Override
  209. public boolean isSupportSwipeBack() {
  210. // 不使用侧滑功能
  211. return false;
  212. }
  213. }