| | |
| | | |
| | | package com.lotaai.canguiayw.device.service; |
| | | |
| | | import android.app.Application; |
| | | import android.app.Service; |
| | | import android.content.Intent; |
| | | import android.os.Handler; |
| | | import android.os.IBinder; |
| | | |
| | | import androidx.annotation.Nullable; |
| | |
| | | import com.blankj.utilcode.util.ConvertUtils; |
| | | import com.blankj.utilcode.util.LogUtils; |
| | | import com.blankj.utilcode.util.ThreadUtils; |
| | | import com.lotaai.canguiayw.CThreadPoolExecutor; |
| | | import com.lotaai.canguiayw.common.SettingConfig; |
| | | import com.lotaai.canguiayw.device.CanGuiCommand; |
| | | import com.lotaai.canguiayw.device.CanguiStatus; |
| | |
| | | import com.lotaai.canguiayw.device.MessageType; |
| | | import com.lotaai.canguiayw.sqllitedb.GridModel; |
| | | import com.lotaai.canguiayw.sqllitedb.SqlliteDbManage; |
| | | import com.pranavpandey.android.dynamic.toasts.DynamicToast; |
| | | |
| | | import org.greenrobot.eventbus.EventBus; |
| | | import org.greenrobot.eventbus.Subscribe; |
| | |
| | | */ |
| | | public class CanGuiService extends Service { |
| | | |
| | | |
| | | private List<byte[]> commandQueue = new LinkedList<byte[]>(); |
| | | SerialPortUtils serialPortUtils = new SerialPortUtils(); |
| | | private HashMap<String,List<GridModel>> gridStatus = new HashMap<>(); |
| | | private HashMap<String, List<GridModel>> gridStatus = new HashMap<>(); |
| | | |
| | | @Nullable |
| | | @Override |
| | |
| | | public void onCreate() { |
| | | super.onCreate(); |
| | | // serialPortUtils.openSerialPort("/dev/ttyS4",4800); |
| | | String str = CacheDiskUtils.getInstance().getString(SettingConfig.getInstance().Cache_Device_Tty); |
| | | if (str == null || "".equals(str)){ |
| | | return; |
| | | |
| | | try { |
| | | String str = CacheDiskUtils.getInstance().getString(SettingConfig.getInstance().Cache_Device_Tty); |
| | | if (str == null || "".equals(str)) { |
| | | return; |
| | | } |
| | | LogUtils.i("串口号" + str); |
| | | serialPortUtils.openSerialPort(str, 4800); |
| | | registerListener(); |
| | | SettingConfig.getInstance().getExecutor().execute(selectRunnable); |
| | | SettingConfig.getInstance().getExecutor().execute(sendRunnable); |
| | | EventBus.getDefault().register(CanGuiService.this); |
| | | } catch (Exception e) { |
| | | /* CThreadPoolExecutor.runOnMainThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | DynamicToast.makeSuccess(getApplicationContext(), "捕获到错误:" + e.getMessage(), 3).show(); |
| | | } |
| | | });*/ |
| | | } |
| | | serialPortUtils.openSerialPort(str,4800); |
| | | registerListener(); |
| | | SettingConfig.getInstance().getExecutor().execute(selectRunnable); |
| | | SettingConfig.getInstance().getExecutor().execute(sendRunnable); |
| | | EventBus.getDefault().register(CanGuiService.this); |
| | | } |
| | | |
| | | //查询柜子的状态 |
| | |
| | | @Override |
| | | public void run() { |
| | | //开始之前先查询是不是已经有了 |
| | | for (int i = 0; i<20;i++) { |
| | | for (int i = 0; i < 20; i++) { |
| | | List<GridModel> lists = SqlliteDbManage.getInstance().selectGridByGui(i); |
| | | if (lists != null && lists.size() > 0) { |
| | | gridStatus.put(Integer.toString(i), lists); |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | while(true){ |
| | | while (true) { |
| | | try { |
| | | Thread.sleep(2000); |
| | | //查询指令 |
| | | byte[] command = CanGuiCommand.getInstance().selectStatusCommand(0,0,0,0,0,0,0,0); |
| | | byte[] command = CanGuiCommand.getInstance().selectStatusCommand(0, 0, 0, 0, 0, 0, 0, 0); |
| | | commandQueue.add(command); |
| | | } catch (InterruptedException e) { |
| | | LogUtils.e(e); |
| | | }catch (Exception e){ |
| | | } catch (Exception e) { |
| | | LogUtils.e(e); |
| | | } |
| | | } |
| | |
| | | Runnable sendRunnable = new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | while(true){ |
| | | while (true) { |
| | | try { |
| | | Thread.sleep(200); |
| | | //发送指令 |
| | | if (!commandQueue.isEmpty() && commandQueue.size()>0) { |
| | | byte[] cc= commandQueue.get(0); |
| | | if (cc!=null) { |
| | | if (!commandQueue.isEmpty() && commandQueue.size() > 0) { |
| | | byte[] cc = commandQueue.get(0); |
| | | if (cc != null) { |
| | | serialPortUtils.sendSerialPort(cc); |
| | | } |
| | | commandQueue.remove(0); |
| | | } |
| | | } catch (InterruptedException e) { |
| | | LogUtils.e(e); |
| | | }catch (Exception e){ |
| | | } catch (Exception e) { |
| | | /* CThreadPoolExecutor.runOnMainThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | DynamicToast.makeSuccess(getApplicationContext(), "捕获到错误2:" + e.getMessage(), 3).show(); |
| | | } |
| | | });*/ |
| | | LogUtils.e(e); |
| | | } |
| | | } |
| | | } |
| | | }; |
| | | |
| | | public void registerListener(){ |
| | | public void registerListener() { |
| | | serialPortUtils.setOnDataReceiveListener(new SerialPortUtils.OnDataReceiveListener() { |
| | | @Override |
| | | public void onDataReceive(byte[] buffer, int size) { |
| | | if (size >3){ |
| | | List<CanguiStatus> statuses = CanGuiCommand.getInstance().analysisRecvByte(buffer); |
| | | if (size > 3) { |
| | | List<CanguiStatus> statuses = CanGuiCommand.getInstance().analysisRecvByte(buffer); |
| | | |
| | | |
| | | boolean isHas = false; |
| | | int gui = -1; |
| | | if (statuses!=null &&statuses.size()>0){ |
| | | if (gridStatus.containsKey(Integer.toString(statuses.get(0).getGuiHao()))){ |
| | | if (statuses != null && statuses.size() > 0) { |
| | | if (gridStatus.containsKey(Integer.toString(statuses.get(0).getGuiHao()))) { |
| | | isHas = true; |
| | | } |
| | | for (int i = 0; i < statuses.size(); i++){ |
| | | |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (int i = 0; i < statuses.size(); i++) { |
| | | CanguiStatus s = statuses.get(i); |
| | | sb.append("柜子号" + s.getGuiHao() + " 格子号" + s.getGridNo()); |
| | | SettingConfig.getInstance().addStatusInList(s); |
| | | if (!isHas) { |
| | | GridModel model = new GridModel(); |
| | |
| | | // LogUtils.i("柜:" + s.getGuiHao(),"格:" + s.getGridNo(),"温度:" + s.getWendu(), |
| | | //// "门:"+s.isDoorIsOpen(),"加热:"+s.isJiareIsOpen(),"灯光:" + s.isDengGuangIsOpen(),"消毒:" + s.isXiaoDuIsOpen()); |
| | | } |
| | | if (gui > -1 && !isHas){ |
| | | |
| | | |
| | | |
| | | |
| | | if (gui > -1 && !isHas) { |
| | | List<GridModel> lists = SqlliteDbManage.getInstance().selectGridByGui(gui); |
| | | if (lists!=null && lists.size()>0){ |
| | | gridStatus.put(Integer.toString(gui),lists); |
| | | if (lists != null && lists.size() > 0) { |
| | | gridStatus.put(Integer.toString(gui), lists); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | @Subscribe(threadMode = ThreadMode.ASYNC) |
| | | public void recvCommand(DeviceMessage mssage){ |
| | | public void recvCommand(DeviceMessage mssage) { |
| | | LogUtils.i("开门命令接收"); |
| | | try { |
| | | if (mssage.getMessageType() == MessageType.SENDMESSAGE.ordinal()){ |
| | | if (mssage.getMessageType() == MessageType.SENDMESSAGE.ordinal()) { |
| | | if (mssage.getDeviceType() == DeviceType.CANGUI.ordinal()) { |
| | | byte[] cc = mssage.getMessageByte(); |
| | | if (cc != null) { |
| | | commandQueue.add(cc); |
| | | |
| | | } |
| | | } |
| | | } |
| | | }catch (Exception e){ |
| | | } catch (Exception e) { |
| | | LogUtils.e(e); |
| | | } |
| | | } |