panlili2024
2024-09-19 071a8328823a2861f93ce556a4da3e4119cab1a3
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.hdl.sdk.ttl_sdk.activity;
 
 
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.hdl.sdk.ttl.HDLAppliances.Config.HDLApConfig;
import com.hdl.sdk.ttl.HDLAppliances.HDLSensor.DryContactSensorBackEvent;
import com.hdl.sdk.ttl.HDLAppliances.HDLSensor.SensorStateBackInfo;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.AppliancesInfo;
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLCommand;
import com.hdl.sdk.ttl_sdk.R;
import com.hdl.sdk.ttl_sdk.base.BaseActivity;
import com.hdl.sdk.ttl_sdk.utlis.HDLLog;
import com.hdl.sdk.ttl_sdk.utlis.HDLUtlis;
 
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
public class SensorActivity extends BaseActivity {
    /**
     * Topbar
     */
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
    private TextView sensorText;
    private TextView sensorStatusText;
    private AppliancesInfo appliancesInfo;
    private String showMessage;
 
 
    /**
     * 复写isRegisterEventBus()  要注册使用EventBus,这里要设置返回true
     *
     * @return true
     */
    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);
        initToolbar();
        initcurState();
        initView();
        displayStateView();
 
        Log.d("panlili", "SensorActivity.java:appliancesInfo-----> " + appliancesInfo.toString());
        //安防传感器无状态获取,只能触发事件上报
        //环境传感器获取状态
        HDLCommand.getSensorStateFromNetwork(appliancesInfo);
    }
 
    /**
     * 初始化Toolbar
     */
    private void initToolbar() {
        topBarBack = findViewById(R.id.ll_top_b_left);
        setViewVisible(topBarBack);
        topBarTitle = findViewById(R.id.tv_top_b_header_title);
        topBarBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
 
    private void initcurState() {
        appliancesInfo = (AppliancesInfo) getIntent().getSerializableExtra("hdl");
        String titleStr = appliancesInfo.getRemarks();
        if (TextUtils.isEmpty(titleStr) || titleStr.equals(HDLUtlis.REMAEK_NULL)) {
            if (TextUtils.isEmpty(appliancesInfo.getDeviceName())) {
                titleStr = "备注为空";
            } else {
                titleStr = appliancesInfo.getDeviceName();
            }
        }
        topBarTitle.setText(titleStr + appliancesInfo.getDeviceType());
 
    }
 
    private void initView() {
        sensorText = findViewById(R.id.sensorText);
        sensorStatusText = findViewById(R.id.sensorStatusText);
    }
 
    private void displayStateView() {
 
        switch (appliancesInfo.getDeviceType()) {
            case HDLApConfig.TYPE_SENSOR_MOVEMENT_DETECTOR: //红外感应传感器
            case HDLApConfig.TYPE_SENSOR_LPG:         //煤气传感器 液化石油气
            case HDLApConfig.TYPE_SENSOR_CO_H2:         //煤气传感器 人工煤气(CO,H2)
            case HDLApConfig.TYPE_SENSOR_CH4:         //煤气传感器 天然气(CH4)
            case HDLApConfig.TYPE_SENSOR_SMOG:          //烟雾传感器
            case HDLApConfig.TYPE_SENSOR_FLOODING:       //水浸传感器
            case HDLApConfig.TYPE_SENSOR_DOOR_MAGNET:   //门磁传感器
            case HDLApConfig.TYPE_SENSOR_EMERGENCY_BUTTON://紧急按钮
            case HDLApConfig.TYPE_SENSOR_TEMP://温度
            case HDLApConfig.TYPE_SENSOR_HUMIDITY://湿度
            case HDLApConfig.TYPE_SENSOR_VOC://VOC
            case HDLApConfig.TYPE_SENSOR_PM_2_POINT_5://PM2.5
            case HDLApConfig.TYPE_SENSOR_C02://C02
            case HDLApConfig.TYPE_SENSOR_PM_10://PM10
                break;
            default:
                finish();//设备类型不对结束页面
                break;
        }
        /**根据需求是否发送一次获取刷新状态请求*/
    }
 
 
    /**
     * 传感器状态回调 Event
     * 干节点状态主动广播--->15D0
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDryContactSensorBackEventMain(DryContactSensorBackEvent event) {
        if (event.getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()
        ) {
 
            if (!event.isSuccess()) {
                showToast("传感器状态上报失败,请重新再试");
                return;
            }
            String mData = "";
            if (appliancesInfo.getDeviceType() == HDLApConfig.TYPE_SENSOR_MOVEMENT_DETECTOR) {//红外感应传感器
                mData = event.getState() == 0 ? "无人" : "有人";
            } else if (appliancesInfo.getDeviceType() == HDLApConfig.TYPE_SENSOR_FLOODING) {//水浸传感器
                mData = event.getState() == 0 ? "正常" : "漏水";
            } else if (appliancesInfo.getDeviceType() == HDLApConfig.TYPE_SENSOR_DOOR_MAGNET) {//门磁传感器
                mData = event.getState() == 0 ? "闭合" : "打开";
            } else {
                mData = event.getState() == 0 ? "正常" : "报警";
            }
 
            sensorText.setVisibility(View.VISIBLE);
            sensorStatusText.setVisibility(View.GONE);
            sensorText.setText("传感器状态:" + mData);
            HDLLog.Log("传感器状态:" + mData);
        }
    }
 
 
    /**
     * 传感器状态回调 Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSensorStateBackInfoMain(SensorStateBackInfo event) {
        if (event.getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()
        ) {
            showMessage = "";
            sensorText.setVisibility(View.GONE);
            sensorStatusText.setVisibility(View.VISIBLE);
            //这个返回的信息是当前状态的
            switch (event.getAppliancesInfo().getDeviceType()) {
                case HDLApConfig.TYPE_SENSOR_TEMP:
                    //传感器 温度
                    if (!event.isSuccess()) {
                        showToast("获取传感器---温度状态失败,请重新再试");
                        return;
                    }
                    float tempValue = (float) event.getAppliancesInfo().getCurState();
                    String tempValueUnit = ((SensorStateBackInfo) event).getUnite();
                    showMessage = tempValue + tempValueUnit;
                    sensorStatusText.setText("传感器---温度:" + showMessage);
                    showToast("传感器---温度:" + tempValue + tempValueUnit);
                    HDLLog.Log("传感器---温度:" + tempValue + tempValueUnit);
                    break;
                case HDLApConfig.TYPE_SENSOR_HUMIDITY:
                    //传感器 湿度
                    if (!event.isSuccess()) {
                        showToast("获取传感器---湿度状态失败,请重新再试");
                        return;
                    }
                    float humidityValue = (float) event.getAppliancesInfo().getCurState();
                    String humidityValueUnit = ((SensorStateBackInfo) event).getUnite();
                    showMessage = humidityValue + humidityValueUnit;
                    sensorStatusText.setText("传感器---湿度:" + showMessage);
                    showToast("传感器---湿度:" + humidityValue + humidityValueUnit);
                    HDLLog.Log("传感器---湿度:" + humidityValue + humidityValueUnit);
                    break;
                case HDLApConfig.TYPE_SENSOR_VOC:
                    //传感器 当前空气质量等级
                    if (!event.isSuccess()) {
                        showToast("获取传感器---当前空气质量等级状态失败,请重新再试");
                        return;
                    }
                    float vocValue = (float) event.getAppliancesInfo().getCurState();
                    String vocValueUnit = ((SensorStateBackInfo) event).getUnite();
                    showMessage = vocValue + vocValueUnit;
                    sensorStatusText.setText("传感器---当前空气质量等级:" + showMessage);
                    showToast("传感器---当前空气质量等级:" + vocValue + vocValueUnit);
                    HDLLog.Log("传感器---当前空气质量等级:" + vocValue + vocValueUnit);
                    break;
                case HDLApConfig.TYPE_SENSOR_PM_2_POINT_5:
                    //传感器 pm2.5
                    if (!event.isSuccess()) {
                        showToast("获取传感器---pm2.5状态失败,请重新再试");
                        return;
                    }
                    float pm2_5Value = (float) event.getAppliancesInfo().getCurState();
                    String pm2_5ValueUnit = ((SensorStateBackInfo) event).getUnite();
                    showMessage = pm2_5Value + pm2_5ValueUnit;
                    sensorStatusText.setText("传感器---pm2.5:" + showMessage);
                    showToast("传感器---pm2.5:" + pm2_5Value + pm2_5ValueUnit);
                    HDLLog.Log("传感器---pm2.5:" + pm2_5Value + pm2_5ValueUnit);
                    break;
                case HDLApConfig.TYPE_SENSOR_C02:
                    //传感器 二氧化碳
                    if (!event.isSuccess()) {
                        showToast("获取传感器---二氧化碳状态失败,请重新再试");
                        return;
                    }
                    float co2Value = (float) event.getAppliancesInfo().getCurState();
                    String co2ValueUnit = ((SensorStateBackInfo) event).getUnite();
                    showMessage = co2Value + co2ValueUnit;
                    sensorStatusText.setText("传感器---二氧化碳:" + showMessage);
                    showToast("传感器---二氧化碳:" + co2Value + co2ValueUnit);
                    HDLLog.Log("传感器---二氧化碳:" + co2Value + co2ValueUnit);
                    break;
                case HDLApConfig.TYPE_SENSOR_PM_10:
                    //传感器 pm10
                    if (appliancesInfo.getChannelNum() != event.getAppliancesInfo().getChannelNum() || !event.isSuccess()) {
                        showToast("获取传感器---pm10状态失败,请重新再试");
                        return;
                    }
                    float pm10Value = (float) event.getAppliancesInfo().getCurState();
                    String pm10ValueUnit = ((SensorStateBackInfo) event).getUnite();
                    showMessage = pm10Value + pm10ValueUnit;
                    sensorStatusText.setText("传感器---pm10:" + showMessage);
                    showToast("传感器---pm10:" + pm10Value + pm10ValueUnit);
                    HDLLog.Log("传感器---pm10:" + pm10Value + pm10ValueUnit);
                    break;
            }
        }
    }
 
}