562935844@qq.com
2023-11-27 566ddb2ea03e2514de50f2ca861a2674f6e840ac
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.hdl.sdk.ttl_sdk.activity;
 
 
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
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.Parser.AirCtrlParser;
import com.hdl.sdk.ttl.HDLAppliances.HDLDoorMachine.DoorMachineBackInfo;
import com.hdl.sdk.ttl.HDLAppliances.HDLDoorMachine.DoorMachineFristBackInfo;
import com.hdl.sdk.ttl.HDLAppliances.HDLDoorMachine.DoorMachineWaringBackInfo;
import com.hdl.sdk.ttl.HDLAppliances.HDLDoorMachine.Parser.DoorMachineParser;
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.DoorMachineFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.DoorMachineFeedFristBackEvent;
import com.hdl.sdk.ttl.Utils.LogUtils.HDLLog;
import com.hdl.sdk.ttl_sdk.R;
import com.hdl.sdk.ttl_sdk.base.BaseActivity;
import com.hdl.sdk.ttl_sdk.utlis.HDLUtlis;
 
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
/**
 * 门锁控制界面
 */
public class DoorMachineActivity extends BaseActivity {
    /**
     * Topbar
     */
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
 
    private Button btn_switch;
    private EditText et_password;
    private TextView mTextView;
    private AppliancesInfo appliancesInfo;
 
    private int gSwitchState;//Demo仅以此作为演示,实际请根据需求开发设计
 
    private ProgressDialog proDialog;
    private byte[] secretBytes = new byte[9];//这里是回路号+8byte密钥
    private DoorMachineBackInfo doorMachineBackInfo;
 
    /**
     * 要注册使用EventBus,这里要设置返回true
     *
     * @return true
     */
    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_door_machine);
        initToolbar();
        initcurState();
        initView();
        initClickOnEvent();
        showStateView();
 
        Log.d("panlili", "DoorMachineActivity.java:appliancesInfo-----> " + appliancesInfo.toString());
        HDLCommand.getDoorMachineDeviceStateFromNetwork(appliancesInfo);
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        proDialog.dismiss();
        HDLLog.I("onDestroy: DoorMachineActivity");
    }
 
    /**
     * 初始化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();
        if (TextUtils.isEmpty(titleStr) || titleStr.equals(HDLUtlis.REMAEK_NULL)) {
            if (TextUtils.isEmpty(appliancesInfo.getDeviceName())) {
                titleStr = "备注为空";
            } else {
                titleStr = appliancesInfo.getDeviceName();
            }
        }
        topBarTitle.setText(titleStr);
    }
 
    private void initView() {
        mTextView = findViewById(R.id.tv_door_machine);
        btn_switch = findViewById(R.id.btn_switch);
        et_password = findViewById(R.id.et_password);
 
        proDialog = new ProgressDialog(this);
        proDialog.setTitle("正在发送控制命令...");
        proDialog.setMessage("请耐心等待");
        proDialog.onStart();
    }
 
    private void initClickOnEvent() {
        btn_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tempStr = et_password.getText().toString();
                if (TextUtils.isEmpty(tempStr)) {
                    showToast("开锁密码不能为空");
                    return;
                }
                proDialog.show();
                //远程开锁,先获取密钥
                HDLCommand.doorMachineGetSecret(appliancesInfo);
            }
        });
    }
 
    private void showStateView() {
        if (appliancesInfo.getArrCurState() != null) {
            DoorMachineBackInfo doorMachineBackInfo = new DoorMachineBackInfo(appliancesInfo);
 
            String stringState = "";
            if (doorMachineBackInfo.getDoorStatus() == DoorMachineParser.doorStatusClose) {
                stringState = "门锁状态:关闭";
            } else if (doorMachineBackInfo.getDoorStatus() == DoorMachineParser.doorStatusOpen) {
                stringState = "门锁状态:打开";
            } else if (doorMachineBackInfo.getDoorStatus() == DoorMachineParser.doorStatusNormallyOpen) {
                stringState = "门锁状态:常开";
            } else {
                stringState = "未知开关状态";
            }
 
            if (doorMachineBackInfo.getNormalOpenMode() == DoorMachineParser.normalOpenModeClose) {
                stringState += "\n常开模式:关闭";
            } else if (doorMachineBackInfo.getDoorStatus() == DoorMachineParser.normalOpenModeOpen) {
                stringState += "\n常开模式:打开";
            } else {
                stringState += "\n未知常开模式开关状态";
            }
 
            stringState += "\n音量:" + doorMachineBackInfo.getDoorVolume();
 
            stringState += "\n电池百分比:" + doorMachineBackInfo.getDoorElectricity();
 
            mTextView.setText(stringState);
        } else {
            mTextView.setText("未获取到门锁状态");
        }
 
    }
 
 
    /**
     * getSwichStateString
     *
     * @param mState
     * @return
     */
    private String getSwichStateString(int mState) {
        String mStrState = "未知";
        gSwitchState = mState; //更新开关状态
        switch (mState) {
            case DoorMachineParser.doorStatusOpen:
                mStrState = "门锁状态:打开";
                break;
            case DoorMachineParser.doorStatusClose:
                mStrState = "门锁状态:关闭";
                break;
            case DoorMachineParser.doorStatusNormallyOpen:
                mStrState = "门锁状态:常开";
                break;
            default:
                mStrState = "未知状态";
                break;
        }
        return mStrState;
    }
 
 
    /**
     * getNormalOpenModeStateString
     *
     * @param mState
     * @return
     */
    private String getNormalOpenModeStateString(int mState) {
        String mStrState = "未知";
        switch (mState) {
            case DoorMachineParser.normalOpenModeClose:
                mStrState = "常开模式:关闭";
                break;
            case DoorMachineParser.normalOpenModeOpen:
                mStrState = "常开模式:打开";
                break;
            default:
                mStrState = "未知状态";
                break;
        }
        return mStrState;
    }
 
 
    /**
     * 门锁远程开锁控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DoorMachineFeedFristBackEventMain(DoorMachineFeedFristBackEvent event) {
        proDialog.dismiss();
        if (event.getDoorMachineFristBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getDoorMachineFristBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getDoorMachineFristBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()) {
            //先判断是否超时
            if (!event.isSuccess()) {
                showToast("门锁远程开锁控制超时,请重新再试");
                return;
            }
 
            DoorMachineFristBackInfo doorMachineFristBackInfo = event.getDoorMachineFristBackInfo();
            if (doorMachineFristBackInfo != null && doorMachineBackInfo != null) {
                secretBytes = doorMachineFristBackInfo.getSecretBytes();
                HDLCommand.doorMachineCtrl(appliancesInfo, secretBytes, et_password.getText().toString(), doorMachineBackInfo.getDoorStatus());
            }
        }
    }
 
 
    /**
     * 门锁远程开锁控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDoorMachineFeedBackEventMain(DoorMachineFeedBackEvent event) {
        proDialog.dismiss();
        if (event.getDoorMachineBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getDoorMachineBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getDoorMachineBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()) {
 
            showToast("门锁远程开锁成功");
            HDLLog.I("门锁远程开锁成功");
        }
    }
 
 
    /**
     * 门锁状态和报警广播Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDoorMachineWaringBackInfoMain(DoorMachineWaringBackInfo event) {
        if (event.appliancesInfo.getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.appliancesInfo.getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.appliancesInfo.getChannelNum() == appliancesInfo.getChannelNum()) {
 
            String type = ((DoorMachineWaringBackInfo) event).getType();
            showToast("收到门锁1F4D广播:" + type);
            HDLLog.I("收到门锁1F4D广播:" + type);
        }
    }
 
    @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_DOOR_MACHINE:
                    if (appliancesInfo.getChannelNum() == event.getAppliancesInfo().getChannelNum()) {
                        if (!event.isSuccess()) {
                            showToast("获取门锁状态失败,请重新再试");
                            return;
                        }
                        String message = "";
                        doorMachineBackInfo = new DoorMachineBackInfo(event.appliancesInfo);
 
                        if (doorMachineBackInfo == null) {
                            showToast("获取门锁状态失败,请重新再试");
                            return;
                        }
 
                        showDoorMachineBackInfo(doorMachineBackInfo);
 
                    }
                    break;
            }
        }
    }
 
    /**
     * showDoorMachineBackInfo
     *
     * @param doorMachineBackInfo
     */
    private void showDoorMachineBackInfo(DoorMachineBackInfo doorMachineBackInfo) {
        String message = "";
 
        message = getSwichStateString(doorMachineBackInfo.getDoorStatus());
        message += "\n" + getNormalOpenModeStateString(doorMachineBackInfo.getNormalOpenMode());
        message += "\n" + "音量:" + doorMachineBackInfo.getDoorVolume();
        message += "\n" + "电池百分比:" + doorMachineBackInfo.getDoorElectricity();
 
        mTextView.setText(message);
        showToast(message);
        HDLLog.I(message);
    }
}