秤精度精确到0.1g并接口因此产生的数据问题;提交页面初始不为0优化;设置页面引起页面跳转问题优化;初始化版本使用项目versionCode

This commit is contained in:
2025-09-18 19:21:19 +08:00
parent d7dc3b3d17
commit c1d4263473
16 changed files with 589 additions and 90 deletions
@@ -0,0 +1,425 @@
package com.shuwei.dish.match.utils;
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
//
//package com.aithings;
import android.os.Build;
import android.util.Log;
import com.blankj.utilcode.util.Utils;
import com.t507.System;
import com.wabon.wbintelligenthardwaresdk.api.SensorScale;
import com.wabon.wbintelligenthardwaresdk.interf.CheckPortListener;
public class Weigher2 {
private static final String TAG = Weigher2.class.getSimpleName();
public static final int STATE_OVER_WEIGHT = 2;
public static final int STATE_STABLE = 1;
public static final int STATE_UNSTABLE = 0;
public static final int ERR_001 = 1001;
public static final int ERR_002 = 1002;
public static final int ERR_003 = 1003;
public static final int ERR_004 = 1004;
public static final int ERR_PCB_NOT_SUPPORT = 2000;
private static SensorScale mSensorScale;
private static String mDevicePort = "/dev/ttyS4";
private static boolean mConnect = false;
private static Listener mListener;
public static void init() {
String var0;
if ((var0 = Build.MODEL) == "pcb_941") {
Log.w(TAG, "pcb not support, " + var0);
Listener var1;
if ((var1 = mListener) != null) {
var1.onFail(2000);
}
} else {
System.init();
initScale();
if (var0 == "pcb_908") {
mDevicePort = "/dev/ttyS4";
}
mSensorScale.openScale(mDevicePort, 115200, (open) -> {
mConnect = open;
Log.d(TAG, "init " + mConnect);
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onInit(Weigher2.mConnect);
}
}
});
});
}
}
public static void unInit() {
// $FF: Couldn't be decompiled
}
public static void config() {
String var0;
if ((var0 = Build.MODEL) == "pcb_941") {
Log.w(TAG, "pcb not support, " + var0);
Listener var2;
if ((var2 = mListener) != null) {
var2.onFail(2000);
}
} else {
System.init();
initScale();
SensorScale var10000 = mSensorScale;
String var10001 = mDevicePort;
CheckPortListener var1 = (open) -> {
};
var10000.config(var10001, 115200, 5000L, var1);
}
}
public static boolean zero() {
mSensorScale.zero(() -> {
Log.d(TAG, "zero ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onZero();
}
}
});
});
return true;
}
public static boolean zeroTwo(int address) {
mSensorScale.zeroTwo(() -> {
Log.d(TAG, "zero ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onZero();
}
}
});
}, address);
return true;
}
public static boolean readParam() {
mSensorScale.readParam(() -> {
Log.d(TAG, "read param ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onReadParam();
}
}
});
});
return true;
}
public static boolean readParamTwo(int address) {
mSensorScale.readParamTwo(() -> {
Log.d(TAG, "read param ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onReadParam();
}
}
});
}, address);
return true;
}
public static boolean rangeCal() {
mSensorScale.rangeCalibration(() -> {
Log.d(TAG, "rang cal ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onRangCal();
}
}
});
});
return true;
}
public static boolean rangeCalValue(int value) {
mSensorScale.rangeCalibrationValue(value, () -> {
Log.d(TAG, "rang cal val ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onRangCal();
}
}
});
});
return true;
}
public static boolean rangeCalValueTwo(int value, int address) {
mSensorScale.rangeCalibrationValueTwo(value, () -> {
Log.d(TAG, "rang cal val ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onRangCal();
}
}
});
}, address);
return true;
}
public static void getWeight() {
mSensorScale.readWeight();
}
public static void startContinuousRead() {
mSensorScale.startContinuousRead();
}
public static void stopContinuousRead() {
mSensorScale.stopContinuousRead();
}
public static void tare() {
mSensorScale.tare(() -> {
Log.d(TAG, "tare ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onTare();
}
}
});
});
}
public static void tareTwo(int address) {
mSensorScale.tareTwo(() -> {
Log.d(TAG, "tare ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onTare();
}
}
});
}, address);
}
public static void readIdentify() {
mSensorScale.readIdentify();
}
public static void readIdentifyTwo(int address) {
mSensorScale.readIdentifyTwo(address);
}
public static void fastFilter() {
mSensorScale.fastFilter(() -> {
Log.d(TAG, "fast filter ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onFastFilter();
}
}
});
}, 0);
}
public static void fastFilter(int speed) {
mSensorScale.fastFilter(() -> {
Log.d(TAG, "fast filter ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onFastFilter();
}
}
});
}, speed);
}
public static void fastFilterTwo(int address) {
mSensorScale.fastFilterTwo(() -> {
Log.d(TAG, "fast filter ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onFastFilter();
}
}
});
}, address, 0);
}
public static void fastFilterTwo(int address, int speed) {
mSensorScale.fastFilterTwo(() -> {
Log.d(TAG, "fast filter ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onFastFilter();
}
}
});
}, address, speed);
}
public static void setParam(int address, int division, int speed, int maxWeight) {
mSensorScale.setParam(() -> {
Log.d(TAG, "setParam ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onSetParam();
}
}
});
}, address, division, speed, maxWeight);
}
public static void setParam(int division, int speed, int maxWeight) {
mSensorScale.setParam(() -> {
Log.d(TAG, "setParam ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onSetParam();
}
}
});
}, division, speed, maxWeight);
}
public static void setIdentify(int rate) {
mSensorScale.setIdentify(rate, () -> {
Log.d(TAG, "setIdentify ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onSetIdentify();
}
}
});
});
}
public static void setIdentifyTwo(int rate, int address) {
mSensorScale.setIdentifyTwo(rate, () -> {
Log.d(TAG, "setIdentify ok");
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onSetIdentify();
}
}
});
}, address);
}
public static void setListener(Listener listener) {
mListener = listener;
}
private static void initScale() {
SensorScale.OnScaleResult var0;
var0 = new SensorScale.OnScaleResult() {
public void readWeight(final int address, final int state, double value) {
if (SensorScale.isLog) {
Log.d(Weigher2.TAG, "readWeight, address=" + address + ", value=" + value);
}
// final int weight = (int)(value * (double)1000.0F);
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onGetWeight(address, state, value);
}
}
});
}
public void readIdentify(final int rate) {
Utils.runOnUiThread(new Runnable() {
public void run() {
if (Weigher2.mListener != null) {
Weigher2.mListener.onReadIdentify(rate);
}
}
});
}
public void fail(int errCode) {
if (Weigher2.mListener != null) {
Weigher2.mListener.onFail(errCode);
}
}
};
//.<init>();
mSensorScale = new SensorScale(var0);
}
public static void setNoPull485Pin(boolean noPull) {
SensorScale.no485Pin = noPull;
}
public interface Listener {
void onInit(boolean var1);
void onZero();
void onTare();
void onSetIdentify();
// void onGetWeight(int var1, int var2, int var3);
void onGetWeight(int var1, int var2, double var3);
void onReadIdentify(int var1);
void onFail(int var1);
void onFastFilter();
void onReadParam();
void onRangCal();
void onSetParam();
}
}
@@ -4,27 +4,39 @@ import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log
import com.aithings.Weigher
import com.shuwei.dish.match.utils.ext.roundedOneDecimalPlace
//import com.aithings.Weigher
import com.wabon.wbintelligenthardwaresdk.api.SensorScale
typealias WeightCallback = (address: Int, state: Int, weight: Double) -> Unit
object WeightUtil {
private const val TAG = "WeightUtil"
var isConnected = false
var weightFuncMap: MutableMap<String, ((address: Int, state: Int, weight: Int) -> Unit)?>? = null
var weightFuncMap: MutableMap<String, WeightCallback?>? =
null
fun init() {
Weigher.init()
Weigher.setListener(object : WeightListenerImpl() {
Weigher2.init()
Weigher2.setListener(object : WeightListenerImpl() {
override fun onInit(connect: Boolean) {
super.onInit(connect)
isConnected = connect
}
override fun onGetWeight(address: Int, state: Int, weight: Int) {
override fun onGetWeight(address: Int, state: Int, weight: Double) {
super.onGetWeight(address, state, weight)
val stateStr = when (state) {
SensorScale.STATE_STABLE -> "稳定"
SensorScale.STATE_UNSTABLE -> "不稳定"
SensorScale.STATE_OVER_WEIGHT -> "量程溢出"
else -> "$state"
}
val useWeight = (weight*1000).roundedOneDecimalPlace()
Log.d(TAG, "readWeight, address=$address,state=$stateStr, weight=$useWeight")
weightFuncMap?.forEach { (key, value) ->
value?.invoke(address, state, weight)
value?.invoke(address, state, useWeight)
}
}
@@ -32,15 +44,24 @@ object WeightUtil {
}
fun getWeight() {
Weigher.getWeight()
Weigher2.getWeight()
}
fun startContinuousRead() {
Weigher.startContinuousRead()
Weigher2.startContinuousRead()
}
fun stopContinuousRead() {
try {
Weigher2.stopContinuousRead()
Weigher2.unInit()
} catch (e: Exception) {
e.printStackTrace()
}
}
fun tareTwo(address: Int) {
Weigher.tareTwo(address)
Weigher2.tareTwo(address)
}
private fun runOnUiThread(action: () -> Unit) {
@@ -50,8 +71,8 @@ object WeightUtil {
}
fun addWeightListener(
weightKey:String,
getWeight: ((address: Int, state: Int, weight: Int) -> Unit) = { _, _, _ -> }
weightKey: String,
getWeight: WeightCallback = { _, _, _ -> }
) {
if (weightFuncMap == null) {
weightFuncMap = mutableMapOf()
@@ -60,7 +81,7 @@ object WeightUtil {
}
}
open class WeightListenerImpl : Weigher.Listener {
open class WeightListenerImpl : Weigher2.Listener {
override fun onInit(connect: Boolean) {
Log.d("WeightUtil", "connect=$connect")
}
@@ -77,15 +98,15 @@ open class WeightListenerImpl : Weigher.Listener {
Log.d("WeightUtil", "鉴别率设置操作成功")
}
override fun onGetWeight(address: Int, state: Int, weight: Int) {
val stateStr = when (state) {
SensorScale.STATE_STABLE -> "稳定"
SensorScale.STATE_UNSTABLE -> "不稳定"
SensorScale.STATE_OVER_WEIGHT -> "量程溢出"
else -> ""
}
val result1 = String.format("$stateStr 重量:%s kg", weight / 1000f)
Log.d("WeightUtil", "ttlReturn, address:$address,$result1")
override fun onGetWeight(address: Int, state: Int, weight: Double) {
// val stateStr = when (state) {
// SensorScale.STATE_STABLE -> "稳定"
// SensorScale.STATE_UNSTABLE -> "不稳定"
// SensorScale.STATE_OVER_WEIGHT -> "量程溢出"
// else -> ""
// }
// val result1 = String.format("$stateStr 重量:%s kg", weight / 1000f)
// Log.d("WeightUtil", "ttlReturn, address:$address,$result1")
}
override fun onReadIdentify(rate: Int) {
@@ -94,11 +115,11 @@ open class WeightListenerImpl : Weigher.Listener {
override fun onFail(errCode: Int) {
val str = when (errCode) {
Weigher.ERR_001 -> "电子称未初始化"
Weigher.ERR_002 -> "开机零位异常"
Weigher.ERR_003 -> "传感器故障"
Weigher.ERR_004 -> "鉴别率超出范围"
Weigher.ERR_PCB_NOT_SUPPORT -> "主板不支持, " + Build.MODEL
Weigher2.ERR_001 -> "电子称未初始化"
Weigher2.ERR_002 -> "开机零位异常"
Weigher2.ERR_003 -> "传感器故障"
Weigher2.ERR_004 -> "鉴别率超出范围"
Weigher2.ERR_PCB_NOT_SUPPORT -> "主板不支持, " + Build.MODEL
else -> ""
}
Log.d("WeightUtil", str)
@@ -22,6 +22,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.math.BigDecimal
import java.math.RoundingMode
fun Context.toast(
message: String?,
@@ -173,3 +175,7 @@ fun View.clickWithDebounce(delay: Long = 300, action: () -> Unit) {
}
}
}
fun Double.roundedOneDecimalPlace(): Double {
return BigDecimal(this).setScale(1, RoundingMode.HALF_UP).toDouble()
}