You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

579 lines
22 KiB
Java

2 months ago
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.InStoreSpinnerVo;
import com.example.tyre.entity.Tyre;
import com.example.tyre.entity.UpTyre;
import com.example.tyre.util.CarSelectionDialog;
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 UpActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
@BindView(R.id.epc)
TextView EPC;
@BindView(R.id.azlw)
TextView azlw;
@BindView(R.id.condition)
Spinner condition;
// @BindView(R.id.carAdapter)
// Spinner carAdapter;
@BindView(R.id.start)
EditText start;
@BindView(R.id.searchButton)
Button searchButton;
@BindView(R.id.ok)
Button ok;
@BindView(R.id.back)
Button back;
@BindView(R.id.th)
TextView th;
@BindView(R.id.zbh)
TextView zbh;
@BindView(R.id.car)
EditText car;
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<EPC> listEPC;
private ArrayList<String> listepc = new ArrayList<String>();
long lastTime;
long nextTime;
private Toast mToast;
private Toast toast;
private KeyReceiver keyReceiver;
3 weeks ago
String min = "EC0001012026010100000001"; // 左边界(包含)
String max = "EC0001012026010100100000"; // 右边界(包含)
2 months ago
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_up);
ButterKnife.bind(this);
manager = MyApplication.getManager();
listEPC = new ArrayList<EPC>();
condition.setOnItemSelectedListener(this);
showLoadingDialog();
check_spinner();
//car_spinner();
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<TagModel> 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<EPC> 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);
}
3 weeks ago
if (listepc != null && !listepc.isEmpty()){
startFlag = false;
String currentEpc = listepc.get(0);
if (isInRange(currentEpc, min, max)) {
//请求后台 查询车辆信息
// car.setText(currentEpc);
//请求后台
}else {
EPC.setText(currentEpc);
//请求后台
debounceHandler.removeMessages(1);
}
debounceHandler.removeMessages(1);
lastScannedEpc = currentEpc;
debounceHandler.sendEmptyMessageDelayed(1, DEBOUNCE_DELAY);
}
clearData();
2 months ago
}
});
Log.e("EPC", "listepc:+ " + listepc);
3 weeks ago
}
/**
*
* @param target
* @param min
* @param max
* @return true=false=
*/
public static boolean isInRange(String target, String min, String max) {
// 空值校验,避免空指针异常
if (target == null || min == null || max == null) {
return false;
2 months ago
}
3 weeks ago
// compareTo规则
// 1. 字符串相等返回0
// 2. 目标字符串 > 对比字符串 返回正数;
// 3. 目标字符串 < 对比字符串 返回负数。
// 因此target >= min 等价于 target.compareTo(min) >= 0
// target <= max 等价于 target.compareTo(max) <= 0。
return target.compareTo(min) >= 0 && target.compareTo(max) <= 0;
2 months ago
}
3 weeks ago
// 防抖延迟时间 (毫秒)
private static final long DEBOUNCE_DELAY = 500;
// 用于存储最后一次扫描到的 EPC
private String lastScannedEpc;
// 用于处理防抖逻辑的 Handler
private Handler debounceHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == 1 && lastScannedEpc != null) {
// 延迟时间到,执行查询
showLoadingDialog();
if (isInRange(lastScannedEpc, min, max)){
//查询车辆数据方法
findCar(lastScannedEpc);
}else {
find(lastScannedEpc);
}
lastScannedEpc = null; // 清空,等待下一次扫描
}
return true;
}
});
2 months ago
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();
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()){
case R.id.condition:
azlw.setText(parent.getItemAtPosition(position).toString());
break;
}
}
// 添加缺失的方法
@Override
public void onNothingSelected(AdapterView<?> parent) {
// 当没有选项被选中时的处理逻辑
// 可以留空,但必须实现这个方法
}
@OnClick({R.id.ok, R.id.back,R.id.searchButton})
public void onViewClicked(View view) {
String rfid=EPC.getText().toString();
String carNo = car.getText().toString();
String millage = start.getText().toString();
String wheel = azlw.getText().toString();
String selfNo = zbh.getText().toString();
switch (view.getId()) {
case R.id.ok:
// String azlw =
if (rfid == null || rfid.isEmpty())
{
new CommonDialog(UpActivity.this).setMessage("请扫描轮胎!").show();
return;
}
if (carNo == null || carNo.isEmpty())
{
new CommonDialog(UpActivity.this).setMessage("请选择安装车辆!").show();
return;
}
if (millage == null || millage.isEmpty())
{
new CommonDialog(UpActivity.this).setMessage("请输入起始里程!").show();
return;
}
if (wheel == null || wheel.isEmpty())
{
new CommonDialog(UpActivity.this).setMessage("请选择安装轮位!").show();
return;
}
if (selfNo == null || selfNo.isEmpty())
{
new CommonDialog(UpActivity.this).setMessage("请输入自编号!").show();
return;
}
up_insert(rfid,carNo,millage,wheel,selfNo);
showLoadingDialog();
break;
case R.id.back:
Intent intent = new Intent(this, HomePageActivity.class);
startActivity(intent);
break;
case R.id.searchButton:
if (carNo == null || carNo.isEmpty())
{
new CommonDialog(UpActivity.this).setMessage("请输入车牌号!").show();
return;
}
showLoadingDialog();
car_spinner(carNo);
break;
}
}
// 安装请求后台
private void up_insert(String rfid,String carNo,String millage,String wheel,String selfNo) {
OkGo.<String>post(MyUrl.url + "/tyre/install/PdaInstallTyre").tag(this)
.params("tyreRfid", rfid)
.params("mileage", millage)
.params("carNo", carNo)
.params("wheelPostion", wheel)
.params("selfNo", selfNo)
.params("CreateBy", SharedPreferencesUtils.getstring("user","admin"))
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
hideLoadingDialog();
String body = response.body();
Gson gson = new Gson();
AjaxResult ajaxResult = gson.fromJson(body, AjaxResult.class);
handleResponse(ajaxResult);
// try {
// Gson gson = new Gson();
// BaseTyre baseTyre = gson.fromJson(body, BaseTyre.class);
// if (baseTyre != null) {
// // 空值处理:若字段为 null 则显示空字符串
// String TyreNo = safeGetString(baseTyre.getTyreNo());
// th.setText(TyreNo);
// }
// } catch (JsonSyntaxException e) {
// return;
// }
}
});
}
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;
}
}
// 基本信息查询
3 weeks ago
private void findCar(String carNumber) {
OkGo.<String>post(MyUrl.url + "/tyre/car/queryCarByRfid").tag(this).params("rfid", carNumber).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String body = response.body();
hideLoadingDialog();
try {
Gson gson = new Gson();
BaseCar baseCar = gson.fromJson(body, BaseCar.class);
if (baseCar != null) {
// 空值处理:若字段为 null 则显示空字符串
String carNo = safeGetString(baseCar.getCarNo());
car.setText(carNo);
}else {
new CommonDialog(UpActivity.this).setMessage("请检查车辆芯片绑定数据!").show();
}
} catch (JsonSyntaxException e) {
return;
}
}
});
}
// 基本信息查询
2 months ago
private void find(String epc) {
OkGo.<String>post(MyUrl.url + "/tyre/tyre/pdaQueryTyreInfo").tag(this).params("tyreEpc", epc).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String body = response.body();
3 weeks ago
hideLoadingDialog();
2 months ago
try {
Gson gson = new Gson();
BaseTyre baseTyre = gson.fromJson(body, BaseTyre.class);
if (baseTyre != null) {
// 空值处理:若字段为 null 则显示空字符串
String TyreNo = safeGetString(baseTyre.getTyreNo());
th.setText(TyreNo);
zbh.setText(baseTyre.getSelfNo());
}else {
new CommonDialog(UpActivity.this).setMessage("系统无此轮胎!").show();
th.setText("");
zbh.setText("");
}
} catch (JsonSyntaxException e) {
return;
}
}
});
}
private String safeGetString(String value) {
return value == null || "null".equals(value) ? "" : value;
}
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);
}
private void check_spinner() {
Gson gson = new Gson();
OkGo.<String>post(MyUrl.url + "/system/dict/data//wheelPositionList").execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
hideLoadingDialog();
String body = response.body();
List<String> stringList =new ArrayList<>();
stringList = gson.fromJson(body, new TypeToken<List<String>>() {
}.getType());
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(UpActivity.this, android.R.layout.simple_list_item_1, stringList);
condition.setAdapter(arrayAdapter);
}
});
condition.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
azlw.setText(parent.getItemAtPosition(position).toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void car_spinner(String carNo) {
OkGo.<String>post(MyUrl.url + "/tyre/car/PdaQueryCarList")
.tag(this).params("carNo", carNo)
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
hideLoadingDialog();
String body = response.body();
Gson gson=new Gson();
List<BaseCar> baseCarList = gson.fromJson(body, new TypeToken<List<BaseCar>>(){}.getType());
Log.e("EPC", "listepc:+ " + baseCarList.size());
if (baseCarList!=null && baseCarList.size()>0){
List<String> carNoList = new ArrayList<>();
for (BaseCar car : baseCarList) {
carNoList.add(car.getCarNo());
}
// 显示自定义弹窗
showCarSelectionDialog(carNoList);
}
}
});
}
private void showCarSelectionDialog(List<String> carNoList) {
CarSelectionDialog dialog = new CarSelectionDialog(this, carNoList);
dialog.setOnCarSelectedListener(new CarSelectionDialog.OnCarSelectedListener() {
@Override
public void onCarSelected(String carNo) {
car.setText(carNo);
// 处理选中的车辆
// 例如设置到EditText或其他控件
// editTextCarNo.setText(carNo);
}
});
dialog.show();
}
// carAdapter.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
// @Override
// public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// car.setText(parent.getItemAtPosition(position).toString());
// }
//
// @Override
// public void onNothingSelected(AdapterView<?> parent) {
// }
// });
}