AdvanceParameterFragment.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.sunzee.ui.fragment;
  2. import android.annotation.SuppressLint;
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.os.Bundle;
  6. import android.support.annotation.NonNull;
  7. import android.support.annotation.Nullable;
  8. import android.support.v7.widget.GridLayoutManager;
  9. import android.support.v7.widget.RecyclerView;
  10. import android.util.Log;
  11. import android.view.LayoutInflater;
  12. import android.view.MotionEvent;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.view.inputmethod.InputMethodManager;
  16. import com.hboxs.serialport.SerialPortDevice;
  17. import com.hboxs.serialport.SerialPortManager;
  18. import com.hboxs.serialport.frame.ResponseFrame;
  19. import com.hboxs.serialport.frame.WriteCommandFrame;
  20. import com.hboxs.serialport.message.Message;
  21. import com.hboxs.serialport.util.AsciiUtils;
  22. import com.hboxs.serialport.util.ByteUtils;
  23. import com.hboxs.serialport.util.HexUtils;
  24. import com.sunzee.R;
  25. import com.sunzee.adapter.AdvanceParameterAdapter;
  26. import com.sunzee.base.BaseApplication;
  27. import com.sunzee.base.MvpFragment;
  28. import com.sunzee.mvp.advanceparameter.AdvanceParameterPresenter;
  29. import com.sunzee.mvp.advanceparameter.AdvanceParameterView;
  30. import com.sunzee.thread.advanceparameter.ThreadPoolAdvanceParameter;
  31. import com.sunzee.utils.HexadecimalUtil;
  32. import com.sunzee.utils.PreventSpeedClickUtil;
  33. import com.sunzee.utils.ToastUtil;
  34. import org.greenrobot.eventbus.EventBus;
  35. import org.greenrobot.eventbus.Subscribe;
  36. import org.greenrobot.eventbus.ThreadMode;
  37. import java.util.ArrayList;
  38. /**
  39. * 进阶参数界面 fragment
  40. */
  41. public class AdvanceParameterFragment extends MvpFragment<AdvanceParameterPresenter> implements AdvanceParameterView {
  42. private RecyclerView mRvCrrency;
  43. private AdvanceParameterAdapter mAdvanceParameterAdapter;
  44. private static final String TAG = "AdvanceParameterFragmen";
  45. private ThreadPoolAdvanceParameter mPoolAdvanceParameter = new ThreadPoolAdvanceParameter();
  46. private PreventSpeedClickUtil mPreventSpeedClickUtil;
  47. @Nullable
  48. @Override
  49. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  50. View inflate = inflater.inflate(R.layout.fragment_general_parameter, container, false);
  51. initView(inflate);
  52. initEvent();
  53. return inflate;
  54. }
  55. @Override
  56. public void onStart() {
  57. super.onStart();
  58. Log.d(TAG, "onStart: ");
  59. EventBus.getDefault().register(this);
  60. showLoading();
  61. mPoolAdvanceParameter.stopAll();
  62. mPoolAdvanceParameter.startALLRead();
  63. }
  64. @Override
  65. public void onDestroyView() {
  66. EventBus.getDefault().unregister(this);
  67. mPoolAdvanceParameter.stopAll();
  68. Log.d(TAG, "onDestroyView: ");
  69. super.onDestroyView();
  70. }
  71. private void initEvent() {
  72. mAdvanceParameterAdapter.setItemListener(new AdvanceParameterAdapter.ItemParaClickListener() {
  73. @Override
  74. public void onItemListener(View view, int position, String text, String paraAddress) { //防止快速点击
  75. /**
  76. * 为什么每次开启之前都需要关掉呢?因为有可能收不到数据,没有停止,那么我们有必要开启之前就关掉。
  77. */
  78. //防止快速点击
  79. if (!mPreventSpeedClickUtil.isFastClick()) {
  80. return;
  81. }
  82. mPoolAdvanceParameter.stopWrite();
  83. showLoading();
  84. View v = mRvCrrency.getChildAt(position);
  85. if (null != mRvCrrency.getChildViewHolder(v)) {
  86. String data = ByteUtils.decimal2fitHex(Integer.valueOf(text));
  87. mPoolAdvanceParameter.startWrite(paraAddress, HexUtils.hexStr2BinStr(data));
  88. }
  89. }
  90. });
  91. mRvCrrency.setOnTouchListener(new View.OnTouchListener() {
  92. @SuppressLint("ClickableViewAccessibility")
  93. @Override
  94. public boolean onTouch(View v, MotionEvent event) {
  95. Log.d(TAG, "onClick: ");
  96. //隐藏键盘
  97. @SuppressLint("WrongConstant") InputMethodManager imm = (InputMethodManager)
  98. mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
  99. imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
  100. return false;
  101. }
  102. });
  103. }
  104. private void initView(View inflate) {
  105. mRvCrrency = inflate.findViewById(R.id.rv_currency);
  106. mAdvanceParameterAdapter = new AdvanceParameterAdapter();
  107. GridLayoutManager layoutManager = new GridLayoutManager(BaseApplication.getContext(), 9, GridLayoutManager.HORIZONTAL, false);
  108. mRvCrrency.setAdapter(mAdvanceParameterAdapter);
  109. mRvCrrency.setLayoutManager(layoutManager);
  110. mPreventSpeedClickUtil = new PreventSpeedClickUtil();
  111. }
  112. @Override
  113. protected AdvanceParameterPresenter createPresenter() {
  114. return new AdvanceParameterPresenter(this);
  115. }
  116. //--------------------------------------------------loading start------------------
  117. @Override
  118. public void showLoading() {
  119. showProgressDialog();
  120. }
  121. @Override
  122. public void hideLoading() {
  123. dismissProgressDialog();
  124. }
  125. public ProgressDialog progressDialog;
  126. public ProgressDialog showProgressDialog() {
  127. progressDialog = new ProgressDialog(mActivity);
  128. progressDialog.setMessage("加载中");
  129. progressDialog.show();
  130. return progressDialog;
  131. }
  132. public void dismissProgressDialog() {
  133. if (progressDialog != null && progressDialog.isShowing()) {
  134. // progressDialog.hide();会导致android.view.WindowLeaked
  135. progressDialog.dismiss();
  136. }
  137. }
  138. //--------------------------------------------------loading end------------------
  139. @Subscribe(threadMode = ThreadMode.MAIN)
  140. public void event(Message messageEvent) {
  141. switch (messageEvent.getType()) {
  142. case connected:
  143. break;
  144. case sendError:
  145. break;
  146. case ack:
  147. Log.d(TAG, "event: 吸入成功");
  148. ToastUtil.showToast("更新成功");
  149. Log.d(TAG, "event: 写入成功");
  150. mPoolAdvanceParameter.stopWrite();
  151. //todo 上传
  152. mvpPresenter.updateAdvanced(mAdvanceParameterAdapter.getParameterBeans());
  153. break;
  154. case nak:
  155. break;
  156. case disconnected:
  157. SerialPortDevice device = new SerialPortDevice("/dev/ttyS2", "9600");
  158. SerialPortManager.getInstance().open(device);
  159. break;
  160. case response:
  161. ResponseFrame responseFrame = (ResponseFrame) messageEvent.getContent();
  162. String name = messageEvent.getName();
  163. if ("M600".equals(name)) {
  164. return;
  165. }
  166. if (responseFrame.isValidSum()) {
  167. //通过校验
  168. Log.d(TAG, "event: " + name);
  169. String result = AsciiUtils.asciiByteArray2HexStr(responseFrame.getData());
  170. ArrayList<String> results = HexadecimalUtil.flipString(result);
  171. mvpPresenter.setAdvanced(name, results, mPoolAdvanceParameter);
  172. }
  173. break;
  174. }
  175. }
  176. @Override
  177. public void getDataSuccess() {
  178. mAdvanceParameterAdapter.setValues(mPoolAdvanceParameter.getValues());
  179. mAdvanceParameterAdapter.notifyDataSetChanged();
  180. //todo 上传
  181. mvpPresenter.updateAdvanced(mAdvanceParameterAdapter.getParameterBeans());
  182. hideLoading();
  183. }
  184. }