📄 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.LineNumberReader;import java.io.PrintWriter;import java.util.Calendar;import java.util.Date;import java.util.Enumeration;import java.util.Hashtable;import java.util.SimpleTimeZone;import java.util.Vector;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 LineNumberReader reader = null; private GPSMeasurement prevGPSMeas, nextGPSMeas; private boolean beaconsAvailable; private Hashtable savedLine; private Vector 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 int getCurrentLineNumber() { return reader == null ? -1 : reader.getLineNumber(); } public void open() throws SpotterException { try { if (filename != null) { reader = new LineNumberReader(new FileReader(filename)); } else { reader = new LineNumberReader(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 Vector(); /* 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, Vector list) { BeaconReading wrs[] = new BeaconReading[list.size()]; int i=0; for (Enumeration it=list.elements(); it.hasMoreElements(); ) { wrs[i++] =(BeaconReading) it.nextElement(); } return new BeaconMeasurement(timestamp, wrs); } public Measurement getMeasurementFromLog() throws SpotterException { if (reader == null && cache.isEmpty()) { return null; } Measurement m = null; if (outputStumblerMeasurements) { if (gpsInterpolationMode == GPS_INTERPOLATE) { m = getInterpolatedMeasurement(); } else if (gpsInterpolationMode == GPS_THRESHOLD) { m = getThresholdedMeasurement(true); } else { m = getThresholdedMeasurement(false); } } else { m = getNextMeasurement(); } if (m != null && m instanceof StumblerMeasurement) { TwoDCoordinate c = (TwoDCoordinate) ((StumblerMeasurement)m).getPosition(); double lat = c.getLatitude(); double lon = c.getLongitude(); if (lat < -90.0 || lat > 90.0 || lon < -180.0 || lon > 180.0 || (lat == 0.0 && lon == 0.0) || Double.isNaN(lat) || Double.isNaN(lon)) {// System.err.println("PlacelabStumblerLogSpotter: Measurement has invalid position: " + m.toString()); //return null; } } return m; } /* this returns either a GPSMeasurement or a BeaconMeasurement object */ private Measurement getNextMeasurement() throws SpotterException { if (reader==null && savedLine==null) return null; Vector list=new Vector(); long currentTimestamp = 0; String currentType = null; String line = null; while (true) { Hashtable 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.EMPTY_WIFI)) { return new BeaconMeasurement(currentTimestamp,BeaconMeasurement.noWifiReadings); } if (type.equals(Types.EMPTY_GSM)) { return new BeaconMeasurement(currentTimestamp,BeaconMeasurement.noGSMReadings); } if (type.equals(Types.EMPTY_BT)) { return new BeaconMeasurement(currentTimestamp,BeaconMeasurement.noBtReadings); } 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); } } else { Measurement mm = getOtherMeasurementType(time, type, allFields); if (mm != null) { return mm; } } 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.addElement(reading); } } // override me protected Measurement getOtherMeasurementType(long time, String type, Hashtable allFields) { return null; } private StumblerMeasurement getThresholdedMeasurement(boolean useThresholding) throws SpotterException { Measurement m = getNextMeasurement();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -