📄 placelabstumblerlogspotter.java
字号:
/* * Created on Jun 1, 2004 */package org.placelab.spotter;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.Calendar;import java.util.Date;import java.util.SimpleTimeZone;import org.placelab.collections.HashMap;import org.placelab.collections.Iterator;import org.placelab.collections.LinkedList;import org.placelab.core.BeaconMeasurement;import org.placelab.core.BeaconReading;import org.placelab.core.Coordinate;import org.placelab.core.GPSMeasurement;import org.placelab.core.Measurement;import org.placelab.core.StumblerMeasurement;import org.placelab.core.TwoDCoordinate;import org.placelab.core.Types;import org.placelab.core.WiFiReading;import org.placelab.stumbler.LogWriter;import org.placelab.util.Cmdline;import org.placelab.util.Logger;import org.placelab.util.NumUtil;import org.placelab.util.StringUtil;public class PlacelabStumblerLogSpotter extends LogSpotter { private String filename=null; private InputStream is=null; private BufferedReader reader = null; private GPSMeasurement prevGPSMeas, nextGPSMeas; private boolean beaconsAvailable; private HashMap savedLine; private LinkedList cache; private boolean outputStumblerMeasurements = true; // output StumblerMeasurements or raw GPS and BeaconMeasurements private int gpsThreshold = GPS_DEFAULT_THRESHOLD; private int gpsInterpolationMode=GPS_THRESHOLD; public static int GPS_INTERPOLATE=1; public static int GPS_THRESHOLD=2; public static int GPS_NONE=3; public static int GPS_DEFAULT_THRESHOLD = 5000; public PlacelabStumblerLogSpotter(String file) { filename = file; } public PlacelabStumblerLogSpotter(InputStream in) { is = in; } public void open() throws SpotterException { try { if (filename != null) { reader = new BufferedReader(new FileReader(filename)); } else { reader = new BufferedReader(new InputStreamReader(is)); } } catch (FileNotFoundException e) { throw new SpotterException(e); } if (reader == null) { throw new SpotterException("No reader available"); } prevGPSMeas = nextGPSMeas = null; savedLine = null; cache = new LinkedList(); /* start with beaconsAvailable = true to avoid generating a single empty reading for the first GPS line */ beaconsAvailable = true; } public void close() throws SpotterException { if (reader != null) { try { reader.close(); } catch (IOException e) { throw new SpotterException(e); } reader = null; } } public void setGPSMethod(int method, int thresholdMillis) { gpsInterpolationMode = method; gpsThreshold = thresholdMillis; } public int getGPSMethod() { return gpsInterpolationMode; } public int getGPSThreshold() { return gpsThreshold; } /** * @return Returns true if we are outputting StumblerMeasurements instead of raw * GPS and Beacon Measurements. */ public boolean isOutputStumblerMeasurements() { return outputStumblerMeasurements; } /** * @param outputStumblerMeasurements Set whether to output StumblerMeasurements * instead of raw GPS and Beacon Measurements. */ public void setOutputStumblerMeasurements(boolean outputStumblerMeasurements) { this.outputStumblerMeasurements = outputStumblerMeasurements; } private BeaconMeasurement newBeaconMeasurement(long timestamp, LinkedList list) { BeaconReading wrs[] = new BeaconReading[list.size()]; int i=0; for (Iterator it=list.iterator(); it.hasNext(); ) { wrs[i++] =(BeaconReading) it.next(); } return new BeaconMeasurement(timestamp, wrs); } public Measurement getMeasurementFromLog() throws SpotterException { if (reader == null && cache.isEmpty()) { return null; } if (outputStumblerMeasurements) { if (gpsInterpolationMode == GPS_INTERPOLATE) { return getInterpolatedMeasurement(); } else if (gpsInterpolationMode == GPS_THRESHOLD) { return getThresholdedMeasurement(true); } else { return getThresholdedMeasurement(false); } } else { return getNextMeasurement(); } } /* this returns either a GPSMeasurement or a BeaconMeasurement object */ private Measurement getNextMeasurement() throws SpotterException { if (reader==null && savedLine==null) return null; LinkedList list=new LinkedList(); long currentTimestamp = 0; String currentType = null; String line = null; while (true) { HashMap allFields; if (savedLine == null) { try { line = reader.readLine(); } catch (Throwable ex) { throw new SpotterException(ex); } if (line == null) { try { reader.close(); } catch (IOException ex) { } reader = null; if (list.isEmpty()) return null; else return newBeaconMeasurement(currentTimestamp, list); } allFields = StringUtil.storageStringToHashMap(line); if (allFields==null) continue; } else { allFields = savedLine; savedLine = null; } String type = (String)allFields.get(Types.TYPE); String timeStr = (String)allFields.get(Types.TIME); if (type==null || timeStr==null) continue; long time=0; try { time = Long.parseLong(timeStr); } catch (NumberFormatException e) { continue; } if ((currentType != null) && (/*!type.equals(currentType) ||*/ (time != currentTimestamp))) { /* we are done with this group of readings */ savedLine = allFields;// System.out.println("Doing " + type + " vs " + currentType); return newBeaconMeasurement(currentTimestamp, list); } if (type.equals(Types.GPS)) { String status = (String)allFields.get(Types.STATUS); if (status != null && status.equalsIgnoreCase("a")) { // in v2 logs lat and lon are stored as regular signed // double values // not NMEA lat and lons. String lat = (String) allFields.get(Types.LATITUDE); String lon = (String) allFields.get(Types.LONGITUDE);// System.out.println("GPS triggered "); return new GPSMeasurement(time, Types.newCoordinate(lat, lon), allFields); } else { return new GPSMeasurement(time, Types.newCoordinate(), allFields); } } BeaconReading reading = null; try { reading = Types.newReading(allFields); } catch (IllegalArgumentException ex) { // ignore bad line continue; } if (reading == null) continue; if (currentType==null) { currentType = type; currentTimestamp = time; } list.add(reading); } } private StumblerMeasurement getThresholdedMeasurement(boolean useThresholding) throws SpotterException { Measurement m = getNextMeasurement(); if (m==null) { if (!beaconsAvailable && prevGPSMeas != null) { return new StumblerMeasurement(prevGPSMeas.getTimestamp(), prevGPSMeas.getPosition()); } else { return null; } } /* the measurement is either a GPSMeasurement or a BeaconMeasurement */ if (m instanceof BeaconMeasurement) { BeaconMeasurement meas = (BeaconMeasurement) m; Coordinate pos; if (prevGPSMeas != null) { if (useThresholding) { pos = (meas.getTimestamp() - prevGPSMeas.getTimestamp() > gpsThreshold ? Types.newCoordinate() : prevGPSMeas.getPosition()); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -