221 lines
8.0 KiB
Java
221 lines
8.0 KiB
Java
package com.example.jinyu_rfid;
|
|
|
|
import androidx.databinding.DataBindingUtil;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.widget.PopupMenu;
|
|
import android.widget.Toast;
|
|
|
|
import com.example.jinyu_rfid.adapter.ResultAdapter;
|
|
import com.example.jinyu_rfid.base.BaseActivity;
|
|
import com.example.jinyu_rfid.base.MyRecultCall;
|
|
import com.example.jinyu_rfid.base.MyResult;
|
|
import com.example.jinyu_rfid.been.ConfigurationTable;
|
|
import com.example.jinyu_rfid.been.ReadTyreNoResult;
|
|
import com.example.jinyu_rfid.broadcast.ScanERCodeReceiver;
|
|
import com.example.jinyu_rfid.callback.DataReturnCall;
|
|
import com.example.jinyu_rfid.databinding.ActivityWriteUserBinding;
|
|
import com.example.jinyu_rfid.rfid.C5106Device;
|
|
import com.example.jinyu_rfid.rfid.RFIDModel;
|
|
import com.example.jinyu_rfid.uitls.ASCIIUtil;
|
|
import com.example.jinyu_rfid.uitls.SharedPreferencesUtils;
|
|
import com.google.gson.reflect.TypeToken;
|
|
import com.lzy.okgo.OkGo;
|
|
import com.lzy.okgo.model.Response;
|
|
|
|
import org.litepal.LitePal;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
|
|
import okhttp3.RequestBody;
|
|
|
|
public class WriteUserActivity extends BaseActivity implements DataReturnCall, PopupMenu.OnMenuItemClickListener {
|
|
private Intent intent1;
|
|
private Intent intent2;
|
|
private RFIDModel rfidModel;
|
|
private ActivityWriteUserBinding binding;
|
|
private ResultAdapter adapter;
|
|
private List<ReadTyreNoResult> list;
|
|
private PopupMenu popupMenu;
|
|
|
|
@SuppressLint("UnspecifiedRegisterReceiverFlag")
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
binding = DataBindingUtil.setContentView(this, R.layout.activity_write_user);
|
|
intent1 = new Intent(this, ReadActivity.class);
|
|
intent1.putExtra("work", 1);
|
|
intent2 = new Intent(this, ConfigurationTableActivity.class);
|
|
ScanERCodeReceiver scanERCodeReceiver = new ScanERCodeReceiver(this);
|
|
registerReceiver(scanERCodeReceiver, new IntentFilter("com.rfid.SCAN"));
|
|
adapter = new ResultAdapter(this);
|
|
binding.setAdapter(adapter);
|
|
popupMenu = new PopupMenu(this, binding.writeMenu);
|
|
popupMenu.setOnMenuItemClickListener(this);
|
|
getMenuInflater().inflate(R.menu.activity_menu, popupMenu.getMenu());
|
|
rfidModel = new C5106Device(this);
|
|
}
|
|
|
|
|
|
public void writeReadEPC(View view) {
|
|
rfidModel.sanEpc(6);
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
rfidModel.close();
|
|
}
|
|
|
|
private boolean writeUserState = false;
|
|
|
|
// 写入
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
public void writeUser(View view) {
|
|
if (list == null || list.isEmpty()) return;
|
|
var epcStr = binding.writeEpcCode.getText().toString();
|
|
if (epcStr.isEmpty()) return;
|
|
// EPC编码~胎号~品牌~规格~花纹~层级~轮胎名称~速度级别~负荷指数~轮辋直径~扁平比~销售区域
|
|
List<ConfigurationTable> tagList = LitePal.findAll(ConfigurationTable.class);
|
|
StringBuilder tagUserStr = new StringBuilder(tagList.get(0).isState() ? epcStr : "_");
|
|
tagUserStr.append("~");
|
|
for (int i = 1; i < 12; i++) {
|
|
var configuration = tagList.get(i);
|
|
var state = configuration.isState();
|
|
if (state) {
|
|
try {
|
|
var result = list.get(i - 1);
|
|
var propertyContent = result.getPropertyContent();
|
|
tagUserStr.append(propertyContent == null ? "_" : propertyContent);
|
|
} catch (IndexOutOfBoundsException e) {
|
|
tagUserStr.append("_");
|
|
}
|
|
} else {
|
|
tagUserStr.append("_");
|
|
}
|
|
tagUserStr.append("~");
|
|
}
|
|
String user = ASCIIUtil.str2Hex(tagUserStr.toString());
|
|
|
|
if (!writeUserState) {
|
|
Log.e("TAG", "请求");
|
|
Map<String, String> map = new HashMap<>();
|
|
map.put("EpcCode", epcStr);
|
|
map.put("TyreNo", list.get(0).getPropertyContent());
|
|
map.put("token", "123456");
|
|
map.put("Language", SharedPreferencesUtils.getstring("languageIndex","0"));
|
|
Log.e("TAG", "writeUser:" + 2);
|
|
OkGo.<MyResult>post(url + "/write").upRequestBody(RequestBody.create(JSON, gson.toJson(map))).execute(new MyRecultCall(dialog, this) {
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
@Override
|
|
public void onSuccess(Response<MyResult> response) {
|
|
super.onSuccess(response);
|
|
var body = response.body();
|
|
if (body.getSaveFlag().equals("1")) {
|
|
Toast.makeText(WriteUserActivity.this, getString(R.string.binding_success), Toast.LENGTH_SHORT).show();
|
|
writeUserState = true;
|
|
return;
|
|
}
|
|
tipDialog.shouDialog(false, body.getSaveMessage());
|
|
}
|
|
});
|
|
|
|
}
|
|
if (writeUserState){
|
|
Log.e("TAG", "写入");
|
|
var writeState = rfidModel.writeUser(validateDataLength(user), epcStr);
|
|
tipDialog.shouDialog(writeState,getString(writeState ? R.string.write_success : R.string.write_failed));
|
|
if (writeState) {
|
|
list.clear();
|
|
adapter.notifyDataSetChanged();
|
|
binding.writeEpcCode.setText(null);
|
|
binding.writeTire.setText(null);
|
|
writeUserState=false;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public String validateDataLength(String data) {
|
|
int length = data.length();
|
|
int remainder = length % 4;
|
|
|
|
if (remainder != 0) {
|
|
int paddingLength = 4 - remainder;
|
|
StringBuilder paddedData = new StringBuilder(data);
|
|
for (int i = 0; i < paddingLength; i++) {
|
|
paddedData.append("0");
|
|
}
|
|
return paddedData.toString();
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
|
|
@Override
|
|
public void readEpcCodeInfo(String info, boolean state, String stateInfo) {
|
|
if (state) {
|
|
binding.writeEpcCode.setText(info);
|
|
super.music.start();
|
|
return;
|
|
}
|
|
binding.writeEpcCode.setText(null);
|
|
// Log.e("TAG", "读取状态:" + stateInfo);
|
|
}
|
|
|
|
// 扫码
|
|
@Override
|
|
public void readerCodeInfo(String erCode) {
|
|
binding.writeTire.setText(erCode);
|
|
Map<String, String> map = new HashMap<>();
|
|
map.put("TyreNo", erCode);
|
|
map.put("token", "123456");
|
|
map.put("Language", SharedPreferencesUtils.getstring("languageIndex","0"));
|
|
OkGo.<MyResult>post(url + "/readTyreNo").upRequestBody(RequestBody.create(JSON, gson.toJson(map))).execute(new MyRecultCall(dialog, this) {
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
@Override
|
|
public void onSuccess(Response<MyResult> response) {
|
|
super.onSuccess(response);
|
|
var body = response.body();
|
|
if (body.getResultFlag().equals("1")) {
|
|
|
|
list = gson.fromJson(body.getJson(), new TypeToken<List<ReadTyreNoResult>>() {
|
|
}.getType());
|
|
list.remove(0);
|
|
adapter.setList(list);
|
|
adapter.notifyDataSetChanged();
|
|
return;
|
|
}
|
|
Toast.makeText(WriteUserActivity.this, body.getResultData().toString(), Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
|
|
public void writeGoMenu(View view) {
|
|
popupMenu.show();
|
|
}
|
|
|
|
@Override
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
if (item.getItemId() == R.id.menu_read) {
|
|
|
|
startActivity(intent1);
|
|
} else {
|
|
startActivity(intent2);
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
} |