陈力
2023-05-29 2b270f01facd18f5d2c105e48279482d3ff4b9c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.lotaai.canguiayw.device.service;
 
import android.app.Service;
import android.content.Intent;
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.common.SettingConfig;
import com.lotaai.canguiayw.device.CanGuiCommand;
import com.lotaai.canguiayw.device.CanguiStatus;
import com.lotaai.canguiayw.device.DeviceMessage;
import com.lotaai.canguiayw.device.MessageType;
 
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
 
import android_serialport_api.SerialPortUtils;
 
/**
 * 1、定时去查询柜子的状态
 * 2、通过EventBus接收并处理设备反馈的指令解析状态
 * 3、接收指令进行设备的控制,指令的执行用一个queue来排队完成
 */
public class CanGuiService extends Service {
 
    private List<byte[]> commandQueue = new LinkedList<byte[]>();
    SerialPortUtils serialPortUtils = new SerialPortUtils();
 
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
        serialPortUtils.openSerialPort("/dev/ttyS4",4800);
        registerListener();
        SettingConfig.getInstance().getExecutor().execute(selectRunnable);
        SettingConfig.getInstance().getExecutor().execute(sendRunnable);
        EventBus.getDefault().register(CanGuiService.this);
    }
 
    //查询柜子的状态
    Runnable selectRunnable = new Runnable() {
        @Override
        public void run() {
            while(true){
                try {
                    Thread.sleep(2000);
                    //查询指令
                    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){
                    LogUtils.e(e);
                }
            }
        }
    };
 
 
    //查询柜子的状态
    Runnable sendRunnable = new Runnable() {
        @Override
        public void run() {
            while(true){
                try {
                    Thread.sleep(200);
                    //发送指令
                    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){
                    LogUtils.e(e);
                }
            }
        }
    };
 
    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 (statuses!=null){
                        for (int i = 0; i < statuses.size(); i++){
                            CanguiStatus s = statuses.get(i);
                            SettingConfig.getInstance().addStatusInList(s);
                            LogUtils.i("柜:" + s.getGuiHao(),"格:" + s.getGridNo(),"温度:" + s.getWendu(),
                                    "门:"+s.isDoorIsOpen(),"加热:"+s.isJiareIsOpen(),"灯光:" + s.isDengGuangIsOpen(),"消毒:" + s.isXiaoDuIsOpen());
                        }
                    }
                }
            }
        });
    }
 
    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void recvCommand(DeviceMessage mssage){
        try {
            if (mssage.getMessageType() == MessageType.SENDMESSAGE.ordinal()){
                byte[] cc = mssage.getMessageByte();
                if (cc!=null) {
                    commandQueue.add(cc);
                }
            }
        }catch (Exception e){
            LogUtils.e(e);
        }
    }
}