|
@@ -1,7 +1,10 @@
|
|
|
package com.wdkl.nursewatch.utils;
|
|
|
|
|
|
+import android.content.Intent;
|
|
|
import android.support.annotation.Nullable;
|
|
|
+import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
+import android.widget.Toast;
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
import com.google.gson.GsonBuilder;
|
|
@@ -11,6 +14,10 @@ import com.wdkl.nursewatch.entity.MessageEntity;
|
|
|
import com.wdkl.nursewatch.entity.MessageEvent;
|
|
|
import com.wdkl.nursewatch.entity.WebsocketEntity;
|
|
|
import com.wdkl.nursewatch.entity.WebsocketEntityReal;
|
|
|
+import com.wdkl.nursewatch.presentation.ui.activities.HomeActivity;
|
|
|
+import com.wdkl.nursewatch.presentation.ui.activities.WelcomeActivity;
|
|
|
+import com.wdkl.nursewatch.presentation.ui.activitiesphone.LoginActivityPhone;
|
|
|
+import com.wdkl.nursewatch.presentation.ui.activitiesphone.SettingActivityPhone;
|
|
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
|
|
|
@@ -23,15 +30,16 @@ import okhttp3.WebSocket;
|
|
|
import okhttp3.WebSocketListener;
|
|
|
import okio.ByteString;
|
|
|
|
|
|
-public class WebsocketUtil {
|
|
|
-
|
|
|
+import static com.wdkl.nursewatch.presentation.ui.activitiesphone.SettingActivityPhone.settingActivityPhone;
|
|
|
|
|
|
+public class WebsocketUtil {
|
|
|
private String TAG = WebsocketUtil.class.getSimpleName();
|
|
|
- //原有的
|
|
|
+ //原有的
|
|
|
// public static String serverUrl = "ws://id.wdklian.com:10011/call";
|
|
|
- //public static String serverUrl = "ws://192.168.3.88:10011/call";//吴总做测试 20190417
|
|
|
|
|
|
//后面修改的
|
|
|
+// public static String serverUrl = "ws://192.168.1.128:10011/ch/2/";
|
|
|
+
|
|
|
public static String serverUrl = "ws://39.108.248.223:10011/ch/2/";
|
|
|
|
|
|
public static int WEBSOCKET_SUCCESS = 0; //连接成功
|
|
@@ -41,7 +49,7 @@ public class WebsocketUtil {
|
|
|
public static int WEBSOCKET_ONFAILURE = 4; //连接失败
|
|
|
public static int WEBSOCKET_STATE = -1; //连接状态
|
|
|
|
|
|
- public OkHttpClient client;
|
|
|
+ public static OkHttpClient client;
|
|
|
public WebSocket webSocket;
|
|
|
public Gson gson;
|
|
|
public GsonBuilder builder;
|
|
@@ -71,11 +79,11 @@ public class WebsocketUtil {
|
|
|
return webSocket;
|
|
|
}
|
|
|
|
|
|
- public OkHttpClient getHttpClient() {
|
|
|
+ public static OkHttpClient getHttpClient() {
|
|
|
return client;
|
|
|
}
|
|
|
|
|
|
- public void connect(String serverID,String machineID, String nurseID) {
|
|
|
+ public void connect(String serverID, String machineID, String nurseID) {
|
|
|
LogUtil.d(TAG, "connect machineID==" + machineID + "==nurseID==" + nurseID);
|
|
|
if (!StringUtils.notEmpty(machineID)) return;
|
|
|
WebsocketEntity websocketEntity = new WebsocketEntity();
|
|
@@ -84,8 +92,22 @@ public class WebsocketUtil {
|
|
|
websocketEntity.setNurseID(nurseID);
|
|
|
websocketEntity.setUDPData("");
|
|
|
websocketEntity.setDeviceType("2");
|
|
|
- Log.e(TAG," 网址:"+serverUrl+websocketEntity.getServerID()+
|
|
|
- "_"+"1"+"_"+websocketEntity.getNurseID());
|
|
|
+ if (!TextUtils.isEmpty(Constants.DeviceMac)) {
|
|
|
+ websocketEntity.setDeviceMac(Constants.DeviceMac);
|
|
|
+ } else {
|
|
|
+ Constants.DeviceMac = PhoneUtils.getLocalMacAddress(MyApplication.getAppContext(), false);
|
|
|
+ websocketEntity.setDeviceMac(Constants.DeviceMac);
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.e(TAG,""+Constants.DeviceMac);
|
|
|
+
|
|
|
+ if (TextUtils.isEmpty(Constants.DeviceMac)) {
|
|
|
+ Toast.makeText(MyApplication.getAppContext(), "获取MAC地址失败 不能注册", Toast.LENGTH_LONG).show();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.e(TAG, " 网址:" + serverUrl + websocketEntity.getServerID() +
|
|
|
+ "_" + websocketEntity.getDepartmentsID() + "_" + websocketEntity.getNurseID() + "_" + websocketEntity.getDeviceMac());
|
|
|
start(websocketEntity);
|
|
|
}
|
|
|
|
|
@@ -95,8 +117,8 @@ public class WebsocketUtil {
|
|
|
|
|
|
this.websocketEntity = websocketEntity;
|
|
|
|
|
|
- Request request = new Request.Builder().url(serverUrl+websocketEntity.getServerID()+
|
|
|
- "_"+"1"+"_"+websocketEntity.getNurseID()).build();
|
|
|
+ Request request = new Request.Builder().url(serverUrl + websocketEntity.getServerID() +
|
|
|
+ "_" + websocketEntity.getDepartmentsID() + "_" + websocketEntity.getNurseID() + "_" + websocketEntity.getDeviceMac()).build();
|
|
|
|
|
|
EchoWebSocketListener listener = new EchoWebSocketListener(websocketEntity);
|
|
|
client = new OkHttpClient.Builder().readTimeout(3, TimeUnit.SECONDS).build();
|
|
@@ -136,17 +158,17 @@ public class WebsocketUtil {
|
|
|
|
|
|
@Override
|
|
|
public void onMessage(WebSocket webSocket, String text) {//收到文本信息
|
|
|
- LogUtil.d(TAG, "onMessage==1" + "text==" + text);
|
|
|
- WEBSOCKET_STATE = WEBSOCKET_SUCCESS;
|
|
|
+ Log.e(TAG, "onMessage..." + "收到的信息text " + text);
|
|
|
+// WEBSOCKET_STATE = WEBSOCKET_SUCCESS;
|
|
|
// 0:索引 1:主机id 2:门口机id 3:子机id 4:sip地址 5:房间号 6:床号 7:护理等级 8:消息类型 9:名称
|
|
|
if ("Hello!".equals(text)) {
|
|
|
EventBus.getDefault().post(new MessageEvent("success", Constants.EVENT_LOGIN_STATUS_MESSAGE));
|
|
|
} else if (text.equals("{\"type\":\"ping\"}")) {
|
|
|
LogUtil.d(TAG, "onMessage 服务端收到心跳信号,返回来了");
|
|
|
} else {
|
|
|
- Log.e(TAG,"onMessage..."+text);
|
|
|
+ Log.e(TAG, "IS_LOGOUT..." + Constants.IS_LOGOUT);
|
|
|
|
|
|
-// if (Constants.IS_LOGOUT) return;
|
|
|
+ if (Constants.IS_LOGOUT) return;
|
|
|
// WebsocketEntityReal websocketEntityReal = gson.fromJson(text, WebsocketEntityReal.class);
|
|
|
// text = websocketEntityReal.getUDPData();
|
|
|
// if (!text.equals("") && "$".equals(text.substring(0, 1))) { //判断是否为美元符号开头,如果是则是android 端的命令
|
|
@@ -156,6 +178,45 @@ public class WebsocketUtil {
|
|
|
// onMessageCall(text, data);
|
|
|
// }
|
|
|
|
|
|
+ if ((websocketEntity.getServerID() + "_" + websocketEntity.getDepartmentsID() +
|
|
|
+ "_" + websocketEntity.getNurseID() + "_" + websocketEntity.getDeviceMac()).equals(text)) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ SharedPreferencesUtil.putStringSp(SharedPreferencesUtil.LOGIN_TAG, "");
|
|
|
+ SharedPreferencesUtil.putStringSp(SharedPreferencesUtil.LOGIN_SERVERID, "");
|
|
|
+ SharedPreferencesUtil.putStringSp(SharedPreferencesUtil.LOGIN_DEPARTMENTSID, "");
|
|
|
+ SharedPreferencesUtil.putStringSp(SharedPreferencesUtil.LOGIN_NURSE_ID, "");
|
|
|
+ SharedPreferencesUtil.putStringSp(SharedPreferencesUtil.LOGIN_NURSE_NAME, "");
|
|
|
+
|
|
|
+ Log.e(TAG,"当前设备类型 "+PhoneUtils.isWatch(MyApplication.getAppContext()));
|
|
|
+ if(PhoneUtils.isWatch(MyApplication.getAppContext())){
|
|
|
+ Log.e(TAG,"当前设备类型 "+MyApplication.getAppContext());
|
|
|
+// Intent intent = new Intent(HomeActivity.homeActivity, HomeActivity.class);
|
|
|
+// HomeActivity.homeActivity.startActivity(intent);
|
|
|
+ Intent intent = new Intent(MyApplication.getAppContext(), HomeActivity.class);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ MyApplication.getAppContext().startActivity(intent);
|
|
|
+
|
|
|
+ }else {
|
|
|
+ Intent intent2 = new Intent(MyApplication.getAppContext(), LoginActivityPhone.class);
|
|
|
+ MyApplication.getAppContext().startActivity(intent2);
|
|
|
+ }
|
|
|
+
|
|
|
+ Constants.IS_LOGOUT = true;
|
|
|
+// WebsocketUtil.getInstance().getWebSocket().cancel();
|
|
|
+ if(webSocket != null){
|
|
|
+ webSocket.close(1000, "主动关闭");
|
|
|
+ }
|
|
|
+ client.dispatcher().executorService().shutdown();
|
|
|
+ PalyPhonetics.setPalyDismiss(true);
|
|
|
+ MyApplication.speakEntityList.clear();
|
|
|
+ SaveCallListUtil.deleteAllEntity();
|
|
|
+ SaveCallListUtil.deleteAllEntityNow();
|
|
|
+ ToastUtil.showToast(MyApplication.getAppContext(), "解绑成功,请重新绑定");
|
|
|
+ WebsocketUtil.WEBSOCKET_STATE = WebsocketUtil.WEBSOCKET_ONCLOSED;
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
if (!text.equals("") && "$".equals(text.substring(0, 1))) { //判断是否为美元符号开头,如果是则是android 端的命令
|
|
|
text = delHeadAndEnd(text, "$", "#");
|
|
|
String[] data = text.split(Character.toString((char) 3));
|
|
@@ -169,7 +230,7 @@ public class WebsocketUtil {
|
|
|
@Override
|
|
|
public void onMessage(WebSocket webSocket, ByteString bytes) {//收到二进制信息
|
|
|
LogUtil.d(TAG, "onMessage==2");
|
|
|
- WEBSOCKET_STATE = WEBSOCKET_SUCCESS;
|
|
|
+// WEBSOCKET_STATE = WEBSOCKET_SUCCESS;
|
|
|
if (Constants.IS_LOGOUT) return;
|
|
|
}
|
|
|
|
|
@@ -219,9 +280,13 @@ public class WebsocketUtil {
|
|
|
String serverId = SharedPreferencesUtil.getStringSp(SharedPreferencesUtil.LOGIN_SERVERID);
|
|
|
String loginMachineId = SharedPreferencesUtil.getStringSp(SharedPreferencesUtil.LOGIN_DEPARTMENTSID);
|
|
|
String nurseId = SharedPreferencesUtil.getStringSp(SharedPreferencesUtil.LOGIN_NURSE_ID);
|
|
|
-// if (StringUtils.notEmpty(loginMachineId) && StringUtils.notEmpty(nurseId) && !Constants.IS_LOGOUT && WEBSOCKET_STATE != WEBSOCKET_SUCCESS && WEBSOCKET_STATE != WEBSOCKET_ONOPEN) {
|
|
|
- WebsocketUtil.getInstance().connect(serverId,loginMachineId, nurseId);
|
|
|
-// }
|
|
|
+
|
|
|
+ Log.e("TAG","loginMachineId "+StringUtils.notEmpty(loginMachineId)+" nurseId "+StringUtils.notEmpty(nurseId)+
|
|
|
+ " IS_LOGOUT "+Constants.IS_LOGOUT+ " WEBSOCKET_STATE "+WEBSOCKET_STATE+" WEBSOCKET_STATE "+WEBSOCKET_STATE);
|
|
|
+
|
|
|
+ if (StringUtils.notEmpty(loginMachineId) && StringUtils.notEmpty(nurseId) && !Constants.IS_LOGOUT && WEBSOCKET_STATE != WEBSOCKET_ONOPEN) {
|
|
|
+ WebsocketUtil.getInstance().connect(serverId, loginMachineId, nurseId);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public synchronized void onMessageCall(String text, String[] data) {
|