00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package gov.lanl.isr.sensix;
00036
00037
00038 public class SenseUtil
00039 {
00040 public static String capabilityToString(byte capability) {
00041 switch (capability) {
00042 case SenseCorba.LIGHT:
00043 return "Light";
00044 case SenseCorba.TEMP:
00045 return "Temperature";
00046 case SenseCorba.ACCEL:
00047 return "Accelerometer";
00048 case SenseCorba.MAG:
00049 return "Magnetometer";
00050 case SenseCorba.MIC:
00051 return "Microphone";
00052 case SenseCorba.HUMID:
00053 return "Humidity";
00054 case SenseCorba.PRESS:
00055 return "Pressure";
00056 case SenseCorba.GPS:
00057 return "GPS";
00058 case SenseCorba.BATTERY:
00059 return "Battery";
00060 }
00061 return "Unknown";
00062 }
00063
00064 public static byte[] marshall(String cmd) {
00065 byte[] bytes, tmp = new byte[cmd.length()];
00066 int bidx = 0;
00067 String cur = cmd;
00068
00069 while (true) {
00070 try {
00071 int sidx = 0;
00072
00073 if (cur.substring(0, 1).equals(" ") ||
00074 cur.substring(0, 1).equals("\t") ||
00075 cur.substring(0, 1).equals("\n") ||
00076 cur.substring(0, 1).equals("\r")) {
00077 sidx = 1;
00078 }
00079 else if (cur.substring(0, 1).equals(")")) {
00080 tmp[bidx] = (byte)0x29;
00081 sidx = 1;
00082 }
00083 else if (cur.substring(0, 1).equals("(")) {
00084 tmp[bidx] = (byte)0x28;
00085 sidx = 1;
00086 }
00087 else if (cur.substring(0, 1).equals(",")) {
00088 tmp[bidx] = (byte)0x2c;
00089 sidx = 1;
00090 }
00091 else if (cur.substring(0, 1).equals("=")) {
00092 tmp[bidx] = (byte)0x3d;
00093 sidx = 1;
00094 }
00095 else if (cur.substring(0, 2).equals("&&")) {
00096 tmp[bidx] = (byte)0x26;
00097 sidx = 2;
00098 }
00099
00100 else if (cur.substring(0, 3).equalsIgnoreCase("sum")) {
00101 tmp[bidx] = SenseCorba.SUMMA;
00102 sidx = 3;
00103 }
00104 else if (cur.substring(0, 3).equalsIgnoreCase("gps")) {
00105 tmp[bidx] = SenseCorba.GPS;
00106 sidx = 3;
00107 }
00108 else if (cur.substring(0, 3).equalsIgnoreCase("mag")) {
00109 tmp[bidx] = SenseCorba.MAG;
00110 sidx = 3;
00111 }
00112 else if (cur.substring(0, 3).equalsIgnoreCase("mic")) {
00113 tmp[bidx] = SenseCorba.MIC;
00114 sidx = 3;
00115 }
00116 else if (cur.substring(0, 4).equalsIgnoreCase("mean")) {
00117 tmp[bidx] = SenseCorba.BARX;
00118 sidx = 4;
00119 }
00120 else if (cur.substring(0, 4).equalsIgnoreCase("rate")) {
00121 tmp[bidx] = SenseCorba.FREQ;
00122 sidx = 4;
00123 }
00124 else if (cur.substring(0, 4).equalsIgnoreCase("temp")) {
00125 tmp[bidx] = SenseCorba.TEMP;
00126 sidx = 4;
00127 }
00128 else if (cur.substring(0, 5).equalsIgnoreCase("sense")) {
00129 tmp[bidx] = SenseCorba.ALPHA;
00130 sidx = 5;
00131 }
00132 else if (cur.substring(0, 5).equalsIgnoreCase("delta")) {
00133 tmp[bidx] = SenseCorba.DELTA;
00134 sidx = 5;
00135 }
00136 else if (cur.substring(0, 5).equalsIgnoreCase("sigma")) {
00137 tmp[bidx] = SenseCorba.SIGMA;
00138 sidx = 5;
00139 }
00140 else if (cur.substring(0, 5).equalsIgnoreCase("angle")) {
00141 tmp[bidx] = SenseCorba.ANGLE;
00142 sidx = 5;
00143 }
00144 else if (cur.substring(0, 5).equalsIgnoreCase("level")) {
00145 tmp[bidx] = SenseCorba.LEVEL;
00146 sidx = 5;
00147 }
00148 else if (cur.substring(0, 5).equalsIgnoreCase("light")) {
00149 tmp[bidx] = SenseCorba.LIGHT;
00150 sidx = 5;
00151 }
00152 else if (cur.substring(0, 5).equalsIgnoreCase("accel")) {
00153 tmp[bidx] = SenseCorba.ACCEL;
00154 sidx = 5;
00155 }
00156 else if (cur.substring(0, 6).equalsIgnoreCase("recite")) {
00157 tmp[bidx] = SenseCorba.IOTA;
00158 sidx = 6;
00159 }
00160 else if (cur.substring(0, 6).equalsIgnoreCase("lambda")) {
00161 tmp[bidx] = SenseCorba.LAMBDA;
00162 sidx = 6;
00163 }
00164 else if (cur.substring(0, 6).equalsIgnoreCase("sensor")) {
00165 tmp[bidx] = SenseCorba.SENSOR;
00166 sidx = 6;
00167 }
00168 else if (cur.substring(0, 7).equalsIgnoreCase("battery")) {
00169 tmp[bidx] = SenseCorba.BATTERY;
00170 sidx = 7;
00171 }
00172 else if (cur.substring(0, 8).equalsIgnoreCase("duration")) {
00173 tmp[bidx] = SenseCorba.TIME;
00174 sidx = 8;
00175 }
00176 else if (cur.substring(0, 8).equalsIgnoreCase("distance")) {
00177 tmp[bidx] = SenseCorba.DIST;
00178 sidx = 8;
00179 }
00180 else if (cur.substring(0, 8).equalsIgnoreCase("humidity")) {
00181 tmp[bidx] = SenseCorba.HUMID;
00182 sidx = 8;
00183 }
00184 else if (cur.substring(0, 8).equalsIgnoreCase("pressure")) {
00185 tmp[bidx] = SenseCorba.PRESS;
00186 sidx = 8;
00187 }
00188 else if (cur.substring(0, 9).equalsIgnoreCase("peaksense")) {
00189 tmp[bidx] = SenseCorba.BETA;
00190 sidx = 9;
00191 }
00192 else if (cur.substring(0, 10).equalsIgnoreCase("timeseries")) {
00193 tmp[bidx] = SenseCorba.THETA;
00194 sidx = 10;
00195 }
00196 else if (cur.substring(0, 10).equalsIgnoreCase("microphone")) {
00197 tmp[bidx] = SenseCorba.MIC;
00198 sidx = 10;
00199 }
00200 else if (cur.substring(0, 11).equalsIgnoreCase("temperature")) {
00201 tmp[bidx] = SenseCorba.TEMP;
00202 sidx = 11;
00203 }
00204 else if (cur.substring(0, 12).equalsIgnoreCase("magnetometer")) {
00205 tmp[bidx] = SenseCorba.MAG;
00206 sidx = 12;
00207 }
00208 else if (cur.substring(0, 13).equalsIgnoreCase("spatialseries")) {
00209 tmp[bidx] = SenseCorba.PSI;
00210 sidx = 13;
00211 }
00212
00213 else if (cur.substring(0, 13).equalsIgnoreCase("low threshold")) {
00214 tmp[bidx] = SenseCorba.THR_MINUS;
00215 sidx = 13;
00216 }
00217 else if (cur.substring(0, 13).equalsIgnoreCase("accelerometer")) {
00218 tmp[bidx] = SenseCorba.ACCEL;
00219 sidx = 13;
00220 }
00221 else if (cur.substring(0, 14).equalsIgnoreCase("high threshold")) {
00222 tmp[bidx] = SenseCorba.THR_PLUS;
00223 sidx = 14;
00224 }
00225
00226 cur = cur.substring(sidx);
00227 bidx++;
00228 } catch (IndexOutOfBoundsException e) {
00229 break;
00230 }
00231 }
00232 bidx--;
00233 if (bidx < 0)
00234 return null;
00235
00236 bytes = new byte[bidx];
00237 System.arraycopy(tmp, 0, bytes, 0, bidx);
00238 return bytes;
00239 }
00240
00241
00242 public static boolean isIntegerData(char discrim) {
00243 switch (discrim) {
00244 case 'd':
00245 case 'i':
00246 case 'u':
00247 case 'x':
00248 return true;
00249 default:
00250 return false;
00251 }
00252 }
00253
00254 public static boolean isFloatingPointData(char discrim) {
00255 switch (discrim) {
00256 case 'f':
00257 case 'e':
00258 case 'g':
00259 case 'a':
00260 return true;
00261 default:
00262 return false;
00263 }
00264 }
00265
00266
00267 public static void appUsage(String appname, String jarname) {
00268 System.err.println(appname + " usage: java -jar " + jarname +
00269 " [--level h_level] [--id node_id] " +
00270 "<-debug> <-info> <-sim> <-help>");
00271 System.err.println(" where:");
00272 System.err.println(" 'h_level' (required) represents " +
00273 "this node's position in the network hierarchy " +
00274 "in [0 - 255]");
00275 System.err.println(" node_id (required) is a unique " +
00276 "unsigned integer identifier for this node " +
00277 "in [0 - 4294967295]");
00278 System.err.println(" debug increases the verbosity of output");
00279 System.err.println(" info slightly increases the output");
00280 System.err.println(" sim toggles simulator interaction");
00281 System.err.println(" help produces this output and exits");
00282 }
00283
00284
00286
00287
00288 public int service_port, id;
00289 public boolean simulate;
00290 public String simulator;
00291 public byte level, debug_level;
00292 public String[] corba_args;
00293
00294 public SenseUtil(String appname, String jarname, String args[]) {
00295 boolean level_set = false;
00296 boolean id_set = false;
00297 String[] temp_args = new String[args.length];
00298 int num_corba_args = 0;
00299
00300 service_port = 0;
00301 level = 0;
00302 id = 0;
00303 debug_level = 0;
00304 simulate = false;
00305 simulator = "";
00306
00307 for (int i = 0, j = 0; i < args.length; i++) {
00308 String argument = args[i];
00309 int idx = args[i].lastIndexOf('-') + 1;
00310 if (idx > 0 && idx < 3)
00311 argument = args[i].substring(idx);
00312
00313 if (argument.equalsIgnoreCase("debug"))
00314 debug_level = 2;
00315 else if (argument.equalsIgnoreCase("info"))
00316 debug_level = 1;
00317 else if (argument.equalsIgnoreCase("level")) {
00318 i++;
00319 if (i < args.length) {
00320 try {
00321 level = (new Integer(args[i])).byteValue();
00322 level_set = true;
00323 } catch (NumberFormatException e) {
00324 System.err.println(appname + ": Bad level argument: " +
00325 args[i]);
00326 level_set = false;
00327 }
00328 }
00329 }
00330 else if (argument.equalsIgnoreCase("sim")) {
00331 i++;
00332 if (i < args.length) {
00333 simulator = args[i];
00334 simulate = true;
00335 }
00336 }
00337 else if (argument.equalsIgnoreCase("id")) {
00338 i++;
00339 if (i < args.length) {
00340 try {
00341 id = Integer.parseInt(args[i]);
00342 id_set = true;
00343 } catch (NumberFormatException e) {
00344 System.err.println(appname + ": Bad id argument: " +
00345 args[i]);
00346 id_set = false;
00347 }
00348 }
00349 }
00350 else if (argument.equalsIgnoreCase("help")) {
00351 appUsage(appname, jarname);
00352 System.exit(0);
00353 }
00354 else {
00355 temp_args[j] = args[i];
00356 num_corba_args++;
00357 j++;
00358 }
00359 }
00360 if (level_set == false || id_set == false) {
00361 appUsage(appname, jarname);
00362 System.exit(1);
00363 }
00364 corba_args = new String[temp_args.length];
00365 System.arraycopy(temp_args, 0, corba_args, 0, corba_args.length);
00366 }
00367 }