BluetoothWeighDialog.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package com.example.chemical.weidith;
  2. import android.graphics.Color;
  3. import android.graphics.drawable.ColorDrawable;
  4. import android.os.Bundle;
  5. import android.text.Editable;
  6. import android.text.TextUtils;
  7. import android.view.Gravity;
  8. import android.view.View;
  9. import android.view.Window;
  10. import android.view.WindowManager;
  11. import android.widget.Toast;
  12. import androidx.appcompat.app.AppCompatDialog;
  13. import com.blankj.utilcode.util.LogUtils;
  14. import com.blankj.utilcode.util.ThreadUtils;
  15. import com.bumptech.glide.Glide;
  16. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  17. import com.bumptech.glide.request.RequestOptions;
  18. import com.example.chemical.ChemicalApp;
  19. import com.example.chemical.R;
  20. import com.example.chemical.databinding.DialogBluetoothWeighBinding;
  21. import com.example.chemical.ui.plan.add.AddActivity;
  22. import com.example.chemical.utils.bluetooth.BluetoothTool;
  23. import com.kongzue.dialogx.dialogs.PopTip;
  24. import com.kongzue.dialogx.dialogs.WaitDialog;
  25. import com.rc.httpcore.HttpConfig;
  26. import com.rc.httpcore.bean.HxpChemicalVo;
  27. import com.rc.httpcore.client.HttpTool;
  28. import org.json.JSONObject;
  29. import okhttp3.Response;
  30. public class BluetoothWeighDialog extends AppCompatDialog {
  31. private DialogBluetoothWeighBinding binding;
  32. private final AddActivity addActivity;
  33. private final HxpChemicalVo hxpChemicalVo;
  34. private DialogCallBack dialogCallBack;
  35. public BluetoothWeighDialog(AddActivity addActivity, HxpChemicalVo hxpChemicalVo, DialogCallBack dialogCallBack) {
  36. super(addActivity);
  37. this.addActivity = addActivity;
  38. this.hxpChemicalVo = hxpChemicalVo;
  39. this.dialogCallBack = dialogCallBack;
  40. }
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. requestWindowFeature(Window.FEATURE_NO_TITLE);
  45. binding = DialogBluetoothWeighBinding.inflate(getLayoutInflater());
  46. setContentView(binding.getRoot());
  47. Window window = getWindow();
  48. if (null != window) {
  49. window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  50. window.setGravity(Gravity.CENTER);
  51. window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
  52. }
  53. // 提示图片加载
  54. if (null != ChemicalApp.confs && ChemicalApp.confs.getWeighHintPicture() != null && !ChemicalApp.confs.getWeighHintPicture().isEmpty()) {
  55. Glide.with(addActivity)
  56. .load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.confs.getWeighHintPicture())
  57. .placeholder(R.mipmap.img_syt_cz)
  58. .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
  59. .into(binding.hintIV);
  60. }
  61. // 净含量
  62. binding.netWtET.setText(String.valueOf(hxpChemicalVo.getNetContent()));
  63. // 规格
  64. binding.specsTV.setText(hxpChemicalVo.getSpecNum() + hxpChemicalVo.getSpecUnit());
  65. // 蓝牙称
  66. ThreadUtils.executeByCached(weightTask);
  67. // 确认
  68. binding.confirmBT.setOnClickListener(v -> {
  69. Editable netWtETText = binding.netWtET.getText();
  70. if (null == netWtETText || TextUtils.isEmpty(netWtETText)) {
  71. binding.netWtET.setError("请输入净含量");
  72. return;
  73. }
  74. double netWt = -1.0;
  75. try {
  76. netWt = Double.parseDouble(netWtETText.toString());
  77. } catch (Exception e) {
  78. binding.netWtET.setError("请检查净含量内容正确");
  79. return;
  80. }
  81. if (netWt <= 0) {
  82. binding.netWtET.setError("请检查净含量内容正确");
  83. return;
  84. }
  85. // 净含量不可大于规格
  86. if (netWt > hxpChemicalVo.getSpecNum()) {
  87. binding.netWtET.setError("净含量不能大于规格");
  88. return;
  89. }
  90. // 净含量不可大于称重
  91. if (netWt > hxpChemicalVo.getWeigh()) {
  92. binding.netWtET.setError("净含量不能大于称重");
  93. return;
  94. }
  95. hxpChemicalVo.setNetContent(netWt);
  96. WaitDialog.show("校验中...");
  97. ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<String>() {
  98. @Override
  99. public String doInBackground() throws Throwable {
  100. Response response = HttpTool.addStockCheck(hxpChemicalVo);
  101. if (response.isSuccessful()) {
  102. String json = response.body().string();
  103. JSONObject jsonObject = new JSONObject(json);
  104. int code = jsonObject.getInt("code");
  105. if (code == 200) {
  106. return "ok";
  107. } else {
  108. return jsonObject.getString("message");
  109. }
  110. } else {
  111. return response.message();
  112. }
  113. }
  114. @Override
  115. public void onSuccess(String result) {
  116. WaitDialog.dismiss();
  117. if ("ok".equals(result)) {
  118. hxpChemicalVo.setJoinType(1);
  119. dialogCallBack.confirm(BluetoothWeighDialog.this);
  120. } else if (null == result || result.isEmpty()) {
  121. dialogCallBack.cancel(BluetoothWeighDialog.this, "称重失败,系统异常!");
  122. } else {
  123. PopTip.show(result);
  124. }
  125. }
  126. });
  127. });
  128. setCancelable(false);
  129. setCanceledOnTouchOutside(false);
  130. }
  131. @Override
  132. protected void onStop() {
  133. super.onStop();
  134. BluetoothTool.INSTANCE.disconnect();
  135. ThreadUtils.cancel(weightTask);
  136. }
  137. ThreadUtils.SimpleTask<Object> weightTask = new ThreadUtils.SimpleTask<Object>() {
  138. @Override
  139. public Object doInBackground() throws Throwable {
  140. BluetoothTool.INSTANCE.connect(bluetoothCallBack);
  141. return null;
  142. }
  143. @Override
  144. public void onSuccess(Object result) {
  145. }
  146. };
  147. public interface DialogCallBack {
  148. // 成功
  149. void confirm(BluetoothWeighDialog bluetoothWeighDialog);
  150. // 取消
  151. void cancel(BluetoothWeighDialog bluetoothWeighDialog, String msg);
  152. }
  153. BluetoothTool.BluetoothCallBack bluetoothCallBack = new BluetoothTool.BluetoothCallBack() {
  154. @Override
  155. public void callBack(String weight) {
  156. LogUtils.d(weight);
  157. hxpChemicalVo.setWeigh(Double.parseDouble(weight));
  158. ThreadUtils.runOnUiThread(() -> {
  159. binding.weighTV.setText(weight + "g");
  160. if (binding.weightRL.getVisibility() != View.VISIBLE) {
  161. binding.weightRL.setVisibility(View.VISIBLE);
  162. }
  163. if (binding.loadingLAV.getVisibility() == View.VISIBLE) {
  164. binding.loadingLAV.cancelAnimation();
  165. binding.loadingLAV.setVisibility(View.GONE);
  166. }
  167. });
  168. }
  169. @Override
  170. public void connectFailed(String msg) {
  171. dialogCallBack.cancel(BluetoothWeighDialog.this, msg);
  172. }
  173. };
  174. }