JLChen
2021-11-05 de72a7843ceb868c89fc11983e315849caa28573
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
package com.hdl.sdk.ttl_sdk.activity;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.hdl.sdk.ttl.HDLAppliances.Config.HDLApConfig;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.AppliancesInfo;
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLCommand;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.CommonSwitchCtrlBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.CommonSwitchStateBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.LogicFeedBackEvent;
import com.hdl.sdk.ttl_sdk.R;
import com.hdl.sdk.ttl_sdk.base.BaseActivity;
 
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
/**
 * 通用开关设备演示
 */
public class CtrlCommonSwitchActivity extends BaseActivity {
 
    /**Topbar*/
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
    private Button btnOpen,btnClose;
    private TextView switchText;
    private AppliancesInfo appliancesInfo;
    private int switchState = 0;
 
 
    /**
     * 复写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_logic);
        initToolbar();
        initcurState();
        initView();
        initOnClick();
        displayStateView();
 
        //从网络上查询刷新一次设备状态,待调试
        HDLCommand.getCommonSwitchStateFromNetwork(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() {
        btnOpen = findViewById(R.id.btnOpen);
 
        btnClose = findViewById(R.id.btnClose);
        switchText = findViewById(R.id.switchText);
 
 
 
 
 
    }
 
    private void initOnClick() {
 
        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //0=关 255=开
                HDLCommand.commonSwitchCtrl(appliancesInfo, 255);
            }
        });
 
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HDLCommand.commonSwitchCtrl(appliancesInfo,0);
            }
        });
    }
 
    private void displayStateView(){
 
        switch (appliancesInfo.getDeviceType()) {
            case HDLApConfig.TYPE_COMMON_SWITCH:
                break;
            default:
                finish();//设备类型不对结束页面
                break;
        }
        /**根据需求是否发送一次获取刷新状态请求*/
 
    }
 
 
    /**
     * 逻辑模块控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCommonSwitchCtrlEventMain(CommonSwitchCtrlBackEvent event) {
        if (event.getCommonSwitchBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getCommonSwitchBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getCommonSwitchBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()
        ) {
            if (!event.isSuccess()) {
                showToast("通用开关控制超时,请重新再试");
                return;
            }
 
            showToast("通用开关控制成功");
 
            switchState =  event.getCommonSwitchBackInfo().getSwitchState();
            if(switchState>0){
                 switchText.setText("当前状态:开");
            }else{
                switchText.setText("当前状态:关");
            }
        }
 
    }
 
    /**
     * 逻辑模块控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCommonSwitchStateEventMain(CommonSwitchStateBackEvent event) {
        if (event.getCommonSwitchBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getCommonSwitchBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getCommonSwitchBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()
        ) {
            if (!event.isSuccess()) {
                showToast("通用开关读取状态超时,请重新再试");
                return;
            }
 
//            showToast("通用开关控制成功");
 
            switchState =  event.getCommonSwitchBackInfo().getSwitchState();
            if(switchState>0){
                switchText.setText("当前状态:开");
            }else{
                switchText.setText("当前状态:关");
            }
        }
 
    }
 
}