ControlDialog.java
7.45 KB
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
package com.xgimi.smartscreen;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.gimi.common.cinema.model.MessageEvent;
import com.gimi.common.cinema.utils.Utils;
import com.xgimi.gimicinema.R;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketTimeoutException;
import static com.xgimi.smartscreen.MainActivity.UPDATE_CURRENT_CTRL_SCREEN;
public class ControlDialog extends Dialog implements View.OnClickListener {
private static final String TAG = "ControlDialog";
private Context mContext;
private String mDeviceIp;
private int sn;
private TextView tvDeviceInfo;
private MessageThread messageThread;
private static final int PORT = 8009;
public ControlDialog(Context context, String DeviceIp) {
super(context);
this.mContext = context;
this.mDeviceIp = DeviceIp;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_control);
TextView tvDeviceIp = (TextView) findViewById(R.id.tv_dialog_ip);
tvDeviceIp.setText("[ " + mDeviceIp + " ]");
tvDeviceInfo = (TextView) findViewById(R.id.tv_device_info);
tvDeviceInfo.setMovementMethod(ScrollingMovementMethod.getInstance());
tvDeviceInfo.setText("序列号:\n\n软件版本:\n\n硬件版本:\n\n幕布状态:");
TextView tvUp = (TextView) findViewById(R.id.tv_up);
tvUp.setOnClickListener(this);
TextView tvDown = (TextView) findViewById(R.id.tv_down);
tvDown.setOnClickListener(this);
TextView tvControl = (TextView) findViewById(R.id.tv_control);
tvControl.setOnClickListener(this);
messageThread = new MessageThread();
new Thread(messageThread).start();
new Handler().postDelayed(new Runnable() {
public void run() {
getDeviceInfo();
}
}, 1000);
}
private void getDeviceInfo() {
JSONObject dataGetInfo = new JSONObject();
try {
dataGetInfo.put("cmd", 1);
} catch (JSONException e) {
e.printStackTrace();
}
Message msgGetInfo = new Message();
msgGetInfo.what = 0x345;
msgGetInfo.obj = dataGetInfo;
messageThread.sendHandler.sendMessage(msgGetInfo);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_up:
MessageEvent msgEvent = new MessageEvent();
msgEvent.setEventId(SmartScreenBean.DOWNACTION);
msgEvent.setMessage(mDeviceIp);
EventBus.getDefault().post(msgEvent);
break;
case R.id.tv_down:
MessageEvent msgEvent1 = new MessageEvent();
msgEvent1.setEventId(SmartScreenBean.DOWNACTION);
msgEvent1.setMessage(mDeviceIp);
EventBus.getDefault().post(msgEvent1);
break;
case R.id.tv_control:
Utils.saveInt(mContext, "screen-sn", sn);
Utils.saveString(mContext, "screen-ip", mDeviceIp);
MessageEvent msgEvent2 = new MessageEvent();
msgEvent2.setEventId(UPDATE_CURRENT_CTRL_SCREEN);
msgEvent2.setMessage("UPDATE_CURRENT_CTRL_SCREEN");
EventBus.getDefault().post(msgEvent2);
break;
}
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x123) {
String data = (msg.obj).toString();
try {
JSONObject jsonObject = new JSONObject(data);
int cmd = jsonObject.getInt("cmd");
if (cmd == 1) {
sn = jsonObject.getInt("sn");
String swver = jsonObject.getString("swver");
String hdver = jsonObject.getString("hdver");
String status = jsonObject.getString("status");
tvDeviceInfo.setText("序列号:" + sn + "\n\n软件版本:" + swver + "\n\n硬件版本:" + hdver + "\n\n幕布状态:" + status);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
public class MessageThread implements Runnable {
//接收UI线程消息的Handler对象
public Handler sendHandler;
private Socket socket;
BufferedReader br = null;
PrintWriter pw = null;
@Override
public void run() {
try {
socket = new Socket(mDeviceIp, PORT);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
//启动一个线程来读取数据
new Thread(new Runnable() {
@Override
public void run() {
String data = null;
try {
while ((data = br.readLine()) != null) {
Log.i(TAG, "接收到的数据:" + data);
Message msg = new Message();
msg.what = 0x123;
msg.obj = data;
handler.sendMessage(msg);
}
} catch (IOException e) {
Log.i(TAG, "接收数据异常:" + e.getMessage());
e.printStackTrace();
}
}
}).start();
Looper.prepare();
sendHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x345) {
Log.i(TAG, "写数据:" + (msg.obj).toString());
pw.write((msg.obj).toString());
// pw.write("{\"cmd\":2,\"action\":0}");
pw.flush();
}
}
};
//启动Looper
Looper.loop();
} catch (SocketTimeoutException e) {
Log.i(TAG, "网络连接超时!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (pw != null) {
pw.close();
}
if (br != null) {
br.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}