Commit 56d0caf29ba0a6e771093e2a822140518d1742db

Authored by jinyan.yi
1 parent 2ecad102

协议调整

@@ -201,17 +201,17 @@ public class SmartControlService extends BaseService implements EventListener<St @@ -201,17 +201,17 @@ public class SmartControlService extends BaseService implements EventListener<St
201 @Subscribe(threadMode = ThreadMode.MAIN) 201 @Subscribe(threadMode = ThreadMode.MAIN)
202 public void onMoonEvent(MessageEvent messageEvent) { 202 public void onMoonEvent(MessageEvent messageEvent) {
203 switch (messageEvent.getEventId()) { 203 switch (messageEvent.getEventId()) {
204 - case SocketService.JUST_OPEN_DOOR: 204 + case SocketService1.JUST_OPEN_DOOR:
205 openDoor(); 205 openDoor();
206 Log.d("event bus", "open door" + messageEvent.getMessage()); 206 Log.d("event bus", "open door" + messageEvent.getMessage());
207 break; 207 break;
208 - case SocketService.USER_OPEN_DOOR_AND_GET_MOVIE: 208 + case SocketService1.USER_OPEN_DOOR_AND_GET_MOVIE:
209 needReport = true; 209 needReport = true;
210 openDoor(); 210 openDoor();
211 presenter.getOrderInfo(this); 211 presenter.getOrderInfo(this);
212 Log.d("event bus", "open door" + messageEvent.getMessage()); 212 Log.d("event bus", "open door" + messageEvent.getMessage());
213 break; 213 break;
214 - case SocketService.USER_OPEN_DOOR: 214 + case SocketService1.USER_OPEN_DOOR:
215 needReport = true; 215 needReport = true;
216 openDoor(); 216 openDoor();
217 // if (rightSn) { 217 // if (rightSn) {
1 -package com.qnbar.smc.service;  
2 -  
3 -import android.app.Service;  
4 -import android.content.Context;  
5 -import android.content.Intent;  
6 -import android.os.IBinder;  
7 -import android.text.TextUtils;  
8 -import android.util.Log;  
9 -import android.widget.Toast;  
10 -import com.gimi.common.cinema.model.MessageEvent;  
11 -import com.gimi.common.cinema.model.RoomInfo;  
12 -import com.gimi.common.cinema.utils.SystemUtils;  
13 -import com.gimi.common.cinema.utils.Utils;  
14 -import com.google.gson.Gson;  
15 -import com.google.gson.JsonSyntaxException;  
16 -import com.qnbar.smc.utils.LightOperationUtils;  
17 -import com.xgimi.gimicinema.activity.QrCodeShowActivity;  
18 -import com.xgimi.gimicinema.application.FangTangApplication;  
19 -import com.xgimi.smartscreen.encrypt.AuthCode;  
20 -import org.greenrobot.eventbus.EventBus;  
21 -  
22 -import java.io.IOException;  
23 -import java.io.InputStream;  
24 -import java.io.OutputStream;  
25 -import java.lang.ref.WeakReference;  
26 -import java.net.Socket;  
27 -import java.util.Arrays;  
28 -  
29 -public class SocketService extends Service {  
30 - private static final String TAG = "BackService";  
31 - private static final long HEART_BEAT_RATE = 5 * 1000;  
32 -  
33 - public static final int JUST_OPEN_DOOR = 1701;  
34 - public static final int USER_OPEN_DOOR_AND_GET_MOVIE = 1703;  
35 - public static final int USER_OPEN_DOOR = 1704;//get the movie again charge show new movie  
36 - // public static final int USER_OPEN_DOOR_AND_GET_MOVIE = 1703;  
37 - /*  
38 - * 10001 //服务器发送了命令(带命令的返回消息),这个一般是服务器主动发消息的时候code为这个值  
39 - 10002 //连接(认证)失败  
40 - 10003 //还未注册连接就进行认证  
41 - 10004 //终端编号未传  
42 - 10005 //心跳异常,没有找到房间连接信息  
43 - 10006 //心跳异常,连接标识符不一致  
44 - 10007 //收到未知命令  
45 - 10008 //收到无法解析的消息  
46 - 10009 //已经认证过了,不允许再认证  
47 - 10010 //room_sn 已连接,不能再连接  
48 - */  
49 - private static final int VERIFY_SUCCESS = 9999;  
50 - private static final int SUCCESS_MESSAGE = 10000;  
51 - private static final int CONTAIN_MESSAGE = 10001;  
52 - private static final int AUTHENTICATION_FAILED = 10002; //连接(认证)失败  
53 - private static final int CONNECTION_BEFORE_AUTHENTICATION = 10003;//还未注册连接就进行认证  
54 - private static final int NOT_SEND_ROOM_SN = 10004;//终端编号未传  
55 - private static final int HEART_BEAR_ERROR = 10005; //心跳异常,没有找到房间连接信息  
56 - private static final int HEART_BEAR_ERROR_SYMBOL = 10006;//心跳异常,连接标识符不一致  
57 - private static final int OPEN_DOOR = 50001;//开门命令  
58 - private static final int CLEAN_OVER = 50002;//  
59 -  
60 - private static final int ROOM_HAS_REGISTERED = 1001;  
61 - private static final int ROOM_NOT_EXIST_M = 1002;  
62 - private static final int HEAT_BEAT_RTN = 1003;  
63 -  
64 - // online  
65 - public static final String SERVER_HOST_ONLINE = "conn.ft.qnbar.com";// "192.168.1.21";//  
66 - public static final int SERVER_PORT_ONLINE = 8899;  
67 -  
68 - // develop  
69 - public static final String SERVER_HOST_DEVELOP = "10.10.4.6";// "192.168.1.21";//  
70 - public static final int SERVER_PORT_DEVELOP = 9501;  
71 - // test  
72 - public static final String SERVER_HOST = "10.10.4.6";// "192.168.1.21";//  
73 - public static final int SERVER_PORT = 8899;  
74 -  
75 - private String serverHost = SERVER_HOST;  
76 - private int serverPort = SERVER_PORT;  
77 -  
78 -  
79 - public static final String END_SYMBOL = "\\r\\n\\r\\n";//心跳包内容  
80 - // public String testRoomSn = "R170413034374";  
81 - public String testRoomSn = "c";  
82 -  
83 - private ReadThread mReadThread;  
84 - private HeatBeatThread mHeatBeatThread;  
85 - private Gson gson;  
86 -  
87 -// private LocalBroadcastManager mLocalBroadcastManager;  
88 -  
89 - private WeakReference<Socket> mSocket;  
90 -  
91 - // For heart Beat  
92 -// private Handler mHandler = new Handler();  
93 -// private Runnable heartBeatRunnable = new Runnable() {  
94 -//  
95 -// @Override  
96 -// public void run() {  
97 -// if (System.currentTimeMillis() - sendTime >= HEART_BEAT_RATE) {  
98 -// Log.d(TAG, "heart beat");  
99 -// boolean isSuccess = sendMsg(new Gson().toJson(new SocketSendMsg().contractHeartBeatMsg(testRoomSn)) + END_SYMBOL);//就发送一个HEART_BEAT_STRING过去 如果发送失败,就重新初始化一个socket  
100 -// if (!isSuccess) {  
101 -// Log.d(TAG, "heart beat error restart");  
102 -// mHandler.removeCallbacks(heartBeatRunnable);  
103 -// mReadThread.release();  
104 -// mHeatBeatThread.release();  
105 -// releaseLastSocket(mSocket);  
106 -// new InitSocketThread().start();  
107 -// }  
108 -// } else {  
109 -// Log.d(TAG, "heart beat less than beat rate");  
110 -// }  
111 -// mHandler.postDelayed(this, HEART_BEAT_RATE);  
112 -// }  
113 -// };  
114 -  
115 - private long sendTime = 0L;  
116 - private Context context;  
117 -  
118 - @Override  
119 - public IBinder onBind(Intent arg0) {  
120 - return null;  
121 - }  
122 -  
123 - @Override  
124 - public void onCreate() {  
125 - super.onCreate();  
126 - gson = new Gson();  
127 - context = this;  
128 - }  
129 -  
130 - @Override  
131 - public int onStartCommand(Intent intent, int flags, int startId) {  
132 - int ftTest = Utils.getInt(this, "ft-test", 0);  
133 - switch (ftTest) {  
134 - case 0:  
135 - serverHost = SERVER_HOST_ONLINE;  
136 - serverPort = SERVER_PORT_ONLINE;  
137 - break;  
138 - case 1:  
139 - serverHost = SERVER_HOST_DEVELOP;  
140 - serverPort = SERVER_PORT_DEVELOP;  
141 - break;  
142 - case 2:  
143 - serverHost = SERVER_HOST;  
144 - serverPort = SERVER_PORT;  
145 - break;  
146 - }  
147 -  
148 - String roomInfoStr = Utils.getString(this, "room-info");  
149 - if (!TextUtils.isEmpty(roomInfoStr)) {  
150 - Log.d("room-info", "room info not null");  
151 - RoomInfo roomInfo = null;  
152 - try {  
153 - roomInfo = gson.fromJson(roomInfoStr, RoomInfo.class);  
154 - Log.d("room-info", "room info room_sn update");  
155 - testRoomSn = roomInfo.getData().getRoom_sn();  
156 - } catch (JsonSyntaxException e) {  
157 - Log.d("room-info", "room gson parse exception return");  
158 - Toast.makeText(this, "房间信息配置出错,请重新进入应用获取", Toast.LENGTH_SHORT).show();  
159 - e.printStackTrace();  
160 - return super.onStartCommand(intent, flags, startId);  
161 - }  
162 - } else {  
163 - Log.d("room-info", "room info not exist");  
164 - Toast.makeText(this, "没有获取到房间信息,请查看后台配置", Toast.LENGTH_SHORT).show();  
165 - return super.onStartCommand(intent, flags, startId);  
166 - }  
167 -// }  
168 - new InitSocketThread().start();  
169 - Log.d(TAG, "socket service onCreate");  
170 - return super.onStartCommand(intent, flags, startId);  
171 - }  
172 -  
173 - public boolean sendMsg(String msg) {  
174 - if (null == mSocket || null == mSocket.get()) {  
175 - return false;  
176 - }  
177 - Log.d(TAG, "send msg:" + msg);  
178 - Socket soc = mSocket.get();  
179 - try {  
180 - if (!soc.isClosed() && !soc.isOutputShutdown()) {  
181 - OutputStream os = soc.getOutputStream();  
182 - String message = msg;  
183 - os.write(message.getBytes());  
184 - os.flush();  
185 - sendTime = System.currentTimeMillis();//每次发送成数据,就改一下最后成功发送的时间,节省心跳间隔时间  
186 - } else {  
187 - return false;  
188 - }  
189 - } catch (IOException e) {  
190 - e.printStackTrace();  
191 - return false;  
192 - }  
193 - return true;  
194 - }  
195 -  
196 - private void initSocket() throws IOException {//初始化Socket  
197 - Log.d(TAG, "serverHost:serverPort:" + serverHost + ":" + serverPort);  
198 - Socket so = new Socket(serverHost, serverPort);  
199 - mSocket = new WeakReference<Socket>(so);  
200 - mReadThread = new ReadThread(so);  
201 - mReadThread.start();  
202 -// new Thread(heartBeatRunnable).start();  
203 -// mHandler.post(heartBeatRunnable);//初始化成功后,就准备发送心跳包  
204 - mHeatBeatThread = new HeatBeatThread(so);  
205 -// mHeatBeatThread.start();//上面的 one plus 3t NetworkOnMainThreadException!!!  
206 - }  
207 -  
208 -  
209 - private void releaseLastSocket(WeakReference<Socket> mSocket) {  
210 - try {  
211 - if (null != mSocket) {  
212 - Socket sk = mSocket.get();  
213 - if (!sk.isClosed()) {  
214 - sk.close();  
215 - }  
216 - sk = null;  
217 - mSocket = null;  
218 - }  
219 - } catch (IOException e) {  
220 - e.printStackTrace();  
221 - }  
222 - }  
223 -  
224 - class InitSocketThread extends Thread {  
225 - @Override  
226 - public void run() {  
227 - super.run();  
228 - try {  
229 - initSocket();  
230 - } catch (IOException e) {  
231 - e.printStackTrace();  
232 - Log.d(TAG, "init socket thread error,restart again after 10's");  
233 - try {  
234 - Thread.sleep(HEART_BEAT_RATE);  
235 - } catch (InterruptedException e1) {  
236 - e1.printStackTrace();  
237 - }  
238 - this.run();  
239 - }  
240 - }  
241 - }  
242 -  
243 - private String varifyMsg = "";  
244 -  
245 - // Thread to read content from Socket  
246 - class ReadThread extends Thread {  
247 - private WeakReference<Socket> mWeakSocket;  
248 - private boolean isStart = true;  
249 -  
250 - public ReadThread(Socket socket) {  
251 - mWeakSocket = new WeakReference<Socket>(socket);  
252 - }  
253 -  
254 - public void release() {  
255 - isStart = false;  
256 - releaseLastSocket(mWeakSocket);  
257 - }  
258 -  
259 - @Override  
260 - public void run() {  
261 - super.run();  
262 - Socket socket = mWeakSocket.get();  
263 - if (null != socket) {  
264 - try {  
265 - if (!sendRegister) {  
266 - Log.d(TAG, "send register mes");  
267 - SocketSendMsg ssm = new SocketSendMsg().contractRegisterMsg(testRoomSn);  
268 - String msg = gson.toJson(ssm) + END_SYMBOL;  
269 - sendMsg(msg);  
270 - Log.d(TAG, "" + msg);  
271 - sendRegister = true;  
272 - }  
273 - Log.d(TAG, "send register mes end");  
274 - InputStream is = socket.getInputStream();  
275 - byte[] buffer = new byte[1024 * 4];  
276 - int length = 0;  
277 - while (!socket.isClosed() && !socket.isInputShutdown()  
278 - && isStart && ((length = is.read(buffer)) != -1)) {  
279 - if (length > 0) {  
280 - String message = new String(Arrays.copyOf(buffer,  
281 - length)).trim();  
282 - Log.d(TAG, "end:" + message.contains(END_SYMBOL) + "");  
283 - Log.d(TAG, "recv msg:" + message);  
284 - try {  
285 - if (message.endsWith(END_SYMBOL)) {  
286 - message = message.replace(END_SYMBOL, "");  
287 - }  
288 - SocketResponse socketResponse = gson.fromJson(message/*.substring(0, message.length() - 8)*/, SocketResponse.class);  
289 - switch (socketResponse.getCode()) {  
290 - case SUCCESS_MESSAGE:  
291 - Log.d(TAG, "success:" + socketResponse.getCmd());  
292 - if (!TextUtils.isEmpty(socketResponse.getData().getVerify())) {  
293 - varifyMsg = AuthCode.getDecodeStr(socketResponse.getData().getVerify());  
294 - SocketSendMsg ssm = new SocketSendMsg().contractVerifyMsg(testRoomSn, varifyMsg);  
295 - String msg = gson.toJson(ssm) + END_SYMBOL;  
296 - sendMsg(msg);  
297 - }  
298 - break;  
299 - case VERIFY_SUCCESS:  
300 - Log.d(TAG, "VERIFY_SUCCESS:" + socketResponse.getCmd());  
301 - mHeatBeatThread.start();//上面的 one plus 3t NetworkOnMainThreadException!!!  
302 - Log.d(TAG, "verify success start heart beat");  
303 - break;  
304 - case CONTAIN_MESSAGE:  
305 - if (socketResponse.getData() != null) {  
306 - if (socketResponse.getCmd() == OPEN_DOOR) {  
307 -// new SystemUtils().openFtLed(context.getApplicationContext());  
308 - switch (socketResponse.getData().getUser()) {  
309 - //10用户,20管理员,默认值为0  
310 - case 10:  
311 - new SystemUtils().openFtLed(context.getApplicationContext());  
312 - if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {  
313 - LightOperationUtils.open();  
314 - LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));  
315 - }  
316 - sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when new user come in");  
317 - if (socketResponse.getData().getFirst() == 1) {  
318 -// LightOperationUtils.open();  
319 -// LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));  
320 - sendMessage(USER_OPEN_DOOR_AND_GET_MOVIE, "user first open the door");  
321 - } else {  
322 - sendMessage(USER_OPEN_DOOR, "user open the door");  
323 - }  
324 - break;  
325 - case 20:  
326 - if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {  
327 - Log.d("LightOperationUtils", "admin open light");  
328 - LightOperationUtils.open();  
329 - LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));  
330 - }  
331 - sendMessage(JUST_OPEN_DOOR, "administrator open the door");  
332 - break;  
333 - case 0:  
334 - sendMessage(JUST_OPEN_DOOR, "get zero none user or administrator open the door");  
335 - break;  
336 - default:  
337 - sendMessage(JUST_OPEN_DOOR, "none user or administrator open the door");  
338 - break;  
339 - }  
340 - } else if (socketResponse.getCmd() == CLEAN_OVER) {  
341 - Log.d("LightOperationUtils", "admin clean over close light");  
342 - LightOperationUtils.setLightValue(5);  
343 - LightOperationUtils.close();  
344 - new SystemUtils().closeFtLed(context.getApplicationContext());  
345 - sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when clean over");  
346 - }  
347 - }  
348 - break;  
349 - default:  
350 - Log.d(TAG, "print msg:" + socketResponse.toString());  
351 - }  
352 -// if (("sendMessage").equals(socketResponse.getCmd())) {  
353 -// MessageEvent messageEvent = new MessageEvent();  
354 -// messageEvent.setEventId(JUST_OPEN_DOOR);  
355 -// messageEvent.setMessage("click item");  
356 -// EventBus.getDefault().post(messageEvent);  
357 -// }  
358 - } catch (JsonSyntaxException e) {  
359 - Log.d(TAG, message);  
360 - e.printStackTrace();  
361 - }  
362 - //收到服务器过来的消息,就通过Broadcast发送出去  
363 -// if (message.equals(END_SYMBOL)) {//处理心跳回复  
364 -// Intent intent = new Intent(HEART_BEAT_ACTION);  
365 -// mLocalBroadcastManager.sendBroadcast(intent);  
366 -// } else {  
367 -// //其他消息回复  
368 -// Intent intent = new Intent(MESSAGE_ACTION);  
369 -// intent.putExtra("message", message);  
370 -// mLocalBroadcastManager.sendBroadcast(intent);  
371 -// }  
372 - }  
373 - }  
374 - } catch (IOException e) {  
375 - e.printStackTrace();  
376 - }  
377 - }  
378 - }  
379 - }  
380 -  
381 - private void sendMessage(int type, String msg) {  
382 - MessageEvent messageEvent = new MessageEvent();  
383 - messageEvent.setEventId(type);  
384 - messageEvent.setMessage(msg);  
385 - EventBus.getDefault().post(messageEvent);  
386 - }  
387 -  
388 - boolean sendRegister = false;  
389 -  
390 -  
391 - // Thread to read content from Socket  
392 - class HeatBeatThread extends Thread {  
393 - private WeakReference<Socket> mWeakSocket;  
394 - private boolean isStart = true;  
395 -  
396 - public HeatBeatThread(Socket socket) {  
397 - mWeakSocket = new WeakReference<Socket>(socket);  
398 - }  
399 -  
400 - public void release() {  
401 - isStart = false;  
402 - releaseLastSocket(mWeakSocket);  
403 - }  
404 -  
405 - @Override  
406 - public void run() {  
407 - super.run();  
408 - Socket socket = mWeakSocket.get();  
409 - if (null != socket) {  
410 - while (isStart) {  
411 - if (System.currentTimeMillis() - sendTime >= HEART_BEAT_RATE) {  
412 - Log.d(TAG, "heart beat:" + Thread.currentThread().getId());  
413 - boolean isSuccess = sendMsg(new Gson().toJson(new SocketSendMsg().contractHeartBeatMsg(testRoomSn)) + END_SYMBOL);//就发送一个HEART_BEAT_STRING过去 如果发送失败,就重新初始化一个socket  
414 - if (!isSuccess) {  
415 - Log.d(TAG, "heart beat error restart:" + Thread.currentThread().getId());  
416 -// mHandler.removeCallbacks(heartBeatRunnable);  
417 - mReadThread.release();  
418 - mHeatBeatThread.release();  
419 - sendRegister = false;  
420 - releaseLastSocket(mSocket);  
421 - new InitSocketThread().start();  
422 - }  
423 - } else {  
424 - Log.d(TAG, "heart beat less than beat rate:" + Thread.currentThread().getId());  
425 - }  
426 - try {  
427 - Thread.sleep(HEART_BEAT_RATE);  
428 - } catch (InterruptedException e) {  
429 - e.printStackTrace();  
430 - }  
431 - }  
432 - }  
433 - }  
434 - }  
435 -  
436 - @Override  
437 - public void onDestroy() {  
438 - Log.d(TAG, "socket service destroy");  
439 - super.onDestroy();  
440 - }  
441 -}  
@@ -21,11 +21,13 @@ import com.google.gson.Gson; @@ -21,11 +21,13 @@ import com.google.gson.Gson;
21 import com.google.gson.JsonSyntaxException; 21 import com.google.gson.JsonSyntaxException;
22 import com.qnbar.smc.model.Lights; 22 import com.qnbar.smc.model.Lights;
23 import com.qnbar.smc.soketProtocol.fromServer.EquipmentControl; 23 import com.qnbar.smc.soketProtocol.fromServer.EquipmentControl;
  24 +import com.qnbar.smc.soketProtocol.fromServer.VerifyCode;
24 import com.qnbar.smc.soketProtocol.toServer.EquipmentStatus; 25 import com.qnbar.smc.soketProtocol.toServer.EquipmentStatus;
25 import com.qnbar.smc.soketProtocol.MainRequest; 26 import com.qnbar.smc.soketProtocol.MainRequest;
26 import com.qnbar.smc.soketProtocol.MainResponse; 27 import com.qnbar.smc.soketProtocol.MainResponse;
27 import com.qnbar.smc.soketProtocol.fromServer.OpenDoor; 28 import com.qnbar.smc.soketProtocol.fromServer.OpenDoor;
28 import com.qnbar.smc.soketProtocol.fromServer.ReportEquStatus; 29 import com.qnbar.smc.soketProtocol.fromServer.ReportEquStatus;
  30 +import com.qnbar.smc.soketProtocol.toServer.SocketSendMsg;
29 import com.qnbar.smc.utils.LightOperationUtils; 31 import com.qnbar.smc.utils.LightOperationUtils;
30 import com.telink.bluetooth.light.ConnectionStatus; 32 import com.telink.bluetooth.light.ConnectionStatus;
31 import com.xgimi.gimicinema.ICinemaControl; 33 import com.xgimi.gimicinema.ICinemaControl;
@@ -43,14 +45,18 @@ import java.lang.ref.WeakReference; @@ -43,14 +45,18 @@ import java.lang.ref.WeakReference;
43 import java.net.Socket; 45 import java.net.Socket;
44 import java.util.Arrays; 46 import java.util.Arrays;
45 47
46 -import static com.qnbar.smc.service.SocketService.JUST_OPEN_DOOR;  
47 -import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR;  
48 -import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR_AND_GET_MOVIE; 48 +//import static com.qnbar.smc.service.SocketService.JUST_OPEN_DOOR;
  49 +//import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR;
  50 +//import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR_AND_GET_MOVIE;
49 51
50 public class SocketService1 extends BaseService { 52 public class SocketService1 extends BaseService {
51 private static final String TAG = "BackService1"; 53 private static final String TAG = "BackService1";
52 private static final long HEART_BEAT_RATE = 2 * 1000; 54 private static final long HEART_BEAT_RATE = 2 * 1000;
53 55
  56 + public static final int JUST_OPEN_DOOR = 1701;
  57 + public static final int USER_OPEN_DOOR_AND_GET_MOVIE = 1703;
  58 + public static final int USER_OPEN_DOOR = 1704;
  59 +
54 // public static final int USER_OPEN_DOOR_AND_GET_MOVIE = 1703; 60 // public static final int USER_OPEN_DOOR_AND_GET_MOVIE = 1703;
55 /* 61 /*
56 * 10001 //服务器发送了命令(带命令的返回消息),这个一般是服务器主动发消息的时候code为这个值 62 * 10001 //服务器发送了命令(带命令的返回消息),这个一般是服务器主动发消息的时候code为这个值
@@ -311,8 +317,7 @@ public class SocketService1 extends BaseService { @@ -311,8 +317,7 @@ public class SocketService1 extends BaseService {
311 while (!socket.isClosed() && !socket.isInputShutdown() 317 while (!socket.isClosed() && !socket.isInputShutdown()
312 && isStart && ((length = is.read(buffer)) != -1)) { 318 && isStart && ((length = is.read(buffer)) != -1)) {
313 if (length > 0) { 319 if (length > 0) {
314 - String message = new String(Arrays.copyOf(buffer,  
315 - length)).trim(); 320 + String message = new String(Arrays.copyOf(buffer, length)).trim();
316 Log.d(TAG, "recv msg:" + message); 321 Log.d(TAG, "recv msg:" + message);
317 try { 322 try {
318 if (message.endsWith(END_SYMBOL)) { 323 if (message.endsWith(END_SYMBOL)) {
@@ -361,10 +366,10 @@ public class SocketService1 extends BaseService { @@ -361,10 +366,10 @@ public class SocketService1 extends BaseService {
361 case RETURN_VERIFY_CODE: 366 case RETURN_VERIFY_CODE:
362 Log.d(TAG, "RETURN_VERIFY_CODE"); 367 Log.d(TAG, "RETURN_VERIFY_CODE");
363 if (!TextUtils.isEmpty(socketResponse.getData())) { 368 if (!TextUtils.isEmpty(socketResponse.getData())) {
364 - OpenDoor openDoorResponse = gson.fromJson(socketResponse.getData(), OpenDoor.class); 369 + VerifyCode verifyResponse = gson.fromJson(socketResponse.getData(), VerifyCode.class);
365 370
366 - if (!TextUtils.isEmpty(openDoorResponse.getVerify())) {  
367 - String verifyMsg = AuthCode.getDecodeStr(openDoorResponse.getVerify()); 371 + if (!TextUtils.isEmpty(verifyResponse.getVerify())) {
  372 + String verifyMsg = AuthCode.getDecodeStr(verifyResponse.getVerify());
368 SocketSendMsg ssm = new SocketSendMsg().contractVerifyMsg(testRoomSn, verifyMsg); 373 SocketSendMsg ssm = new SocketSendMsg().contractVerifyMsg(testRoomSn, verifyMsg);
369 String msg = gson.toJson(ssm) + END_SYMBOL; 374 String msg = gson.toJson(ssm) + END_SYMBOL;
370 sendMsg(msg); 375 sendMsg(msg);
  1 +package com.qnbar.smc.soketProtocol.fromServer;
  2 +
  3 +/**
  4 + * Created by jinyan.yi on 2017/6/2.
  5 + */
  6 +
  7 +public class VerifyCode {
  8 + private String verify;
  9 +
  10 + public String getVerify() {
  11 + return verify;
  12 + }
  13 +
  14 + public void setVerify(String verify) {
  15 + this.verify = verify;
  16 + }
  17 +
  18 + @Override
  19 + public String toString() {
  20 + return "VerifyCode{" +
  21 + "verify='" + verify + '\'' +
  22 + '}';
  23 + }
  24 +}
  1 +package com.qnbar.smc.soketProtocol.toServer;
  2 +
  3 +/**
  4 + * Created by wugian on 2017/3/29
  5 + */
  6 +public class SocketSendMsg {
  7 + public static final int REGISTER = 20010;
  8 + public static final int VERIFY = 20020;
  9 + public static final int HEART_BEAT = 20025;
  10 + /* 'cmd'=>20010,//意义和下面的cmd对应
  11 + 'msg'=>'',
  12 + 'data'=>[
  13 + 'room_sn'=>'',终端串号
  14 + 'verify'=>'',//认证时发送
  15 + ],*/
  16 + private int cmd;
  17 + private String msg;
  18 + private DataEntity data;
  19 +
  20 + public int getCmd() {
  21 + return cmd;
  22 + }
  23 +
  24 + public void setCmd(int cmd) {
  25 + this.cmd = cmd;
  26 + }
  27 +
  28 + public String getMsg() {
  29 + return msg;
  30 + }
  31 +
  32 + public void setMsg(String msg) {
  33 + this.msg = msg;
  34 + }
  35 +
  36 + public DataEntity getData() {
  37 + return data;
  38 + }
  39 +
  40 + public void setData(DataEntity data) {
  41 + this.data = data;
  42 + }
  43 +
  44 + public static class DataEntity {
  45 + private String room_sn;
  46 + private String verify;
  47 +
  48 + public String getRoom_sn() {
  49 + return room_sn;
  50 + }
  51 +
  52 + public void setRoom_sn(String room_sn) {
  53 + this.room_sn = room_sn;
  54 + }
  55 +
  56 + public String getVerify() {
  57 + return verify;
  58 + }
  59 +
  60 + public void setVerify(String verify) {
  61 + this.verify = verify;
  62 + }
  63 +
  64 + @Override
  65 + public String toString() {
  66 + return "DataEntity{" +
  67 + "room_sn='" + room_sn + '\'' +
  68 + ", verify='" + verify + '\'' +
  69 + '}';
  70 + }
  71 + }
  72 +
  73 + public SocketSendMsg contractRegisterMsg(String roomSn) {
  74 + this.cmd = REGISTER;
  75 + getDataEntity(roomSn, null);
  76 + return this;
  77 + }
  78 +
  79 + private void getDataEntity(String roomSn, String varify) {
  80 + DataEntity dataEntity = new DataEntity();
  81 + dataEntity.setRoom_sn(roomSn);
  82 + dataEntity.setVerify(varify);
  83 + this.data = dataEntity;
  84 + }
  85 +
  86 + public SocketSendMsg contractVerifyMsg(String roomSn, String varifyMsg) {
  87 + this.cmd = VERIFY;
  88 + getDataEntity(roomSn, varifyMsg);
  89 + return this;
  90 + }
  91 +
  92 + public SocketSendMsg contractHeartBeatMsg(String roomSn) {
  93 + this.cmd = HEART_BEAT;
  94 + getDataEntity(roomSn, null);
  95 + return this;
  96 + }
  97 +
  98 + @Override
  99 + public String toString() {
  100 + return "SocketSendMsg{" +
  101 + "cmd=" + cmd +
  102 + ", msg='" + msg + '\'' +
  103 + ", data=" + data +
  104 + '}';
  105 + }
  106 +}
@@ -24,7 +24,7 @@ import com.gimi.common.cinema.model.MessageEvent; @@ -24,7 +24,7 @@ import com.gimi.common.cinema.model.MessageEvent;
24 import com.gimi.common.cinema.model.SambaMsg; 24 import com.gimi.common.cinema.model.SambaMsg;
25 import com.gimi.common.cinema.utils.T; 25 import com.gimi.common.cinema.utils.T;
26 import com.gimi.common.cinema.utils.WifiApManger; 26 import com.gimi.common.cinema.utils.WifiApManger;
27 -import com.qnbar.smc.service.SocketService; 27 +import com.qnbar.smc.service.SocketService1;
28 import com.xgimi.gimicinema.BuildConfig; 28 import com.xgimi.gimicinema.BuildConfig;
29 import com.xgimi.gimicinema.R; 29 import com.xgimi.gimicinema.R;
30 import com.xgimi.gimicinema.mview.ISettingView; 30 import com.xgimi.gimicinema.mview.ISettingView;
@@ -336,7 +336,7 @@ public class SettingActivity extends BaseActivity implements ISettingView { @@ -336,7 +336,7 @@ public class SettingActivity extends BaseActivity implements ISettingView {
336 } 336 }
337 337
338 public void testOpenDoor(View view) { 338 public void testOpenDoor(View view) {
339 - openDoor(SocketService.JUST_OPEN_DOOR, "setting test open door"); 339 + openDoor(SocketService1.JUST_OPEN_DOOR, "setting test open door");
340 // PollingUtils.stopPollingService(this.getApplicationContext(), PollingServiceDemo.class, PollingService.ACTION); 340 // PollingUtils.stopPollingService(this.getApplicationContext(), PollingServiceDemo.class, PollingService.ACTION);
341 // new SystemUtils().closeFtLed(this);;//test close led,could open by power 341 // new SystemUtils().closeFtLed(this);;//test close led,could open by power
342 } 342 }
@@ -49,7 +49,7 @@ public class CinemaControlService extends Service { @@ -49,7 +49,7 @@ public class CinemaControlService extends Service {
49 private int currentState = 0; 49 private int currentState = 0;
50 private String currentPath = null; 50 private String currentPath = null;
51 // private static final int SAVE_BEAT = 500; 51 // private static final int SAVE_BEAT = 500;
52 - private static final int SAVE_BEAT = 50;//default 50 52 + private static final int SAVE_BEAT = 3;//default 50
53 private int saveBeat = SAVE_BEAT; 53 private int saveBeat = SAVE_BEAT;
54 54
55 private final ICinemaControl.Stub cinemaControl = new ICinemaControl.Stub() { 55 private final ICinemaControl.Stub cinemaControl = new ICinemaControl.Stub() {
Please register or login to post a comment