package com.basic.security.utils;
|
|
import android.os.Handler;
|
|
import com.basic.security.activity.MainActivity;
|
import com.basic.security.base.BaseApplication;
|
import com.basic.security.manager.ClearBeginManager;
|
import com.basic.security.manager.RealTimeMetricsManager;
|
import com.basic.security.manager.TodayBeginManager;
|
import com.basic.security.model.RealTimeMetrics;
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.select.Elements;
|
import java.io.BufferedInputStream;
|
import java.io.BufferedOutputStream;
|
import java.io.IOException;
|
import java.net.Socket;
|
import java.text.SimpleDateFormat;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
public class MetricsAcceptedClient extends Thread {
|
|
Socket mSocket;
|
private static final int MAX_BUFFER = 15;
|
public static final String CRLF = System.getProperty("line.separator");
|
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
public MetricsAcceptedClient(Socket client) {
|
mSocket = client;
|
}
|
|
public static String getAck() {
|
String ack = "";
|
String statusLine = "HTTP/1.0 200 OK"+CRLF;
|
String contentTypeLine = "Content-type: text/html"+CRLF ;
|
String connectionClose = "Connection: Keep-Alive"+CRLF ;
|
String contentLengthLine = "Content-Length: 0"+CRLF +CRLF;;
|
ack = statusLine + contentTypeLine+connectionClose+contentLengthLine;
|
return ack;
|
}
|
|
public static void parseXml(String xml) {
|
try {
|
Map<String, String> realTimeMetrics = new HashMap<>();
|
|
xml = xml.substring(xml.indexOf("<?xml version"));
|
Document doc = Jsoup.parse(xml);
|
Elements rTReportElements = doc.getElementsByTag("RTReport");
|
Elements rTCountElements = doc.getElementsByTag("RTCount");
|
Elements transmitTimeElements = doc.getElementsByTag("TransmitTime");
|
|
realTimeMetrics.put(RealTimeMetrics.TransmitTime, transmitTimeElements.first().text());
|
String Date = rTReportElements.first().attr("Date");
|
Date = Date.replace("T", " ");
|
realTimeMetrics.put(RealTimeMetrics.Date, sdf.parse(Date).getTime()+"");
|
realTimeMetrics.put(RealTimeMetrics.TotalEnters, rTCountElements.first().attr("TotalEnters"));
|
realTimeMetrics.put(RealTimeMetrics.TotalExits, rTCountElements.first().attr("TotalExits"));
|
|
RealTimeMetricsManager.saveRealTimeMetrics(realTimeMetrics);
|
ClearBeginManager.initClearBegin();
|
ClearBeginManager.reset();
|
TodayBeginManager.initTodayBegin();
|
|
((MainActivity)BaseApplication.getApplication().activity).fragment_home.refresh();
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public void run() {
|
BufferedInputStream inputStream = null;
|
BufferedOutputStream outputStream = null;
|
try {
|
byte[] byteBuff = new byte[4090];
|
inputStream = new BufferedInputStream(mSocket.getInputStream());
|
outputStream = new BufferedOutputStream(mSocket.getOutputStream());
|
String ack = getAck();
|
while (true) {
|
try {
|
int len = inputStream.read(byteBuff);
|
String xml = new String(byteBuff, 0, len);
|
outputStream.write(ack.getBytes());
|
outputStream.flush();
|
parseXml(xml);
|
if (Thread.currentThread().isInterrupted())
|
break;
|
} catch (Exception e) {
|
e.printStackTrace();
|
break;
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (outputStream != null) {
|
try {
|
outputStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (inputStream != null) {
|
try {
|
inputStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (mSocket != null) {
|
try {
|
mSocket.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
|
}
|