添加读取监控
parent
4dc308f5e5
commit
56fdf39d8e
@ -0,0 +1,20 @@
|
|||||||
|
package com.example.pdaplccontrol;
|
||||||
|
|
||||||
|
final class PlcCurrentValues {
|
||||||
|
|
||||||
|
private final short lineNumber;
|
||||||
|
private final short weightKg;
|
||||||
|
|
||||||
|
PlcCurrentValues(short lineNumber, short weightKg) {
|
||||||
|
this.lineNumber = lineNumber;
|
||||||
|
this.weightKg = weightKg;
|
||||||
|
}
|
||||||
|
|
||||||
|
short getLineNumber() {
|
||||||
|
return lineNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
short getWeightKg() {
|
||||||
|
return weightKg;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,36 +1,30 @@
|
|||||||
package com.example.pdaplccontrol;
|
package com.example.pdaplccontrol;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
final class WeightInputParser {
|
final class WeightInputParser {
|
||||||
|
|
||||||
private static final BigDecimal GRAMS_PER_KG = new BigDecimal("1000");
|
|
||||||
|
|
||||||
private WeightInputParser() {
|
private WeightInputParser() {
|
||||||
}
|
}
|
||||||
|
|
||||||
static short kgToGrams(String input) {
|
static short parseKg(String input) {
|
||||||
if (input == null || input.trim().isEmpty()) {
|
if (input == null || input.trim().isEmpty()) {
|
||||||
throw new IllegalArgumentException("请输入重量");
|
throw new IllegalArgumentException("请输入重量");
|
||||||
}
|
}
|
||||||
|
|
||||||
BigDecimal kg;
|
int kg;
|
||||||
try {
|
try {
|
||||||
kg = new BigDecimal(input.trim());
|
kg = Integer.parseInt(input.trim());
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw new IllegalArgumentException("重量格式不正确");
|
throw new IllegalArgumentException("重量必须为整数kg");
|
||||||
}
|
}
|
||||||
|
|
||||||
BigDecimal grams = kg.movePointRight(3).stripTrailingZeros();
|
if (kg < PlcConfig.MIN_WEIGHT_KG || kg > PlcConfig.MAX_WEIGHT_KG) {
|
||||||
if (kg.compareTo(BigDecimal.ZERO) < 0
|
|
||||||
|| grams.compareTo(BigDecimal.valueOf(PlcConfig.MAX_WEIGHT_GRAMS)) > 0) {
|
|
||||||
throw new IllegalArgumentException("重量范围为 0-30kg");
|
throw new IllegalArgumentException("重量范围为 0-30kg");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
return (short) kg;
|
||||||
return grams.shortValueExact();
|
}
|
||||||
} catch (ArithmeticException e) {
|
|
||||||
throw new IllegalArgumentException("重量最多精确到 0.001kg");
|
static String kgToDisplayText(short weightKg) {
|
||||||
}
|
return String.valueOf(weightKg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue