Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. private static void searchDevice(String arg) throws IOException{
  2.         String[] bases = arg.split(";");
  3.         String[] realCoords=bases[bases.length-1].split(",");
  4.         List<Base_Station> stations = new ArrayList<Base_Station>();
  5.         for(String base:bases){
  6.             if(bases[bases.length-1]==base)
  7.                 break;
  8.             String[] base_data=base.split(",");
  9.             base_data[6]=(base_data[6].equals("-1")?String.valueOf(-1):base_data[6]);
  10.             Base_Station station=new Base_Station(base_data);
  11.             station.recovering();
  12.             stations.add(station);
  13.         }
  14.        
  15.         List<Base_Station> stationsOK = new ArrayList<Base_Station>();
  16.         ArrayList<String> CIDs = new ArrayList<String>(stations.size());
  17.         for(Base_Station station:stations) {
  18.             String bs_number = String.valueOf(Integer.parseInt(station.CID, 16)/10);
  19.             if(!CIDs.contains(bs_number)) {
  20.                 CIDs.add(bs_number);
  21.                 stationsOK.add(station);
  22.             }
  23.             else {
  24.                 if(station.Rxl>stationsOK.get(CIDs.indexOf(bs_number)).Rxl) {
  25.                     stationsOK.set(CIDs.indexOf(bs_number),station);
  26.                 }
  27.             }
  28.         }
  29.         CIDs.clear();
  30.         stations.clear();
  31.         Collections.sort(stationsOK, new Comparator<Base_Station>(){
  32.             @Override
  33.             public int compare(Base_Station o2, Base_Station o1) {
  34.                 return (o1.Arfc>o2.Arfc?1:(o1.Arfc<o2.Arfc?-1:0));
  35.             }
  36.         });
  37.         stationsOK=stationsOK.subList(0, (stationsOK.size()>=6?6:stationsOK.size()));//оставляе� 6
  38.         new FileWriter(file,false);
  39.         for(Base_Station station:stationsOK) {
  40.             Base_Station bs = new Base_Station(station.data);
  41.             bs.recovering();
  42.             stations.add(bs);
  43.         }
  44.         result(true, stationsOK);
  45.         stationsOK=stations;
  46.         result(false, stationsOK);
  47.         FileWriter fw = new FileWriter(file,true);
  48.         fw.write(realCoords[0]+" "+realCoords[1]);
  49.         fw.close();
  50.     }
  51.    
  52.     public static void result(boolean choice, List<Base_Station> stationsOK) throws IOException {
  53.         List<Base_Station> stations = new ArrayList<Base_Station>();
  54.         long a = 6378137;
  55.         long b = 6356752;
  56.         boolean def=true;
  57.         double cosPhi=0;
  58.         for(Base_Station station:stationsOK) {
  59.             String coord=(choice?getYandexCoord(station.MCC,station.MNC,station.LAC,station.CID)+" Y":getGoogleCoord(station.MCC,station.MNC,station.LAC,station.CID)+" G");
  60.             //System.out.println("Base Station "+station.CID+" coords: " +coord);
  61.             def=(coord.equals(" Y")?false:def);
  62.             def=(coord.equals(" G")?false:def);
  63.             try {
  64.                 FileWriter fw = new FileWriter(file,true);
  65.                 fw.write(station.CID+" " +new DecimalFormat("#0.0000000").format(Double.valueOf(coord.split(" ")[0])).replace(",", ".")+" "+new DecimalFormat("#0.0000000").format(Double.valueOf(coord.split(" ")[1])).replace(",", ".") +"\r\n");
  66.                 fw.close();
  67.             } catch (IOException ignored) {}
  68.             double phi = Math.toRadians(Double.valueOf(coord.split(" ")[0]));
  69.             double psi = Math.toRadians(Double.valueOf(coord.split(" ")[1]));
  70.             //if(cosPhi==0) {
  71.             r=a*b/Math.sqrt(b*b*Math.cos(phi)*Math.cos(phi)+a*a*Math.sin(phi)*Math.sin(phi));
  72.                 cosPhi = Math.cos(phi);
  73.             //}
  74.             station.coord0=new Point(phi*r,cosPhi*psi*r);
  75.            
  76.             //System.out.println("Base Station "+station.CID+" coords: " +station.coord0.x + ' ' + station.coord0.y+' '+cosPhi+' '+0.5022231252570837*psi*r);
  77.            
  78.             if(station.R>=0) {
  79.                 stations.add(station);
  80.             }
  81.         }
  82.         Solve.r=r;
  83.         String result = "Point 0 0 0";
  84.         if(def) {
  85.             if(stationsOK.size()==stations.size()) {
  86.                 result = Solve.solve(stationsOK, false);
  87.                 sp="RADIUS";
  88.             }
  89.             if(Double.valueOf(result.split(" ")[1])==0&&Double.valueOf(result.split(" ")[2])==0&&!stations.isEmpty()) {        
  90.                 result = Solve.searchRxl(stationsOK, stations);
  91.                 sp="Rxl & RADIUS";
  92.             }
  93.             if(Double.valueOf(result.split(" ")[1])==0&&Double.valueOf(result.split(" ")[2])==0) {         
  94.                 result = Solve.fullSearchRxl(stationsOK);
  95.                 sp="Rxl";
  96.             }
  97.             System.out.println("Successful");
  98.         }
  99.         FileWriter fw = new FileWriter(file,true);
  100.         for(Base_Station station:stationsOK) {
  101.             try {
  102.                 fw.write(station.CID+" " + station.R +" "+ station.deltaR +"\r\n");
  103.             } catch (IOException ignored) {}
  104.         }
  105.         String[] answerStr = result.split(" ");
  106.         double x = Double.valueOf(answerStr[1])/r;
  107.         double y = Double.valueOf(answerStr[2])/r/Math.cos(x);
  108.         fw.write(new DecimalFormat("#0.0000000").format(Math.toDegrees(x)).replace(",", ".")+" "+new DecimalFormat("#0.0000000").format(Math.toDegrees(y)).replace(",", ".")+" "+ Double.parseDouble(answerStr[3])+"\r\n");
  109.         fw.close();
  110.         answerStr[1]=new DecimalFormat("#0.0000000").format(Math.toDegrees(x)).replace(",", ".");
  111.         answerStr[2]=new DecimalFormat("#0.0000000").format(Math.toDegrees(y)).replace(",", ".");
  112.     }
  113.     public static String getYandexCoord(String MCC, String MNC, String LAC, String CID) {
  114.         String res="";
  115.         try {
  116.             URLConnection connection = new URL("http://mobile.maps.yandex.net/cellid_location/?&cellid="+Integer.parseInt(CID,16)
  117.                     +"&operatorid="+Integer.parseInt(MNC)+"&countrycode="+Integer.parseInt(MCC)+"&lac="+Integer.parseInt(LAC,16)).openConnection();
  118.             BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  119.             String line;
  120.             StringBuilder sb = new StringBuilder();
  121.             while((line = br.readLine())!=null) {
  122.                 sb.append(line);
  123.             }
  124.             int begin = sb.toString().indexOf("latitude")+10;
  125.             int end = sb.toString().indexOf(" ", begin)-1;
  126.             res=sb.toString().substring(begin,end)+" ";
  127.             begin = sb.toString().indexOf("longitude")+11;
  128.             end = sb.toString().indexOf(" ", begin)-1;
  129.             res+=sb.toString().substring(begin,end);
  130.         } catch (IOException e) {
  131.             System.out.println(e);
  132.         }
  133.         return res;
  134.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement