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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package com.hdl.sdk.hdl_sdk.activity;
 
 
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import com.hdl.sdk.hdl_core.HDLAppliances.Config.HDLApConfig;
import com.hdl.sdk.hdl_core.HDLAppliances.HDLFreshAir.FreshAirBackInfo;
import com.hdl.sdk.hdl_core.HDLAppliances.HDLFreshAir.Parser.FreshAirParser;
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.AppliancesInfo;
import com.hdl.sdk.hdl_core.HDLDeviceManger.Core.HDLCommand;
import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.DeviceStateEvent;
import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.FreshAirFeedBackEvent;
import com.hdl.sdk.hdl_sdk.base.BaseActivity;
import com.hdl.sdk.hdl_sdk.utlis.HDLLog;
 
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
 
/**
 * 新风系统控制界面
 */
public class FreshAirActivity extends BaseActivity {
 
    private Button btn_switch, btn_mode, btn_speed;
    private TextView mTextView;
    private AppliancesInfo appliancesInfo;
 
    private int airSwitchState;//Demo仅以此作为演示,实际请根据需求开发设计
    private int airModeState;
    private int airSpeedState;
    private int ctrlId;
 
    private ProgressDialog proDialog;
 
    /**
     * 要注册使用EventBus,这里要设置返回true
     *
     * @return true
     */
    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.hdl.sdk.hdl_sdk.R.layout.activity_fresh_air);
        initcurState();
        initView();
        initClickOnEvent();
        showStateView();
        //此方法为主动获取单一设备状态,逻辑模块、背景音乐模块没有这个api
        //目前仅支持灯光,窗帘,空调不发送获取状态命令只从SDK本地获取,传感器,地热模块,新风系统是发送获取状态命令从网络上获取。
        HDLCommand.getDeviceState(appliancesInfo);
    }
 
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        proDialog.dismiss();
        HDLLog.I("onDestroy: FreshAirActivity");
    }
 
    private void initcurState() {
        appliancesInfo = (AppliancesInfo) getIntent().getSerializableExtra("hdl");
    }
 
    private void initView() {
        mTextView = findViewById(com.hdl.sdk.hdl_sdk.R.id.tv_fresh_air);
        btn_switch = findViewById(com.hdl.sdk.hdl_sdk.R.id.btn_switch);
        btn_speed = findViewById(com.hdl.sdk.hdl_sdk.R.id.btn_speed);
        btn_mode = findViewById(com.hdl.sdk.hdl_sdk.R.id.btn_mode);
 
        proDialog = new ProgressDialog(this);
        proDialog.setTitle("正在发送控制命令...");
        proDialog.setMessage("请耐心等待");
        proDialog.onStart();
    }
 
    private void initClickOnEvent() {
 
        btn_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                proDialog.show();
                ctrlId = FreshAirParser.freshAirSwich;
                //演示当前状态为关,设置为开。开,设置为关。
                if (airSwitchState == 0) {
                    //新风开
                    HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirSwich, FreshAirParser.freshAirOn);
                } else {
                    //新风关
                    HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirSwich, FreshAirParser.freshAirOff);
                }
            }
        });
 
        //        0 关,1低,2中,3高
        btn_speed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                proDialog.show();
                ctrlId = FreshAirParser.freshAirSpeed;
                switch (airSpeedState) {
                    case 0:
                        //若当前风速为关,则点击按钮设置为低速
                        HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirSpeed, FreshAirParser.freshAirSpeedLow);
                        break;
                    case 1:
                        //若当前风速为低速,则点击按钮设置为中速
                        HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirSpeed, FreshAirParser.freshAirSpeedMid);
                        break;
                    case 2:
                        //若当前风速为中速,则点击按钮设置为高速
                        HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirSpeed, FreshAirParser.freshAirSpeedHigh);
                        break;
                    case 3:
                        //若当前风速为高速,则点击按钮设置为关
                        HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirSpeed, FreshAirParser.freshAirSpeedOff);
                        break;
                    default:
                        //设置为关
                        HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirSpeed, FreshAirParser.freshAirSpeedOff);
                        break;
 
 
                }
            }
        });
 
 
//        0手动,1 自动,2 智能,3 定时
        btn_mode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                proDialog.show();
                ctrlId = FreshAirParser.freshAirMode;
                switch (airModeState) {
                    case 0:
                        //若当前模式为手动,则点击按钮设置为自动
                        HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirMode, FreshAirParser.freshAirModeAuto);
                        break;
                    case 1:
                        //若当前模式为自动,则点击按钮设置为智能
                        HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirMode, FreshAirParser.freshAirModeIntelligent);
                        break;
                    case 2:
                        //若当前模式智能,则点击按钮设置为定时
                        HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirMode, FreshAirParser.freshAirModeTiming);
                        break;
                    case 3:
                        //若当前模式定时,则点击按钮设置为手动
                        HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirMode, FreshAirParser.freshAirModeManual);
                        break;
                    default:
//                        手动
                        HDLCommand.freshAirCtrl(appliancesInfo, FreshAirParser.freshAirMode, FreshAirParser.freshAirModeManual);
                        break;
 
                }
 
 
            }
        });
 
 
    }
 
    /**
     * mTextView 上显示当前状态
     */
    private void showStateView() {
        if (appliancesInfo.getFeedbackState() != null) {
            String stringFAState = "";
//            //方法1:
//            byte[] freshAirCurState = appliancesInfo.getFeedbackState();
//            for (int index = 0; index < freshAirCurState.length; index++) {
//                if (index == 1 && (freshAirCurState[index] & 0xFF) == 0) {
//                    airSwitchState = 0;
//                    stringFAState += " 新风系统:关闭";
//                    //如果空调关闭状态,则无需再遍历
//                    break;
//                }
//                if (index == 1 && (freshAirCurState[index] & 0xFF) == 1) {
//                    airSwitchState = 1;
//                    stringFAState += " 新风系统:打开";
//                }
//
//                switch (index) {
//                    case 2:
//                        stringFAState += "\n" + getSpeedStateString(freshAirCurState[index] & 0xFF);
//                    case 3:
//                        //模式(0手动,1 自动,2 智能,3 定时)
//                        stringFAState += "\n" + getModeStateString(freshAirCurState[index] & 0xFF);
//                    default:
//                        break;
//                }
//            }
            //方法2:
            FreshAirBackInfo mFreshAirBackInfo = new FreshAirBackInfo(appliancesInfo);
            if (mFreshAirBackInfo == null) {
                stringFAState = "未获取到新风系统状态";
            } else {
 
                if (mFreshAirBackInfo.getIsOn() == 0) {
                    stringFAState = "新风系统:关闭";
                } else if (mFreshAirBackInfo.getIsOn() == 1) {
                    stringFAState = "新风系统:打开";
                    stringFAState += "\n" + getSpeedStateString(mFreshAirBackInfo.getFreshAirSpeed());
                    stringFAState += "\n" + getModeStateString(mFreshAirBackInfo.getFreshAirMode());
                } else {
                    stringFAState = "未知开关状态";
                }
 
            }
            mTextView.setText(stringFAState);
        } else {
            mTextView.setText("未获取到新风设备状态");
        }
 
    }
 
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFreshAirFeedBackEventMain(FreshAirFeedBackEvent event) {
        proDialog.dismiss();
        if (event.getFreshAirBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getFreshAirBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getFreshAirBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()
        ) {
            //先判断是否超时
            if (!event.isSuccess()) {
                showToast("新风控制超时,请重新再试");
                return;
            }
 
            String message = "";
            FreshAirBackInfo mFreshAirBackInfo = event.getFreshAirBackInfo();
            switch (ctrlId) {   //ctrlId 当前控制类型
                case FreshAirParser.freshAirSwich:
                    message = getSwichStateString(mFreshAirBackInfo.getIsOn());
                    break;
                case FreshAirParser.freshAirSpeed:
                    message = getSpeedStateString(mFreshAirBackInfo.getFreshAirSpeed());
                    break;
                case FreshAirParser.freshAirMode:
                    message = getModeStateString(mFreshAirBackInfo.getFreshAirMode());
                    break;
                default:
                    break;
 
            }
 
            //*********根据需求显示*********
            message += "\n" + "室内温度:" + mFreshAirBackInfo.getInTemp();
            message += "\n" + "室外温度:" + mFreshAirBackInfo.getOutTemp();
            message += "\n" + "室内湿度:" + mFreshAirBackInfo.getHumidty();
            message += "\n" + "PM2.5:" + mFreshAirBackInfo.getInTemp();
            message += "\n" + "TVOC:" + mFreshAirBackInfo.getInTemp();
            message += "\n" + "CO2:" + mFreshAirBackInfo.getInTemp();
            //*********根据需求显示*********
 
            mTextView.setText(message);
            showToast(message);
            HDLLog.I(message);
        }
    }
 
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeviceStateEventMain(DeviceStateEvent event) {
        proDialog.dismiss();
        if (event.getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
        ) {
            //这个返回的信息是当前状态的
            switch (event.getAppliancesInfo().getDeviceType()) {
                case HDLApConfig.TYPE_FRESH_AIR:
                    if (appliancesInfo.getChannelNum() == event.getAppliancesInfo().getChannelNum()) {
                        if (!event.isSuccess()) {
                            showToast("获取新风状态失败,请重新再试");
                            return;
                        }
                        String message = "";
                        FreshAirBackInfo mFreshAirBackInfo = new FreshAirBackInfo(event.getAppliancesInfo());
                        if (mFreshAirBackInfo == null) {
                            showToast("获取新风状态失败,请重新再试");
                            return;
                        }
 
                        if(mFreshAirBackInfo.getIsOn() == FreshAirParser.freshAirOn) {
                            message = getSwichStateString(mFreshAirBackInfo.getIsOn());
                            message += "\n" + getSpeedStateString(mFreshAirBackInfo.getFreshAirSpeed());
                            message += "\n" + getModeStateString(mFreshAirBackInfo.getFreshAirMode());
 
                            message += "\n" + "室内温度:" + mFreshAirBackInfo.getInTemp();
                            message += "\n" + "室外温度:" + mFreshAirBackInfo.getOutTemp();
                            message += "\n" + "室内湿度:" + mFreshAirBackInfo.getHumidty();
                            message += "\n" + "PM2.5:" + mFreshAirBackInfo.getInTemp();
                            message += "\n" + "TVOC:" + mFreshAirBackInfo.getInTemp();
                            message += "\n" + "CO2:" + mFreshAirBackInfo.getInTemp();
                        }else {
                            message = getSwichStateString(mFreshAirBackInfo.getIsOn());
                        }
 
                        mTextView.setText(message);
                        showToast(message);
                        HDLLog.I(message);
 
                    }
                    break;
            }
        }
    }
 
 
    /**
     * getSwichStateString
     *
     * @param mState
     * @return
     */
    private String getSwichStateString(int mState) {
        String mStrState = "未知";
        airSwitchState = mState; //更新开关状态
 
        switch (mState) {
            case FreshAirParser.freshAirOn:
                mStrState = "新风系统:打开";
                break;
            case FreshAirParser.freshAirOff:
                mStrState = "新风系统:关闭";
                break;
 
            default:
                mStrState = "未知状态";
                break;
        }
        return mStrState;
    }
 
    /**
     * getSpeedStateString
     *
     * @param mState
     * @return
     */
    private String getSpeedStateString(int mState) {
        String mStrState = "未知";
        airSpeedState = mState; //更新当前风速状态
        switch (mState) {
            case FreshAirParser.freshAirSpeedOff:
                mStrState = "风速:关";
                break;
            case FreshAirParser.freshAirSpeedLow:
                mStrState = "风速:低";
                break;
            case FreshAirParser.freshAirSpeedMid:
                mStrState = "风速:中";
                break;
            case FreshAirParser.freshAirSpeedHigh:
                mStrState = "风速:高";
                break;
 
            default:
                mStrState = "未知风速状态";
                break;
        }
        return mStrState;
    }
 
    /**
     * getModeStateString
     *
     * @param mState
     * @return
     */
    private String getModeStateString(int mState) {
        String mStrState = "未知";
        airModeState = mState;  //更新模式状态
        switch (mState) {
            case FreshAirParser.freshAirModeManual:
                mStrState = "模式:手动";
                break;
            case FreshAirParser.freshAirModeAuto:
                mStrState = "模式:自动";
                break;
            case FreshAirParser.freshAirModeIntelligent:
                mStrState = "模式:智能";
                break;
            case FreshAirParser.freshAirModeTiming:
                mStrState = "模式:定时";
                break;
 
            default:
                mStrState = "未知模式状态";
                break;
        }
        return mStrState;
    }
}