ca501adc0dfeee5082deb1a50c701876aeb3efaa.svn-base 8.9 KB

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