diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/DishPartAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/DishPartAdapter.kt index 5d0fd31..c20ecc3 100644 --- a/app/src/main/java/com/shuwei/dish/match/adapter/DishPartAdapter.kt +++ b/app/src/main/java/com/shuwei/dish/match/adapter/DishPartAdapter.kt @@ -9,6 +9,7 @@ import com.chad.library.adapter4.viewholder.QuickViewHolder import com.shuwei.dish.match.R import com.shuwei.dish.match.databinding.ListItemDishCookBinding import com.shuwei.dish.match.entity.CookFoodGoodsEntity +import com.shuwei.dish.match.utils.ext.roundedOneDecimalPlace import java.text.DecimalFormat class DishPartAdapter(list: MutableList) : @@ -26,8 +27,9 @@ class DishPartAdapter(list: MutableList) : holder.binding.run { tvDishName.text = item!!.goodsName tvDishType.text = if (item.materialType == 1) "主辅材:主材" else if (item.materialType == 2) "主辅材:辅材" else "" + //"${DecimalFormat("#").format(item.useWeight)}克" tvDishWeight.text = - if (item.useWeight == null || item.useWeight == 0.toDouble()) "" else "${DecimalFormat("#").format(item.useWeight)}克" + if (item.useWeight == null || item.useWeight == 0.toDouble()) "" else "${item.useWeight!!.roundedOneDecimalPlace()}克" tvDishWeight.setTextColor( ContextCompat.getColor( context, diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/TextCellAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/TextCellAdapter.kt index 4437834..58c90b6 100644 --- a/app/src/main/java/com/shuwei/dish/match/adapter/TextCellAdapter.kt +++ b/app/src/main/java/com/shuwei/dish/match/adapter/TextCellAdapter.kt @@ -23,6 +23,7 @@ import com.shuwei.dish.match.utils.ext.buildSpannableString import com.shuwei.dish.match.utils.ext.clickWithDebounce import com.shuwei.dish.match.utils.ext.dp import com.shuwei.dish.match.utils.ext.invisible +import com.shuwei.dish.match.utils.ext.roundedOneDecimalPlace import com.shuwei.dish.match.utils.ext.visible class TextCellAdapter(var list: MutableList) : @@ -123,7 +124,8 @@ class TextCellAdapter(var list: MutableList) : val weight = item.useWeight ?: 0.toDouble() val weightColor = if (weight == 0.toDouble()) "#999999" else "#00BC71" appendText( - "${DecimalFormat("#").format(weight)}g", +// "${DecimalFormat("#").format(weight)}g", + "${weight.roundedOneDecimalPlace()}g", ForegroundColorSpan(weightColor.toColorInt()), AbsoluteSizeSpan(30, true) ) diff --git a/app/src/main/java/com/shuwei/dish/match/base/BaseApp.kt b/app/src/main/java/com/shuwei/dish/match/base/BaseApp.kt index 5e01c3f..dd93a92 100644 --- a/app/src/main/java/com/shuwei/dish/match/base/BaseApp.kt +++ b/app/src/main/java/com/shuwei/dish/match/base/BaseApp.kt @@ -30,6 +30,7 @@ class BaseApp : Application() { var token: String? = null var deviceId: String? = null + var appVersion: String = "1" @Volatile private var sharedPref: SharedPreferences? = null diff --git a/app/src/main/java/com/shuwei/dish/match/db/dao/AppDao.kt b/app/src/main/java/com/shuwei/dish/match/db/dao/AppDao.kt index 72cbc38..19cf5e1 100644 --- a/app/src/main/java/com/shuwei/dish/match/db/dao/AppDao.kt +++ b/app/src/main/java/com/shuwei/dish/match/db/dao/AppDao.kt @@ -65,8 +65,11 @@ interface AppDao { @Query("SELECT * FROM dm_seasoning WHERE isDel = 0 AND goodsId = :goodsId") suspend fun getSeasoningByGoodsId(goodsId: Int): SeasoningEntity? +// @Query("SELECT * FROM dm_seasoning WHERE isDel = 0 AND goodsId != -1 AND goodsName != '' ORDER BY sort ASC") +// fun getAllStream(): Flow> + @Query("SELECT * FROM dm_seasoning WHERE isDel = 0 AND goodsId != -1 AND goodsName != '' ORDER BY sort ASC") - fun getAllStream(): Flow> + fun getAllStream(): MutableList @Query("SELECT * FROM dm_seasoning WHERE isDel = 0 AND goodsName LIKE '%' || :query || '%'") suspend fun search(query: String): MutableList diff --git a/app/src/main/java/com/shuwei/dish/match/dialog/BottomDialog2.kt b/app/src/main/java/com/shuwei/dish/match/dialog/BottomDialog2.kt index 11cc48f..dd22178 100644 --- a/app/src/main/java/com/shuwei/dish/match/dialog/BottomDialog2.kt +++ b/app/src/main/java/com/shuwei/dish/match/dialog/BottomDialog2.kt @@ -144,7 +144,7 @@ class BottomDialog2( binding.tvWeight.text = getTextSpan(weight) } }) - binding.tvWeight.text = getTextSpan(0) + binding.tvWeight.text = getTextSpan(0.0) if (clickName.isNullOrBlank().not()) { binding.etSheetInput.setText(clickName!!.trim()) @@ -225,11 +225,11 @@ class BottomDialog2( }) } - fun getTextSpan(weight: Int): SpannableStringBuilder { + fun getTextSpan(weight: Double): SpannableStringBuilder { var topWeight = "$weight" var bottomUnit = "克" if (weight >= 1000) { - topWeight = "${weight / 1000F}" + topWeight = "$weight" bottomUnit = "千克" } return buildSpannableString { diff --git a/app/src/main/java/com/shuwei/dish/match/ui/DeviceConfigActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/DeviceConfigActivity.kt index 29e5464..59d6ce4 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/DeviceConfigActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/DeviceConfigActivity.kt @@ -8,6 +8,7 @@ import android.text.style.ForegroundColorSpan import android.text.style.LineHeightSpan import android.text.style.StyleSpan import android.util.Log +import android.util.SparseArray import android.util.SparseIntArray import android.widget.FrameLayout import android.widget.TextView @@ -39,6 +40,7 @@ import com.shuwei.dish.match.databinding.ActivityDeviceConfigBinding import com.shuwei.dish.match.dialog.BottomDialog2 import com.shuwei.dish.match.utils.SpTool import com.shuwei.dish.match.utils.ext.clickWithDebounce +import com.shuwei.dish.match.utils.ext.gone class DeviceConfigActivity : BaseActivity() { @@ -65,7 +67,8 @@ class DeviceConfigActivity : BaseActivity() { private lateinit var appViewModel: AppViewModel private var cookMode: Int = 0 - private val weightArray = SparseIntArray() +// private val weightArray = SparseIntArray() + private val weightArray = SparseArray() private val addressArray = AddressUtil.getWeighAddressArray() override fun onCreate(savedInstanceState: Bundle?) { @@ -80,14 +83,14 @@ class DeviceConfigActivity : BaseActivity() { }, titleAction = { it.text = "设备配置" }, rightIconActon = { - it.visible() - it.alpha = 0.0F - it.setImageResource(R.drawable.ic_setting) - it.setOnClickListener { - detector.setOnDelayedMultiClickListener(it) { - defaultDataSettingDialog() - } - } + it.gone() +// it.alpha = 0.0F +// it.setImageResource(R.drawable.ic_setting) +// it.setOnClickListener {_-> +// detector.setOnDelayedMultiClickListener(it) { +// defaultDataSettingDialog() +// } +// } }) loadQualitySpan(isEnable = false) @@ -193,9 +196,11 @@ class DeviceConfigActivity : BaseActivity() { appViewModel.clearAllSeasoning { record?.list?.forEach { entity -> entity.useWeight = weightArray[addressArray[entity.sort]].toDouble() - appViewModel.saveSeasoning(entity) {} + appViewModel.saveSeasoning(entity) { + updateGridData(entity) + } } - loadSeasoning() +// loadSeasoning() } }.onFailure { it.printStackTrace() } } diff --git a/app/src/main/java/com/shuwei/dish/match/ui/InitActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/InitActivity.kt index f7f7a03..6e21d48 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/InitActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/InitActivity.kt @@ -28,6 +28,7 @@ import com.shuwei.dish.match.viewmodel.factory.AppFactory import kotlinx.coroutines.delay import kotlinx.coroutines.flow.flow import kotlinx.coroutines.launch +import kotlin.toString class InitActivity : BaseActivity() { companion object { @@ -47,35 +48,36 @@ class InitActivity : BaseActivity() { // setHeaderBackground(isHomePage = true) setHeaderBgVisible(false) + BaseApp.appVersion = AppUtil.getAppVersionCode(this).toString() -// // 获取 ANDROID_ID -// var androidId = -// Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID) -// androidId = "39a7abdd06b3c7ab" -// BaseApp.deviceId = androidId -// SpTool.put(SpTool.DEVICE_ID, androidId) -// BaseApp.configUrl = UrlConfig.BASE_URL -// BaseApp.canteenId = "0" + // 获取 ANDROID_ID + var androidId = + Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID) + androidId = "39a7abdd06b3c7ab" + BaseApp.deviceId = androidId + SpTool.put(SpTool.DEVICE_ID, androidId) + BaseApp.configUrl = UrlConfig.BASE_URL + BaseApp.canteenId = "0" // // TODO: 以上保存deviceId用于临时使用,后续改为下面注释方式 - var deviceId = AppUtil.getUDID(this) - Log.d(TAG, "onCreate: deviceId=$deviceId") -// deviceId = "39a7abdd06b3c7ab" - deviceId = "3ea47dc0-3cf0-3c2f-909c-265a9a65572e" -// deviceId = "6ce7cd59-b875-38b2-b73d-88a570be3212" - - BaseApp.deviceId = deviceId - - SpTool.put(SpTool.DEVICE_ID, deviceId) - val deviceConfigCache = SpTool.getString(SpTool.DEVICE_CONFIG_CACHE) - val checkResult = checkConfigData(deviceConfigCache) - if (checkResult.not()) { - binding.ivQrCode.visible() - binding.btnInit.visible() - //进行初始化操作 - initConfig() - return - } +// var deviceId = AppUtil.getUDID(this) +// Log.d(TAG, "onCreate: deviceId=$deviceId") +//// deviceId = "39a7abdd06b3c7ab" +// deviceId = "3ea47dc0-3cf0-3c2f-909c-265a9a65572e" +//// deviceId = "6ce7cd59-b875-38b2-b73d-88a570be3212" +// +// BaseApp.deviceId = deviceId +// +// SpTool.put(SpTool.DEVICE_ID, deviceId) +// val deviceConfigCache = SpTool.getString(SpTool.DEVICE_CONFIG_CACHE) +// val checkResult = checkConfigData(deviceConfigCache) +// if (checkResult.not()) { +// binding.ivQrCode.visible() +// binding.btnInit.visible() +// //进行初始化操作 +// initConfig() +// return +// } binding.ivQrCode.invisible() binding.btnInit.invisible() @@ -118,21 +120,21 @@ class InitActivity : BaseActivity() { if (it.isEmpty()) { //无调料信息打开采样历史列表页面,点击设置配置调料信息 startActivity() - finish() +// finish() return@loadSeasoning } appViewModel.countCookFood(cookMode = 1) { count -> if (count > 0) { //有烹饪中数据打开采样历史列表页面 startActivity() - finish() +// finish() return@countCookFood } //无烹饪数据打开新增采样页面 startActivity { putExtra(DishSamplingActivity.PAGE_FROM, DishSamplingActivity.HOME) } - finish() +// finish() } } } @@ -169,7 +171,7 @@ class InitActivity : BaseActivity() { } private fun getDeviceConfig() { - val tokenUrl = "${UrlConfig.DEVICE_TOKEN}?qrcodeId=${BaseApp.deviceId}&appVersion=1" + val tokenUrl = "${UrlConfig.DEVICE_TOKEN}?qrcodeId=${BaseApp.deviceId}&appVersion=${BaseApp.appVersion}" HttpUtil.get(url = tokenUrl, doSuccess = { token -> Log.d(TAG, "initConfig: $token") getConfig(token.toString()) @@ -178,7 +180,7 @@ class InitActivity : BaseActivity() { }) } private fun getConfig(token: String) { - val deviceConfigUrl = "${UrlConfig.DEVICE_CONFIG}?equipmentCode=${BaseApp.deviceId}&&appVersion=1" + val deviceConfigUrl = "${UrlConfig.DEVICE_CONFIG}?equipmentCode=${BaseApp.deviceId}&&appVersion=${BaseApp.appVersion}" HttpUtil.get(url = deviceConfigUrl, header = mutableMapOf( "X-Access-Token" to token ), doSuccess = { @@ -192,7 +194,7 @@ class InitActivity : BaseActivity() { HttpUtil.loopGetToken = true SpTool.put(SpTool.DEVICE_CONFIG_CACHE, data) startActivity() - finish() +// finish() }, doFailure = { code, msg -> Log.d(TAG, "getDeviceConfig: $code,$msg") toast("初始化设备失败,请稍后重试,code=${code},msg=${msg}") @@ -217,4 +219,9 @@ class InitActivity : BaseActivity() { return true } + override fun onDestroy() { + WeightUtil.stopContinuousRead() + super.onDestroy() + } + } \ No newline at end of file diff --git a/app/src/main/java/com/shuwei/dish/match/ui/PrepareCookActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/PrepareCookActivity.kt index ae8cbb7..19b02ca 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/PrepareCookActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/PrepareCookActivity.kt @@ -96,8 +96,8 @@ class PrepareCookActivity : BaseActivity() { return@setOnClickListener } } - val weight = binding.tvDishPartWeight.text.toString().toInt() - if (weight <= 0) { + val weight = binding.tvDishPartWeight.text.toString().toDouble() + if (weight <= 0.toDouble()) { toast("食材用量需要大于0") return@setOnClickListener } diff --git a/app/src/main/java/com/shuwei/dish/match/ui/SamplingListActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/SamplingListActivity.kt index 4233ece..e14c13c 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/SamplingListActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/SamplingListActivity.kt @@ -394,4 +394,15 @@ class SamplingListActivity : BaseActivity() { ) } + private var isVisible = false + override fun onResume() { + super.onResume() + isVisible = true + } + + override fun onPause() { + super.onPause() + isVisible = false + } + } \ No newline at end of file diff --git a/app/src/main/java/com/shuwei/dish/match/ui/SelectDishActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/SelectDishActivity.kt index ded516a..c6d521d 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/SelectDishActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/SelectDishActivity.kt @@ -191,4 +191,15 @@ class SelectDishActivity : BaseActivity() { // dialog = toast("不允许返回操作") } + private var isVisible = false + override fun onResume() { + super.onResume() + isVisible = true + } + + override fun onPause() { + super.onPause() + isVisible = false + } + } \ No newline at end of file diff --git a/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt index 9e34d0f..1fd23fb 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt @@ -27,6 +27,7 @@ import com.shuwei.dish.match.utils.AddressUtil import com.shuwei.dish.match.utils.WeightUtil import com.shuwei.dish.match.utils.ext.clickWithDebounce import com.shuwei.dish.match.utils.ext.gone +import com.shuwei.dish.match.utils.ext.roundedOneDecimalPlace import com.shuwei.dish.match.utils.ext.startActivity import com.shuwei.dish.match.utils.ext.toJsonString import com.shuwei.dish.match.utils.ext.toType @@ -151,9 +152,7 @@ class SubmitFoodActivity : BaseActivity() { } - private val firstGoodsArray by lazy { - SparseArray() - } + private val firstGoodsArray = SparseArray() private fun addViewListener() { addWeightListener() @@ -187,7 +186,7 @@ class SubmitFoodActivity : BaseActivity() { return@addWeightListener } var realUseWeight = firstWeight - weight - realUseWeight = if (realUseWeight > 0) realUseWeight else 0 + realUseWeight = if (realUseWeight > 0) realUseWeight else 0.0 seasoningArray.put(address, realUseWeight.toDouble()) // refreshSeasoningWeight() @@ -195,7 +194,7 @@ class SubmitFoodActivity : BaseActivity() { val item = seasoningItems.firstOrNull { address == addressArray[it.sort]} item?.let { val lastWeight = seasoningArray.get(address) ?: 0.toDouble() - it.useWeight = lastWeight + getCookingSeasoning(address) + it.useWeight = (lastWeight + getCookingSeasoning(address)).roundedOneDecimalPlace() updateGridData(it) } }) @@ -419,7 +418,10 @@ class SubmitFoodActivity : BaseActivity() { seasoningItems.clear() seasoningItems.addAll(list) initConfigData() - setGridData(list) +// setGridData(list) + list.forEach { entity -> + updateGridData(entity.also { it.useWeight = 0.0 }) + } seasoningCookingItems = seasoningItems.map { it.copy() }.toList() } diff --git a/app/src/main/java/com/shuwei/dish/match/ui/fragment/DishListFragment.kt b/app/src/main/java/com/shuwei/dish/match/ui/fragment/DishListFragment.kt index 812c3ad..5d10a83 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/fragment/DishListFragment.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/fragment/DishListFragment.kt @@ -55,6 +55,9 @@ class DishListFragment : BaseFragment() { DishShowAdapter(list = list).apply { isStateViewEnable = true setOnDebouncedItemClick { adapter, view, position -> + if (isAdded.not() || isVisible.not()) { + return@setOnDebouncedItemClick + } activity.judgeDeviceConfig { onItemClick(position) } diff --git a/app/src/main/java/com/shuwei/dish/match/utils/Weigher2.java b/app/src/main/java/com/shuwei/dish/match/utils/Weigher2.java new file mode 100644 index 0000000..4565a82 --- /dev/null +++ b/app/src/main/java/com/shuwei/dish/match/utils/Weigher2.java @@ -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); + } + + } + }; + //.(); + 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(); + } +} + diff --git a/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt b/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt index 2b357d8..cacd6c5 100644 --- a/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt +++ b/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt @@ -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 Unit)?>? = null + var weightFuncMap: MutableMap? = + 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) diff --git a/app/src/main/java/com/shuwei/dish/match/utils/ext/Common.kt b/app/src/main/java/com/shuwei/dish/match/utils/ext/Common.kt index b1fb8c8..4b50b1b 100644 --- a/app/src/main/java/com/shuwei/dish/match/utils/ext/Common.kt +++ b/app/src/main/java/com/shuwei/dish/match/utils/ext/Common.kt @@ -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() +} diff --git a/app/src/main/java/com/shuwei/dish/match/viewmodel/AppViewModel.kt b/app/src/main/java/com/shuwei/dish/match/viewmodel/AppViewModel.kt index 8455a9f..7d4d5fb 100644 --- a/app/src/main/java/com/shuwei/dish/match/viewmodel/AppViewModel.kt +++ b/app/src/main/java/com/shuwei/dish/match/viewmodel/AppViewModel.kt @@ -150,14 +150,13 @@ class AppViewModel(private val rep: AppRepository) : ViewModel() { // } private var isProcessing = false - fun loadSeasoning(action: (list: MutableList) -> Unit) { + fun loadSeasoning(action: (MutableList) -> Unit) { viewModelScope.launch { if (isProcessing) return@launch isProcessing = true - rep.getAllStream().collect { list -> - action(list) - isProcessing = false - } + val list = rep.getAllStream() + action(list) + isProcessing = false } } @@ -197,7 +196,8 @@ class AppViewModel(private val rep: AppRepository) : ViewModel() { } } entity.id = 0 - rep.insertSeasoning(entity) + val id = rep.insertSeasoning(entity) + entity.id = id block() } }