package com.example.tyre; import android.app.ProgressDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import com.android.hdhe.uhf.reader.UhfReader; import com.android.hdhe.uhf.readerInterface.TagModel; import com.example.tyre.entity.AjaxResult; import com.example.tyre.entity.BaseCar; import com.example.tyre.entity.BaseTyre; import com.example.tyre.entity.EPC; import com.example.tyre.entity.UpTyre; import com.example.tyre.util.CommonDialog; import com.example.tyre.util.MyUrl; import com.example.tyre.util.PlayMusic; import com.example.tyre.util.SharedPreferencesUtils; import com.example.tyre.util.Util; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; import com.lzy.okgo.OkGo; import com.lzy.okgo.callback.StringCallback; import com.lzy.okgo.model.Response; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import androidx.appcompat.app.AppCompatActivity; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import cn.pda.serialport.Tools; public class DownActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { @BindView(R.id.epc) TextView EPC; @BindView(R.id.car) TextView car; @BindView(R.id.start) EditText start; @BindView(R.id.azlw) TextView azlw; @BindView(R.id.ok) Button ok; @BindView(R.id.back) Button back; @BindView(R.id.th) TextView th; @BindView(R.id.zbh) TextView zbh; private boolean isStart = true; private ProgressDialog progressDialog; private boolean runFlag = true; private boolean startFlag = false; private UhfReader manager; // UHF manager,UHF Operating handle private ArrayList listEPC; private ArrayList listepc = new ArrayList(); long lastTime; long nextTime; private Toast mToast; private Toast toast; private KeyReceiver keyReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_down); ButterKnife.bind(this); manager = MyApplication.getManager(); listEPC = new ArrayList(); Thread thread = new InventoryThread(); thread.start(); Util.initSoundPool(this); } @Override public void onResume() { super.onResume(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } registerReceiver(); } @Override public void onPause() { startFlag = false; super.onPause(); unregisterReceiver(); } @Override protected void onDestroy() { startFlag = false; runFlag = false; super.onDestroy(); } class InventoryThread extends Thread { private List tagList; byte[] accessPassword = Tools.HexString2Bytes("00000000"); @Override public void run() { super.run(); while (runFlag) { if (startFlag) { tagList = manager.inventoryRealTime(); //实时盘存 if (tagList != null && !tagList.isEmpty()) { //播放提示音 Util.play(1, 0); for (TagModel tag : tagList) { if (tag == null) { String epcStr = ""; // String epcStr = new String(epc); addToList(listEPC, epcStr, (byte) -1); } else { String epcStr = Tools.Bytes2HexString(tag.getmEpcBytes(), tag.getmEpcBytes().length); // String epcStr = new String(epc); byte rssi = tag.getmRssi(); addToList(listEPC, epcStr, rssi); } } } tagList = null; try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } // EPC add to LISTVIEW private void addToList(final List list, final String epc, final byte rssi) { runOnUiThread(new Runnable() { @Override public void run() { // The epc for the first time if (list.isEmpty()) { EPC epcTag = new EPC(); epcTag.setEpc(epc); epcTag.setCount(1); epcTag.setRssi(rssi); list.add(epcTag); listepc.add(epc); }else { for (int i = 0; i < list.size(); i++) { EPC mEPC = list.get(i); // list contain this epc if (epc.equals(mEPC.getEpc())) { mEPC.setCount(mEPC.getCount() + 1); mEPC.setRssi(rssi); list.set(i, mEPC); break; } else if (i == (list.size() - 1)) { // list doesn't contain this epc EPC newEPC = new EPC(); newEPC.setEpc(epc); newEPC.setCount(1); newEPC.setRssi(rssi); list.add(newEPC); listepc.add(epc); } } } // play sound lastTime = SystemClock.elapsedRealtime(); long time = lastTime - nextTime; if (time >= 60) { Util.play(1, 0); nextTime = lastTime; Log.e("TAG", "run: " + time); } } }); Log.e("EPC", "listepc:+ " + listepc); if (listepc != null && !listepc.isEmpty()){ startFlag = false; EPC.setText(listepc.get(0).toString()); //请求后台 find(listepc.get(0).toString()); } clearData(); } private void clearData() { listEPC.removeAll(listEPC); listepc.removeAll(listepc); } private void showLoadingDialog() { progressDialog = new ProgressDialog(this); progressDialog.setMessage("数据加载中..."); progressDialog.setCancelable(false); progressDialog.show(); } private void hideLoadingDialog() { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } } private class KeyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int keyCode = intent.getIntExtra("keyCode", 0); if (keyCode == 0) { keyCode = intent.getIntExtra("keycode", 0); } boolean keyDown = intent.getBooleanExtra("keydown", false); if (keyDown) { if (toast == null) { // toast = Toast.makeText(OutStoreHouseActivity.this, "KeyReceiver:keyCode = down" + keyCode, Toast.LENGTH_SHORT); } else { // toast.setText("KeyReceiver:keyCode = down" + keyCode); } // toast.show(); switch (keyCode) { case KeyEvent.KEYCODE_F1: case KeyEvent.KEYCODE_F2: case KeyEvent.KEYCODE_F3: case KeyEvent.KEYCODE_F4: case KeyEvent.KEYCODE_F5: //扫描 startFlag = true; break; } } } } private void registerReceiver() { keyReceiver = new KeyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.rfid.FUN_KEY"); filter.addAction("android.intent.action.FUN_KEY"); registerReceiver(keyReceiver, filter); } private void unregisterReceiver() { unregisterReceiver(keyReceiver); } @OnClick({R.id.ok, R.id.back}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.ok: String rfid=EPC.getText().toString(); String carNo = car.getText().toString(); String millage = start.getText().toString(); String wheel = azlw.getText().toString(); if (rfid == null || rfid.isEmpty()) { new CommonDialog(DownActivity.this).setMessage("请扫描轮胎!").show(); return; } if (carNo == null || carNo.isEmpty()) { new CommonDialog(DownActivity.this).setMessage("该轮胎未安装!").show(); return; } if (millage == null || millage.isEmpty()) { new CommonDialog(DownActivity.this).setMessage("请输入卸胎里程!").show(); return; } if (wheel == null || wheel.isEmpty()) { new CommonDialog(DownActivity.this).setMessage("该轮胎未安装!").show(); return; } showLoadingDialog(); down_insert(rfid,carNo,millage,wheel); break; case R.id.back: Intent intent = new Intent(this, HomePageActivity.class); startActivity(intent); break; } } @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { } @Override public void onNothingSelected(AdapterView parent) { } // 基本信息查询 private void find(String epc) { OkGo.post(MyUrl.url + "/tyre/tyre/pdaQueryTyreInfo").tag(this).params("tyreEpc", epc) .execute(new StringCallback() { @Override public void onSuccess(Response response) { String body = response.body(); try { Gson gson = new Gson(); BaseTyre baseTyre = gson.fromJson(body, BaseTyre.class); if (baseTyre != null) { // 空值处理:若字段为 null 则显示空字符串 String TyreNo = safeGetString(baseTyre.getTyreNo()); String carNo = safeGetString(baseTyre.getCarNo()); String wheel = safeGetString(baseTyre.getWheelPostion()); String zb = safeGetString(baseTyre.getSelfNo()); th.setText(TyreNo); zbh.setText(zb); car.setText(carNo); azlw.setText(wheel); }else { new CommonDialog(DownActivity.this).setMessage("系统无此轮胎!").show(); th.setText(""); zbh.setText(""); } } catch (JsonSyntaxException e) { return; } } }); } private String safeGetString(String value) { return value == null || "null".equals(value) ? "" : value; } // 删除实时表、卸车记录表 private void down_insert(String rfid,String carNo,String millage,String wheel) { OkGo.post(MyUrl.url + "/tyre/install/PdaUnInstallTyre").tag(this) .params("tyreRfid", rfid) .params("mileage", millage) .params("carNo", carNo) .params("wheelPostion", wheel) .params("CreateBy", SharedPreferencesUtils.getstring("user","admin")) .execute(new StringCallback() { @Override public void onSuccess(Response response) { String body = response.body(); hideLoadingDialog(); Gson gson = new Gson(); AjaxResult ajaxResult = gson.fromJson(body, AjaxResult.class); handleResponse(ajaxResult); } }); } private void handleResponse(AjaxResult result) { switch (result.getCode()) { case "500": new CommonDialog(this).setMessage(result.getMsg()).show(); break; case "0": new CommonDialog(this).setMessage(result.getMsg()).show(); break; } } }