JLChen
2020-03-16 7a951ecdc084ea8643e29e1ade59c1877fd7fbe5
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
package com.hdl.sdk.hdl_sdk.activity;
 
 
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
import com.hdl.sdk.hdl_core.HDLAppliances.Config.HDLApConfig;
import com.hdl.sdk.hdl_core.HDLAppliances.HDLGeothermal.GeothermalBackInfo;
import com.hdl.sdk.hdl_core.HDLAppliances.HDLGeothermal.Parser.GeothermalParser;
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.EventCode;
import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.GeothermalFeedBackEvent;
import com.hdl.sdk.hdl_sdk.R;
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 GeothermalActivity extends BaseActivity {
 
    private Button btn_switch, btn_mode, btn_temp;
    private TextView mTextView;
    private EditText tempEditText;
    private AppliancesInfo appliancesInfo;
 
    private int gSwitchState;//Demo仅以此作为演示,实际请根据需求开发设计
    private int gModeState;
    private int gTempState;
    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(R.layout.activity_geothermal);
 
        initcurState();
        initView();
        initClickOnEvent();
        showStateView();
 
        //此方法为主动获取单一设备状态,逻辑模块、背景音乐模块没有这个api
        //目前仅支持灯光,窗帘,空调不发送获取状态命令只从SDK本地获取,传感器,地热模块,新风系统是发送获取状态命令从网络上获取。
        HDLCommand.getDeviceState(appliancesInfo);
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        proDialog.dismiss();
        HDLLog.I("onDestroy: GeothermalActivity");
    }
 
    private void initcurState() {
        appliancesInfo = (AppliancesInfo) getIntent().getSerializableExtra("hdl");
    }
 
    private void initView() {
        mTextView = findViewById(R.id.tv_geothermal);
        btn_switch = findViewById(R.id.btn_switch);
        btn_mode = findViewById(R.id.btn_mode);
        btn_temp = findViewById(R.id.btn_temp);
        tempEditText = findViewById(R.id.et_temp);
 
        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 = GeothermalParser.gSwich;
                //演示当前状态为关,设置为开。开,设置为关。
                if (gSwitchState == 0) {
                    //地热开
                    HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gSwich, GeothermalParser.gSwichOn);
                } else {
                    //地热关
                    HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gSwich, GeothermalParser.gSwichOff);
                }
            }
        });
 
 
//        0手动,1 自动,2 智能,3 定时
        btn_mode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                proDialog.show();
                ctrlId = GeothermalParser.gMode;
                switch (gModeState) {
                    case 1:
                        //若当前模式为普通,则点击按钮设置为白天模式
                        HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gMode, GeothermalParser.gModeDay);
                        break;
                    case 2:
                        //若当前模式白天,则点击按钮设置为夜间模式
                        HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gMode, GeothermalParser.gModeNight);
                        break;
                    case 3:
                        //若当前模式夜间,则点击按钮设置为离开模式
                        HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gMode, GeothermalParser.gModeLeave);
                        break;
                    case 4:
                        //若当前模式离开,则点击按钮设置为自动模式
                        HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gMode, GeothermalParser.gModeAuto);
                        break;
                    case 5:
                        //若当前模式自动模式,则点击按钮设置为普通模式
                        HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gMode, GeothermalParser.gModeNormal);
                        break;
                    default:
//                        普通模式
                        HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gMode, GeothermalParser.gModeNormal);
                        break;
 
                }
 
 
 
            }
        });
 
 
        btn_temp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
 
                String tempStr = tempEditText.getText().toString();
                if (TextUtils.isEmpty(tempStr)) {
                    showToast("设置的温度不能为空");
                    return;
                }
                int tempInt = Integer.parseInt(tempStr);
 
                if (tempInt < 16 || tempInt > 35) {
                    showToast("温度设置范围为:16~35摄氏度(℃)");
                    return;
                }
                proDialog.show();
 
                HDLCommand.geothermalCtrlTemp(appliancesInfo, tempInt);//自动根据当前模式设置当前模式的温度。
 
//                switch (gModeState) {
//
//                    case 1:
//                        //当前地热模式为普通
//                        HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gNormalTemp, tempInt);//普通温度
//                        ctrlId = GeothermalParser.gNormalTemp;
//                        break;
//                    case 2:
//                        //当前地热模式为白天
//                        HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gDayTemp, tempInt);//白天温度
//                        ctrlId = GeothermalParser.gDayTemp;
//                        break;
//                    case 3:
//                        //当前地热模式为夜间
//                        HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gNightTemp, tempInt);//夜间温度
//                        ctrlId = GeothermalParser.gNightTemp;
//                        break;
//                    case 4:
//                        //当前地热模式为离开
//                        HDLCommand.geothermalCtrl(appliancesInfo, GeothermalParser.gLeaveTemp, tempInt);//离开温度
//                        ctrlId = GeothermalParser.gLeaveTemp;
//                        break;
//                    case 5:
//                        proDialog.dismiss();
//                        //当前地热模式为自动
//                        showToast("自动模式,不能控制温度");
//                        break;
//                    default:
//                        proDialog.dismiss();
//                        //当前地热模式为自动
//                        showToast("未知模式,不能控制温度");
//                        break;
//
//                }
 
 
            }
        });
 
 
    }
 
    private void showStateView() {
        if (appliancesInfo.getFeedbackState() != null) {
            GeothermalBackInfo mGeothermalBackInfo = new GeothermalBackInfo(appliancesInfo, false);
 
            String stringState = "";
            if (mGeothermalBackInfo.getIsOn() == GeothermalParser.gSwichOff) {
                stringState = "地热模块:关闭";
            } else if (mGeothermalBackInfo.getIsOn() == GeothermalParser.gSwichOn) {
                stringState = "地热模块:打开";
 
                stringState += "\n" + getModeStateString(mGeothermalBackInfo.getgMode());
 
                stringState += "\n普通模式温度:" + mGeothermalBackInfo.getgNormalTemp();
 
                stringState += "\n白天模式温度:" + mGeothermalBackInfo.getgDayTemp();
 
                stringState += "\n夜间模式温度:" + mGeothermalBackInfo.getgNightTemp();
 
                stringState += "\n离开模式温度:" + mGeothermalBackInfo.getgLeaveTemp();
            } else {
                stringState = "未知开关状态";
            }
 
            mTextView.setText(stringState);
        } else {
            mTextView.setText("未获取到地热模块状态");
        }
 
    }
 
 
    /**
     * getSwichStateString
     *
     * @param mState
     * @return
     */
    private String getSwichStateString(int mState) {
        String mStrState = "未知";
        gSwitchState = mState; //更新开关状态
        switch (mState) {
            case GeothermalParser.gSwichOn:
                mStrState = "地热模块:打开";
                break;
            case GeothermalParser.gSwichOff:
                mStrState = "地热模块:关闭";
                break;
 
            default:
                mStrState = "未知状态";
                break;
        }
        return mStrState;
    }
 
 
    /**
     * getModeStateString
     *
     * @param mState
     * @return
     */
    private String getModeStateString(int mState) {
        String mStrState = "未知";
        gModeState = mState;  //更新模式状态
        switch (mState) {
            case GeothermalParser.gModeNormal:
                mStrState = "模式:普通";
                break;
            case GeothermalParser.gModeDay:
                mStrState = "模式:白天";
                break;
            case GeothermalParser.gModeNight:
                mStrState = "模式:夜间";
                break;
            case GeothermalParser.gModeLeave:
                mStrState = "模式:离开";
                break;
            case GeothermalParser.gModeAuto:
                mStrState = "模式:自动";
                break;
 
            default:
                mStrState = "未知模式状态";
                break;
        }
        return mStrState;
    }
 
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGeothermalFeedBackEventMain(GeothermalFeedBackEvent event) {
        proDialog.dismiss();
        if (event.getGeothermalBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getGeothermalBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getGeothermalBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()
        ) {
            //先判断是否超时
            if (event.getStatusID() == EventCode.FAILURE) {
                showToast("地热模块控制超时,请重新再试");
                return;
            }
 
            if (event.getStatusID() == EventCode.DATAEMPTY) {
                showToast("地热模块,返回数据为空");
                return;
            }
            GeothermalBackInfo mGeothermalBackInfo = event.getGeothermalBackInfo();
            showGeothermalBackInfo(mGeothermalBackInfo);
 
//            String message = "";
//            GeothermalBackInfo mGeothermalBackInfo = event.getGeothermalBackInfo();
 
//            switch (ctrlId) {   //ctrlId 当前控制类型,只反馈控制的状态
//                case GeothermalParser.gSwich:
//                    message = getSwichStateString(mGeothermalBackInfo.getIsOn());
//                    break;
//                case GeothermalParser.gMode:
//                    message = getModeStateString(mGeothermalBackInfo.getgMode());
//                    break;
//                case GeothermalParser.gNormalTemp:
////                    gTempState = mGeothermalBackInfo.getgNormalTemp();
//                    message = "普通模式温度:" + mGeothermalBackInfo.getgNormalTemp();
//                    break;
//
//
//                case GeothermalParser.gDayTemp:
//                    message = "白天模式温度:" + mGeothermalBackInfo.getgDayTemp();
//                    break;
//                case GeothermalParser.gNightTemp:
//                    message = "夜间模式温度:" + mGeothermalBackInfo.getgNightTemp();
//                    break;
//                case GeothermalParser.gLeaveTemp:
//                    message = "离开模式温度:" + mGeothermalBackInfo.getgLeaveTemp();
//                    break;
//                default:
//                    break;
//
//            }
//
//
//            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_GEOTHERMAL_MODULE:
                    if (appliancesInfo.getChannelNum() == event.getAppliancesInfo().getChannelNum()) {
                        if (!event.isSuccess()) {
                            showToast("获取地热状态失败,请重新再试");
                            return;
                        }
                        String message = "";
                        GeothermalBackInfo mGeothermalBackInfo = new GeothermalBackInfo(event.appliancesInfo, false);
 
                        if (mGeothermalBackInfo == null) {
                            showToast("获取地热状态失败,请重新再试");
                            return;
                        }
 
                        showGeothermalBackInfo(mGeothermalBackInfo);
 
                    }
                    break;
            }
        }
    }
 
    /**
     * showGeothermalBackInfo
     * @param mGeothermalBackInfo
     */
    private void showGeothermalBackInfo(GeothermalBackInfo mGeothermalBackInfo){
        String message = "";
        if(mGeothermalBackInfo.getIsOn() == GeothermalParser.gSwichOn) {
            message = getSwichStateString(mGeothermalBackInfo.getIsOn());
 
            message += "\n" + getModeStateString(mGeothermalBackInfo.getgMode());
            message += "\n" + "普通模式温度:" + mGeothermalBackInfo.getgNormalTemp();
            message += "\n" + "白天模式温度:" + mGeothermalBackInfo.getgDayTemp();
            message += "\n" + "夜间模式温度:" + mGeothermalBackInfo.getgNightTemp();
            message += "\n" + "离开模式温度:" + mGeothermalBackInfo.getgLeaveTemp();
        }else {
            message = getSwichStateString(mGeothermalBackInfo.getIsOn());
        }
 
 
        mTextView.setText(message);
        showToast(message);
        HDLLog.I(message);
    }
}