Commit 81dbb8ad57acd16621437db32b876d97651e9f2a
1 parent
69a8a55d
test room sn add ,constract socket send msg
Showing
3 changed files
with
94 additions
and
14 deletions
| 1 | +package com.qnbar.smc.service; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Created by wugian on 2017/3/29 | |
| 5 | + */ | |
| 6 | +public class SocketSendMsg { | |
| 7 | + public static final String REGISTER = "register"; | |
| 8 | + public static final String RECONNECT = "reconnect"; | |
| 9 | + /** | |
| 10 | + * cmd : register | |
| 11 | + * data : {"room_sn":"000002"} | |
| 12 | + */ | |
| 13 | + | |
| 14 | + private String cmd; | |
| 15 | + private DataEntity data; | |
| 16 | + | |
| 17 | + public String getCmd() { | |
| 18 | + return cmd; | |
| 19 | + } | |
| 20 | + | |
| 21 | + public void setCmd(String cmd) { | |
| 22 | + this.cmd = cmd; | |
| 23 | + } | |
| 24 | + | |
| 25 | + public DataEntity getData() { | |
| 26 | + return data; | |
| 27 | + } | |
| 28 | + | |
| 29 | + public void setData(DataEntity data) { | |
| 30 | + this.data = data; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public static class DataEntity { | |
| 34 | + /** | |
| 35 | + * room_sn : 000002 | |
| 36 | + */ | |
| 37 | + | |
| 38 | + private String room_sn; | |
| 39 | + | |
| 40 | + public String getRoom_sn() { | |
| 41 | + return room_sn; | |
| 42 | + } | |
| 43 | + | |
| 44 | + public void setRoom_sn(String room_sn) { | |
| 45 | + this.room_sn = room_sn; | |
| 46 | + } | |
| 47 | + } | |
| 48 | + | |
| 49 | + public SocketSendMsg contractRegisterMsg(String roomSn) { | |
| 50 | + this.cmd = REGISTER; | |
| 51 | + getDataEntity(roomSn); | |
| 52 | + return this; | |
| 53 | + } | |
| 54 | + | |
| 55 | + private void getDataEntity(String roomSn) { | |
| 56 | + DataEntity dataEntity = new DataEntity(); | |
| 57 | + dataEntity.setRoom_sn(roomSn); | |
| 58 | + this.data = dataEntity; | |
| 59 | + } | |
| 60 | + | |
| 61 | + public SocketSendMsg contractReconnectMsg(String roomSn) { | |
| 62 | + this.cmd = RECONNECT; | |
| 63 | + getDataEntity(roomSn); | |
| 64 | + return this; | |
| 65 | + } | |
| 66 | + | |
| 67 | +} | ... | ... |
| 1 | 1 | package com.qnbar.smc.service; |
| 2 | 2 | |
| 3 | 3 | import android.app.Service; |
| 4 | +import android.content.Context; | |
| 4 | 5 | import android.content.Intent; |
| 5 | 6 | import android.os.Handler; |
| 6 | 7 | import android.os.IBinder; |
| 7 | 8 | import android.util.Log; |
| 8 | 9 | import com.gimi.common.cinema.model.MessageEvent; |
| 10 | +import com.gimi.common.cinema.utils.SystemUtils; | |
| 9 | 11 | import com.google.gson.Gson; |
| 10 | 12 | import com.google.gson.JsonSyntaxException; |
| 13 | +import com.xgimi.gimicinema.BuildConfig; | |
| 11 | 14 | import org.greenrobot.eventbus.EventBus; |
| 12 | 15 | |
| 13 | 16 | import java.io.IOException; |
| ... | ... | @@ -26,17 +29,13 @@ public class SocketService extends Service { |
| 26 | 29 | |
| 27 | 30 | private static final int ROOM_HAS_REGISTERED = 1001; |
| 28 | 31 | private static final int ROOM_NOT_EXIST_M = 1002; |
| 32 | + private static final int HEAT_BEAT_RTN = 1003; | |
| 29 | 33 | |
| 30 | 34 | public static final String HOST = "121.43.189.162";// "192.168.1.21";// |
| 31 | 35 | public static final int PORT = 9501; |
| 32 | 36 | |
| 33 | - public static final String MESSAGE_ACTION = "com.dingmore.terminal.socket"; | |
| 34 | - public static final String HEART_BEAT_ACTION = "com.dingmore.terminal.socket.heart"; | |
| 35 | - | |
| 36 | 37 | public static final String HEART_BEAT_STRING = "\\r\\n\\r\\n";//心跳包内容 |
| 37 | - public static final String REGISTER_STRING = "{\"cmd\":\"register\",\"data\":{\"room_sn\":\"000003\"}}\\r\\n\\r\\n";//注册内容 | |
| 38 | - public static final String RECONNECT_STRING = "{\"cmd\":\"reconnect\",\"data\":{\"room_sn\":\"000003\"}}\\r\\n\\r\\n";//注册内容 | |
| 39 | - | |
| 38 | + public String testRoomSn = "000003"; | |
| 40 | 39 | private ReadThread mReadThread; |
| 41 | 40 | private Gson gson; |
| 42 | 41 | |
| ... | ... | @@ -68,6 +67,7 @@ public class SocketService extends Service { |
| 68 | 67 | }; |
| 69 | 68 | |
| 70 | 69 | private long sendTime = 0L; |
| 70 | + private Context context; | |
| 71 | 71 | |
| 72 | 72 | @Override |
| 73 | 73 | public IBinder onBind(Intent arg0) { |
| ... | ... | @@ -78,10 +78,12 @@ public class SocketService extends Service { |
| 78 | 78 | public void onCreate() { |
| 79 | 79 | super.onCreate(); |
| 80 | 80 | gson = new Gson(); |
| 81 | + context = this; | |
| 82 | + if (!"LETEST".equals(SystemUtils.getPid(context, BuildConfig.MACHINE_TYPE))) { | |
| 83 | + testRoomSn = "000002"; | |
| 84 | + } | |
| 81 | 85 | new InitSocketThread().start(); |
| 82 | 86 | Log.d(TAG, "socket service onCreate"); |
| 83 | -// mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); | |
| 84 | - | |
| 85 | 87 | } |
| 86 | 88 | |
| 87 | 89 | public boolean sendMsg(String msg) { |
| ... | ... | @@ -168,7 +170,8 @@ public class SocketService extends Service { |
| 168 | 170 | try { |
| 169 | 171 | if (!sendRegister) { |
| 170 | 172 | Log.d(TAG, "send register mes"); |
| 171 | - String msg = REGISTER_STRING; | |
| 173 | + SocketSendMsg ssm = new SocketSendMsg().contractRegisterMsg(testRoomSn); | |
| 174 | + String msg = gson.toJson(ssm) + HEART_BEAT_STRING; | |
| 172 | 175 | sendMsg(msg); |
| 173 | 176 | Log.d(TAG, "" + msg); |
| 174 | 177 | sendRegister = true; |
| ... | ... | @@ -187,8 +190,10 @@ public class SocketService extends Service { |
| 187 | 190 | SocketResponse socketResponse = gson.fromJson(message.trim(), SocketResponse.class); |
| 188 | 191 | switch (socketResponse.getCode()) { |
| 189 | 192 | case ROOM_HAS_REGISTERED: |
| 190 | - sendMsg(RECONNECT_STRING); | |
| 191 | - Log.d(TAG, "send reconnect mes"); | |
| 193 | + SocketSendMsg ssm = new SocketSendMsg().contractReconnectMsg(testRoomSn); | |
| 194 | + String msg = gson.toJson(ssm) + HEART_BEAT_STRING; | |
| 195 | + sendMsg(msg); | |
| 196 | + Log.d(TAG, "send reconnect mes: " + msg); | |
| 192 | 197 | break; |
| 193 | 198 | case ROOM_NOT_EXIST_M: |
| 194 | 199 | Log.d(TAG, "re init socket"); |
| ... | ... | @@ -196,6 +201,10 @@ public class SocketService extends Service { |
| 196 | 201 | releaseLastSocket(mWeakSocket); |
| 197 | 202 | initSocket(); |
| 198 | 203 | break; |
| 204 | + case HEAT_BEAT_RTN: | |
| 205 | + Log.d(TAG, "HEAT_BEAT_RTN"); | |
| 206 | + sendTime = System.currentTimeMillis(); | |
| 207 | + break; | |
| 199 | 208 | } |
| 200 | 209 | if (("openDoor").equals(socketResponse.getCmd())) { |
| 201 | 210 | MessageEvent messageEvent = new MessageEvent(); | ... | ... |
| ... | ... | @@ -758,6 +758,7 @@ public class MainActivity extends BaseActivity implements IMainView, EventListen |
| 758 | 758 | |
| 759 | 759 | |
| 760 | 760 | private void show(String msg) { |
| 761 | +// Log.d(TAG, "show: " + msg); | |
| 761 | 762 | Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); |
| 762 | 763 | } |
| 763 | 764 | |
| ... | ... | @@ -768,13 +769,16 @@ public class MainActivity extends BaseActivity implements IMainView, EventListen |
| 768 | 769 | switch (deviceInfo.status) { |
| 769 | 770 | case LightAdapter.STATUS_LOGIN: |
| 770 | 771 | connectMeshAddress = mApplication.getConnectDevice().meshAddress; |
| 771 | - show("main login success"); | |
| 772 | +// show("main login success"); | |
| 773 | + show("灯光配对成功"); | |
| 772 | 774 | break; |
| 773 | 775 | case LightAdapter.STATUS_CONNECTING: |
| 774 | - show("login"); | |
| 776 | +// show("login"); | |
| 777 | + show("与灯光配对"); | |
| 775 | 778 | break; |
| 776 | 779 | case LightAdapter.STATUS_LOGOUT: |
| 777 | - show("disconnect"); | |
| 780 | +// show("disconnect"); | |
| 781 | + show("灯光断开"); | |
| 778 | 782 | break; |
| 779 | 783 | default: |
| 780 | 784 | break; | ... | ... |
Please
register
or
login
to post a comment