⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cricketdaemon.java

📁 java编写的定位后台程序,可以学习使用.
💻 JAVA
字号:
/*           This software may be used and distributed according to the terms of       the GNU General Public License (GPL), incorporated herein by       reference.  Drivers based on this skeleton fall under the GPL and       must retain the authorship (implicit copyright) notice.        This program is distributed in the hope that it will be useful, but       WITHOUT ANY WARRANTY; without even the implied warranty of       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU       General Public License for more details. */package cricketdaemon;import gnu.getopt.*;import cricketdaemon.module.*;import cricketdaemon.beacontable.BeaconTableModule;import cricketdaemon.beaconstat.BeaconStatModule;import cricketdaemon.reader.*;import cricketdaemon.reader.sim.SimReader;import cricketdaemon.reader.sim.SimMapReader;import cricketdaemon.reader.serial.PortReaderCreator;import cricketdaemon.space.SpaceModule;import cricketdaemon.space2.Space2Module;import cricketdaemon.server.ServerModule;import cricketdaemon.logging.LoggingModule;import cricketdaemon.orient.OrientModule;import cricketdaemon.position.PositionModule;import java.util.*;/** * Main() that bootstraps the CricketDaemon. * * @author Allen Miu */public class CricketDaemon{    public static final String VERSION = "CricketDaemon V2.0";    public static final int GPSDPORT = 2947;    public static long   BREAKPOINT        = Long.MAX_VALUE;    public static String CONFIGFILE        = "cricket.conf";    public static String BEACONCONFIGFILE  = "beacons.conf";    public static String BEACONMAPPINGFILE = "beacons.conf";    public static String FLOORPLANFILE     = "ne43-5.flp";    public static String BEACONLOGFILE     = "playback.log";    public static String DEBUGLOGFILE      = "CricketDaemon.log";    public static String GPSDINETADDRESS   = "127.0.0.1";    //    public static String PATHCONFIGFILE    = "pathfinder.conf";    public static String LABELFILE         = "ne43-5.label";    public static String PLAYBACKFILE      = BEACONLOGFILE;        // Performance Default Params    public static Integer STATWINDOWSIZE   = new Integer(5000);    public static int STATSELECT           = 0;    public static double STDDEV_THRESH     = 15.0;    public static double K_THRESH          = 0.5;    public static int MAXDIST              = 1000; // 10meters    // ignore beacon if fewer than ENOUGHSAMPLEs collected over STATWINDOWSIZE    public static int    ENOUGHSAMPLE      = 3;    public static boolean beaconPrintOnly      = false;    public static boolean debugMode            = false;    public static boolean oldCricketListener   = false;    public static boolean positionModuleOn     = false;    public static boolean beaconStatModuleOn   = false;    public static boolean playbackRealTimeMode = true;    public static boolean rawGPSDPrint         = false;    // true if want to run CricketDaemon in single-threaded mode.    public static boolean singleThread = true;    public final static int CRICKETV1   = 1;    public final static int CRICKETV1_1 = 2; // including rev version 3 of old API    public final static int CRICKETV2   = 3; // new Cricket API    // TODO: should obtain version from beacon and/or gpsd interface    //       warning:  clientlib apps need to obtain this info at     //                 run time as well!    public static int listenerVersion = CRICKETV2;    public static void argError()     {	System.err.println("Usage: java cricketdaemon.CricketDaemon [-bbreakpoint] [-c[configFile]] [-d]\n"+			   "       [-D] [-f[playbackFile][-F]] [-g[address]] [-l[debugLogFile]]\n"+			   "       [-L[beaconLogFile]] [-m[beaconMappingFile]] [-o]\n"+			   "       [-p[pathConfigFile]] [-P] [-r] [-s[beaconConfigFile]]\n"+			   "       [-S[windowSize]] [-t[stddev]] [-T[maxdist]] -2\n\n"+			   "2 - Turn on Settings for Cricket Listener v2\n"+			   "b - breakpoint for log playback (specified in beacon id)\n"+			   "c - sets the serial port configuration file (default="+CONFIGFILE+")\n"+			   "d - turns on debug println (prints beacons only)\n"+			   "D - turns on debug println (all)\n"+			   "e - num samples required per beacon (default="+ENOUGHSAMPLE+")\n"+			   "f - plays back beacons from file (default="+PLAYBACKFILE+")\n"+			   "F - suppress real-time mode for option -f\n"+			   "g - gpsd mode (default="+GPSDINETADDRESS+")\n"+			   "j - label file (default="+LABELFILE+")\n"+			   "k - kthresh (default="+K_THRESH+")\n"+			   "l - turns on debug logging (default="+DEBUGLOGFILE+")\n"+			   "L - turns on beacon logging (default="+BEACONLOGFILE+")\n"+			   "m - turns on beacon translation (default="+BEACONMAPPINGFILE+")\n"+			   "M - Map simulation mode (default="+FLOORPLANFILE+")\n"+			   "o - old cricket listener mode\n"+			   //"p - pathfinder simluation (requires -s option)(default="+PATHCONFIGFILE+")\n"+			   "p - specifies server port number (default="+ServerModule.PORT+")\n"+			   "P - turns on PositionModule\n"+			   "r - turns on GPSD debug print\n"+			   "s - simulation mode (default="+BEACONCONFIGFILE+")\n"+			   "S - sets window size in BeaconStatModule (default="+STATWINDOWSIZE+")\n"+			   "T - max distance threshold for outlier rejection (default="+MAXDIST+")\n"+			   "z - select mode=0,mean=1,med=2,max=3,min=4 for distance estimate (default="+STATSELECT+")\n"+			   "Note: Avoid putting a space between the flag and option values\n");	System.exit(1);    }        public static String getOpt(Getopt g, String defaultValue)     {	String arg = g.getOptarg();	if(arg == null) {	    if(defaultValue == null)		argError();	    else		return defaultValue;	}	return arg;    }    public static void main(String args[])    {	long   breakPoint        = BREAKPOINT;	String configFile        = CONFIGFILE;	String beaconConfigFile  = null;	String beaconMappingFile = null;	String floorPlanFile     = null;	String beaconLogFile     = null;	String debugLogFile      = null;	String gpsdInetAddress   = null;	String labelFile         = LABELFILE;	String pathConfigFile    = null;	String playbackFile      = null;	int    portNum           = ServerModule.PORT;		// performace parameters	//boolean useMean          = false;	double  kthresh          = K_THRESH;	double  stddevThresh     = STDDEV_THRESH;	Integer statWindowSize   = STATWINDOWSIZE;	int     statSelect       = STATSELECT;	int     enoughSample     = ENOUGHSAMPLE;		// : means argument must be supplied with flag	// :: means argument after flag is optionsl	Getopt g = new Getopt("CricketDaemon", args, 			      "123b:c::dDe:f::Fg::j::k::l::L::m::M::op::Prs::S::t::T::z:");	int c;	while((c = g.getopt()) != -1) {	    switch(c) {	    case '1':		// FIXME: need redesign:		// The UnitConv.java reads static variable listenerVersion.		// So the apps will not see the runtime changes in CricketDaemon.		Util.assert_nonjava(false, "Cannot use this option.  Must recompile\n"+			    "CricketDaemon with default listenerVersion set to CRICKETV1 in\n"+			    "CricketDaemon.java\n");		listenerVersion = CRICKETV1;		break;	    case '2':		// It so happens that UnitConv behaves the same way for 		// CRICKETV1_1 and CRICKETV2.  So the apps won't be affected.		listenerVersion = CRICKETV1_1;		break;	    case '3':		listenerVersion = CRICKETV2;		break;	    case 'b':		breakPoint = Long.valueOf(getOpt(g, null)).longValue();		break;	    case 'c':		configFile = getOpt(g, null);		break;	    case 'd':		beaconPrintOnly = true;		break;	    case 'e':		enoughSample = Integer.parseInt(getOpt(g, String.valueOf(ENOUGHSAMPLE)));		break;	    case 'D':		debugMode = true;		break;	    case 'f':		playbackFile = getOpt(g, PLAYBACKFILE);		break;	    case 'F':		playbackRealTimeMode = false;		break;	    case 'g':		gpsdInetAddress = getOpt(g, GPSDINETADDRESS);		break;	    case 'j':		labelFile = getOpt(g, LABELFILE);		break;	    case 'k':		kthresh = Double.parseDouble(getOpt(g, String.valueOf(kthresh)));		break;	    case 'l':		debugLogFile = getOpt(g, DEBUGLOGFILE);		break;	    case 'L':		beaconLogFile = getOpt(g, BEACONLOGFILE);		break;	    case 'm':		beaconMappingFile = getOpt(g, BEACONMAPPINGFILE);		break;	    case 'M':		Util.assert_nonjava(listenerVersion >= CRICKETV1_1, 			    "Needs >= CricketV1_1 enabled in source code.");		floorPlanFile = getOpt(g, FLOORPLANFILE);		break;	    case 'o':		oldCricketListener = true;		break;	    case 'p':		//pathConfigFile = getOpt(g, PATHCONFIGFILE);		portNum = Integer.parseInt(getOpt(g, String.valueOf(ServerModule.PORT)));		break;	    case 'P':		positionModuleOn = true;		break;	    case 'r':		rawGPSDPrint = true;		break;	    case 's':		listenerVersion = CRICKETV1_1;		beaconConfigFile = getOpt(g, BEACONCONFIGFILE);		break;	    case 'S':		statWindowSize = Integer.valueOf(getOpt(g, String.valueOf(STATWINDOWSIZE)));		break;	    case 't':		stddevThresh = Double.parseDouble(getOpt(g, String.valueOf(stddevThresh)));		break;	    case 'T':		MAXDIST = Integer.parseInt(getOpt(g, String.valueOf(MAXDIST)));		break;	    case 'z':		statSelect = Integer.parseInt(getOpt(g, String.valueOf(STATSELECT)));		break;	    default:		argError();	    }	}	System.out.println("breakPoint        = "+breakPoint         );	System.out.println("configFile        = "+configFile         );	System.out.println("beaconConfigFile  = "+beaconConfigFile   );	System.out.println("beaconMappingFile = "+beaconMappingFile  );	System.out.println("beaconLogFile     = "+beaconLogFile      );	System.out.println("gpsdInetAddress   = "+gpsdInetAddress    );	System.out.println("labelFile         = "+labelFile          );	System.out.println("listenerVersion   = "+listenerVersion    );	System.out.println("debugLogFile      = "+debugLogFile       );	//	System.out.println("pathConfigFile    = "+pathConfigFile     );	System.out.println("portNum           = "+pathConfigFile     );	System.out.println("playbackFile      = "+playbackFile       );	System.out.println("beaconPrintOnly    = "+beaconPrintOnly   );	System.out.println("debugMode          = "+debugMode         );	System.out.println("oldCricketListener = "+oldCricketListener);	System.out.println("positionModuleOn   = "+positionModuleOn  );	System.out.println("rawGPSDPrint       = "+rawGPSDPrint      );	System.out.println("\n***perf params***\n");	System.out.println("kthresh            = "+kthresh           );	System.out.println("maxdist            = "+MAXDIST           );	System.out.println("statWindowSize     = "+statWindowSize    );	System.out.println("statSelect         = "+statSelect        );	System.out.println("stddevThresh       = "+stddevThresh      );	//System.out.println("useMean            = "+useMean            );	// initialize debug logging	if(debugLogFile != null)	    Util.init(debugLogFile);	ModuleThread reader = null;	ArrayList runningThreads = new ArrayList();	// initialize a reader	if(floorPlanFile != null) {	    if(beaconConfigFile == null)		beaconConfigFile = BEACONCONFIGFILE;	    reader = new SimMapReader(beaconConfigFile, floorPlanFile, labelFile);	}	else if(beaconConfigFile != null) {	    reader = new SimReader(beaconConfigFile, pathConfigFile);	}	else if(playbackFile != null)	    reader = new PlaybackReader(playbackFile, playbackRealTimeMode, breakPoint);	else if(gpsdInetAddress != null) {	    if(listenerVersion == CRICKETV2)		reader = new GPSDReaderV2(gpsdInetAddress, GPSDPORT);	    else		reader = new GPSDReader(gpsdInetAddress, GPSDPORT);	}	else {	    // dynamic class loading: prevents the loading of the	    // javax.comm libraries unless it is necessary.  this feature	    // allows one to run this code without installing the	    // javax.comm extension; gpsd can be used to handle the serial	    // port stuff instead.	    if(oldCricketListener)		reader = PortReaderCreator.		    createPortReader(PortReaderCreator.PATH+				     "OldSerialPortReader",				     configFile);	    else		reader = PortReaderCreator.		    createPortReader(PortReaderCreator.PATH+				     "SerialPortReader",				     configFile);	}	runningThreads.add(reader);		// initialize a reader mapper	ModuleThread rootReader = reader;	//if(beaconMappingFile != null) {	reader = new BeaconTableModule(reader, beaconMappingFile);    	runningThreads.add(reader);	    //}		// initialize LoggingModule	if(beaconLogFile != null || beaconPrintOnly)	    runningThreads.add(new LoggingModule(reader, beaconLogFile));		// initialize inference algorithm		//SpaceModule space = new SpaceModule(r);	//Space2Module space = new Space2Module(reader);	//runningThreads.add(space);	ServerModule server = new ServerModule(portNum, (BeaconTableModule) reader);	//server.registerModuleThread(space);	runningThreads.add(server);	BeaconStatModule bsm = 	    new BeaconStatModule(reader, statWindowSize.intValue(), enoughSample, statSelect);	runningThreads.add(bsm);	server.registerModuleThread(bsm);	PositionModule pos = null;	if(positionModuleOn) {	    pos = new PositionModule(bsm, kthresh, stddevThresh);	    //PositionModule pos = new PositionModule(space);	    runningThreads.add(pos);	    server.registerModuleThread(pos);	}	OrientModule om = new OrientModule(reader, pos);	runningThreads.add(om);	server.registerModuleThread(om);	// start all threads	DebugModule dm = null;	if(debugMode) {	    dm = new DebugModule();	    runningThreads.add(dm);	}	for(int i = 0; i < runningThreads.size(); i++) {	    ModuleThread mt = (ModuleThread) runningThreads.get(i);	    if(!singleThread || (mt instanceof PeriodicModuleThread))		mt.start();	    if(debugMode)		dm.register(mt);	}	if(singleThread)	    rootReader.run();	else {	    	    // todo: implement some gui to start/stop modules	    try { 		reader.join();	    } catch (InterruptedException e) {	    }	    	    // clean up	    for(int i = 0; i < runningThreads.size(); i++)		((Thread) runningThreads.get(i)).interrupt();	    	    Util.exit();	}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -