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; } } } }