添加重量输入以及改变线体号
parent
04c36ca2b7
commit
4dc308f5e5
@ -1,75 +1,114 @@
|
||||
package com.example.pdaplccontrol;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
|
||||
|
||||
private Button[] buttons = new Button[9];
|
||||
private final Button[] lineButtons = new Button[9];
|
||||
private EditText editWeightKg;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
// 初始化 9 个按钮
|
||||
buttons[0] = findViewById(R.id.btnBag1);
|
||||
buttons[1] = findViewById(R.id.btnBag2);
|
||||
buttons[2] = findViewById(R.id.btnBag3);
|
||||
buttons[3] = findViewById(R.id.btnBag4);
|
||||
buttons[4] = findViewById(R.id.btnBag5);
|
||||
buttons[5] = findViewById(R.id.btnBag6);
|
||||
buttons[6] = findViewById(R.id.btnBag7);
|
||||
buttons[7] = findViewById(R.id.btnBag8);
|
||||
buttons[8] = findViewById(R.id.btnBag9);
|
||||
editWeightKg = findViewById(R.id.editWeightKg);
|
||||
setupWeightInput();
|
||||
setupLineButtons();
|
||||
}
|
||||
|
||||
// 绑定点击事件,并设置 Tag 为袋子编号 (1-9)
|
||||
for (int i = 0; i < buttons.length; i++) {
|
||||
buttons[i].setTag((short) (i + 1));
|
||||
buttons[i].setOnClickListener(this);
|
||||
private void setupWeightInput() {
|
||||
editWeightKg.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
||||
@Override
|
||||
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||
boolean isDone = actionId == EditorInfo.IME_ACTION_DONE
|
||||
|| (event != null
|
||||
&& event.getAction() == KeyEvent.ACTION_UP
|
||||
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
|
||||
if (isDone) {
|
||||
sendWeight();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setupLineButtons() {
|
||||
lineButtons[0] = findViewById(R.id.btnLine10);
|
||||
lineButtons[1] = findViewById(R.id.btnLine11);
|
||||
lineButtons[2] = findViewById(R.id.btnLine12);
|
||||
lineButtons[3] = findViewById(R.id.btnLine13);
|
||||
lineButtons[4] = findViewById(R.id.btnLine14);
|
||||
lineButtons[5] = findViewById(R.id.btnLine15);
|
||||
lineButtons[6] = findViewById(R.id.btnLine16);
|
||||
lineButtons[7] = findViewById(R.id.btnLine17);
|
||||
lineButtons[8] = findViewById(R.id.btnLine18);
|
||||
|
||||
for (int i = 0; i < lineButtons.length; i++) {
|
||||
lineButtons[i].setTag((short) (PlcConfig.MIN_LINE_NUMBER + i));
|
||||
lineButtons[i].setOnClickListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (v instanceof Button) {
|
||||
short bagNumber = (short) v.getTag();
|
||||
short lineNumber = (short) v.getTag();
|
||||
|
||||
// 防抖设计:点击后立即禁用所有按钮,防止重复提交
|
||||
setButtonsEnabled(false);
|
||||
Toast.makeText(this, "正在向 PLC 发送 " + bagNumber + " 号袋...", Toast.LENGTH_SHORT).show();
|
||||
setControlsEnabled(false);
|
||||
Toast.makeText(this, "正在向 PLC 发送线体号 " + lineNumber + "...", Toast.LENGTH_SHORT).show();
|
||||
|
||||
// 异步下发到 PLC
|
||||
PlcManager.getInstance().sendBagNumberAsync(bagNumber, new PlcManager.PlcCallback() {
|
||||
@Override
|
||||
public void onSuccess(String message) {
|
||||
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
|
||||
// 恢复按钮状态
|
||||
setButtonsEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(String error) {
|
||||
Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
|
||||
// 恢复按钮状态
|
||||
setButtonsEnabled(true);
|
||||
}
|
||||
});
|
||||
PlcManager.getInstance().sendLineNumberAsync(lineNumber, createCallback());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置所有按钮的可用状态
|
||||
*/
|
||||
private void setButtonsEnabled(boolean enabled) {
|
||||
for (Button btn : buttons) {
|
||||
private void sendWeight() {
|
||||
final short weightGrams;
|
||||
try {
|
||||
weightGrams = WeightInputParser.kgToGrams(editWeightKg.getText().toString());
|
||||
} catch (IllegalArgumentException e) {
|
||||
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
setControlsEnabled(false);
|
||||
Toast.makeText(this, "正在向 PLC 发送重量 " + weightGrams + "g...", Toast.LENGTH_SHORT).show();
|
||||
|
||||
PlcManager.getInstance().sendWeightGramsAsync(weightGrams, createCallback());
|
||||
}
|
||||
|
||||
private PlcManager.PlcCallback createCallback() {
|
||||
return new PlcManager.PlcCallback() {
|
||||
@Override
|
||||
public void onSuccess(String message) {
|
||||
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
|
||||
setControlsEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(String error) {
|
||||
Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
|
||||
setControlsEnabled(true);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void setControlsEnabled(boolean enabled) {
|
||||
editWeightKg.setEnabled(enabled);
|
||||
for (Button btn : lineButtons) {
|
||||
if (btn != null) {
|
||||
btn.setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,36 @@
|
||||
package com.example.pdaplccontrol;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
final class WeightInputParser {
|
||||
|
||||
private static final BigDecimal GRAMS_PER_KG = new BigDecimal("1000");
|
||||
|
||||
private WeightInputParser() {
|
||||
}
|
||||
|
||||
static short kgToGrams(String input) {
|
||||
if (input == null || input.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("请输入重量");
|
||||
}
|
||||
|
||||
BigDecimal kg;
|
||||
try {
|
||||
kg = new BigDecimal(input.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException("重量格式不正确");
|
||||
}
|
||||
|
||||
BigDecimal grams = kg.movePointRight(3).stripTrailingZeros();
|
||||
if (kg.compareTo(BigDecimal.ZERO) < 0
|
||||
|| grams.compareTo(BigDecimal.valueOf(PlcConfig.MAX_WEIGHT_GRAMS)) > 0) {
|
||||
throw new IllegalArgumentException("重量范围为 0-30kg");
|
||||
}
|
||||
|
||||
try {
|
||||
return grams.shortValueExact();
|
||||
} catch (ArithmeticException e) {
|
||||
throw new IllegalArgumentException("重量最多精确到 0.001kg");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
package com.example.pdaplccontrol;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class Moka7BagNumberSenderTest {
|
||||
|
||||
@Test
|
||||
public void encodeBagNumber_oneToNine_usesS7IntBigEndianBytes() {
|
||||
for (short bagNumber = 1; bagNumber <= 9; bagNumber++) {
|
||||
byte[] data = Moka7BagNumberSender.encodeBagNumber(bagNumber);
|
||||
|
||||
assertEquals(PlcConfig.BAG_NUMBER_BYTE_LENGTH, data.length);
|
||||
assertEquals(0, data[0]);
|
||||
assertEquals(bagNumber, data[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package com.example.pdaplccontrol;
|
||||
|
||||
import Moka7.S7;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class Moka7PlcValueSenderTest {
|
||||
|
||||
@Test
|
||||
public void encodeWord_validPositiveValues_usesS7BigEndianBytes() {
|
||||
short[] values = new short[] {10, 18, 0, 12345, 30000};
|
||||
|
||||
for (short value : values) {
|
||||
byte[] data = Moka7PlcValueSender.encodeWord(value);
|
||||
|
||||
assertEquals(PlcConfig.WORD_BYTE_LENGTH, data.length);
|
||||
assertEquals(value, (short) S7.GetShortAt(data, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue