JLChen
2021-08-10 c4b015770e8a29f18e19cc44b3df46c20a4762f4
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
package com.hdl.sdk.hdl_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.SeekBar;
import android.widget.TextView;
 
import com.hdl.sdk.hdl_core.HDLAppliances.Config.HDLApConfig;
import com.hdl.sdk.hdl_core.HDLAppliances.HDLLight.RGBBackInfo;
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.LightRGBCtrlBackEvent;
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 CtrlRGBLightActivity extends BaseActivity {
    /**
     * Topbar
     */
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
    private Button btnOpen, btnClose, btnRGB;
    private EditText etR, etG, etB;
    private SeekBar seekBar;
    private TextView switchText;
    private AppliancesInfo appliancesInfo;
    private int settingBrightness = 0;
    private RGBBackInfo mRGBBackInfo;
 
//    private RGBLightBean mRGBLightBean;
 
 
    /**
     * 复写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_r_g_b_light);
 
        initToolbar();
        initcurState();
        initView();
        initOnClick();
        displayStateView();
        mRGBBackInfo = new RGBBackInfo(appliancesInfo);
 
        //读取状态
        HDLCommand.getRgbOrCctStateFromNetwork(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");
 
    }
 
    private void initView() {
        btnOpen = findViewById(R.id.btnOpen);
        btnClose = findViewById(R.id.btnClose);
        btnRGB = findViewById(R.id.btnRGB);
        seekBar = findViewById(R.id.sb_brightness);
        etR = findViewById(R.id.et_r);
        etG = findViewById(R.id.et_g);
        etB = findViewById(R.id.et_b);
 
        switchText = findViewById(R.id.switchText);
    }
 
    private void initOnClick() {
        btnRGB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(etR.getText().toString()) || TextUtils.isEmpty(etG.getText().toString()) || TextUtils.isEmpty(etB.getText().toString())) {
                    showToast("RGB值 不能为空");
                } else {
                    int rStatus = Integer.parseInt(etR.getText().toString());
                    int gStatus = Integer.parseInt(etG.getText().toString());
                    int bStatus = Integer.parseInt(etB.getText().toString());
                    if (rStatus < 0 || rStatus > 255 || gStatus < 0 || gStatus > 255 || bStatus < 0 || bStatus > 255) {
                        showToast("RGB值范围 0~255");
                        return;
                    }
                    int brightness = settingBrightness == 0 ? 100 : settingBrightness;
                    HDLCommand.lightRGBCtrl(appliancesInfo, brightness, rStatus, gStatus, bStatus);
                }
            }
        });
 
        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int brightness = settingBrightness == 0 ? 100 : settingBrightness;
                HDLCommand.lightRGBCtrl(appliancesInfo, brightness, mRGBBackInfo.getrStatus(), mRGBBackInfo.getgStatus(), mRGBBackInfo.getbStatus());
            }
        });
 
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HDLCommand.lightRGBCtrl(appliancesInfo, 0, mRGBBackInfo.getrStatus(), mRGBBackInfo.getgStatus(), mRGBBackInfo.getbStatus());
            }
        });
 
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
 
            }
 
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
 
            }
 
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (seekBar.getProgress() > 0) {
                    settingBrightness = seekBar.getProgress();
                }
 
                HDLCommand.lightRGBCtrl(appliancesInfo, seekBar.getProgress(), mRGBBackInfo.getrStatus(), mRGBBackInfo.getgStatus(), mRGBBackInfo.getbStatus());
            }
        });
    }
 
    private void displayStateView() {
 
        switch (appliancesInfo.getDeviceType()) {
            case HDLApConfig.TYPE_LIGHT_RGB:
                break;
            default:
                finish();//设备类型不对结束页面
                break;
        }
        /**根据需求是否发送一次获取刷新状态请求*/
 
    }
 
 
    /**
     * showRGBBackInfoInfo
     *
     * @param mRGBBackInfo
     */
    private void showRGBBackInfoInfo(RGBBackInfo mRGBBackInfo) {
        String message = "";
        if (mRGBBackInfo.getBrightness() > 0) {
            //如果大于0才记住当前设置的亮度值,下次打开时继续这个亮度值
            settingBrightness = mRGBBackInfo.getBrightness();
            message = "当前状态:开";
            message += "\n" + "亮度:" + mRGBBackInfo.getBrightness();
            message += "\n" + "R:" + mRGBBackInfo.getrStatus();
            message += "\n" + "G:" + mRGBBackInfo.getgStatus();
            message += "\n" + "B:" + mRGBBackInfo.getbStatus();
        } else {
            message = "当前状态:关";
        }
 
 
        seekBar.setProgress(mRGBBackInfo.getBrightness());
        etR.setText(String.valueOf(mRGBBackInfo.getrStatus()));
        etG.setText(String.valueOf(mRGBBackInfo.getgStatus()));
        etB.setText(String.valueOf(mRGBBackInfo.getbStatus()));
        switchText.setText(message);
        showToast(message);
        HDLLog.I(message);
    }
 
    /**
     * RGB控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRGBCtrlBackEventMain(LightRGBCtrlBackEvent event) {
        if (event.getRGBBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getRGBBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getRGBBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()
        ) {
            if (!event.isSuccess()) {
                showToast("RGB灯控制超时,请重新再试");
                return;
            }
 
//            showToast("RGB灯控制成功");
            mRGBBackInfo = event.getRGBBackInfo();
            showRGBBackInfoInfo(mRGBBackInfo);
        }
    }
 
 
    @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_LIGHT_RGB:
                    if (appliancesInfo.getChannelNum() == event.getAppliancesInfo().getChannelNum()) {
                        if (!event.isSuccess()) {
                            showToast("获取RGB状态失败,请重新再试");
                            return;
                        }
                        String message = "";
                        RGBBackInfo rgbBackInfo = new RGBBackInfo(event.appliancesInfo);
                        if (rgbBackInfo == null) {
                            showToast("获取RGB状态失败,请重新再试");
                            return;
                        }
                        mRGBBackInfo = rgbBackInfo;
                        showRGBBackInfoInfo(mRGBBackInfo);
                    }
                    break;
            }
        }
    }
}