package com.basic.security.utils;
|
import android.os.SystemClock;
|
import android.text.TextUtils;
|
|
import com.basic.security.activity.MainActivity;
|
import com.basic.security.base.BaseApplication;
|
import com.basic.security.manager.AlarmManager;
|
import com.basic.security.utils.Constants;
|
import com.google.gson.JsonArray;
|
import com.google.gson.JsonElement;
|
import com.google.gson.JsonObject;
|
import com.google.gson.JsonParseException;
|
import com.google.gson.JsonParser;
|
|
import java.io.BufferedInputStream;
|
import java.io.BufferedOutputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.net.Socket;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.Comparator;
|
import java.util.HashMap;
|
import java.util.LinkedList;
|
import java.util.List;
|
import java.util.Map;
|
|
public class SocketAcceptedClient extends Thread {
|
|
Socket mSocket;
|
byte[] buff = new byte[1*1024*1024];
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
public static boolean needRestart = false;
|
List<Map<String, String>> newRemoteAlarmList = null;
|
|
private static final int MAX_BUFFER = 15;
|
|
private static LinkedList<byte[]> mRgbCameraQueue = new LinkedList<byte[]>();
|
|
public SocketAcceptedClient(Socket client) {
|
mSocket = client;
|
}
|
|
public Comparator<Map<String, String>> mapComparator = new Comparator<Map<String, String>>() {
|
public int compare(Map<String, String> m1, Map<String, String> m2) {
|
try {
|
double score1 = Double.parseDouble(m1.get("likePer"));
|
double score2 = Double.parseDouble(m2.get("likePer"));
|
return (int)(score1 - score2);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return 0;
|
}
|
};
|
|
public void parseJson(String msg) {
|
|
JsonParser parser = new JsonParser();
|
boolean isJSON = true;
|
JsonElement element = null;
|
try {
|
element = parser.parse(msg);
|
} catch (JsonParseException e) {
|
System.out.println("exception: "+ msg + " " + e);
|
isJSON = false;
|
}
|
newRemoteAlarmList = new ArrayList<>();
|
if (isJSON && element != null) {
|
Object obj = element.getAsJsonObject();
|
JsonArray dataList = (JsonArray) ((JsonObject) obj).get("datalist");
|
for (int i = 0; i < dataList.size(); i++) {
|
JsonObject je = (JsonObject)dataList.get(i);
|
Map<String, String> newRemoteAlarm = new HashMap<>();
|
for(Map.Entry<String, JsonElement> entry : je.entrySet()) {
|
Object value = entry.getValue();
|
String key = entry.getKey();
|
if (value != null) {
|
if ("baseInfo".equals(key)) {
|
List<Map<String, String>> matchPersonMapList = new ArrayList<>();
|
List<String> matchPersonStrList = new ArrayList<>();
|
String matchPersonListStr = "";
|
if (value instanceof JsonArray) {
|
JsonArray matchPersonList = (JsonArray)value;
|
for (int ii = 0; ii < matchPersonList.size(); ii++ ) {
|
JsonObject matchPerson = (JsonObject)matchPersonList.get(ii);
|
Map<String, String> matchPersonMap = new HashMap<>();
|
|
for(Map.Entry<String, JsonElement> matchPersonEntry : matchPerson.entrySet()) {
|
String matchPersonKey = matchPersonEntry.getKey();
|
Object matchPersonValue = matchPersonEntry.getValue();
|
if (matchPersonValue != null) {
|
String matchPersonValueStr = matchPersonValue.toString();
|
if (matchPersonValueStr.startsWith("\"")) {
|
matchPersonValueStr = matchPersonValueStr.substring(1);
|
}
|
if (matchPersonValueStr.endsWith("\"")) {
|
matchPersonValueStr = matchPersonValueStr.substring(0, matchPersonValueStr.length() - 1);
|
}
|
matchPersonMap.put(matchPersonKey, matchPersonValueStr);
|
} else {
|
matchPersonMap.put(matchPersonKey, "");
|
}
|
}
|
matchPersonMap.put("mathPersonStr", matchPersonMap.get("tableName")+ " " + matchPersonMap.get("personName"));
|
matchPersonMapList.add(matchPersonMap);
|
}
|
Collections.sort(matchPersonMapList, mapComparator);
|
for (Map<String, String> matchPersonMap : matchPersonMapList) {
|
matchPersonStrList.add(matchPersonMap.get("mathPersonStr"));
|
}
|
matchPersonListStr = TextUtils.join("`", matchPersonStrList);
|
}
|
newRemoteAlarm.put("matchPersonListStr", matchPersonListStr);
|
} else {
|
newRemoteAlarm.put(entry.getKey(), value.toString());
|
}
|
}
|
}
|
newRemoteAlarmList.add(newRemoteAlarm);
|
}
|
//
|
if (newRemoteAlarmList != null && newRemoteAlarmList.size() > 0) {
|
// System.out.println("receive");
|
// if (1==1) {
|
// return;
|
// }
|
AlarmManager.renameKeys(newRemoteAlarmList);
|
if (AlarmManager.saveRemoteAlarmListToSqlite(newRemoteAlarmList) > 0) {
|
((MainActivity) BaseApplication.getApplication().activity).fragment_home.refreshGridView();
|
}
|
} else {
|
if (((MainActivity)BaseApplication.getApplication().activity).currentFragment ==
|
((MainActivity)BaseApplication.getApplication().activity).fragment_home) {
|
ToastUtil.show("获取报警数据失败");
|
}
|
}
|
}
|
}
|
|
@Override
|
public void run() {
|
try {
|
BufferedOutputStream outputStream = new BufferedOutputStream(mSocket.getOutputStream());
|
BufferedInputStream inputStream = new BufferedInputStream(mSocket.getInputStream());
|
String msg = "";
|
int len = 0;
|
while ((len = inputStream.read(buff)) != -1) {
|
for (int i1 = 0; i1 <len; i1++) {
|
if (buff[i1] == '\0') {
|
try {
|
byte[] msgBuf = byteArrayOutputStream.toByteArray();
|
byteArrayOutputStream.reset();
|
msg = new String(msgBuf, 0, msgBuf.length);
|
msg = msg.substring(0, msg.lastIndexOf("}") + 1);
|
System.out.println("msg="+msg);
|
if (msg.length() > 3) {
|
parseJson(msg);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
} else {
|
byteArrayOutputStream.write(buff[i1]);
|
}
|
}
|
if (needRestart) {
|
needRestart = false;
|
break;
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
}
|