-
Notifications
You must be signed in to change notification settings - Fork 0
/
UiProcess.java
438 lines (336 loc) · 11.5 KB
/
UiProcess.java
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package com.fontlose.relayctrl;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.BitmapDrawable;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class UiProcess {
public static byte MSGOPTON =2;
public static byte MSGOPTOFF =1;
public static byte MSGOPTNONE=0;
public static byte MSGOPTRELAll=0;
public static byte MSGOPTREL1=1;
public static byte MSGOPTREL2=2;
public static byte MSGOPTREL3=3;
public static byte MSGOPTREL4=4;
HandleMsg hUiMsg=null;
Context mct=null;
DataProcess dataProcess=null;
private ImageButton bnConnect;
private EditText etIp,etPort;
private TextView tv1,tv2,tv3,tv4,tvn1,tvn2,tvn3,tvn4;
private ImageView im1,im2,im3,im4;
QPopupWindow createConfigWindow;
public String nameRel1,nameRel2,nameRel3,nameRel4;
SoundPool snd = new SoundPool(10, AudioManager.STREAM_SYSTEM,5);
int hitOkSfx;
ProgressDialog pdialog ;
public void soundPlay()
{
snd.play(hitOkSfx, (float)0.5, (float)0.5, 0, 0, 1);
}
UiProcess uiProcessInstance;
public UiProcess(LinearLayout mainLayout,Context context,HandleMsg hMsg,DataProcess dProcess) {
super();
Button bnRealyOn,bnRelayOff;
hUiMsg=hMsg;
mct=context;
dataProcess=dProcess;
uiProcessInstance = this;
//载入音频流
hitOkSfx = snd.load(mct, R.raw.ping_short, 0);
bnRealyOn=(Button)mainLayout.findViewById(R.id.bnOn1);
bnRealyOn.setOnClickListener(new onRelayButtonClick(MSGOPTREL1,MSGOPTON));
bnRelayOff=(Button)mainLayout.findViewById(R.id.bnOff1);
bnRelayOff.setOnClickListener(new onRelayButtonClick(MSGOPTREL1,MSGOPTOFF));
bnRealyOn=(Button)mainLayout.findViewById(R.id.bnOn2);
bnRealyOn.setOnClickListener(new onRelayButtonClick(MSGOPTREL2,MSGOPTON));
bnRelayOff=(Button)mainLayout.findViewById(R.id.bnOff2);
bnRelayOff.setOnClickListener(new onRelayButtonClick(MSGOPTREL2,MSGOPTOFF));
bnRealyOn=(Button)mainLayout.findViewById(R.id.bnOn3);
bnRealyOn.setOnClickListener(new onRelayButtonClick(MSGOPTREL3,MSGOPTON));
bnRelayOff=(Button)mainLayout.findViewById(R.id.bnOff3);
bnRelayOff.setOnClickListener(new onRelayButtonClick(MSGOPTREL3,MSGOPTOFF));
bnRealyOn=(Button)mainLayout.findViewById(R.id.bnOn4);
bnRealyOn.setOnClickListener(new onRelayButtonClick(MSGOPTREL4,MSGOPTON));
bnRelayOff=(Button)mainLayout.findViewById(R.id.bnOff4);
bnRelayOff.setOnClickListener(new onRelayButtonClick(MSGOPTREL4,MSGOPTOFF));
bnRealyOn=(Button)mainLayout.findViewById(R.id.bnAllOn);
bnRealyOn.setOnClickListener(new onRelayButtonClick(MSGOPTRELAll,MSGOPTON));
bnRelayOff=(Button)mainLayout.findViewById(R.id.bnAllOff);
bnRelayOff.setOnClickListener(new onRelayButtonClick(MSGOPTRELAll,MSGOPTOFF));
tv1=(TextView)mainLayout.findViewById(R.id.tv1);
tv2=(TextView)mainLayout.findViewById(R.id.tv2);
tv3=(TextView)mainLayout.findViewById(R.id.tv3);
tv4=(TextView)mainLayout.findViewById(R.id.tv4);
tvn1=(TextView)mainLayout.findViewById(R.id.tvname1);
tvn2=(TextView)mainLayout.findViewById(R.id.tvname2);
tvn3=(TextView)mainLayout.findViewById(R.id.tvname3);
tvn4=(TextView)mainLayout.findViewById(R.id.tvname4);
im1=(ImageView)mainLayout.findViewById(R.id.im1);
im2=(ImageView)mainLayout.findViewById(R.id.im2);
im3=(ImageView)mainLayout.findViewById(R.id.im3);
im4=(ImageView)mainLayout.findViewById(R.id.im4);
loadConfigure();
updateName();
setRelayState(0);
etIp =(EditText)mainLayout.findViewById(R.id.etIp);
etPort=(EditText)mainLayout.findViewById(R.id.etPort);
loadIpPort();
bnConnect=(ImageButton)mainLayout.findViewById(R.id.bnConnect);
bnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
snd.play(hitOkSfx, (float)0.5, (float)0.5, 0, 0, 1);
// TODO Auto-generated method stub
if(isEditEnable())
{
String sip =etIp.getText().toString().trim();
String sport =etPort.getText().toString().trim();
Pattern pa=Pattern.compile("^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$");
Matcher ma=pa.matcher(sip);
if(ma.matches()==false)
{
RelayCtrlActivity.showMessage(mct.getString(R.string.msg6));
return;
}
int port=0;
try {
port=Integer.parseInt(sport);
} catch (Exception e) {
RelayCtrlActivity.showMessage(mct.getString(R.string.msg7));
return ;
}
dataProcess.startBackgroundThread(sip, port, uiProcessInstance);
// if(dataProcess.startConn(sip, port))
// {
// RelayCtrlActivity.showMessage(mct.getString(R.string.msg2));
// bnConnect.setImageResource( R.drawable.true1);
//
// hUiMsg.stateCheck(1);
// setEditEnable(false);
// }
// else
// {
//// RelayCtrlActivity.showMessage(mct.getString(R.string.msg1));
// }
}
else
{
stopConn();
}
}
});
}
public void onConnected(boolean result){
if (result){
RelayCtrlActivity.showMessage(mct.getString(R.string.msg2));
bnConnect.setImageResource( R.drawable.true1);
hUiMsg.stateCheck(1);
setEditEnable(false);
}else
{
RelayCtrlActivity.showMessage(mct.getString(R.string.msg1));
}
}
public void stopConn( )
{
dataProcess.stopConn();
hUiMsg.stateCheck(0);
RelayCtrlActivity.showMessage(mct.getString(R.string.msg3));
bnConnect.setImageResource(R.drawable.false0);
setEditEnable(true);
setRelayState(0);
}
public void setEditEnable(boolean en)
{
etIp.setEnabled(en);
etPort.setEnabled(en);
if(en==false)
{
etIp.setInputType(InputType.TYPE_NULL);
etPort.setInputType(InputType.TYPE_NULL);
}
else
{
etIp.setInputType(InputType.TYPE_CLASS_TEXT);
etPort.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
public boolean isEditEnable()
{
return etIp.isEnabled();
}
class onRelayButtonClick implements View.OnClickListener
{
private int operate_l,relayId_l;
public onRelayButtonClick(byte relayId,byte operate)
{
relayId_l=relayId;
operate_l=operate;
}
Button bnActivity,bn;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
snd.play(hitOkSfx, (float)0.5, (float)0.5, 0, 0, 1);
if(hUiMsg==null) return;
Message msg=new Message();
msg.what=DataProcess.RELAYOPT;
msg.arg1=relayId_l;
msg.arg2=operate_l;
hUiMsg.sendMessage(msg);
}
}
private byte[] state_l=new byte[]{(byte)0xFF,(byte)0xff,(byte)0xFF,(byte)0xff};
public void setRelayState(int state)
{//继电器1
byte sat=(byte)(state&0xff);
if(state_l[0]!=sat)
{
state_l[0]=sat;
setRelayOnoff(tv1,im1,sat);
}
state=state>>8;
sat=(byte)(state&0xff);
if(state_l[1]!=sat)
{
state_l[1]=sat;
setRelayOnoff(tv2,im2,sat);
}
state=state>>8;
sat=(byte)(state&0xff);
if(state_l[2]!=sat)
{
state_l[2]=sat;
setRelayOnoff(tv3,im3,sat);
}
state=state>>8;
sat=(byte)(state&0xff);
if(state_l[3]!=sat)
{
state_l[3]=sat;
setRelayOnoff(tv4,im4,sat);
}
}
public void setRelayOnoff(TextView tv,ImageView im,byte state)
{//继电器1
if(state==0x1)
{
tv.setText(R.string.state0);
im.setImageResource(R.drawable.off);
}
else if(state==0x2)
{
tv.setText(R.string.state1);
im.setImageResource(R.drawable.on);
}
else
{
tv.setText(R.string.state2);
im.setImageResource(R.drawable.off);
}
}
private EditText etRelay1,etRelay2,etRelay3,etRelay4;
public QPopupWindow createConfigWindow(){/*增加连接按钮*/
LayoutInflater factory=LayoutInflater.from(mct);
LinearLayout lout=(LinearLayout)factory.inflate(R.layout.name,null);
QPopupWindow popupWindow = new QPopupWindow(lout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,true);
etRelay1=(EditText)lout.findViewById(R.id.etRelay1);
etRelay2=(EditText)lout.findViewById(R.id.etRelay2);
etRelay3=(EditText)lout.findViewById(R.id.etRelay3);
etRelay4=(EditText)lout.findViewById(R.id.etRelay4);
etRelay1.setText(nameRel1);
etRelay2.setText(nameRel2);
etRelay3.setText(nameRel3);
etRelay4.setText(nameRel4);
Button bnClk=(Button)lout.findViewById(R.id.bnSet);
bnClk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//soundPlay();
createConfigWindow.dismiss();
nameRel1=etRelay1.getText().toString();
nameRel2=etRelay2.getText().toString();
nameRel3=etRelay3.getText().toString();
nameRel4=etRelay4.getText().toString();
saveConfigure();
updateName();
RelayCtrlActivity.showMessage(mct.getString(R.string.setSuccess));
}
});
bnClk=(Button)lout.findViewById(R.id.bnCancel);
bnClk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//soundPlay();
createConfigWindow.dismiss();
}
});
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setAnimationStyle(R.style.PopupAnimation);
popupWindow.update();
return popupWindow;
}
public void loadConfigure()
{
SharedPreferences uiState = mct.getSharedPreferences("system", mct.MODE_PRIVATE);
nameRel1 =uiState.getString("relay1", mct.getString(R.string.relay1));
nameRel2 =uiState.getString("relay2", mct.getString(R.string.relay2));
nameRel3 =uiState.getString("relay3", mct.getString(R.string.relay3));
nameRel4 =uiState.getString("relay4", mct.getString(R.string.relay4));
}
public void saveConfigure()
{
SharedPreferences uiState = mct.getSharedPreferences("system", mct.MODE_PRIVATE);
Editor et=uiState.edit();
et.putString("relay1",nameRel1);
et.putString("relay2",nameRel2);
et.putString("relay3",nameRel3);
et.putString("relay4",nameRel4);
et.commit();
}
public void updateName()
{
tvn1.setText(nameRel1);
tvn2.setText(nameRel2);
tvn3.setText(nameRel3);
tvn4.setText(nameRel4);
}
public void saveIpPort()
{
SharedPreferences uiState = mct.getSharedPreferences("system", mct.MODE_PRIVATE);
Editor et=uiState.edit();
et.putString("ip",etIp.getText().toString());
et.putString("port",etPort.getText().toString());
et.commit();
}
public void loadIpPort()
{
SharedPreferences uiState = mct.getSharedPreferences("system", mct.MODE_PRIVATE);
etIp.setText(uiState.getString("ip","192.168.1.100" ));
etPort.setText(uiState.getString("port", "5000"));
}
}