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("当前状态:关");
|
}
|
}
|
|
}
|
|
}
|