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
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
package com.hdl.sdk.ttl_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.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.DeviceStateEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.LightFeedBackEvent;
import com.hdl.sdk.ttl_sdk.R;
import com.hdl.sdk.ttl_sdk.base.BaseActivity;
import com.hdl.sdk.ttl_sdk.utlis.HDLLog;
import com.hdl.sdk.ttl_sdk.utlis.HDLUtlis;
 
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
/**
 * Created by JLChen on 2019/7/4
 * 调光模块,继电器模块控制页面
 */
public class CtrlLightActivity extends BaseActivity {
    /**
     * Topbar
     */
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
    private Button lightBtn, brightnessBtn;
    private EditText brightnessEt;
    private TextView lightText;
    private AppliancesInfo appliancesInfo;
    private int lightState;
 
    /**
     * 复写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_light);
        initToolbar();
        initView();
        initOnClick();
        initcurState();
        displayStateView();
 
//        //从SDK本地获取
//        HDLCommand.getDeviceStateFromLocal(appliancesInfo);
        //从网络上查询刷新一次设备状态,待调试
        HDLCommand.getLightDeviceStateFromNetwork(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() {
        lightBtn = findViewById(R.id.ctrlbtn);
        lightText = findViewById(R.id.lightText);
        brightnessBtn = findViewById(R.id.ctrl_brightness);
        brightnessEt = findViewById(R.id.et_brightness);
 
    }
 
    private void initOnClick() {
 
        lightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HDLCommand.lightCtrl(appliancesInfo, lightState);
                HDLLog.Log("lightBtn: 开关灯" + lightState);
            }
        });
        brightnessBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String brightnessStr = brightnessEt.getText().toString();
                if (TextUtils.isEmpty(brightnessStr)) {
                    showToast("设置的亮度不能为空");
                    return;
                }
                HDLCommand.lightCtrl(appliancesInfo, Integer.parseInt(brightnessStr));
                HDLLog.Log("brightnessBtn: 设置亮度" + brightnessStr);
            }
        });
    }
 
    private void displayStateView() {
        switch (appliancesInfo.getDeviceType()) {
            case HDLApConfig.TYPE_LIGHT_DIMMER:
            case HDLApConfig.TYPE_LIGHT_RELAY:
            case HDLApConfig.TYPE_LIGHT_MIX_DIMMER:
            case HDLApConfig.TYPE_LIGHT_MIX_RELAY:
                if (appliancesInfo.getCurState() != null) {
                    int curLightState = HDLUtlis.getIntegerByObject(appliancesInfo.getCurState());
 
                    lightText.setText("当前灯光亮度:" + curLightState);
 
                    if (curLightState == 0) {
                        lightState = 100;
                        lightBtn.setText("开灯");
                    } else {
                        lightState = 0;
                        lightBtn.setText("关灯");
                    }
                } else {
                    lightText.setText("未获取到灯光亮度");
                    lightBtn.setText("未获取到灯光亮度");
                }
                break;
 
            default:
                finish();//设备类型不对结束页面
                break;
        }
        /**根据需求是否发送一次获取刷新状态请求*/
    }
 
    /**
     * 灯光控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLightFeedBackInfoEventMain(LightFeedBackEvent event) {
 
        if (event.getLightCtrlBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getLightCtrlBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getLightCtrlBackInfo().getChannelNum() == appliancesInfo.getChannelNum()
                && event.getLightCtrlBackInfo().getPhysicsChannelNum() == appliancesInfo.getPhysicsChannelNum()
 
        ) {
            //        先判断是否超时
            if (!event.isSuccess()) {
                showToast("灯光控制超时,请重新再试");
                lightBtn.setText("灯光控制超时,请重新再试");
                return;
            }
            int brightness = event.getLightCtrlBackInfo().getBrightness();
            lightText.setText("当前亮度 = " + brightness);
 
            if (brightness == 0) {
                lightState = 100;
                lightBtn.setText("开灯");
            } else {
                lightState = 0;
                lightBtn.setText("关灯");
            }
 
        /*以下为灯光推送示例代码,可以识别哪个继电器,哪个调光灯,哪个回路,也可用作控制回馈。
        按需求调用*/
            String remarks = event.getLightCtrlBackInfo().getRemarks();//获取返回的灯光备注。如果每个灯光回路备注都唯一,可以直接通过备注判断
            String parentRemarks = event.getLightCtrlBackInfo().getParentRemarks();//获取继电器或调光灯备注。这里可以知道是哪个设备返回的
            int num = event.getLightCtrlBackInfo().getChannelNum();//获取回路号。这里可以获取到这个继电器或调光灯的回路号
            showToast(remarks + " 回路,回路号为:" + num + " 返回" + " 亮度为:" + brightness);
            HDLLog.Log("当前亮度 = " + brightness);
        }
    }
 
    /**
     * 获取单一设备状态回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeviceStateEventMain(DeviceStateEvent event) {
        if (event.getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
        ) {
            //这个返回的信息是当前状态的
            switch (event.getAppliancesInfo().getDeviceType()) {
                case HDLApConfig.TYPE_LIGHT_DIMMER:
                case HDLApConfig.TYPE_LIGHT_RELAY:
                case HDLApConfig.TYPE_LIGHT_MIX_DIMMER:
                case HDLApConfig.TYPE_LIGHT_MIX_RELAY:
                    if (appliancesInfo.getChannelNum() == event.getAppliancesInfo().getChannelNum()) {
                        if (!event.isSuccess()) {
                            showToast("获取灯光状态失败,请重新再试");
                            return;
                        }
                        int brightness = HDLUtlis.getIntegerByObject(event.getAppliancesInfo().getCurState());
                        if (brightness == 0) {
                            lightState = 100;
                            lightBtn.setText("开灯");
                        } else {
                            lightState = 0;
                            lightBtn.setText("关灯");
                        }
                        lightText.setText("当前亮度 = " + brightness);
                        showToast("获取状态返回:亮度 = " + brightness);
                    }
                    break;
                default:
                    //不处理
                    break;
            }
        }
    }
}