-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataProcess.java
146 lines (121 loc) · 3.13 KB
/
DataProcess.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
package com.fontlose.relayctrl;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
public class DataProcess {
public static byte RELAYOPT=1;
public static byte RELAYCHK=2;
public static byte RELAYSTATE=3;
public static byte APPQUIT=4;
public static byte CLOSETCP=5;
public static byte MAINMENU=6;
protected Socket socket ;//Socket 数据
//目标端口
public boolean State;
private byte[] sData=new byte[1024];//接收缓存
ReadThread readThread=null;
HandleMsg hOptMsg=null;
Context mct=null;
/**
* @param s
* @param ctrlHandle
*/
public DataProcess(HandleMsg hmsg,Context context)
{
socket = new Socket();
hOptMsg=hmsg;
mct=context;
}
public boolean sendData(byte[] data) throws IOException {
// TODO Auto-generated method stub
OutputStream out=socket.getOutputStream();
if(out==null) return false;
out.write(data);
return true;
}
public boolean stopConn() {
State=false;
if(readThread==null) return false;
readThread.abortRead();
return false;
}
UiProcess uiProcess;
public void startBackgroundThread(String ip,int port, UiProcess uiProcess){
this.uiProcess = uiProcess;
new conntectToDevice().execute(ip, Integer.toString(port));
}
public boolean startConn( String ip,int port) {
if(socket.isClosed()) socket=new Socket();
SocketAddress remoteAddr=new InetSocketAddress(ip,port);
try {
socket.connect(remoteAddr, 2000);
} catch (Exception e) {
socket=new Socket();
return false;
}
this.readThread=new ReadThread(hOptMsg, sData,socket);
readThread.start();
State=true;
return true;
}
public class conntectToDevice extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
String ip = params[0];
String port = params[1];
boolean result = startConn(ip, Integer.parseInt(port));
return result;
}
@Override
protected void onPostExecute(Boolean result) {
onConnected(result);
}
}
private void onConnected(boolean result) {
uiProcess.onConnected(result);
}
public byte[] packageCmd(byte id,byte opt)
{
if(id>5) return null;
byte[] cmd=new byte[]{0x55,0x01,0x01,0,0,0,0,0};
if(id==5)
{
cmd[2]=0;
}
else if(id==0)
{
cmd[3]=opt;
cmd[4]=opt;
cmd[5]=opt;
cmd[6]=opt;
}
else cmd[2+id]=opt;
cmd[7]=(byte)(cmd[0]+cmd[1]+cmd[2]+cmd[3]+cmd[4]+cmd[5]+cmd[6]);
return cmd;
}
public void sendrelayCmd(int id,int opt)
{
byte[] cmd=packageCmd((byte)id,(byte)opt);
if(cmd==null) return;
if((readThread==null)||(readThread.state==false))
{
RelayCtrlActivity.showMessage(mct.getString(R.string.msg5));
hOptMsg.stateCheck(0);
return;
}
try {
sendData(cmd);
if(id!=5) hOptMsg.stateCheck(2);
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
RelayCtrlActivity.showMessage(mct.getString(R.string.msg4));
// hOptMsg.sendEmptyMessage(DataProcess.CLOSETCP);
}
}
}