JLChen
2020-10-20 1884d3f3eba4c5193e0a5f71853c42ef9cd131b8
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
package com.hdl.sdk.hdl_sdk.activity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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.CCTBackInfo;
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.LightCCTCtrlBackEvent;
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 CtrlCCTLightActivity extends BaseActivity {
    /**
     * Topbar
     */
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
    private Button btnOpen, btnClose;
    //    private EditText etR,etG,etB;
    private SeekBar seekBar, seekBarCCT;
    private TextView switchText;
    private AppliancesInfo appliancesInfo;
    private int settingBrightness = 0;
    private int settingCCT = 2000;
    private CCTBackInfo mCCTBackInfo;
 
    //默认色温范围值 2000K 到7000K,不一定所有设备都是这个范围
    private int MIN_CCT = 2000;
    private int MAX_CCT = 7000;
 
 
    /**
     * 复写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_cct_light);
 
        initToolbar();
        initcurState();
        initView();
        initOnClick();
        displayStateView();
        mCCTBackInfo = new CCTBackInfo(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);
        seekBar = findViewById(R.id.sb_brightness);
        seekBarCCT = findViewById(R.id.sb_cct);
        switchText = findViewById(R.id.switchText);
    }
 
    private void initOnClick() {
 
 
        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int brightness = settingBrightness == 0 ? 100 : settingBrightness;
                if (settingCCT > MAX_CCT) {
                    settingCCT = MAX_CCT;
                } else if (settingCCT < MIN_CCT) {
                    settingCCT = MIN_CCT;
                }
                HDLCommand.lightCCTCtrl(appliancesInfo, brightness, settingCCT);
            }
        });
 
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (settingCCT > MAX_CCT) {
                    settingCCT = MAX_CCT;
                } else if (settingCCT < MIN_CCT) {
                    settingCCT = MIN_CCT;
                }
                HDLCommand.lightCCTCtrl(appliancesInfo, 0, settingCCT);
            }
        });
 
        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();
                }
                if (settingCCT > MAX_CCT) {
                    settingCCT = MAX_CCT;
                } else if (settingCCT < MIN_CCT) {
                    settingCCT = MIN_CCT;
                }
                HDLCommand.lightCCTCtrl(appliancesInfo, seekBar.getProgress(), settingCCT);
            }
        });
 
        seekBarCCT.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) {
                settingCCT = seekBarProgressToValue(MIN_CCT, MAX_CCT, seekBar.getProgress());
                if (settingCCT > MAX_CCT) {
                    settingCCT = MAX_CCT;
                } else if (settingCCT < MIN_CCT) {
                    settingCCT = MIN_CCT;
                }
 
                HDLLog.I("色温设置:" + settingCCT + "K");
                int brightness = settingBrightness == 0 ? 100 : settingBrightness;
                HDLCommand.lightCCTCtrl(appliancesInfo, brightness, settingCCT);
            }
        });
 
    }
 
    private void displayStateView() {
 
        switch (appliancesInfo.getDeviceType()) {
            case HDLApConfig.TYPE_LIGHT_CCT:
                break;
            default:
                finish();//设备类型不对结束页面
                break;
        }
        /**根据需求是否发送一次获取刷新状态请求*/
 
    }
 
 
    /**
     * showCCTBackInfo
     *
     * @param mCCTBackInfo
     */
    private void showCCTBackInfo(CCTBackInfo mCCTBackInfo) {
        String message = "";
        if (mCCTBackInfo.getBrightness() > 0) {
            //如果大于0才记住当前设置的亮度值,下次打开时继续这个亮度值
            settingBrightness = mCCTBackInfo.getBrightness();
 
            message = "当前状态:开";
            message += "\n" + "亮度:" + mCCTBackInfo.getBrightness();
            message += "\n" + "色温值:" + mCCTBackInfo.getCctValue() + "K";
 
        } else {
            message = "当前状态:关";
        }
 
        settingCCT = mCCTBackInfo.getCctValue();
        seekBar.setProgress(mCCTBackInfo.getBrightness());
        int cctProgress = seekBarValueToProgress(MIN_CCT, MAX_CCT, mCCTBackInfo.getCctValue());
        seekBarCCT.setProgress(cctProgress);
        switchText.setText(message);
        showToast(message);
        HDLLog.I(message);
    }
 
    /**
     * RGB控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLightCCTCtrlBackEventMain(LightCCTCtrlBackEvent event) {
        if (event.getCCTBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getCCTBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getCCTBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()
        ) {
            if (!event.isSuccess()) {
                showToast("RGB灯控制超时,请重新再试");
                return;
            }
 
//            showToast("RGB灯控制成功");
            mCCTBackInfo = event.getCCTBackInfo();
            showCCTBackInfo(mCCTBackInfo);
        }
    }
 
 
    @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_CCT:
                    if (appliancesInfo.getChannelNum() == event.getAppliancesInfo().getChannelNum()) {
                        if (!event.isSuccess()) {
                            showToast("获取CCT灯状态失败,请重新再试");
                            return;
                        }
                        String message = "";
                        CCTBackInfo cctBackInfo = new CCTBackInfo(event.appliancesInfo);
                        if (cctBackInfo == null) {
                            showToast("获取CCT灯状态失败,请重新再试");
                            return;
                        }
                        mCCTBackInfo = cctBackInfo;
                        showCCTBackInfo(mCCTBackInfo);
                    }
                    break;
            }
        }
    }
 
 
    /**
     * 根据滑条刻度值,给定最大最小值 计算出对应显示值
     * 滑条刻度值范围0~100
     *
     * @param maxValue  最大值
     * @param minValue  最小值
     * @param mProgress 滑条值
     * @return 计算后的显示值
     */
    private int seekBarProgressToValue(int minValue, int maxValue, int mProgress) {
        int progress = mProgress;
 
        if (progress < 0) {
            progress = 0;
        } else if (progress > 100) {
            progress = 100;
        }
        int intValue = 0;
        intValue = (maxValue - minValue) * progress / 100 + minValue;
        return intValue;
    }
 
    /**
     * 根据显示值,给定最大最小值 计算出对滑条刻度
     * 滑条刻度值范围0~100
     *
     * @param maxValue  最大值
     * @param minValue  最小值
     * @param mIntValue 显示值
     * @return 计算后的滑条值
     */
    private int seekBarValueToProgress(int minValue, int maxValue, int mIntValue) {
        int intValue = mIntValue;
 
        if (intValue < minValue) {
            intValue = minValue;
        } else if (intValue > maxValue) {
            intValue = maxValue;
        }
        int progress = 0;
        progress = (intValue - minValue) * 100 / (maxValue - minValue);
        return progress;
 
 
    }
}