JLChen
2021-03-16 3617e6eecac94965554487afc50d39b0584324fb
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
package android.serialport.api.sample;
 
import java.io.InputStream;
import java.io.OutputStream;
 
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.serialport.api.SerialPort;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class Fragment_KNX extends Fragment {
 
    private static Activity CurentActivity;
    private OutputStream mOutputStream;
    private InputStream mInputStream;
    private KNX_Serialport mKNX_SerialPort;
    private SerialPort mSerialPort;
 
    private View mView;
    private static Button mSwitchButton_1;
    private static Button mLight_1;
    private static EditText mObjectEditText1_1;
    private static EditText mObjectEditText1_2;
 
    private static Button mSwitchButton_2;
    private static Button mLight_2;
    private static EditText mObjectEditText2_1;
    private static EditText mObjectEditText2_2;
 
    private static Button mSwitchButton_3;
    private static Button mLight_3;
    private static EditText mObjectEditText3_1;
    private static EditText mObjectEditText3_2;
 
    private static Button mSwitchButton_4;
    private static Button mLight_4;
    private static EditText mObjectEditText4_1;
    private static EditText mObjectEditText4_2;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 
        mView = inflater.inflate(R.layout.fragment_knx, container, false);
 
        iniView();
 
        iniData();
 
        return mView;
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
 
    @Override
    public void onStart() {
        super.onStart();
    }
 
    @Override
    public void onStop() {
        super.onStop();
    }
 
    private void iniView() {
 
        mSwitchButton_1 = (Button) mView.findViewById(R.id.switch_btn1);
        mLight_1 = (Button) mView.findViewById(R.id.light1_1);
        mSwitchButton_1.setOnClickListener(OnClick);
        mLight_1.setOnClickListener(OnClickLight);
        mLight_1.setTag((int) 0);
        mLight_1.setBackground(getResources().getDrawable(R.drawable.light_def));
 
        mSwitchButton_2 = (Button) mView.findViewById(R.id.switch_btn2);
        mLight_2 = (Button) mView.findViewById(R.id.light_2);
        mSwitchButton_2.setOnClickListener(OnClick);
        mLight_2.setOnClickListener(OnClickLight);
        mLight_2.setTag((int) 0);
        mLight_2.setBackground(getResources().getDrawable(R.drawable.light_def));
 
        mSwitchButton_3 = (Button) mView.findViewById(R.id.switch_btn3);
        mLight_3 = (Button) mView.findViewById(R.id.light_3);
        mSwitchButton_3.setOnClickListener(OnClick);
        mLight_3.setOnClickListener(OnClickLight);
        mLight_3.setTag((int) 0);
        mLight_3.setBackground(getResources().getDrawable(R.drawable.light_def));
 
        mSwitchButton_4 = (Button) mView.findViewById(R.id.switch_btn4);
        mLight_4 = (Button) mView.findViewById(R.id.light_4);
        mSwitchButton_4.setOnClickListener(OnClick);
        mLight_4.setOnClickListener(OnClickLight);
        mLight_4.setTag((int) 0);
        mLight_4.setBackground(getResources().getDrawable(R.drawable.light_def));
 
        mObjectEditText1_1 = (EditText) mView.findViewById(R.id.edit_obj1_1);
        mObjectEditText1_2 = (EditText) mView.findViewById(R.id.edit_obj1_2);
        mObjectEditText1_2.setTag("");
 
        mObjectEditText2_1 = (EditText) mView.findViewById(R.id.edit_obj2_1);
        mObjectEditText2_2 = (EditText) mView.findViewById(R.id.edit_obj2_2);
        mObjectEditText2_2.setTag("");
 
        mObjectEditText3_1 = (EditText) mView.findViewById(R.id.edit_obj3_1);
        mObjectEditText3_2 = (EditText) mView.findViewById(R.id.edit_obj3_2);
        mObjectEditText3_2.setTag("");
 
        mObjectEditText4_1 = (EditText) mView.findViewById(R.id.edit_obj4_1);
        mObjectEditText4_2 = (EditText) mView.findViewById(R.id.edit_obj4_2);
        mObjectEditText4_2.setTag("");
    }
 
    private void iniData() {
 
        CurentActivity = getActivity();
        
        SetControlData(mObjectEditText1_2, mObjectEditText1_2.getText().toString());    
        
        SetControlData(mObjectEditText2_2, mObjectEditText2_2.getText().toString());        
        //
        SetControlData(mObjectEditText3_2, mObjectEditText3_2.getText().toString());
        //
        SetControlData(mObjectEditText4_2, mObjectEditText4_2.getText().toString());    
    
        getSerialPort();
 
        KNX_ReceiveAndSend.Start(mKNX_SerialPort, mOutputStream, mInputStream);
    }
 
    /**
     * Open the serial port, input and output streams
     */
    private void getSerialPort() {
 
        mKNX_SerialPort = new KNX_Serialport();
 
        try {
 
            mSerialPort = mKNX_SerialPort.getSerialPort();
            mOutputStream = mSerialPort.getOutputStream();
            mInputStream = mSerialPort.getInputStream();
 
        } catch (Exception e) {
            e.getMessage();
        }
    }
 
    private static void SetLightState(Button btn, int _state) {
 
        if (_state == 0) {
            btn.setBackground(CurentActivity.getResources().getDrawable(R.drawable.light_def));
            btn.setTag(0);
        } else {
            btn.setBackground(CurentActivity.getResources().getDrawable(R.drawable.light));
            btn.setTag(1);
        }
    }
 
    private static void SetControlData(View v, String str) {
 
        if (str.equals(""))
            return;
        try {
            int obj1 = Integer.parseInt(str.split("/")[0].trim());
            int obj2 = Integer.parseInt(str.split("/")[1].trim());
            int obj3 = Integer.parseInt(str.split("/")[2].trim());
            v.setTag(KNX.set_Group_addr(obj1, obj2, obj3));
        } catch (Exception e) {
            e.getMessage();
        }
    }
 
    public static Handler mHandler = new Handler() {
 
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
 
            String str = Integer.toString(msg.arg1);
            if (str.equals(mObjectEditText1_2.getTag().toString())) {
                SetLightState(mLight_1, msg.arg2);
 
            } else if (str.equals(mObjectEditText2_2.getTag().toString())) {
                SetLightState(mLight_2, msg.arg2);
 
            } else if (str.equals(mObjectEditText3_2.getTag().toString())) {
                SetLightState(mLight_3, msg.arg2);
 
            } else if (str.equals(mObjectEditText4_2.getTag().toString())) {
                SetLightState(mLight_4, msg.arg2);
 
            }
        }
 
    };
 
    private android.view.View.OnClickListener OnClickLight = new android.view.View.OnClickListener() {
 
        @Override
        public void onClick(View v) {
 
            if (mObjectEditText1_1.getText().toString() == "" || mObjectEditText2_1.getText().toString() == ""
                    || mObjectEditText3_1.getText().toString() == "" || mObjectEditText4_1.getText().toString() == ""
                    || mObjectEditText2_2.getText().toString() == "" || mObjectEditText2_2.getText().toString() == ""
                    || mObjectEditText3_2.getText().toString() == "" || mObjectEditText4_2.getText().toString() == "") {
                Toast.makeText(CurentActivity, "Group_addr can not be empty", Toast.LENGTH_SHORT).show();
                return;
            }
 
            if (v.equals(mLight_1)) {
 
                try {
                    String str = mObjectEditText1_1.getText().toString();
                    int obj1 = Integer.parseInt(str.split("/")[0].trim());
                    int obj2 = Integer.parseInt(str.split("/")[1].trim());
                    int obj3 = Integer.parseInt(str.split("/")[2].trim());
                    int state = Integer.parseInt(mLight_1.getTag().toString());
                    state = state == 0 ? 1 : 0;
                    byte[] sendBytes = KNX.get_SendBuffer(12345, obj1, obj2, obj3, state);
                    String[] tmp = new String[sendBytes.length];
                    for (int i = 0; i < sendBytes.length; i++) {
                        tmp[i] = Integer.toHexString(sendBytes[i] & 0xFF);
                    }
 
                    mOutputStream.write(sendBytes, 0, sendBytes[1] & 0xFF);
                    // 根据长度截取数据发送
                    mOutputStream.flush();
 
                } catch (Exception e) {
                    e.getMessage();
                }
            } else if (v.equals(mLight_2)) {
                try {
                    String str = mObjectEditText2_1.getText().toString();
                    int obj1 = Integer.parseInt(str.split("/")[0].trim());
                    int obj2 = Integer.parseInt(str.split("/")[1].trim());
                    int obj3 = Integer.parseInt(str.split("/")[2].trim());
                    int state = Integer.parseInt(mLight_2.getTag().toString());
                    state = state == 0 ? 1 : 0;
                    byte[] sendBytes = KNX.get_SendBuffer(12345, obj1, obj2, obj3, state);
                    String[] tmp = new String[sendBytes.length];
                    for (int i = 0; i < sendBytes.length; i++) {
                        tmp[i] = Integer.toHexString(sendBytes[i] & 0xFF);
                    }
 
                    mOutputStream.write(sendBytes, 0, sendBytes[1] & 0xFF);
                    // 根据长度截取数据发送
                    mOutputStream.flush();
 
                } catch (Exception e) {
                    e.getMessage();
                }
            } else if (v.equals(mLight_3)) {
                try {
                    String str = mObjectEditText3_1.getText().toString();
                    int obj1 = Integer.parseInt(str.split("/")[0].trim());
                    int obj2 = Integer.parseInt(str.split("/")[1].trim());
                    int obj3 = Integer.parseInt(str.split("/")[2].trim());
                    int state = Integer.parseInt(mLight_3.getTag().toString());
                    state = state == 0 ? 1 : 0;
                    byte[] sendBytes = KNX.get_SendBuffer(12345, obj1, obj2, obj3, state);
                    String[] tmp = new String[sendBytes.length];
                    for (int i = 0; i < sendBytes.length; i++) {
                        tmp[i] = Integer.toHexString(sendBytes[i] & 0xFF);
                    }
 
                    mOutputStream.write(sendBytes, 0, sendBytes[1] & 0xFF);
                    // 根据长度截取数据发送
                    mOutputStream.flush();
 
                } catch (Exception e) {
                    e.getMessage();
                }
            } else if (v.equals(mLight_4)) {
                try {
                    String str = mObjectEditText4_1.getText().toString();
                    int obj1 = Integer.parseInt(str.split("/")[0].trim());
                    int obj2 = Integer.parseInt(str.split("/")[1].trim());
                    int obj3 = Integer.parseInt(str.split("/")[2].trim());
                    int state = Integer.parseInt(mLight_4.getTag().toString());
                    state = state == 0 ? 1 : 0;
                    byte[] sendBytes = KNX.get_SendBuffer(12345, obj1, obj2, obj3, state);
                    String[] tmp = new String[sendBytes.length];
                    for (int i = 0; i < sendBytes.length; i++) {
                        tmp[i] = Integer.toHexString(sendBytes[i] & 0xFF);
                    }
 
                    mOutputStream.write(sendBytes, 0, sendBytes[1] & 0xFF);
                    // 根据长度截取数据发送
                    mOutputStream.flush();
 
                } catch (Exception e) {
                    e.getMessage();
                }
            }
        }
    };
 
    private android.view.View.OnClickListener OnClick = new android.view.View.OnClickListener() {
 
        @Override
        public void onClick(View v) {
 
            if (v.equals(mSwitchButton_1)) {
 
                SetControlData(mObjectEditText1_2, mObjectEditText1_2.getText().toString());
            } else if (v.equals(mSwitchButton_2)) {
                //
                SetControlData(mObjectEditText2_2, mObjectEditText2_2.getText().toString());                
            } else if (v.equals(mSwitchButton_3)) {
                //
                SetControlData(mObjectEditText3_2, mObjectEditText3_2.getText().toString());    
            } else if (v.equals(mSwitchButton_4)) {
                //
                SetControlData(mObjectEditText4_2, mObjectEditText4_2.getText().toString());    
            }
        }
    };
}