panlili2024
2024-09-19 071a8328823a2861f93ce556a4da3e4119cab1a3
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
package com.hdl.sdk.ttl_sdk.activity;
 
 
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
 
import com.hdl.sdk.ttl.HDLAppliances.Config.HDLApConfig;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.AppliancesInfo;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.DevicesData;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.ScenesData;
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLCommand;
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLDeviceManager;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.DevicesInfoEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.LogicFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.SceneFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.ScenesInfoEvent;
import com.hdl.sdk.ttl.Utils.LogUtils.HDLLog;
import com.hdl.sdk.ttl_sdk.R;
import com.hdl.sdk.ttl_sdk.adapter.HDLMainListAdapter;
import com.hdl.sdk.ttl_sdk.adapter.HDLSceneListAdapter;
import com.hdl.sdk.ttl_sdk.base.BaseActivity;
 
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by panlili on 2024/01/19
 * 场景控制页面
 */
public class CtrlSceneActivity extends BaseActivity {
    /**
     * Topbar
     */
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
    private Button btnSearch;
    private RecyclerView mRecyclerView;
    private TextView tvResult;
 
    private List<ScenesData> mScenesDataList = new ArrayList<>();
    private ScenesData scenesData;
    private HDLSceneListAdapter hdlSceneListAdapter;
    private String subnetID, deviceID;
    private ProgressDialog mProgressDialog;
 
    public static void open(Context mContext, String subnetID, String deviceID) {
        Intent intent = new Intent(mContext, CtrlSceneActivity.class);
        intent.putExtra("subnetID", subnetID);
        intent.putExtra("deviceID", deviceID);
        mContext.startActivity(intent);
    }
 
    /**
     * 复写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_scene);
 
        subnetID = getIntent().getStringExtra("subnetID");
        deviceID = getIntent().getStringExtra("deviceID");
        initToolbar();
        initView();
    }
 
    /**
     * 初始化Toolbar
     */
    private void initToolbar() {
        topBarBack = findViewById(R.id.ll_top_b_left);
        setViewVisible(topBarBack);
        topBarTitle = findViewById(R.id.tv_top_b_header_title);
        topBarTitle.setText("场景");
        topBarBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
 
    private void initView() {
        btnSearch = findViewById(R.id.btn_search);
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(subnetID) || TextUtils.isEmpty(deviceID)) {
                    Toast.makeText(CtrlSceneActivity.this, "请输入网关的子网号设备号!", Toast.LENGTH_SHORT).show();
                    return;
                }
                /**全部重新搜索,清空原场景列表数据*/
                clearListView();
 
                HDLCommand.getHomeScenes(Integer.parseInt(subnetID), Integer.parseInt(deviceID));
                mProgressDialog.show();
            }
        });
 
        tvResult = findViewById(R.id.tv_scene);
 
        mRecyclerView = findViewById(R.id.listView_scenes);
        // 定义一个线性布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(this);
        // 设置布局管理器
        mRecyclerView.setLayoutManager(manager);
        // 设置adapter
        hdlSceneListAdapter = new HDLSceneListAdapter(this, mScenesDataList);
        mRecyclerView.setAdapter(hdlSceneListAdapter);
 
        hdlSceneListAdapter.setOnItemClickLitener(new HDLSceneListAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(int position) {
                //执行场景
                scenesData = mScenesDataList.get(position);
                HDLCommand.sceneCtrl(scenesData);
            }
        });
 
        mProgressDialog = new ProgressDialog(CtrlSceneActivity.this);
        mProgressDialog.setTitle("正在获取数据...");
        mProgressDialog.setMessage("请耐心等待");
        mProgressDialog.onStart();
 
    }
 
    /**
     * 清空数据并刷新列表
     */
    private void clearListView() {
        if (mScenesDataList != null) mScenesDataList.clear();
 
        hdlSceneListAdapter.notifyDataSetChanged();
    }
 
    /**
     * ====================EventBus 订阅事件 ====================
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onScenesInfoEventMain(ScenesInfoEvent event) {
        mProgressDialog.dismiss();
        if (!event.isSuccess()) {
            Toast.makeText(CtrlSceneActivity.this, "搜索超时,请重新再试", Toast.LENGTH_SHORT).show();
            tvResult.setText("搜索超时,请重新再试");
            return;
        }
        mScenesDataList = event.getDesSceneList();
        HDLLog.I("onScenesInfoEventMain size=" + mScenesDataList.size());
        hdlSceneListAdapter.setData(mScenesDataList);
        hdlSceneListAdapter.notifyDataSetChanged();
    }
 
    /**
     * 场景控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSceneFeedBackEventMain(SceneFeedBackEvent event) {
//        先判断是否超时
        if (event.getSceneCtrlBackInfo().getAreaCodeID() == scenesData.getAreaCodeID()
                && event.getSceneCtrlBackInfo().getSceneID() == scenesData.getSceneID()) {
            if (!event.isSuccess()) {
                showToast("场景控制超时,请重新再试");
                return;
            }
            showToast("场景控制成功");
        }
    }
 
 
}