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
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.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.hdl.sdk.ttl.HDLAppliances.Config.HDLApConfig;
import com.hdl.sdk.ttl.HDLAppliances.HDLCurtain.Parser.CurtainCtrlParser;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.AppliancesInfo;
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLCommand;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.CurtainDeviceStateEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.CurtainFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.DeviceStateEvent;
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;
 
import java.math.BigDecimal;
 
/**
 * Created by JLChen on 2019/7/4
 * 窗帘类模块控制页面
 */
public class CtrlCurtainActivity extends BaseActivity {
    /**
     * Topbar
     */
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
    private Button curtainBtn, curtainOpenBtn, curtainCloseBtn, curtainStopBtn, curtainPrecentBtn;
    private Button curtainUp, curtainDown;
    private TextView curText1, curText2;
    private AppliancesInfo appliancesInfo;
    private int curtainState;
    private String showMessage;
    private EditText etPrecent;
 
    /**
     * 复写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_curtain);
        initToolbar();
        initView();
        initOnClick();
        initcurState();
        displayStateView();
        HDLCommand.getCurtainDeviceStateFromNetwork(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() {
        curtainBtn = findViewById(R.id.curtainbtn);
        curtainOpenBtn = findViewById(R.id.curtain_openbtn);
        curtainCloseBtn = findViewById(R.id.curtain_closebtn);
        curtainStopBtn = findViewById(R.id.curtain_stopbtn);
        curtainPrecentBtn = findViewById(R.id.curtain_precentbtn);
        curText1 = findViewById(R.id.curtainText1);
        curText2 = findViewById(R.id.curtainText2);
        curtainUp = findViewById(R.id.curtain_upbtn);
        curtainDown = findViewById(R.id.curtain_downbtn);
        etPrecent = findViewById(R.id.et_curtain_precent);
    }
 
    private void initOnClick() {
        /*type:
        0= HDL 窗帘控制版本V1.0(窗帘号<17 0 = 停止, 1 = 打开, 2 = 关闭
                              窗帘号>=17 0-100%,表示打开百分比)
        1=窗帘控制V1.1开关停
        2=窗帘控制V1.1百分比
        3=窗帘控制V1.1角度向上
        4=窗帘控制V1.1角度向下
        5=点动*/
        curtainBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //窗帘模块第二个参数 为CurtainCtrlParser.curtainOn,CurtainCtrlParser.curtainOff,CurtainCtrlParser.curtainPause其中一个
                if (curtainState == CurtainCtrlParser.curtainOn) {
                    curtainState = CurtainCtrlParser.curtainOff;
                } else {
                    curtainState = CurtainCtrlParser.curtainOn;
                }
                //窗帘模块用V1.0
                HDLCommand.curtainCtrl(appliancesInfo, curtainState, 0);
 
            }
        });
 
        curtainOpenBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HDLCommand.curtainCtrl(appliancesInfo, CurtainCtrlParser.curtainOn, 1);
            }
        });
 
        curtainCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HDLCommand.curtainCtrl(appliancesInfo, CurtainCtrlParser.curtainOff, 1);
            }
        });
 
        curtainStopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HDLCommand.curtainCtrl(appliancesInfo, CurtainCtrlParser.curtainPause, 1);
            }
        });
 
        curtainPrecentBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(etPrecent.getText().toString())) {
                    Toast.makeText(CtrlCurtainActivity.this, "请输入需要控制的窗帘百分比!", Toast.LENGTH_SHORT).show();
                    return;
                }
 
                HDLCommand.curtainCtrl(appliancesInfo, Integer.parseInt(etPrecent.getText().toString()), 2);//50为百分比
            }
        });
 
        curtainUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HDLCommand.curtainCtrl(appliancesInfo, 0, 5);//上点动0x00,state固定为0
            }
        });
 
        curtainDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HDLCommand.curtainCtrl(appliancesInfo, 128, 5);//上点动0x80,state固定为128
            }
        });
    }
 
    private void displayStateView() {
        if (appliancesInfo.getDeviceType() == HDLApConfig.TYPE_CURTAIN_MODULE) {
            //窗帘模块
            curText2.setVisibility(View.GONE);
            curtainOpenBtn.setVisibility(View.GONE);
            curtainCloseBtn.setVisibility(View.GONE);
            curtainStopBtn.setVisibility(View.GONE);
            curtainPrecentBtn.setVisibility(View.GONE);
            curtainUp.setVisibility(View.GONE);
            curtainDown.setVisibility(View.GONE);
 
            if (appliancesInfo.getCurState() != null) {
                String stringCurtainState = "";
                /**注意类型 curState保存本地再获取后后变成Double类型了*/
                int curCurtainState = HDLUtlis.getIntegerByObject(appliancesInfo.getCurState());
 
                HDLLog.Log("curCurtainState = " + curCurtainState);
                switch (curCurtainState) {
                    case CurtainCtrlParser.TYPE_STATE_PAUSE:
                        stringCurtainState += "窗帘模块停止状态";
                        curtainState = CurtainCtrlParser.curtainOn;//初始化窗帘控制状态
                        break;
                    case CurtainCtrlParser.TYPE_STATE_OPEN:
                        stringCurtainState += "窗帘模块开状态";
                        curtainState = CurtainCtrlParser.curtainOff;//初始化窗帘控制状态
                        break;
                    case CurtainCtrlParser.TYPE_STATE_CLOSE:
                        stringCurtainState += "窗帘模块关状态";
                        curtainState = CurtainCtrlParser.curtainOn;//初始化窗帘控制状态
                        break;
                    default:
                        stringCurtainState = "未获取到窗帘模块状态";
                        curtainState = CurtainCtrlParser.curtainOff;//初始化窗帘控制状态
                        break;
                }
                curText1.setText(stringCurtainState);
                curtainBtn.setText(stringCurtainState);
            } else {
                curText1.setText("未获取到窗帘模块状态");
                curtainBtn.setText("未获取到窗帘模块状态");
            }
 
        } else if (appliancesInfo.getDeviceType() == HDLApConfig.TYPE_CURTAIN_ROLLER
                || appliancesInfo.getDeviceType() == HDLApConfig.TYPE_CURTAIN_GLYSTRO) {
            //开合帘、卷帘
            curText1.setVisibility(View.GONE);
            curtainBtn.setVisibility(View.GONE);
            curtainUp.setVisibility(View.GONE);
            curtainDown.setVisibility(View.GONE);
            if (appliancesInfo.getCurState() != null) {
                curText2.setText("当前窗帘状态:" + HDLUtlis.getIntegerByObject(appliancesInfo.getCurState()));
            } else {
                curText2.setText("未获取到窗帘模块状态");
            }
        } else if (appliancesInfo.getDeviceType() == HDLApConfig.TYPE_CURTAIN_SHANGRILA) {
            //香格里拉帘
            curText1.setVisibility(View.GONE);
            curtainBtn.setVisibility(View.GONE);
            if (appliancesInfo.getCurState() != null) {
                curText2.setText("当前窗帘状态:" + HDLUtlis.getIntegerByObject(appliancesInfo.getCurState()));
            } else {
                curText2.setText("未获取到窗帘模块状态");
            }
        } else {
            //设备类型不对结束页面
            finish();
        }
 
        /**根据需求是否发送一次获取刷新状态请求*/
 
    }
 
 
    /**
     * 窗帘模块控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCurtainFeedBackInfoEventMain(CurtainFeedBackEvent event) {
//        先判断是否超时
        if (event.getCurtainCtrlBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getCurtainCtrlBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getCurtainCtrlBackInfo().getNum() == appliancesInfo.getChannelNum()) {
 
            if (!event.isSuccess()) {
                showToast("窗帘控制超时,请重新再试");
                return;
            }
 
            int curState = event.getCurtainCtrlBackInfo().getState();
            int precent = event.getCurtainCtrlBackInfo().getPrecent();
            //窗帘模块:curState:0=停止,1=打开,2=关闭。
            //开合帘电机,卷帘电机,香格里拉帘:curState:1-100开合度。也会返回0,1,2的状态
            //                            precent:1-100开合度
            //建议开合帘电机,卷帘电机按停止后再读取当前状态来获取当前状态值
 
            String remarks = event.getCurtainCtrlBackInfo().getRemarks();
            int num = event.getCurtainCtrlBackInfo().getNum();
            HDLLog.Log(remarks + " 回路号:" + num + " 返回" + " 状态为:" + curState);
            if (event.getCurtainCtrlBackInfo().getAppliancesInfo().getDeviceType() == HDLApConfig.TYPE_CURTAIN_MODULE) {
                showMessage = "";
                //判断是否为窗帘模块
                switch (curState) {
                    case CurtainCtrlParser.curtainOff:
                        showMessage = "窗帘关";
                        curtainBtn.setText(showMessage);
                        curText1.setText(showMessage);
                        curtainState = curState;
                        HDLLog.Log("窗帘控制 :窗帘关" + "  回路号:" + num);
                        break;
                    case CurtainCtrlParser.curtainOn:
                        showMessage = "窗帘开";
                        curtainBtn.setText(showMessage);
                        curText1.setText(showMessage);
                        curtainState = curState;
                        HDLLog.Log("窗帘控制 :窗帘开" + "  回路号:" + num);
                        break;
                    case CurtainCtrlParser.curtainPause:
                        showMessage = "窗帘暂停";
                        curtainBtn.setText(showMessage);
                        curText1.setText(showMessage);
                        curtainState = curState;
                        HDLLog.Log("窗帘控制 :窗帘暂停" + "  回路号:" + num);
                        break;
                }
                showToast(showMessage);
            } else {
                //新协议,type=2为百分比
                if (event.getCurtainCtrlBackInfo().getType() == 2) {
                    //开合帘或卷帘 显示百分比
                    curText2.setText("窗帘开到" + precent + "%");
                    curtainState = precent;
                } else if (event.getCurtainCtrlBackInfo().getType() == 0) {
                    //开合帘或卷帘 显示百分比
                    curText2.setText("窗帘开到" + curState + "%");
                    curtainState = curState;
                } else {
                    //开合帘或卷帘 显示百分比
                    curText2.setText("窗帘开到" + curState + "%");
                    curtainState = curState;
                }
            }
 
        }
 
    }
 
    /**
     * 窗帘新协议状态读取回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCurtainDeviceStateEventMain(CurtainDeviceStateEvent event) {
        if (event.getCurtainCtrlBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getCurtainCtrlBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getCurtainCtrlBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()) {
 
            if (!event.isSuccess()) {
                showToast("获取窗帘状态失败,请重新再试");
                return;
            }
 
            int curState = event.getCurtainCtrlBackInfo().getState();
            int curPrecent = event.getCurtainCtrlBackInfo().getPrecent();
            //窗帘模块:curState:0=停止,1=打开,2=关闭。
            //开合帘电机,卷帘电机,香格里拉帘:curState:1-100开合度。也会返回0,1,2的状态
            //                            precent:1-100开合度
            //建议开合帘电机,卷帘电机按停止后再读取当前状态来获取当前状态值
 
            String remarks = event.getCurtainCtrlBackInfo().getRemarks();
            int num = event.getCurtainCtrlBackInfo().getNum();
            HDLLog.Log(remarks + " 回路号:" + num + " 返回" + " 状态为:" + curState);
 
            switch (event.getCurtainCtrlBackInfo().getAppliancesInfo().getDeviceType()) {
                case HDLApConfig.TYPE_CURTAIN_GLYSTRO:
                case HDLApConfig.TYPE_CURTAIN_ROLLER:
                case HDLApConfig.TYPE_CURTAIN_SHANGRILA:
                    if (event.getCurtainCtrlBackInfo().getType() == 2) {//新协议,type=2为百分比
                        //开合帘或卷帘 显示百分比
                        int precent = event.getCurtainCtrlBackInfo().getPrecent();
                        showMessage = "窗帘开到" + precent + "%";
                        curText2.setText(showMessage);
                        showToast("获取成功:" + showMessage);
                    } else if (event.getCurtainCtrlBackInfo().getType() == 0) {//旧协议,type=0为百分比
                        //开合帘或卷帘 显示百分比
                        int state = event.getCurtainCtrlBackInfo().getState();
                        showMessage = "窗帘开到" + state + "%";
                        curText2.setText(showMessage);
                        showToast("获取成功:" + showMessage);
                    }
 
                    break;
                default:
                    break;
            }
        }
    }
 
    /**
     * 获取单一设备状态回调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_CURTAIN_GLYSTRO:
                case HDLApConfig.TYPE_CURTAIN_ROLLER:
                case HDLApConfig.TYPE_CURTAIN_MODULE:
                case HDLApConfig.TYPE_CURTAIN_SHANGRILA:
                    if (appliancesInfo.getChannelNum() == event.getAppliancesInfo().getChannelNum()) {
                        if (!event.isSuccess()) {
                            showToast("获取窗帘状态失败,请重新再试");
                            return;
                        }
                        showMessage = "";
                        //窗帘模块:curState:0=停止,1=打开,2=关闭。
                        //开合帘电机,卷帘电机:curState:1-100开合度。
                        int curState = HDLUtlis.getIntegerByObject(event.getAppliancesInfo().getCurState());
                        if (event.getAppliancesInfo().getDeviceType() == HDLApConfig.TYPE_CURTAIN_MODULE) {//判断是否为窗帘模块,否则为开合帘或卷帘电机
                            switch (curState) {
                                case CurtainCtrlParser.curtainOff:
                                    showMessage = "窗帘关";
                                    curtainBtn.setText(showMessage);
                                    curText1.setText(showMessage);
                                    curtainState = curState;
                                    break;
                                case CurtainCtrlParser.curtainOn:
                                    showMessage = "窗帘开";
                                    curtainBtn.setText(showMessage);
                                    curText1.setText(showMessage);
                                    curtainState = curState;
                                    break;
                                case CurtainCtrlParser.curtainPause:
                                    showMessage = "窗帘暂停";
                                    curtainBtn.setText(showMessage);
                                    curText1.setText(showMessage);
                                    curtainState = curState;
                                    break;
                            }
                        } else {
                            showMessage = "窗帘开到" + curState + "%";
                            curText2.setText(showMessage);
                            curtainState = curState;
                        }
 
                        showToast("获取成功:" + showMessage);
                    }
                    break;
                default:
                    break;
            }
        }
    }
 
}