| | |
| | | package com.doumee.keyCabinet.ui.keyCabinet; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.annotation.Nullable; |
| | | import androidx.appcompat.app.AppCompatActivity; |
| | | import androidx.lifecycle.Observer; |
| | | |
| | | import android.os.Bundle; |
| | | import android.os.Handler; |
| | | import android.os.Message; |
| | | import android.text.SpannableString; |
| | | import android.text.Spanned; |
| | | import android.text.TextUtils; |
| | | import android.text.style.TextAppearanceSpan; |
| | | import android.view.View; |
| | | import android.widget.TextView; |
| | | import android.widget.Toast; |
| | | |
| | | import com.doumee.keyCabinet.MApplication; |
| | | import com.doumee.keyCabinet.R; |
| | | import com.doumee.keyCabinet.base.MyBaseActivity; |
| | | import com.doumee.keyCabinet.bean.KeyCabinetBean; |
| | | import com.doumee.keyCabinet.dao.CabinetGridDo; |
| | | import com.doumee.keyCabinet.dao.DaoManager; |
| | | import com.doumee.keyCabinet.databinding.KeyCabinetActivityBinding; |
| | | import com.doumee.keyCabinet.ui.face.adapter.ErrRcvAdapter; |
| | | import com.doumee.keyCabinet.event.CloseGridOneResultEvent; |
| | | import com.doumee.keyCabinet.event.KeyResultEvent; |
| | | import com.doumee.keyCabinet.event.GetKeyStatusEvent; |
| | | import com.doumee.keyCabinet.event.OpenGridEvent; |
| | | import com.doumee.keyCabinet.event.OpenGridOneResultEvent; |
| | | import com.doumee.keyCabinet.ui.keyCabinet.adapter.CabinetRcvAdapter; |
| | | import com.doumee.lib_coremodel.base.BaseActivity; |
| | | import com.doumee.keyCabinet.utils.i485.SportUtils; |
| | | import com.doumee.lib_coremodel.bean.event.ActionEventData; |
| | | import com.doumee.lib_coremodel.util.RecyclerHelp; |
| | | import com.doumee.lib_coremodel.util.SpUtil; |
| | | import com.doumee.lib_coremodel.util.StringUtil; |
| | | |
| | | import org.greenrobot.eventbus.EventBus; |
| | | import org.greenrobot.eventbus.Subscribe; |
| | | import org.greenrobot.eventbus.ThreadMode; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | |
| | | import dagger.hilt.android.AndroidEntryPoint; |
| | | |
| | | @AndroidEntryPoint |
| | | public class KeyCabinetActivity extends MyBaseActivity<KeyCabinetVM, KeyCabinetActivityBinding> { |
| | | private CabinetRcvAdapter adapter; |
| | | private KeyCabinetBean selectBean; |
| | | //0=取 1=还 |
| | | private int flag; |
| | | |
| | | //状态,0:待开门,1:待关门 |
| | | private int status; |
| | | |
| | | private int ysDownCount = 10; |
| | | |
| | | @Override |
| | | public int getLayoutId() { |
| | |
| | | |
| | | @Override |
| | | public void initView(@Nullable Bundle savedInstanceState) { |
| | | isAddCountTimer = false; |
| | | isAddCountTimer = true; |
| | | normalConfig(); |
| | | getDB().setModel(getVM()); |
| | | |
| | | flag = MApplication.getLoginBean().getFlag(); |
| | | adapter = new CabinetRcvAdapter(this, R.layout.cabinet_rcv_item, new CabinetRcvAdapter.OnItemClick() { |
| | | @Override |
| | | public void onItemClick(KeyCabinetBean item) { |
| | | |
| | | selectBean = item; |
| | | } |
| | | }); |
| | | RecyclerHelp.bindVG(this,getDB().rcvKm,3,30); |
| | |
| | | |
| | | @Override |
| | | public void initData(@Nullable Bundle savedInstanceState) { |
| | | getVM().getDatasLD().observe(this, new Observer<List<KeyCabinetBean>>() { |
| | | @Override |
| | | public void onChanged(List<KeyCabinetBean> beans) { |
| | | if(beans.size()>0){ |
| | | beans.get(0).getIsSelected().set(true); |
| | | beans.get(0).getText1Color().set(0xffffffff); |
| | | beans.get(0).getText2Color().set(0xffffffff); |
| | | selectBean = beans.get(0); |
| | | changeBtBg(); |
| | | adapter.refreshData(beans); |
| | | getVM().emptyVisibility.set(View.GONE); |
| | | //更新信息 |
| | | updateDao(beans); |
| | | }else { |
| | | getVM().emptyVisibility.set(View.VISIBLE); |
| | | } |
| | | } |
| | | }); |
| | | getVM().getDatas(); |
| | | } |
| | | |
| | | private void updateDao(List<KeyCabinetBean> beans){ |
| | | List<CabinetGridDo> gridDos = DaoManager.getCabinetGridDao().loadAll(); |
| | | HashMap<String,CabinetGridDo> oldMap = new HashMap<>(); |
| | | for(CabinetGridDo d:gridDos){ |
| | | oldMap.put(d.getGridKey(),d); |
| | | } |
| | | List<CabinetGridDo> addList = new ArrayList<>(); |
| | | List<CabinetGridDo> updateList = new ArrayList<>(); |
| | | for(KeyCabinetBean bean:beans){ |
| | | String key = SportUtils.intToHex(Integer.parseInt(bean.getBoardCode()))+SportUtils.intToHex(Integer.parseInt(bean.getChannelCode())); |
| | | if(oldMap.containsKey(key)){ |
| | | //修改 |
| | | CabinetGridDo gridDo = oldMap.get(key); |
| | | gridDo.setGridId(bean.getId()); |
| | | gridDo.setCabinetName(bean.getCode()); |
| | | gridDo.setKeyCode(bean.getKeyCode()); |
| | | gridDo.setUpdateTime(StringUtil.DateToStr(new Date())); |
| | | gridDo.setBindStatus(bean.getBindStatus()); |
| | | gridDo.setGridStatus(bean.getStatus()); |
| | | gridDo.setKeyStatus(bean.getKeyStatus()); |
| | | gridDo.setWorkingStatus(bean.getWorkingStatus()); |
| | | updateList.add(gridDo); |
| | | }else { |
| | | //新增 |
| | | CabinetGridDo gridDo = new CabinetGridDo(); |
| | | gridDo.setGridId(bean.getId()); |
| | | gridDo.setCabinetName(bean.getCode()); |
| | | gridDo.setGridKey(key); |
| | | gridDo.setKeyCode(bean.getKeyCode()); |
| | | gridDo.setBindStatus(bean.getBindStatus()); |
| | | gridDo.setGridStatus(bean.getStatus()); |
| | | gridDo.setKeyStatus(bean.getKeyStatus()); |
| | | gridDo.setWorkingStatus(bean.getWorkingStatus()); |
| | | gridDo.setUpdateTime(StringUtil.DateToStr(new Date())); |
| | | addList.add(gridDo); |
| | | } |
| | | } |
| | | if(addList.size()>0){ |
| | | DaoManager.getCabinetGridDao().insert(addList); |
| | | } |
| | | if(updateList.size()>0){ |
| | | CabinetGridDo[] upList = new CabinetGridDo[updateList.size()]; |
| | | for(int i=0;i<updateList.size();i++){ |
| | | upList[i] = updateList.get(i); |
| | | } |
| | | DaoManager.getCabinetGridDao().update(upList); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void timeChange(String djs) { |
| | | getDB().tvDjs.setText(djs); |
| | | } |
| | | |
| | | private boolean isOpening = false; |
| | | @Override |
| | | protected void doRegister(int type, ActionEventData data) { |
| | | stopCountTimer(); |
| | | downTime = 30; |
| | | switch (type){ |
| | | case 1: |
| | | if(selectBean==null){ |
| | | Toast.makeText(mContext, "请选择柜门", Toast.LENGTH_SHORT).show(); |
| | | return; |
| | | } |
| | | if(isOpening){ |
| | | return; |
| | | } |
| | | isOpening = true; |
| | | //开启柜门 |
| | | getVM().openGridDriver(selectBean); |
| | | downTime = 60; |
| | | break; |
| | | case 2: |
| | | getDB().clTip1.setVisibility(View.GONE); |
| | | if(status==0) { |
| | | //重新开门 |
| | | openTime = StringUtil.DateToStr(new Date()); |
| | | EventBus.getDefault().post(new OpenGridEvent(selectBean.getChannelCode(), selectBean.getBoardCode())); |
| | | } |
| | | downTime = 60; |
| | | break; |
| | | case 3: |
| | | isOpening = false; |
| | | //开启失败 |
| | | getDB().btTip.setText("重新开门"); |
| | | getDB().imgJg1.setImageResource(R.mipmap.ic_jjfail); |
| | | getDB().tvJg1.setText("开门失败,请联系管理员"); |
| | | getDB().tvJg2.setText(getErrPhoneText(),TextView.BufferType.SPANNABLE); |
| | | getDB().btTip.setVisibility(View.VISIBLE); |
| | | getDB().clTip1.setVisibility(View.VISIBLE); |
| | | getDB().clTip2.setVisibility(View.GONE); |
| | | break; |
| | | case 4: |
| | | isOpening = false; |
| | | //开启成功 |
| | | status=1; |
| | | if("0".equals(flag)) { |
| | | getDB().imgTip2.setImageResource(R.mipmap.ic_notclose_key); |
| | | getDB().tvTip22.setText("请领取钥匙后关闭柜门"); |
| | | }else { |
| | | getDB().imgTip2.setImageResource(R.mipmap.ic_notclose); |
| | | getDB().tvTip22.setText("请归还钥匙后关闭柜门"); |
| | | } |
| | | getDB().tvTip21.setText(getTipText(selectBean.getCode(),"柜门打开成功"), TextView.BufferType.SPANNABLE); |
| | | getDB().clTip1.setVisibility(View.GONE); |
| | | getDB().clTip2.setVisibility(View.VISIBLE); |
| | | EventBus.getDefault().post(new GetKeyStatusEvent()); |
| | | ysDownCount = 8; |
| | | break; |
| | | case 7: |
| | | //提醒关柜门 |
| | | getDB().imgTip2.setImageResource(R.mipmap.ic_notclose); |
| | | getDB().tvTip22.setText("请关闭柜门"); |
| | | getDB().tvTip21.setText(getTipText(selectBean.getCode(),"柜门未关闭"), TextView.BufferType.SPANNABLE); |
| | | getDB().clTip2.setVisibility(View.VISIBLE); |
| | | getDB().clTip1.setVisibility(View.GONE); |
| | | break; |
| | | case 5: |
| | | //关门-钥匙领取成功 |
| | | status = 0; |
| | | getDB().imgJg1.setImageResource(R.mipmap.ic_success); |
| | | getDB().tvJg1.setText(getTipText(selectBean.getCarCode(),"钥匙领取成功"), TextView.BufferType.SPANNABLE); |
| | | getDB().tvJg2.setText("柜门已关闭"); |
| | | getDB().btTip.setVisibility(View.GONE); |
| | | getDB().clTip1.setVisibility(View.VISIBLE); |
| | | getDB().clTip2.setVisibility(View.GONE); |
| | | downTime = 10; |
| | | break; |
| | | case 6: |
| | | //关门-钥匙领取失败 |
| | | status = 0; |
| | | getDB().btTip.setText("重新开门"); |
| | | getDB().imgJg1.setImageResource(R.mipmap.ic_jjfail); |
| | | getDB().tvJg1.setText(getTipText(selectBean.getCarCode(),"钥匙未领取"), TextView.BufferType.SPANNABLE); |
| | | getDB().tvJg2.setText("柜门已关闭"); |
| | | getDB().btTip.setVisibility(View.VISIBLE); |
| | | getDB().clTip1.setVisibility(View.VISIBLE); |
| | | getDB().clTip2.setVisibility(View.GONE); |
| | | break; |
| | | case 10: |
| | | status = 0; |
| | | getDB().imgJg1.setImageResource(R.mipmap.ic_jjfail); |
| | | getDB().tvJg1.setText("钥匙存放位置错误"); |
| | | getDB().tvJg2.setText(getErrPhoneText(),TextView.BufferType.SPANNABLE); |
| | | getDB().btTip.setVisibility(View.GONE); |
| | | getDB().clTip1.setVisibility(View.VISIBLE); |
| | | getDB().clTip2.setVisibility(View.GONE); |
| | | break; |
| | | case 8: |
| | | //关门-钥匙归还成功 |
| | | status = 0; |
| | | getDB().imgJg1.setImageResource(R.mipmap.ic_success); |
| | | getDB().tvJg1.setText(getTipText(selectBean.getCarCode(),"钥匙归还成功"), TextView.BufferType.SPANNABLE); |
| | | getDB().tvJg2.setText("柜门已关闭"); |
| | | getDB().btTip.setVisibility(View.GONE); |
| | | getDB().clTip1.setVisibility(View.VISIBLE); |
| | | getDB().clTip2.setVisibility(View.GONE); |
| | | downTime = 10; |
| | | break; |
| | | case 9: |
| | | //关门-钥匙归还失败 |
| | | status = 0; |
| | | getDB().btTip.setText("重新归还"); |
| | | getDB().imgJg1.setImageResource(R.mipmap.ic_jjfail); |
| | | getDB().tvJg1.setText("未识别到钥匙"); |
| | | getDB().tvJg2.setText(getErrPhoneText(), TextView.BufferType.SPANNABLE); |
| | | getDB().btTip.setVisibility(View.VISIBLE); |
| | | getDB().clTip1.setVisibility(View.VISIBLE); |
| | | getDB().clTip2.setVisibility(View.GONE); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | initCountTimer(); |
| | | } |
| | | |
| | | private SpannableString getTipText(String name,String msg){ |
| | | String text = name+msg; |
| | | SpannableString styledText = new SpannableString(text); |
| | | styledText.setSpan(new TextAppearanceSpan(this, R.style.style_tip1), 0, name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| | | styledText.setSpan(new TextAppearanceSpan(this,R.style.style_tip2), name.length()+1, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| | | return styledText; |
| | | } |
| | | |
| | | private SpannableString getErrPhoneText(){ |
| | | String phone = MApplication.getConfigBean().getLinkPhone(); |
| | | String text = "如有问题请联系管理员"+phone; |
| | | SpannableString styledText = new SpannableString(text); |
| | | styledText.setSpan(new TextAppearanceSpan(this, R.style.style_tip2), 0, text.length()-phone.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| | | styledText.setSpan(new TextAppearanceSpan(this,R.style.style_tip1), text.length()-phone.length()+1, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| | | return styledText; |
| | | } |
| | | |
| | | private void changeBtBg(){ |
| | | if(selectBean!=null){ |
| | | getDB().tvKm.setBackgroundResource(R.drawable.shape_r25_blue_bg); |
| | | }else { |
| | | getDB().tvKm.setBackgroundResource(R.drawable.shape_mb_bt_faile); |
| | | } |
| | | } |
| | | |
| | | @Subscribe(threadMode= ThreadMode.MAIN) |
| | | public void KeyResultEvent(KeyResultEvent e){ |
| | | if(!isFinishing()){ |
| | | //钥匙变更了 |
| | | if(selectBean!=null) { |
| | | String key = SportUtils.intToHex(Integer.parseInt(selectBean.getBoardCode())) |
| | | + SportUtils.intToHex(Integer.parseInt(selectBean.getChannelCode())); |
| | | //查询柜格 |
| | | CabinetGridDo gridDo = DaoManager.getCabinetGridDao().getGridByKey(key); |
| | | if (gridDo != null) { |
| | | if(flag==0){ |
| | | //取 |
| | | if(TextUtils.isEmpty(gridDo.getCurKeyCode())){ |
| | | //空了 |
| | | doRegister(7,null); |
| | | } |
| | | }else { |
| | | //还 |
| | | if(!TextUtils.isEmpty(gridDo.getCurKeyCode())){ |
| | | //有钥匙了 |
| | | doRegister(7,null); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | | super.onDestroy(); |
| | | } |
| | | |
| | | private String openTime = ""; |
| | | @Subscribe(threadMode= ThreadMode.MAIN) |
| | | public void OpenGridOneResultEvent(OpenGridOneResultEvent e){ |
| | | if(!isFinishing()){ |
| | | if(selectBean!=null){ |
| | | String key = SportUtils.intToHex(Integer.parseInt(selectBean.getBoardCode())) |
| | | +SportUtils.intToHex(Integer.parseInt(selectBean.getChannelCode())); |
| | | if(key.equals(e.getKey())) { |
| | | if (e.getIsOpen()==1) { |
| | | //开门成功 |
| | | if (isOpened) { |
| | | return; |
| | | } |
| | | isOpened = true; |
| | | Toast.makeText(mContext, "操作4", Toast.LENGTH_LONG).show(); |
| | | doRegister(4, null); |
| | | } else { |
| | | //失败 |
| | | //如果是钥匙不对继续开门 |
| | | CabinetGridDo gridDo = DaoManager.getCabinetGridDao().getGridByKey(key); |
| | | if (gridDo != null) { |
| | | if(gridDo.getUpdateTime().compareTo(openTime)<0){ |
| | | //开门前数据,不能使用 |
| | | EventBus.getDefault().post(new GetKeyStatusEvent()); |
| | | return; |
| | | } |
| | | if (!gridDo.getCurKeyCode().equals(selectBean.getKeyCode())) { |
| | | //钥匙不匹配 |
| | | doRegister(10,null); |
| | | EventBus.getDefault().post(new OpenGridEvent(selectBean.getChannelCode(), selectBean.getBoardCode())); |
| | | return; |
| | | } |
| | | } |
| | | doRegister(3, null); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private boolean isOpened = false; |
| | | @Subscribe(threadMode= ThreadMode.MAIN) |
| | | public void CloseGridOneResultEvent(CloseGridOneResultEvent e){ |
| | | if(!isFinishing()){ |
| | | if(selectBean!=null){ |
| | | String key = SportUtils.intToHex(Integer.parseInt(selectBean.getBoardCode())) |
| | | +SportUtils.intToHex(Integer.parseInt(selectBean.getChannelCode())); |
| | | if(key.equals(e.getKey())) { |
| | | stopCountTimer(); |
| | | if(flag==0) { |
| | | //领取 |
| | | if (e.getIsOpen() == 0) { |
| | | //关门成功 |
| | | //查询钥匙号 |
| | | CabinetGridDo gridDo = DaoManager.getCabinetGridDao().getGridByKey(key); |
| | | if (gridDo != null) { |
| | | if(gridDo.getUpdateTime().compareTo(openTime)<0){ |
| | | //开门前数据,不能使用 |
| | | EventBus.getDefault().post(new GetKeyStatusEvent()); |
| | | return; |
| | | } |
| | | //Toast.makeText(mContext, "1:"+gridDo.getKeyCode()+",2:"+selectBean.getKeyCode(), Toast.LENGTH_LONG).show(); |
| | | if (!TextUtils.isEmpty(gridDo.getCurKeyCode())) { |
| | | if (gridDo.getCurKeyCode().equals(selectBean.getKeyCode())) { |
| | | //领取失败 |
| | | isOpened = false; |
| | | doRegister(6, null); |
| | | getVM().closeGrid(gridDo); |
| | | }else { |
| | | //钥匙不匹配 |
| | | Toast.makeText(mContext, "操作10", Toast.LENGTH_LONG).show(); |
| | | doRegister(10,null); |
| | | EventBus.getDefault().post(new OpenGridEvent(selectBean.getChannelCode(), selectBean.getBoardCode())); |
| | | } |
| | | } else { |
| | | //钥匙拿走了 |
| | | isOpened = false; |
| | | doRegister(5, null); |
| | | getVM().closeGrid(gridDo); |
| | | } |
| | | }else { |
| | | Toast.makeText(mContext, "未查询到", Toast.LENGTH_LONG).show(); |
| | | } |
| | | } else { |
| | | //关门失败 |
| | | } |
| | | }else { |
| | | //归还 |
| | | if (e.getIsOpen() == 0) { |
| | | //关门成功 |
| | | //查询钥匙号 |
| | | CabinetGridDo gridDo = DaoManager.getCabinetGridDao().getGridByKey(key); |
| | | if (gridDo != null) { |
| | | if(gridDo.getUpdateTime().compareTo(openTime)<0){ |
| | | //开门前数据,不能使用 |
| | | EventBus.getDefault().post(new GetKeyStatusEvent()); |
| | | return; |
| | | } |
| | | //Toast.makeText(mContext, "1:"+gridDo.getKeyCode()+",2:"+selectBean.getKeyCode(), Toast.LENGTH_LONG).show(); |
| | | if (!TextUtils.isEmpty(gridDo.getCurKeyCode())) { |
| | | if (gridDo.getCurKeyCode().equals(selectBean.getKeyCode())) { |
| | | //相同归还成功 |
| | | isOpened = false; |
| | | doRegister(8, null); |
| | | getVM().closeGrid(gridDo); |
| | | }else { |
| | | //钥匙不匹配 |
| | | doRegister(10,null); |
| | | EventBus.getDefault().post(new OpenGridEvent(selectBean.getChannelCode(), selectBean.getBoardCode())); |
| | | } |
| | | } else { |
| | | //钥匙未归还 |
| | | isOpened = false; |
| | | doRegister(9, null); |
| | | getVM().closeGrid(gridDo); |
| | | } |
| | | }else { |
| | | Toast.makeText(mContext, "未查询到", Toast.LENGTH_LONG).show(); |
| | | } |
| | | } else { |
| | | //关门失败 |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |