display/app/src/main/java/com/xixun/communicate/Report.java
2024-09-02 18:54:34 +08:00

226 lines
8.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xixun.communicate;
import android.location.Criteria;
import android.location.Location;
import com.xixun.display.MainActivity;
import com.xixun.display.MyLog;
import com.xixun.display.MyService;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
/**
* Created by Administrator on 2018/08/09.
* 163.53.193.72
*/
public class Report
{
LedCardCommunication ledCardCommunication=null;
MyService ms=null;
String strCardID=null;
String strDoMain=null;
int iPort=0;
String strIpAddress=null;
DatagramSocket udpSendSocket =null;
public Report(LedCardCommunication ledCardCommunication,String strDoMain,int iPort)
{ this.ledCardCommunication=ledCardCommunication;
this.strDoMain=strDoMain;
this.iPort=iPort;
if (ledCardCommunication==null) return;
if (strDoMain==null) return;
if (strDoMain.length()<"w.c".length()) return;
ms=ledCardCommunication.clientsManage.ms;
this.strCardID=ledCardCommunication.Get_DisplayID();
new Thread() {
public void run() {
Process();
}
}.start();
}
private void Process()
{ Criteria c = new Criteria();//Criteria类是设置定位的标准信息系统会根据你的要求匹配最适合你的定位供应商一个定位的辅助信息的类
{
c.setPowerRequirement(Criteria.POWER_LOW);//设置低耗电
c.setAltitudeRequired(true);//设置需要海拔
c.setBearingAccuracy(Criteria.ACCURACY_COARSE);//设置COARSE精度标准
c.setAccuracy(Criteria.ACCURACY_LOW);//设置低精度
//... Criteria 还有其他属性,就不一一介绍了
Location best = LocationUtils.getBestLocation(ms, c);
if (best == null) {
MyLog.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Best location is null");
} else {
MyLog.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!best location: lat==" + best.getLatitude() + " lng==" + best.getLongitude());
}
Location net = LocationUtils.getNetWorkLocation(ms);
if (net == null) {
MyLog.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!net location is null");
} else {
MyLog.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!net location: lat==" + net.getLatitude() + " lng==" + net.getLongitude());
}
}
while(NeedExit()==false) {
try {
Thread.sleep(5*1000); // sleep(1000); // 第一次时,先等待其他服务端口套接字线程准备好。
try{
InetAddress x = java.net.InetAddress.getByName(strDoMain);
strIpAddress = x.getHostAddress();//得到字符串形式的ip地址.
if (strIpAddress==null) continue;
if (strIpAddress.length()<"1.1.1.1".length()) continue;
MyLog.i(strDoMain+" IP"+strIpAddress);
//strIpAddress="192.168.1.255";//112";
}
catch (UnknownHostException e)
{ //e.printStackTrace();
//MyLog.i("域名解析出错");
continue;
}
udpSendSocket = new DatagramSocket(null);
udpSendSocket.setReuseAddress(true);
udpSendSocket.bind(new InetSocketAddress(iPort));
String staAlias=null;
int iCounter=0;
while (NeedExit()==false)
{
Location best = LocationUtils.getBestLocation(ms, c);
iCounter++;
if (iCounter>10)
{
iCounter=0;
staAlias=null;
}
if (staAlias==null)
{
staAlias=ledCardCommunication.GetAlias();
}
//if (best != null)
{
String str =
"{"
+ "\"CardID\":\"" + strCardID
+ "\",\"Alias\":\"" + staAlias
+ "\",\"Display\":\"" + (ledCardCommunication.GetSecreenOn()?"ON":"OFF")
+ "\",\"PlayProgram\":\"" + ledCardCommunication.GetPlayProgram()
+ "\",\"Latitude\":\"" + ((best != null) ? best.getLatitude() :null)
+ "\",\"Longitude\":\"" + ((best != null) ? best.getLongitude() :null)
+ "\",\"Speed\":\"" + ((best != null) ? best.getSpeed() :null)
+"\"}";
byte sendDataBuffer[] = str.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendDataBuffer, sendDataBuffer.length, InetAddress.getByName(strIpAddress), iPort);
udpSendSocket.send(sendPacket);
MyLog.i( str );
}
Thread.sleep(15*1000);
}
}catch(Exception e){
MyLog.i(Thread.currentThread().getStackTrace()[2].getFileName() + "-->"
+ "Function: " +Thread.currentThread().getStackTrace()[2].getMethodName() + "-->"
+ "Line" +Thread.currentThread().getStackTrace()[2].getLineNumber() + ":\r\n"
+ " Error:" + e.getMessage() + "\r\n\r\n"
);
}finally {
if (udpSendSocket!=null) {
udpSendSocket.close();
udpSendSocket=null;
}
}
}
Exit();
}
/**
* 通过GPS获取定位信息
public void getGPSLocation() {
Location gps = LocationUtils.getGPSLocation(this);
if (gps == null) {
//设置定位监听因为GPS定位第一次进来可能获取不到通过设置监听可以在有效的时间范围内获取定位信息
LocationUtils.addLocationListener(context, LocationManager.GPS_PROVIDER, new LocationUtils.ILocationListener() {
@Override
public void onSuccessLocation(Location location) {
if (location != null) {
Toast.makeText(MainActivity.this, "gps onSuccessLocation location: lat==" + location.getLatitude() + " lng==" + location.getLongitude(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "gps location is null", Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(this, "gps location: lat==" + gps.getLatitude() + " lng==" + gps.getLongitude(), Toast.LENGTH_SHORT).show();
}
}*/
/**
* 通过网络等获取定位信息
private void getNetworkLocation() {
Location net = LocationUtils.getNetWorkLocation(Activity);
if (net == null) {
Toast.makeText(this, "net location is null", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "network location: lat==" + net.getLatitude() + " lng==" + net.getLongitude(), Toast.LENGTH_SHORT).show();
}
}*/
/**
* 采用最好的方式获取定位信息
private void getBestLocation() {
Criteria c = new Criteria();//Criteria类是设置定位的标准信息系统会根据你的要求匹配最适合你的定位供应商一个定位的辅助信息的类
c.setPowerRequirement(Criteria.POWER_LOW);//设置低耗电
c.setAltitudeRequired(true);//设置需要海拔
c.setBearingAccuracy(Criteria.ACCURACY_COARSE);//设置COARSE精度标准
c.setAccuracy(Criteria.ACCURACY_LOW);//设置低精度
//... Criteria 还有其他属性,就不一一介绍了
Location best = LocationUtils.getBestLocation(Activity, c);
if (best == null) {
Toast.makeText(this, " best location is null", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "best location: lat==" + best.getLatitude() + " lng==" + best.getLongitude(), Toast.LENGTH_SHORT).show();
}
}
*/
boolean bNeedExit=false;
public boolean NeedExit() { return bNeedExit; }
public void Exit()
{ if (NeedExit()) return;
bNeedExit=true;
}
}