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
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
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.HDLAppliances.HDLAirCondition.AirTechSysBackInfo;
import com.hdl.sdk.ttl.HDLAppliances.HDLAirCondition.Parser.AirCtrlParser;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.AppliancesInfo;
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLCommand;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.AirTechSysFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.DeviceStateEvent;
import com.hdl.sdk.ttl_sdk.R;
import com.hdl.sdk.ttl_sdk.base.BaseActivity;
import com.hdl.sdk.ttl_sdk.utlis.HDLLog;
 
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
 
/**
 * Created by panlili on 2024/5/28
 * KNX科技希桐控制页面
 * 科技系统 小类101
 * 5~35摄氏度(℃)
 */
public class CtrlAirKNXTechSysActivity extends BaseActivity {
    /**
     * Topbar
     */
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
 
    private Button airBtnSwitch, airBtnMode, airBtnTemp;
    private TextView airText, airHumidity;
    private EditText airTempEd;
    private AppliancesInfo appliancesInfo;
 
 
    private int airSwitchState;//Demo仅以此作为演示,实际请根据需求开发设计
    private int airModeState;
    private int airTempState;
 
    /**
     * 复写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_knxtechnology_system);
        initToolbar();
        initView();
        initOnClick();
        initcurState();
        showStateView();
 
        HDLCommand.getTechSysDeviceStateFromNetwork(appliancesInfo);
        HDLCommand.getTechSysDeviceHumidityFromNetwork(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() {
        airBtnSwitch = findViewById(R.id.airbtn_switch);
        airBtnMode = findViewById(R.id.airbtn_mode);
        airBtnTemp = findViewById(R.id.airbtn_tempBtn);
        airTempEd = findViewById(R.id.airet_tempet);
        airText = findViewById(R.id.airText);
        airHumidity = findViewById(R.id.airText_humidity);
    }
 
    private void initOnClick() {
        airBtnSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //演示当前状态为关,设置为开。开,设置为关。
                if (airSwitchState == 0) {
                    HDLCommand.airCtrl(appliancesInfo, AirCtrlParser.airSwich, AirCtrlParser.airOn);//空调开
                } else {
                    HDLCommand.airCtrl(appliancesInfo, AirCtrlParser.airSwich, AirCtrlParser.airOff);//空调关
                }
            }
        });
 
        airBtnMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (airModeState) {
                    case 0:
                        //若当前空调模式为制冷,则点击按钮设置为制热
                        HDLCommand.airCtrl(appliancesInfo, AirCtrlParser.airMode, AirCtrlParser.airModeHeatTem);//空调模式制热
                        break;
                    case 1:
                        //若当前空调模式为制热,则点击按钮设置为制冷
                        HDLCommand.airCtrl(appliancesInfo, AirCtrlParser.airMode, AirCtrlParser.airModeRefTem);//空调模式制冷
                        break;
                }
            }
        });
 
        airBtnTemp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tempStr = airTempEd.getText().toString();
                if (TextUtils.isEmpty(tempStr)) {
                    showToast("设置的温度不能为空");
                    return;
                }
                int tempInt = Integer.parseInt(tempStr);
 
                if (tempInt < 5 || tempInt > 35) {
                    showToast("温度设置范围为:5~35摄氏度(℃)");
                    return;
                }
 
                switch (airModeState) {
                    case 0:
                        //当前空调模式为制冷
                        HDLCommand.airCtrl(appliancesInfo, AirCtrlParser.refTem, tempInt);//制冷温度
                        break;
                    case 1:
                        //当前空调模式为制热
                        HDLCommand.airCtrl(appliancesInfo, AirCtrlParser.heatTem, tempInt);//制热温度
                        break;
                }
            }
        });
    }
 
 
    private void showStateView() {
        if (appliancesInfo.getArrCurState() != null) {
            AirTechSysBackInfo mAirTechSysBackInfo = new AirTechSysBackInfo(appliancesInfo);
            String stringState = "";
            if (mAirTechSysBackInfo.getIsOn() == AirCtrlParser.airOff) {
                stringState = "KNX科技系统模块:关闭";
            } else if (mAirTechSysBackInfo.getIsOn() == AirCtrlParser.airOn) {
                stringState = "KNX科技系统模块:打开";
 
                stringState += "\n" + getModeStateString(mAirTechSysBackInfo.getAirMode());//模式
                stringState += "\n室内温度:" + mAirTechSysBackInfo.getIndoorTemp();
 
                if (AirCtrlParser.airModeRefTem == mAirTechSysBackInfo.getAirMode()) {
                    stringState += "\n制冷模式温度:" + mAirTechSysBackInfo.getRefTemp();
                } else if (AirCtrlParser.airModeHeatTem == mAirTechSysBackInfo.getAirMode()) {
                    stringState += "\n制热模式温度:" + mAirTechSysBackInfo.getHeatTemp();
                }
 
            } else {
                stringState = "未知开关状态";
            }
            airText.setText(stringState);
        } else {
            airText.setText("未获取到KNX科技系统模块状态");
        }
    }
 
 
    /**
     * getModeStateString
     *
     * @param mState
     * @return
     */
    private String getModeStateString(int mState) {
        String mStrState = "未知";
        airModeState = mState;  //更新模式状态
        switch (mState) {
            case AirCtrlParser.airModeRefTem:
                mStrState = "模式:制冷";
                break;
            case AirCtrlParser.airModeHeatTem:
                mStrState = "模式:制热";
                break;
            default:
                mStrState = "未知模式状态";
                break;
        }
        return mStrState;
    }
 
 
    /**
     * getSwichStateString
     *
     * @param mState
     * @return
     */
    private String getSwichStateString(int mState) {
        String mStrState = "未知";
        airSwitchState = mState; //更新开关状态
        switch (mState) {
            case AirCtrlParser.airOn:
                mStrState = "KNX科技系统模块:打开";
                break;
            case AirCtrlParser.airOff:
                mStrState = "KNX科技系统模块:关闭";
                break;
            default:
                mStrState = "未知状态";
                break;
        }
        return mStrState;
    }
 
 
    private void showAirTechSysBackInfo(AirTechSysBackInfo mAirTechSysBackInfo) {
        String message = "";
        if (mAirTechSysBackInfo.getIsOn() == AirCtrlParser.airOn) {
            message = getSwichStateString(mAirTechSysBackInfo.getIsOn());
            message += "\n" + getModeStateString(mAirTechSysBackInfo.getAirMode());//模式
            message += "\n室内温度:" + mAirTechSysBackInfo.getIndoorTemp();
 
            if (AirCtrlParser.airModeRefTem == mAirTechSysBackInfo.getAirMode()) {
                message += "\n制冷模式温度:" + mAirTechSysBackInfo.getRefTemp();
            } else if (AirCtrlParser.airModeHeatTem == mAirTechSysBackInfo.getAirMode()) {
                message += "\n制热模式温度:" + mAirTechSysBackInfo.getHeatTemp();
            }
 
        } else {
            message = getSwichStateString(mAirTechSysBackInfo.getIsOn());
        }
        airText.setText(message);
//        showToast(message);
        HDLLog.Log(message);
        HDLLog.Log("室内湿度:" + mAirTechSysBackInfo.getIndoorHumidity());
    }
 
    /**
     * 科技系统模块控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAirTechSysFeedBackEventMain(AirTechSysFeedBackEvent event) {
        if (event.getAirTechSysBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getAirTechSysBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getAirTechSysBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()
        ) {
            //        先判断是否超时
            if (!event.isSuccess()) {
                showToast("KNX科技系统控制超时,请重新再试");
                return;
            }
            AirTechSysBackInfo mAirTechSysBackInfo = event.getAirTechSysBackInfo();
            showAirTechSysBackInfo(mAirTechSysBackInfo);
            if (!TextUtils.isEmpty(mAirTechSysBackInfo.getIndoorHumidity())){
                airHumidity.setText("室内湿度:" + mAirTechSysBackInfo.getIndoorHumidity());
            }
        }
    }
 
    /**
     * 获取单一设备状态回调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_AC_KNXTECHSYS:
                    if (appliancesInfo.getChannelNum() == event.getAppliancesInfo().getChannelNum()) {
                        if (!event.isSuccess()) {
                            showToast("获取科技系统状态失败,请重新再试");
                            return;
                        }
                        AirTechSysBackInfo mAirTechSysBackInfo = new AirTechSysBackInfo(event.getAppliancesInfo());
                        if (mAirTechSysBackInfo == null) {
                            showToast("获取科技状态失败,请重新再试");
                            return;
                        }
                        showAirTechSysBackInfo(mAirTechSysBackInfo);
                    }
                    break;
                default:
                    //不处理
                    break;
            }
        }
    }
 
 
}