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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package com.hdl.sdk.ttl_sdk.activity;
 
 
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.hdl.sdk.ttl.HDLAppliances.Config.HDLApConfig;
import com.hdl.sdk.ttl.HDLAppliances.HDLFreshAir.FreshAirJinMaoBackInfo;
import com.hdl.sdk.ttl.HDLAppliances.HDLFreshAir.Parser.FreshAirJinMaoParser;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.AppliancesInfo;
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLCommand;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.DeviceStateEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.FreshAirJinMaoFeedBackEvent;
import com.hdl.sdk.ttl_sdk.R;
import com.hdl.sdk.ttl_sdk.base.BaseActivity;
import com.hdl.sdk.ttl_sdk.utlis.HDLLog;
 
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
 
/**
 * 金茂新风设备
 * * 开关    0-关机,1-开机
 * * 运行模式    1-通风,2-加湿
 * * 节能舒适选择    1-舒适,2-节能
 * * 风速档位    0-自动,1-低速,2-中速,3-高速
 */
public class FreshAirJinMaoActivity extends BaseActivity {
    /**
     * Topbar
     */
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
 
    private TextView tv_nowInTemp, tv_nowInHumidty, tv_filterRemaining, tv_timeout;
    private Button btn_switch, btn_mode, btn_mode2, btn_speed, btn_setHumidty;
    private TextView mesText;
    private EditText et_Humidty;
    private AppliancesInfo appliancesInfo;
 
 
    private int switchState;//Demo仅以此作为演示,实际请根据需求开发设计
    private int modeState;//运行模式状态
    private int mode2State;//节能模式状态
    private int speedState;
    private int humidtyState;
 
 
    /**
     * 复写isRegisterEventBus()  要注册使用EventBus,这里要设置返回true
     *
     * @return true
     */
    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ctrl_jinmao_freshair);
        initToolbar();
        initView();
        initOnClick();
        initcurState();
        showStateView();
 
        HDLCommand.getFreshAirDeviceStateFromNetwork(appliancesInfo);
 
//        HDLCommand.getHVACDeviceStateFromNetwork(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();
        topBarTitle.setText(titleStr);
    }
 
 
    private void initView() {
        tv_nowInTemp = findViewById(R.id.tv_nowInTemp);
        tv_nowInHumidty = findViewById(R.id.tv_nowInHumidty);
        tv_filterRemaining = findViewById(R.id.tv_filterRemaining);
        tv_timeout = findViewById(R.id.tv_timeout);
        mesText = findViewById(R.id.mesText);
 
 
        btn_switch = findViewById(R.id.btn_switch);
        btn_mode = findViewById(R.id.btn_mode);
        btn_mode2 = findViewById(R.id.btn_mode2);
        btn_speed = findViewById(R.id.btn_speed);
        btn_setHumidty = findViewById(R.id.btn_setHumidty);
 
        et_Humidty = findViewById(R.id.et_Humidty);
 
    }
 
    private void initOnClick() {
        /**
         * 开关状态控制
         */
        btn_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //演示当前状态为关,设置为开。开,设置为关。
                if (switchState == FreshAirJinMaoParser.freshAirOff) {
                    HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetSwich, FreshAirJinMaoParser.freshAirOn);//打开
                } else {
                    HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetSwich, FreshAirJinMaoParser.freshAirOff);//关闭
                }
            }
        });
 
        /**
         * 运行模式控制
         */
        btn_mode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (modeState) {
                    case FreshAirJinMaoParser.freshAirModeVentilation:
                        //若当前新风运行模式为通风,则点击按钮设置为加湿
                        HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetMode, FreshAirJinMaoParser.freshAirModeHumidify);
                        break;
                    case FreshAirJinMaoParser.freshAirModeHumidify:
                        //若当前新风运行模式为加湿,则点击按钮设置为通风
                        HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetMode, FreshAirJinMaoParser.freshAirModeVentilation);
                        break;
                    default:
                        //设置为通风模式
                        HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetMode, FreshAirJinMaoParser.freshAirModeVentilation);
                        break;
                }
 
            }
        });
 
        /**
         * 节能舒适模式控制
         */
        btn_mode2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (mode2State) {
                    case FreshAirJinMaoParser.energySavingModeComfortable:
                        //若当前新风为舒适,则点击按钮设置为节能
                        HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetEnergySavingMode, FreshAirJinMaoParser.energySavingModeEs);
                        break;
                    case FreshAirJinMaoParser.energySavingModeEs:
                        //若当前新风为节能,则点击按钮设置为舒适
                        HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetEnergySavingMode, FreshAirJinMaoParser.energySavingModeComfortable);
                        break;
                    default:
                        //设置为舒适模式
                        HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetEnergySavingMode, FreshAirJinMaoParser.energySavingModeComfortable);
                        break;
                }
 
            }
        });
 
 
        /**
         * 风速档位设置
         */
        btn_speed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (speedState) {
                    case FreshAirJinMaoParser.freshAirSpeedAuto:
                        //若当前风速为自动,则点击按钮设置为低速
                        HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetSpeed, FreshAirJinMaoParser.freshAirSpeed1);
                        break;
                    case FreshAirJinMaoParser.freshAirSpeed1:
                        //若当前风速为低速,则点击按钮设置为中速
                        HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetSpeed, FreshAirJinMaoParser.freshAirSpeed2);
                        break;
                    case FreshAirJinMaoParser.freshAirSpeed2:
                        //若当前风速为中速,则点击按钮设置为高速
                        HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetSpeed, FreshAirJinMaoParser.freshAirSpeed3);
                        break;
                    case FreshAirJinMaoParser.freshAirSpeed3:
                        //若当前风速为高速,则点击按钮设置为自动
                        HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetSpeed, FreshAirJinMaoParser.freshAirSpeedAuto);
                        break;
                    default:
                        //设置为自动
                        HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetSpeed, FreshAirJinMaoParser.freshAirSpeedAuto);
                        break;
 
                }
            }
        });
 
        /**
         * 湿度设置
         */
        btn_setHumidty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String mHumidtyStr = et_Humidty.getText().toString();
                if (TextUtils.isEmpty(mHumidtyStr)) {
                    showToast("设置的湿度不能为空");
                    return;
                }
                int mHumidtyInt = Integer.parseInt(mHumidtyStr);
 
                if (mHumidtyInt < 0 || mHumidtyInt > 100) {
                    showToast("湿度设置范围为:0~100%");
                    return;
                }
 
                HDLCommand.freshAirJinMaoCtrl(appliancesInfo, FreshAirJinMaoParser.SetHumidty, mHumidtyInt);
 
 
            }
        });
 
 
    }
 
    /**
     *
     */
    private void showStateView() {
        if (appliancesInfo.getArrCurState() != null) {
            FreshAirJinMaoBackInfo mFreshAirJinMaoBackInfo = new FreshAirJinMaoBackInfo(appliancesInfo);
            showFreshAirJinMaoBackInfo(mFreshAirJinMaoBackInfo);
        } else {
            mesText.setText("未获取到新风状态");
        }
    }
 
    /**
     * 刷新设备状态
     */
    private void refreshState(FreshAirJinMaoBackInfo mFreshAirJinMaoBackInfo) {
        switchState = mFreshAirJinMaoBackInfo.getIsOn();
        modeState = mFreshAirJinMaoBackInfo.getFreshAirMode();
        mode2State = mFreshAirJinMaoBackInfo.getEnergySavingMode();
        speedState = mFreshAirJinMaoBackInfo.getFreshAirSpeed();
        humidtyState = mFreshAirJinMaoBackInfo.getSetHumidty();
 
 
        tv_nowInTemp.setText(mFreshAirJinMaoBackInfo.getNowInTemp() + "℃");
        tv_nowInHumidty.setText(mFreshAirJinMaoBackInfo.getNowInHumidty() + "%");
        tv_filterRemaining.setText(mFreshAirJinMaoBackInfo.getFilterRemaining() + "%");
        String timeoutStateStr = mFreshAirJinMaoBackInfo.getUseTimeoutState() > 0 ? "超时,请更换" : "正常";
        tv_timeout.setText(timeoutStateStr);
        if (mFreshAirJinMaoBackInfo.getUseTimeoutState() > 0) {
            showToast("报警信息:滤网使用超时,需要更换!");
        }
    }
 
    /**
     * getModeStateString
     *
     * @param mState
     * @return
     */
    private String getModeStateString(int mState) {
        String mStrState = "未知";
        modeState = mState;  //更新模式状态
        switch (mState) {
            case 1:
                mStrState = "运行模式:通风";
                break;
            case 2:
                mStrState = "运行模式:加湿";
                break;
            default:
                mStrState = "未知运行模式";
                break;
        }
        return mStrState;
    }
 
    /**
     * @param mState
     * @return
     */
    private String getMode2StateString(int mState) {
        String mStrState = "未知";
        mode2State = mState;  //更新节能模式状态
        switch (mState) {
            case 1:
                mStrState = "节能模式:舒适";
                break;
            case 2:
                mStrState = "节能模式:节能";
                break;
            default:
                mStrState = "未知节能模式";
                break;
        }
        return mStrState;
    }
 
    /**
     * getSpeedStateString
     *
     * @param mState
     * @return
     */
    private String getSpeedStateString(int mState) {
        String mStrState = "未知";
        speedState = mState; //更新当前风速状态
        switch (mState) {
            case 0:
                mStrState = "风速:自动";
                break;
            case 1:
                mStrState = "风速:低速";
                break;
            case 2:
                mStrState = "风速:中速";
                break;
            case 3:
                mStrState = "风速:高速";
                break;
 
            default:
                mStrState = "未知风速状态";
                break;
        }
        return mStrState;
    }
 
 
    /**
     * getSwichStateString
     *
     * @param mState
     * @return
     */
    private String getSwichStateString(int mState) {
        String mStrState = "未知";
        switchState = mState; //更新开关状态
        switch (mState) {
            case FreshAirJinMaoParser.freshAirOn:
                mStrState = "新风:打开";
                break;
            case FreshAirJinMaoParser.freshAirOff:
                mStrState = "新风:关闭";
                break;
 
            default:
                mStrState = "未知状态";
                break;
        }
        return mStrState;
    }
 
 
    /**
     * 处理设备状态
     *
     * @param mFreshAirJinMaoBackInfo
     */
    private void showFreshAirJinMaoBackInfo(FreshAirJinMaoBackInfo mFreshAirJinMaoBackInfo) {
        if (mFreshAirJinMaoBackInfo != null) {
            String message = "";
            if (mFreshAirJinMaoBackInfo.getIsOn() == FreshAirJinMaoParser.freshAirOn) {
                message = getSwichStateString(mFreshAirJinMaoBackInfo.getIsOn());
                message += "\n" + getModeStateString(mFreshAirJinMaoBackInfo.getFreshAirMode());//运行模式
                message += "\n" + getMode2StateString(mFreshAirJinMaoBackInfo.getEnergySavingMode());//节能模式
                message += "\n" + getSpeedStateString(mFreshAirJinMaoBackInfo.getFreshAirSpeed());//风速
                message += "\n当前设置湿度:" + mFreshAirJinMaoBackInfo.getSetHumidty();//当前湿度设置值
                refreshState(mFreshAirJinMaoBackInfo);
            } else {
                message = getSwichStateString(mFreshAirJinMaoBackInfo.getIsOn());
            }
            mesText.setText(message);
            showToast(message);
            HDLLog.Log(message);
        } else {
            mesText.setText("未获取到新风状态");
        }
    }
 
 
    /**
     * 新风控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFreshAirJinMaoFeedBackEventEventMain(FreshAirJinMaoFeedBackEvent event) {
        if (event.getFreshAirJinMaoBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getFreshAirJinMaoBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getFreshAirJinMaoBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()) {
            //先判断是否超时
            if (!event.isSuccess()) {
                showToast("新风控制超时,请重新再试");
                return;
            }
            FreshAirJinMaoBackInfo mFreshAirJinMaoBackInfo = event.getFreshAirJinMaoBackInfo();
            showFreshAirJinMaoBackInfo(mFreshAirJinMaoBackInfo);
 
        }
    }
 
    /**
     * 获取单一设备状态回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeviceStateEventMain(DeviceStateEvent event) {
        if (event.getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
        ) {
            //这个返回的信息是当前状态的
            switch (event.getAppliancesInfo().getDeviceType()) {
                case HDLApConfig.TYPE_FRESH_AIR_JINMAO:
                    if (appliancesInfo.getChannelNum() == event.getAppliancesInfo().getChannelNum()) {
                        if (!event.isSuccess()) {
                            showToast("获取新风状态失败,请重新再试");
                            return;
                        }
                        FreshAirJinMaoBackInfo mFreshAirJinMaoBackInfo = new FreshAirJinMaoBackInfo(event.getAppliancesInfo());
 
                        if (mFreshAirJinMaoBackInfo == null) {
                            showToast("获取新风状态失败,请重新再试");
                            return;
                        }
                        showFreshAirJinMaoBackInfo(mFreshAirJinMaoBackInfo);
                    }
                    break;
                default:
                    //不处理
                    break;
            }
        }
    }
 
 
}