List of usage examples for java.nio ByteBuffer compact
public abstract ByteBuffer compact();
From source file:hivemall.mf.BPRMatrixFactorizationUDTF.java
private final void runIterativeTraining(@Nonnegative final int iterations) throws HiveException { final ByteBuffer inputBuf = this.inputBuf; final NioFixedSegment fileIO = this.fileIO; assert (inputBuf != null); assert (fileIO != null); final long numTrainingExamples = count; final Reporter reporter = getReporter(); final Counter iterCounter = (reporter == null) ? null : reporter.getCounter("hivemall.mf.BPRMatrixFactorization$Counter", "iteration"); try {//from w w w . ja va 2 s .c o m if (lastWritePos == 0) {// run iterations w/o temporary file if (inputBuf.position() == 0) { return; // no training example } inputBuf.flip(); int iter = 2; for (; iter <= iterations; iter++) { reportProgress(reporter); setCounterValue(iterCounter, iter); while (inputBuf.remaining() > 0) { int u = inputBuf.getInt(); int i = inputBuf.getInt(); int j = inputBuf.getInt(); // invoke train count++; train(u, i, j); } cvState.multiplyLoss(0.5d); cvState.logState(iter, eta()); if (cvState.isConverged(iter, numTrainingExamples)) { break; } if (cvState.isLossIncreased()) { etaEstimator.update(1.1f); } else { etaEstimator.update(0.5f); } inputBuf.rewind(); } LOG.info("Performed " + Math.min(iter, iterations) + " iterations of " + NumberUtils.formatNumber(numTrainingExamples) + " training examples on memory (thus " + NumberUtils.formatNumber(count) + " training updates in total) "); } else {// read training examples in the temporary file and invoke train for each example // write training examples in buffer to a temporary file if (inputBuf.position() > 0) { writeBuffer(inputBuf, fileIO, lastWritePos); } else if (lastWritePos == 0) { return; // no training example } try { fileIO.flush(); } catch (IOException e) { throw new HiveException("Failed to flush a file: " + fileIO.getFile().getAbsolutePath(), e); } if (LOG.isInfoEnabled()) { File tmpFile = fileIO.getFile(); LOG.info( "Wrote " + numTrainingExamples + " records to a temporary file for iterative training: " + tmpFile.getAbsolutePath() + " (" + FileUtils.prettyFileSize(tmpFile) + ")"); } // run iterations int iter = 2; for (; iter <= iterations; iter++) { setCounterValue(iterCounter, iter); inputBuf.clear(); long seekPos = 0L; while (true) { reportProgress(reporter); // TODO prefetch // writes training examples to a buffer in the temporary file final int bytesRead; try { bytesRead = fileIO.read(seekPos, inputBuf); } catch (IOException e) { throw new HiveException("Failed to read a file: " + fileIO.getFile().getAbsolutePath(), e); } if (bytesRead == 0) { // reached file EOF break; } assert (bytesRead > 0) : bytesRead; seekPos += bytesRead; // reads training examples from a buffer inputBuf.flip(); int remain = inputBuf.remaining(); assert (remain > 0) : remain; for (; remain >= RECORD_BYTES; remain -= RECORD_BYTES) { int u = inputBuf.getInt(); int i = inputBuf.getInt(); int j = inputBuf.getInt(); // invoke train count++; train(u, i, j); } inputBuf.compact(); } cvState.multiplyLoss(0.5d); cvState.logState(iter, eta()); if (cvState.isConverged(iter, numTrainingExamples)) { break; } if (cvState.isLossIncreased()) { etaEstimator.update(1.1f); } else { etaEstimator.update(0.5f); } } LOG.info("Performed " + Math.min(iter, iterations) + " iterations of " + NumberUtils.formatNumber(numTrainingExamples) + " training examples using a secondary storage (thus " + NumberUtils.formatNumber(count) + " training updates in total)"); } } finally { // delete the temporary file and release resources try { fileIO.close(true); } catch (IOException e) { throw new HiveException("Failed to close a file: " + fileIO.getFile().getAbsolutePath(), e); } this.inputBuf = null; this.fileIO = null; } }
From source file:edu.hawaii.soest.kilonalu.tchain.TChainSource.java
/** * A method that executes the streaming of data from the source to the RBNB * server after all configuration of settings, connections to hosts, and * thread initiatizing occurs. This method contains the detailed code for * streaming the data and interpreting the stream. */// ww w . j a v a2 s . c o m protected boolean execute() { logger.debug("TChainSource.execute() called."); // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; SocketChannel socket = getSocketConnection(); // while data are being sent, read them into the buffer try { // create four byte placeholders used to evaluate up to a four-byte // window. The FIFO layout looks like: // ------------------------- // in ---> | One | Two |Three|Four | ---> out // ------------------------- byte byteOne = 0x00, // set initial placeholder values byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00; // Create a buffer that will store the sample bytes as they are read ByteBuffer sampleBuffer = ByteBuffer.allocate(getBufferSize()); // create a byte buffer to store bytes from the TCP stream ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize()); // add a channel of data that will be pushed to the server. // Each sample will be sent to the Data Turbine as an rbnb frame. ChannelMap rbnbChannelMap = new ChannelMap(); // while there are bytes to read from the socket ... while (socket.read(buffer) != -1 || buffer.position() > 0) { // prepare the buffer for reading buffer.flip(); // while there are unread bytes in the ByteBuffer while (buffer.hasRemaining()) { byteOne = buffer.get(); logger.debug("char: " + (char) byteOne + "\t" + "b1: " + new String(Hex.encodeHex((new byte[] { byteOne }))) + "\t" + "b2: " + new String(Hex.encodeHex((new byte[] { byteTwo }))) + "\t" + "b3: " + new String(Hex.encodeHex((new byte[] { byteThree }))) + "\t" + "b4: " + new String(Hex.encodeHex((new byte[] { byteFour }))) + "\t" + "sample pos: " + sampleBuffer.position() + "\t" + "sample rem: " + sampleBuffer.remaining() + "\t" + "sample cnt: " + sampleByteCount + "\t" + "buffer pos: " + buffer.position() + "\t" + "buffer rem: " + buffer.remaining() + "\t" + "state: " + state); // Use a State Machine to process the byte stream. // Start building an rbnb frame for the entire sample, first by // inserting a timestamp into the channelMap. This time is merely // the time of insert into the data turbine, not the time of // observations of the measurements. That time should be parsed out // of the sample in the Sink client code switch (state) { case 0: // sample line ending is '\r\n' (carraige return, newline) // note bytes are in reverse order in the FIFO window if (byteOne == this.firstDelimiterByte && byteTwo == this.secondDelimiterByte) { // we've found the end of a sample, move on state = 1; break; } else { break; } case 1: // read the rest of the bytes to the next EOL characters // sample line is terminated by record delimiter bytes (usually \r\n or \n) // note bytes are in reverse order in the FIFO window if (byteOne == this.firstDelimiterByte && byteTwo == this.secondDelimiterByte) { // rewind the sample to overwrite the line ending so we can add // in the timestamp (then add the line ending) sampleBuffer.position(sampleBuffer.position() - 1); --sampleByteCount; // add the delimiter to the end of the sample. byte[] delimiterAsBytes = getFieldDelimiter().getBytes("US-ASCII"); for (byte delim : delimiterAsBytes) { sampleBuffer.put(delim); sampleByteCount++; } // then add a timestamp to the end of the sample DATE_FORMAT.setTimeZone(TZ); byte[] sampleDateAsBytes = DATE_FORMAT.format(new Date()).getBytes("US-ASCII"); for (byte b : sampleDateAsBytes) { sampleBuffer.put(b); sampleByteCount++; } // add the last two bytes found (usually \r\n) to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); sampleByteCount++; sampleBuffer.put(byteTwo); sampleByteCount++; } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); sampleByteCount++; sampleBuffer.put(byteTwo); sampleByteCount++; } // extract just the length of the sample bytes out of the // sample buffer, and place it in the channel map as a // byte array. Then, send it to the data turbine. byte[] sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); // send the sample to the data turbine rbnbChannelMap.PutTimeAuto("server"); String sampleString = new String(sampleArray, "US-ASCII"); int channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, sampleString); getSource().Flush(rbnbChannelMap); logger.info("Sample: " + sampleString.substring(0, sampleString.length() - 2) + " sent data to the DataTurbine. "); byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; sampleBuffer.clear(); sampleByteCount = 0; rbnbChannelMap.Clear(); logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap."); //state = 0; } else { // not 0x0D20 // still in the middle of the sample, keep adding bytes sampleByteCount++; // add each byte found if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); logger.debug("Compacting sampleBuffer ..."); sampleBuffer.put(byteOne); } break; } // end if for 0x0D20 EOL } // end switch statement // shift the bytes in the FIFO window byteFour = byteThree; byteThree = byteTwo; byteTwo = byteOne; } //end while (more unread bytes) // prepare the buffer to read in more bytes from the stream buffer.compact(); } // end while (more socket bytes to read) socket.close(); } catch (IOException e) { // handle exceptions // In the event of an i/o exception, log the exception, and allow execute() // to return false, which will prompt a retry. failed = true; e.printStackTrace(); return !failed; } catch (SAPIException sapie) { // In the event of an RBNB communication exception, log the exception, // and allow execute() to return false, which will prompt a retry. failed = true; sapie.printStackTrace(); return !failed; } return !failed; }
From source file:hivemall.fm.FactorizationMachineUDTF.java
protected void runTrainingIteration(int iterations) throws HiveException { final ByteBuffer inputBuf = this._inputBuf; final NioStatefullSegment fileIO = this._fileIO; assert (inputBuf != null); assert (fileIO != null); final long numTrainingExamples = _t; final boolean adaregr = _va_rand != null; final Reporter reporter = getReporter(); final Counter iterCounter = (reporter == null) ? null : reporter.getCounter("hivemall.fm.FactorizationMachines$Counter", "iteration"); try {/*from w w w . j a v a2 s. c om*/ if (fileIO.getPosition() == 0L) {// run iterations w/o temporary file if (inputBuf.position() == 0) { return; // no training example } inputBuf.flip(); int iter = 2; for (; iter <= iterations; iter++) { reportProgress(reporter); setCounterValue(iterCounter, iter); while (inputBuf.remaining() > 0) { int bytes = inputBuf.getInt(); assert (bytes > 0) : bytes; int xLength = inputBuf.getInt(); final Feature[] x = new Feature[xLength]; for (int j = 0; j < xLength; j++) { x[j] = instantiateFeature(inputBuf); } double y = inputBuf.getDouble(); // invoke train ++_t; train(x, y, adaregr); } if (_cvState.isConverged(iter, numTrainingExamples)) { break; } inputBuf.rewind(); } LOG.info("Performed " + Math.min(iter, iterations) + " iterations of " + NumberUtils.formatNumber(numTrainingExamples) + " training examples on memory (thus " + NumberUtils.formatNumber(_t) + " training updates in total) "); } else {// read training examples in the temporary file and invoke train for each example // write training examples in buffer to a temporary file if (inputBuf.remaining() > 0) { writeBuffer(inputBuf, fileIO); } try { fileIO.flush(); } catch (IOException e) { throw new HiveException("Failed to flush a file: " + fileIO.getFile().getAbsolutePath(), e); } if (LOG.isInfoEnabled()) { File tmpFile = fileIO.getFile(); LOG.info( "Wrote " + numTrainingExamples + " records to a temporary file for iterative training: " + tmpFile.getAbsolutePath() + " (" + FileUtils.prettyFileSize(tmpFile) + ")"); } // run iterations int iter = 2; for (; iter <= iterations; iter++) { setCounterValue(iterCounter, iter); inputBuf.clear(); fileIO.resetPosition(); while (true) { reportProgress(reporter); // TODO prefetch // writes training examples to a buffer in the temporary file final int bytesRead; try { bytesRead = fileIO.read(inputBuf); } catch (IOException e) { throw new HiveException("Failed to read a file: " + fileIO.getFile().getAbsolutePath(), e); } if (bytesRead == 0) { // reached file EOF break; } assert (bytesRead > 0) : bytesRead; // reads training examples from a buffer inputBuf.flip(); int remain = inputBuf.remaining(); if (remain < INT_BYTES) { throw new HiveException("Illegal file format was detected"); } while (remain >= INT_BYTES) { int pos = inputBuf.position(); int recordBytes = inputBuf.getInt(); remain -= INT_BYTES; if (remain < recordBytes) { inputBuf.position(pos); break; } final int xLength = inputBuf.getInt(); final Feature[] x = new Feature[xLength]; for (int j = 0; j < xLength; j++) { x[j] = instantiateFeature(inputBuf); } double y = inputBuf.getDouble(); // invoke training ++_t; train(x, y, adaregr); remain -= recordBytes; } inputBuf.compact(); } if (_cvState.isConverged(iter, numTrainingExamples)) { break; } } LOG.info("Performed " + Math.min(iter, iterations) + " iterations of " + NumberUtils.formatNumber(numTrainingExamples) + " training examples on a secondary storage (thus " + NumberUtils.formatNumber(_t) + " training updates in total)"); } } finally { // delete the temporary file and release resources try { fileIO.close(true); } catch (IOException e) { throw new HiveException("Failed to close a file: " + fileIO.getFile().getAbsolutePath(), e); } this._inputBuf = null; this._fileIO = null; } }
From source file:hivemall.GeneralLearnerBaseUDTF.java
protected final void runIterativeTraining(@Nonnegative final int iterations) throws HiveException { final ByteBuffer buf = this.inputBuf; final NioStatefulSegment dst = this.fileIO; assert (buf != null); assert (dst != null); final long numTrainingExamples = count; final Reporter reporter = getReporter(); final Counters.Counter iterCounter = (reporter == null) ? null : reporter.getCounter("hivemall.GeneralLearnerBase$Counter", "iteration"); try {/*from ww w .ja va2 s.c o m*/ if (dst.getPosition() == 0L) {// run iterations w/o temporary file if (buf.position() == 0) { return; // no training example } buf.flip(); for (int iter = 2; iter <= iterations; iter++) { cvState.next(); reportProgress(reporter); setCounterValue(iterCounter, iter); while (buf.remaining() > 0) { int recordBytes = buf.getInt(); assert (recordBytes > 0) : recordBytes; int featureVectorLength = buf.getInt(); final FeatureValue[] featureVector = new FeatureValue[featureVectorLength]; for (int j = 0; j < featureVectorLength; j++) { featureVector[j] = readFeatureValue(buf, featureType); } float target = buf.getFloat(); train(featureVector, target); } buf.rewind(); if (is_mini_batch) { // Update model with accumulated delta batchUpdate(); } if (cvState.isConverged(numTrainingExamples)) { break; } } logger.info("Performed " + cvState.getCurrentIteration() + " iterations of " + NumberUtils.formatNumber(numTrainingExamples) + " training examples on memory (thus " + NumberUtils.formatNumber(numTrainingExamples * cvState.getCurrentIteration()) + " training updates in total) "); } else {// read training examples in the temporary file and invoke train for each example // write training examples in buffer to a temporary file if (buf.remaining() > 0) { writeBuffer(buf, dst); } try { dst.flush(); } catch (IOException e) { throw new HiveException("Failed to flush a file: " + dst.getFile().getAbsolutePath(), e); } if (logger.isInfoEnabled()) { File tmpFile = dst.getFile(); logger.info( "Wrote " + numTrainingExamples + " records to a temporary file for iterative training: " + tmpFile.getAbsolutePath() + " (" + FileUtils.prettyFileSize(tmpFile) + ")"); } // run iterations for (int iter = 2; iter <= iterations; iter++) { cvState.next(); setCounterValue(iterCounter, iter); buf.clear(); dst.resetPosition(); while (true) { reportProgress(reporter); // TODO prefetch // writes training examples to a buffer in the temporary file final int bytesRead; try { bytesRead = dst.read(buf); } catch (IOException e) { throw new HiveException("Failed to read a file: " + dst.getFile().getAbsolutePath(), e); } if (bytesRead == 0) { // reached file EOF break; } assert (bytesRead > 0) : bytesRead; // reads training examples from a buffer buf.flip(); int remain = buf.remaining(); if (remain < SizeOf.INT) { throw new HiveException("Illegal file format was detected"); } while (remain >= SizeOf.INT) { int pos = buf.position(); int recordBytes = buf.getInt(); remain -= SizeOf.INT; if (remain < recordBytes) { buf.position(pos); break; } int featureVectorLength = buf.getInt(); final FeatureValue[] featureVector = new FeatureValue[featureVectorLength]; for (int j = 0; j < featureVectorLength; j++) { featureVector[j] = readFeatureValue(buf, featureType); } float target = buf.getFloat(); train(featureVector, target); remain -= recordBytes; } buf.compact(); } if (is_mini_batch) { // Update model with accumulated delta batchUpdate(); } if (cvState.isConverged(numTrainingExamples)) { break; } } logger.info("Performed " + cvState.getCurrentIteration() + " iterations of " + NumberUtils.formatNumber(numTrainingExamples) + " training examples on a secondary storage (thus " + NumberUtils.formatNumber(numTrainingExamples * cvState.getCurrentIteration()) + " training updates in total)"); } } catch (Throwable e) { throw new HiveException("Exception caused in the iterative training", e); } finally { // delete the temporary file and release resources try { dst.close(true); } catch (IOException e) { throw new HiveException("Failed to close a file: " + dst.getFile().getAbsolutePath(), e); } this.inputBuf = null; this.fileIO = null; } }
From source file:hivemall.topicmodel.ProbabilisticTopicModelBaseUDTF.java
protected final void runIterativeTraining(@Nonnegative final int iterations) throws HiveException { final ByteBuffer buf = this.inputBuf; final NioStatefulSegment dst = this.fileIO; assert (buf != null); assert (dst != null); final long numTrainingExamples = model.getDocCount(); long numTrain = numTrainingExamples / miniBatchSize; if (numTrainingExamples % miniBatchSize != 0L) { numTrain++;/*w ww .j av a 2 s .c o m*/ } final Reporter reporter = getReporter(); final Counters.Counter iterCounter = (reporter == null) ? null : reporter.getCounter("hivemall.topicmodel.ProbabilisticTopicModel$Counter", "iteration"); try { if (dst.getPosition() == 0L) {// run iterations w/o temporary file if (buf.position() == 0) { return; // no training example } buf.flip(); int iter = 2; float perplexity = cumPerplexity / numTrain; float perplexityPrev; for (; iter <= iterations; iter++) { perplexityPrev = perplexity; cumPerplexity = 0.f; reportProgress(reporter); setCounterValue(iterCounter, iter); while (buf.remaining() > 0) { int recordBytes = buf.getInt(); assert (recordBytes > 0) : recordBytes; int wcLength = buf.getInt(); final String[] wordCounts = new String[wcLength]; for (int j = 0; j < wcLength; j++) { wordCounts[j] = NIOUtils.getString(buf); } update(wordCounts); } buf.rewind(); // mean perplexity over `numTrain` mini-batches perplexity = cumPerplexity / numTrain; logger.info("Mean perplexity over mini-batches: " + perplexity); if (Math.abs(perplexityPrev - perplexity) < eps) { break; } } logger.info("Performed " + Math.min(iter, iterations) + " iterations of " + NumberUtils.formatNumber(numTrainingExamples) + " training examples on memory (thus " + NumberUtils.formatNumber(numTrainingExamples * Math.min(iter, iterations)) + " training updates in total) "); } else {// read training examples in the temporary file and invoke train for each example // write training examples in buffer to a temporary file if (buf.remaining() > 0) { writeBuffer(buf, dst); } try { dst.flush(); } catch (IOException e) { throw new HiveException("Failed to flush a file: " + dst.getFile().getAbsolutePath(), e); } if (logger.isInfoEnabled()) { File tmpFile = dst.getFile(); logger.info( "Wrote " + numTrainingExamples + " records to a temporary file for iterative training: " + tmpFile.getAbsolutePath() + " (" + FileUtils.prettyFileSize(tmpFile) + ")"); } // run iterations int iter = 2; float perplexity = cumPerplexity / numTrain; float perplexityPrev; for (; iter <= iterations; iter++) { perplexityPrev = perplexity; cumPerplexity = 0.f; setCounterValue(iterCounter, iter); buf.clear(); dst.resetPosition(); while (true) { reportProgress(reporter); // TODO prefetch // writes training examples to a buffer in the temporary file final int bytesRead; try { bytesRead = dst.read(buf); } catch (IOException e) { throw new HiveException("Failed to read a file: " + dst.getFile().getAbsolutePath(), e); } if (bytesRead == 0) { // reached file EOF break; } assert (bytesRead > 0) : bytesRead; // reads training examples from a buffer buf.flip(); int remain = buf.remaining(); if (remain < SizeOf.INT) { throw new HiveException("Illegal file format was detected"); } while (remain >= SizeOf.INT) { int pos = buf.position(); int recordBytes = buf.getInt(); remain -= SizeOf.INT; if (remain < recordBytes) { buf.position(pos); break; } int wcLength = buf.getInt(); final String[] wordCounts = new String[wcLength]; for (int j = 0; j < wcLength; j++) { wordCounts[j] = NIOUtils.getString(buf); } update(wordCounts); remain -= recordBytes; } buf.compact(); } // mean perplexity over `numTrain` mini-batches perplexity = cumPerplexity / numTrain; logger.info("Mean perplexity over mini-batches: " + perplexity); if (Math.abs(perplexityPrev - perplexity) < eps) { break; } } logger.info("Performed " + Math.min(iter, iterations) + " iterations of " + NumberUtils.formatNumber(numTrainingExamples) + " training examples on a secondary storage (thus " + NumberUtils.formatNumber(numTrainingExamples * Math.min(iter, iterations)) + " training updates in total)"); } } catch (Throwable e) { throw new HiveException("Exception caused in the iterative training", e); } finally { // delete the temporary file and release resources try { dst.close(true); } catch (IOException e) { throw new HiveException("Failed to close a file: " + dst.getFile().getAbsolutePath(), e); } this.inputBuf = null; this.fileIO = null; } }
From source file:edu.hawaii.soest.kilonalu.ctd.SeahorseSource.java
/** * A method that executes the streaming of data from the source to the RBNB * server after all configuration of settings, connections to hosts, and * thread initiatizing occurs. This method contains the detailed code for * streaming the data and interpreting the stream. *//*from w w w. ja v a 2s . c om*/ protected boolean execute() { logger.debug("SeahorseSource.execute() called."); // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; this.socketChannel = getSocketConnection(); // while data are being sent, read them into the buffer try { // create four byte placeholders used to evaluate up to a four-byte // window. The FIFO layout looks like: // ------------------------- // in ---> | One | Two |Three|Four | ---> out // ------------------------- byte byteOne = 0x00, // set initial placeholder values byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00; // define a byte array that will be used to manipulate the incoming bytes byte[] resultArray; String resultString; // Create a buffer that will store the result bytes as they are read ByteBuffer resultBuffer = ByteBuffer.allocate(getBufferSize()); // create a byte buffer to store bytes from the TCP stream ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize()); this.rbnbChannelMap = new ChannelMap(); this.channelIndex = 0; // initiate the session with the modem, test if is network registered this.command = this.MODEM_COMMAND_PREFIX + this.REGISTRATION_STATUS_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); // while there are bytes to read from the socketChannel ... while (socketChannel.read(buffer) != -1 || buffer.position() > 0) { // prepare the buffer for reading buffer.flip(); // while there are unread bytes in the ByteBuffer while (buffer.hasRemaining()) { byteOne = buffer.get(); //logger.debug("b1: " + new String(Hex.encodeHex((new byte[]{byteOne}))) + "\t" + // "b2: " + new String(Hex.encodeHex((new byte[]{byteTwo}))) + "\t" + // "b3: " + new String(Hex.encodeHex((new byte[]{byteThree}))) + "\t" + // "b4: " + new String(Hex.encodeHex((new byte[]{byteFour}))) + "\t" + // "result pos: " + resultBuffer.position() + "\t" + // "result rem: " + resultBuffer.remaining() + "\t" + // "result cnt: " + resultByteCount + "\t" + // "buffer pos: " + buffer.position() + "\t" + // "buffer rem: " + buffer.remaining() + "\t" + // "state: " + state //); // Use a State Machine to process the byte stream. // Start building an rbnb frame for the entire sample, first by // inserting a timestamp into the channelMap. This time is merely // the time of insert into the data turbine, not the time of // observations of the measurements. That time should be parsed out // of the sample in the Sink client code switch (state) { case 0: // the network registration status should end in OK\r\n // note bytes are in reverse order in the FIFO window if (byteOne == 0x0A && byteTwo == 0x0D && byteThree == 0x4B && byteFour == 0x4F) { logger.debug("Received the registration status result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the network registration status string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Network Registration Result: " + resultString.trim()); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // send a request for the signal strength this.command = this.MODEM_COMMAND_PREFIX + this.SIGNAL_STRENGTH_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 1; break; } else { this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } break; } case 1: // report the signal strength of the Iridium modem // the signal strength status should end in OK\r\n // note bytes are in reverse order in the FIFO window if (byteOne == 0x0A && byteTwo == 0x0D && byteThree == 0x4B && byteFour == 0x4F) { logger.debug("Received the signal strength result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the signal strength status string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Signal Strength Result: " + resultString.trim()); int signalStrengthIndex = resultString.indexOf(this.SIGNAL_STRENGTH) + 5; int signalStrength = new Integer( resultString.substring(signalStrengthIndex, signalStrengthIndex + 1)) .intValue(); // test if the signal strength is above the threshold if (signalStrength > SIGNAL_THRESHOLD) { resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 2; break; // the signal strength is too low, check again } else { resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // resend a request for the signal strength this.command = this.MODEM_COMMAND_PREFIX + this.SIGNAL_STRENGTH_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 1; break; } } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 2: // handle the RING command from the instrument // listen for the RING command // note bytes are in reverse order in the FIFO window if (byteOne == 0x47 && byteTwo == 0x4E && byteThree == 0x49 && byteFour == 0x52) { logger.debug("Received the RING command."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // answer the call this.command = this.MODEM_COMMAND_PREFIX + this.ANSWER_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 3; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 3: // acknowledge the connection // the ready status string should end in READY\r // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x59 && byteThree == 0x44 && byteFour == 0x41) { logger.debug("Received the ready status result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the connect rate and ready status string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); // test the connect rate logger.debug("Result from ATA: " + resultString); if (resultString.indexOf(this.CONNECT_RATE) > 0) { logger.debug("Connect Rate Result: " + this.CONNECT_RATE); // test the ready status if (resultString.indexOf(this.READY_STATUS) > 0) { logger.debug("Connect Rate Result: " + this.READY_STATUS); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // acknowledge the ready status this.command = this.ACKNOWLEDGE_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to receive the ACK streamingThread.sleep(this.SLEEP_INTERVAL); // query the instrument id this.command = this.ID_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 4; break; } else { logger.debug("The ready status differs from: " + this.READY_STATUS); // throw an exception here? break; } } else { logger.debug("The connect rate differs from: " + this.CONNECT_RATE); // throw an exception here? break; } } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 4: // get the instrument id // the instrument ID string should end in \r if (byteOne == 0x0D) { logger.debug("Received the instrument ID result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the instrument ID string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Seahorse Instrument ID: " + resultString.trim()); // set the platformID variable this.platformID = resultString.substring(0, resultString.length() - 1); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // query the battery voltage this.command = this.BATTERY_VOLTAGE_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 5; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 5: // get the seahorse battery voltage // the battery voltage string should end in \r if (byteOne == 0x0D) { logger.debug("Received the instrument battery voltage result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the battery voltage string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Seahorse Battery Voltage: " + resultString.trim()); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // query the GPS location this.command = this.GPRMC_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 6; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 6: // the GPRMC string should end in END\r // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x44 && byteThree == 0x4E && byteFour == 0x45) { logger.debug("Received the GPRMS result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the GPRMC string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Seahorse GPRMC string: " + resultString.trim()); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // query the file name for transfer this.command = this.FILENAME_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 7; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 7: // the file name string should end in .Z\r // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x5A && byteThree == 0x2E) { logger.debug("Received the file name result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the file name string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("File name result: " + resultString.trim()); resultString = resultString.trim(); int fileNameIndex = resultString.indexOf(this.FILENAME_PREFIX); //extract just the filename from the result (excise the "FILE=") this.fileNameToDownload = resultString.substring( (fileNameIndex + (this.FILENAME_PREFIX).length()), resultString.length()); logger.debug("File name to download: " + this.fileNameToDownload); // test to see if the GFN command returns FILES=NONE if (!(resultString.indexOf(this.END_OF_FILES) > 0)) { // there is a file to download. parse the file name, // get the number of blocks to transfer this.command = this.NUMBER_OF_BLOCKS_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 8; break; } else { // We have downloaded all files. Parse the data string, // build the channel map, and flush the data to the Dataturbine // by iterating through the data matrix. The metadata and // ASCII data strings are flushed once with the first matrix // row. // Parse the data file, not the cast file. try { // parse the CTD data file this.ctdParser = new CTDParser(this.dataFileString); // convert the raw frequencies and voltages to engineering // units and return the data as a matrix CTDConverter ctdConverter = new CTDConverter(this.ctdParser); ctdConverter.convert(); RealMatrix convertedDataMatrix = ctdConverter.getConvertedDataValuesMatrix(); // Register the data and metadata channels; failed = register(); if (!failed) { // format the first sample date and use it as the first insert // date. Add the sampleInterval on each iteration to insert // subsequent data rows. Sample interval is by default // 4 scans/second for the CTD. DATE_FORMAT.setTimeZone(TZ); this.sampleDateTime = Calendar.getInstance(); this.sampleDateTime .setTime(DATE_FORMAT.parse(ctdParser.getFirstSampleTime())); for (int row = 0; row < convertedDataMatrix.getRowDimension(); row++) { // Only insert the metadata fields and full ASCII text strings // with the first row of data if (row == 0) { // Add the samplingMode data to the channel map this.channelIndex = this.rbnbChannelMap.Add("samplingMode"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getSamplingMode()); // Add the firstSampleTime data to the channel map this.channelIndex = this.rbnbChannelMap.Add("firstSampleTime"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getFirstSampleTime()); // Add the fileName data to the channel map this.channelIndex = this.rbnbChannelMap.Add("fileName"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getFileName()); // Add the temperatureSerialNumber data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureSerialNumber"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getTemperatureSerialNumber()); // Add the conductivitySerialNumber data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivitySerialNumber"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getConductivitySerialNumber()); // Add the systemUpLoadTime data to the channel map this.channelIndex = this.rbnbChannelMap.Add("systemUpLoadTime"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getSystemUpLoadTime()); // Add the cruiseInformation data to the channel map this.channelIndex = this.rbnbChannelMap.Add("cruiseInformation"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getCruiseInformation()); // Add the stationInformation data to the channel map this.channelIndex = this.rbnbChannelMap.Add("stationInformation"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getStationInformation()); // Add the shipInformation data to the channel map this.channelIndex = this.rbnbChannelMap.Add("shipInformation"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getShipInformation()); // Add the chiefScientist data to the channel map this.channelIndex = this.rbnbChannelMap.Add("chiefScientist"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getChiefScientist()); // Add the organization data to the channel map this.channelIndex = this.rbnbChannelMap.Add("organization"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getOrganization()); // Add the areaOfOperation data to the channel map this.channelIndex = this.rbnbChannelMap.Add("areaOfOperation"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getAreaOfOperation()); // Add the instrumentPackage data to the channel map this.channelIndex = this.rbnbChannelMap.Add("instrumentPackage"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getInstrumentPackage()); // Add the mooringNumber data to the channel map this.channelIndex = this.rbnbChannelMap.Add("mooringNumber"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getMooringNumber()); // Add the instrumentLatitude data to the channel map this.channelIndex = this.rbnbChannelMap.Add("instrumentLatitude"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getInstrumentLatitude() }); // Add the instrumentLongitude data to the channel map this.channelIndex = this.rbnbChannelMap.Add("instrumentLongitude"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getInstrumentLongitude() }); // Add the depthSounding data to the channel map this.channelIndex = this.rbnbChannelMap.Add("depthSounding"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getDepthSounding() }); // Add the profileNumber data to the channel map this.channelIndex = this.rbnbChannelMap.Add("profileNumber"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getProfileNumber()); // Add the profileDirection data to the channel map this.channelIndex = this.rbnbChannelMap.Add("profileDirection"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getProfileDirection()); // Add the deploymentNotes data to the channel map this.channelIndex = this.rbnbChannelMap.Add("deploymentNotes"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getDeploymentNotes()); // Add the mainBatteryVoltage data to the channel map this.channelIndex = this.rbnbChannelMap.Add("mainBatteryVoltage"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getMainBatteryVoltage() }); // Add the lithiumBatteryVoltage data to the channel map this.channelIndex = this.rbnbChannelMap .Add("lithiumBatteryVoltage"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getLithiumBatteryVoltage() }); // Add the operatingCurrent data to the channel map this.channelIndex = this.rbnbChannelMap.Add("operatingCurrent"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getOperatingCurrent() }); // Add the pumpCurrent data to the channel map this.channelIndex = this.rbnbChannelMap.Add("pumpCurrent"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPumpCurrent() }); // Add the channels01ExternalCurrent data to the channel map this.channelIndex = this.rbnbChannelMap .Add("channels01ExternalCurrent"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getChannels01ExternalCurrent() }); // Add the channels23ExternalCurrent data to the channel map this.channelIndex = this.rbnbChannelMap .Add("channels23ExternalCurrent"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getChannels23ExternalCurrent() }); // Add the loggingStatus data to the channel map this.channelIndex = this.rbnbChannelMap.Add("loggingStatus"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getLoggingStatus()); // Add the numberOfScansToAverage data to the channel map this.channelIndex = this.rbnbChannelMap .Add("numberOfScansToAverage"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getNumberOfScansToAverage() }); // Add the numberOfSamples data to the channel map this.channelIndex = this.rbnbChannelMap.Add("numberOfSamples"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getNumberOfSamples() }); // Add the numberOfAvailableSamples data to the channel map this.channelIndex = this.rbnbChannelMap .Add("numberOfAvailableSamples"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getNumberOfAvailableSamples() }); // Add the sampleInterval data to the channel map this.channelIndex = this.rbnbChannelMap.Add("sampleInterval"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getSampleInterval() }); // Add the measurementsPerSample data to the channel map this.channelIndex = this.rbnbChannelMap .Add("measurementsPerSample"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getMeasurementsPerSample() }); // Add the transmitRealtime data to the channel map this.channelIndex = this.rbnbChannelMap.Add("transmitRealtime"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getTransmitRealtime()); // Add the numberOfCasts data to the channel map this.channelIndex = this.rbnbChannelMap.Add("numberOfCasts"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getNumberOfCasts() }); // Add the minimumConductivityFrequency data to the channel map this.channelIndex = this.rbnbChannelMap .Add("minimumConductivityFrequency"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getMinimumConductivityFrequency() }); // Add the pumpDelay data to the channel map this.channelIndex = this.rbnbChannelMap.Add("pumpDelay"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getPumpDelay() }); // Add the automaticLogging data to the channel map this.channelIndex = this.rbnbChannelMap.Add("automaticLogging"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getAutomaticLogging()); // Add the ignoreMagneticSwitch data to the channel map this.channelIndex = this.rbnbChannelMap.Add("ignoreMagneticSwitch"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getIgnoreMagneticSwitch()); // Add the batteryType data to the channel map this.channelIndex = this.rbnbChannelMap.Add("batteryType"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getBatteryType()); // Add the batteryCutoff data to the channel map this.channelIndex = this.rbnbChannelMap.Add("batteryCutoff"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getBatteryCutoff()); // Add the pressureSensorType data to the channel map this.channelIndex = this.rbnbChannelMap.Add("pressureSensorType"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getPressureSensorType()); // Add the pressureSensorRange data to the channel map this.channelIndex = this.rbnbChannelMap.Add("pressureSensorRange"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getPressureSensorRange()); // Add the sbe38TemperatureSensor data to the channel map this.channelIndex = this.rbnbChannelMap .Add("sbe38TemperatureSensor"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getSbe38TemperatureSensor()); // Add the gasTensionDevice data to the channel map this.channelIndex = this.rbnbChannelMap.Add("gasTensionDevice"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getGasTensionDevice()); // Add the externalVoltageChannelZero data to the channel map this.channelIndex = this.rbnbChannelMap .Add("externalVoltageChannelZero"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getExternalVoltageChannelZero()); // Add the externalVoltageChannelOne data to the channel map this.channelIndex = this.rbnbChannelMap .Add("externalVoltageChannelOne"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getExternalVoltageChannelOne()); // Add the externalVoltageChannelTwo data to the channel map this.channelIndex = this.rbnbChannelMap .Add("externalVoltageChannelTwo"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getExternalVoltageChannelTwo()); // Add the externalVoltageChannelThree data to the channel map this.channelIndex = this.rbnbChannelMap .Add("externalVoltageChannelThree"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getExternalVoltageChannelThree()); // Add the echoCommands data to the channel map this.channelIndex = this.rbnbChannelMap.Add("echoCommands"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getEchoCommands()); // Add the outputFormat data to the channel map this.channelIndex = this.rbnbChannelMap.Add("outputFormat"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getOutputFormat()); // Add the temperatureCalibrationDate data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureCalibrationDate"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getTemperatureCalibrationDate()); // Add the temperatureCoefficientTA0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureCoefficientTA0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA0() }); // Add the temperatureCoefficientTA1 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureCoefficientTA1"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA1() }); // Add the temperatureCoefficientTA2 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureCoefficientTA2"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA2() }); // Add the temperatureCoefficientTA3 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureCoefficientTA3"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA3() }); // Add the temperatureOffsetCoefficient data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureOffsetCoefficient"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getTemperatureOffsetCoefficient() }); // Add the conductivityCalibrationDate data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCalibrationDate"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getConductivityCalibrationDate()); // Add the conductivityCoefficientG data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientG"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientG() }); // Add the conductivityCoefficientH data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientH"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientH() }); // Add the conductivityCoefficientI data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientI"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientI() }); // Add the conductivityCoefficientJ data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientJ"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientJ() }); // Add the conductivityCoefficientCF0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientCF0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCF0() }); // Add the conductivityCoefficientCPCOR data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientCPCOR"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCPCOR() }); // Add the conductivityCoefficientCTCOR data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientCTCOR"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCTCOR() }); // Add the conductivityCoefficientCSLOPE data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientCSLOPE"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser .getConductivityCoefficientCSLOPE() }); // Add the pressureSerialNumber data to the channel map this.channelIndex = this.rbnbChannelMap.Add("pressureSerialNumber"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getPressureSerialNumber()); // Add the pressureCoefficientPA0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPA0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA0() }); // Add the pressureCoefficientPA1 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPA1"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA1() }); // Add the pressureCoefficientPA2 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPA2"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA2() }); // Add the pressureCoefficientPTCA0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCA0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA0() }); // Add the pressureCoefficientPTCA1 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCA1"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA1() }); // Add the pressureCoefficientPTCA2 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCA2"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA2() }); // Add the pressureCoefficientPTCB0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCB0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB0() }); // Add the pressureCoefficientPTCB1 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCB1"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB1() }); // Add the pressureCoefficientPTCB2 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCB2"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB2() }); // Add the pressureCoefficientPTEMPA0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTEMPA0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA0() }); // Add the pressureCoefficientPTEMPA1 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTEMPA1"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA1() }); // Add the pressureCoefficientPTEMPA2 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTEMPA2"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA2() }); // Add the pressureOffsetCoefficient data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureOffsetCoefficient"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureOffsetCoefficient() }); // Insert the file into the channel map. this.channelIndex = this.rbnbChannelMap.Add(this.rbnbChannelName); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.dataFileString); this.channelIndex = this.rbnbChannelMap.Add("ASCIICastData"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.castFileString); } // Add in the matrix data row to the map here List<String> variableNames = ctdParser.getDataVariableNames(); List<String> variableUnits = ctdParser.getDataVariableUnits(); // iterate through the variable names and add them to // the channel map. for (int variableIndex = 0; variableIndex < variableNames .size(); variableIndex++) { // Add the variable name to the channel map this.channelIndex = this.rbnbChannelMap .Add(variableNames.get(variableIndex)); // The matrix is a double array, so set the data type below this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); // add the data to the map from the [row,column] of the // matrix (row is from the outer for loop) this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { convertedDataMatrix.getEntry(row, variableIndex) }); } // Flush the channel map to the RBNB double sampleTimeAsSecondsSinceEpoch = (double) (this.sampleDateTime .getTimeInMillis() / 1000); this.rbnbChannelMap.PutTime(sampleTimeAsSecondsSinceEpoch, 0d); getSource().Flush(this.rbnbChannelMap); logger.info("Flushed data to the DataTurbine."); this.rbnbChannelMap.Clear(); // samples are taken 4x per second, so increment the // sample time by 250 milliseconds for the next insert this.sampleDateTime.add(Calendar.MILLISECOND, 250); } // end for loop } // end if !failed } catch (Exception e) { logger.debug("Failed to parse the CTD data file: " + e.getMessage()); } // there are no more files to read. close the Tx session. this.command = this.CLOSE_TRANSFER_SESSION_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); // clean up resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 10; break; } } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 8: // the number of blocks string should end in \r if (byteOne == 0x0D) { logger.debug("Received the number of blocks result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the number of blocks string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Number of bytes reported: " + resultString.trim()); int numberOfBlocksIndex = resultString.indexOf(this.BLOCKSIZE_PREFIX); // If 'BLOCKSIZE=' is not found, set the index to 0 if (numberOfBlocksIndex == -1) { numberOfBlocksIndex = 0; } resultString = resultString.substring( (numberOfBlocksIndex + (this.BLOCKSIZE_PREFIX).length()), resultString.length()); // convert the string to an integer try { this.numberOfBlocks = new Integer(resultString.trim()).intValue(); logger.debug("Number of bytes to download: " + this.numberOfBlocks); } catch (java.lang.NumberFormatException nfe) { failed = true; nfe.printStackTrace(); logger.debug("Failed to convert returned string value " + "to an integer value. The returned string is: " + this.numberOfBlocks); } // test to see if the GNB command returns DONE\r if (!(resultString.indexOf(this.TRANSFER_COMPLETE) > 0)) { // there are bytes to transfer. send the transfer command this.command = this.TRANSFER_BLOCKS_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); //resultBuffer.clear(); dont clear the buffer this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 9; break; } else { // there are no more bytes to transfer. // Decompress the file, which is under zlib compression. Inflater inflater = new Inflater(); inflater.setInput(resultBuffer.array()); byte[] output = new byte[resultBuffer.capacity()]; int numDecompressed = inflater.inflate(output); // set the appropriate string variable if (this.fileNameToDownload.indexOf(DATA_FILE_PREFIX) > 0) { this.dataFileString = new String(output); //report the file contents to the log logger.debug("File " + this.fileNameToDownload + ": "); logger.debug(this.dataFileString); } else { this.castFileString = new String(output); //report the file contents to the log logger.debug("File " + this.fileNameToDownload + ": "); logger.debug(this.castFileString); } // Ask for the next file. this.command = this.FILENAME_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); //resultBuffer.clear(); dont clear the buffer this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 7; //back to the file name state break; } } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 9: // transfer up to the reported number of bytes if (this.resultByteCount == this.numberOfBlocks) { // we have downloaded the reported bytes. get the next section. // get the number of blocks to transfer this.command = this.NUMBER_OF_BLOCKS_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); //resultBuffer.clear(); this.resultByteCount = 0; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 8; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 10: // the response from the modem should end in BYE\r // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x45 && byteThree == 0x59 && byteFour == 0x42) { logger.debug("Received the BYE command."); // continue to disconnect. send the escape sequence this.command = this.ESCAPE_SEQUENCE_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); resultBuffer.clear(); this.resultByteCount = 0; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 11; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 11: // the response from the modem should end in OK\r\n // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x0A && byteThree == 0x4B && byteFour == 0x4F) { // now hang up. this.command = this.MODEM_COMMAND_PREFIX + this.HANGUP_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); resultBuffer.clear(); this.resultByteCount = 0; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 12; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 12: // the response from the modem should end in OK\r\n // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x0A && byteThree == 0x4B && byteFour == 0x4F) { // we are done. re-test if is network registered this.command = this.MODEM_COMMAND_PREFIX + this.REGISTRATION_STATUS_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); resultBuffer.clear(); this.resultByteCount = 0; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 0; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } } // end switch statement // shift the bytes in the FIFO window byteFour = byteThree; byteThree = byteTwo; byteTwo = byteOne; } //end while (more unread bytes) // prepare the buffer to read in more bytes from the stream buffer.compact(); } // end while (more socketChannel bytes to read) socketChannel.close(); } catch (IOException e) { // handle exceptions // In the event of an i/o exception, log the exception, and allow execute() // to return false, which will prompt a retry. failed = true; e.printStackTrace(); return !failed; } catch (java.lang.InterruptedException ine) { failed = true; ine.printStackTrace(); return !failed; } catch (java.util.zip.DataFormatException dfe) { failed = true; dfe.printStackTrace(); return !failed; } return !failed; }
From source file:com.sonicle.webtop.mail.Service.java
public static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { // prepare the buffer to be drained buffer.flip();//from ww w . j av a 2s .c om // write to the channel, may block dest.write(buffer); // If partial transfer, shift remainder down // If buffer is empty, same as doing clear() buffer.compact(); } // EOF will leave buffer in fill state buffer.flip(); // make sure the buffer is fully drained. while (buffer.hasRemaining()) { dest.write(buffer); } }
From source file:edu.hawaii.soest.kilonalu.ctd.CTDSource.java
/** * A method that executes the streaming of data from the source to the RBNB * server after all configuration of settings, connections to hosts, and * thread initiatizing occurs. This method contains the detailed code for * streaming the data and interpreting the stream. *///from w w w . ja v a2 s . co m protected boolean execute() { logger.debug("CTDSource.execute() called."); // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; // test the connection type if (this.connectionType.equals("serial")) { // create a serial connection to the local serial port this.channel = getSerialConnection(); } else if (this.connectionType.equals("socket")) { // otherwise create a TCP or UDP socket connection to the remote host this.channel = getSocketConnection(); } else { logger.info("There was an error establishing either a serial or " + "socket connection to the instrument. Please be sure " + "the connection type is set to either 'serial' or 'socket'."); return false; } // while data are being sent, read them into the buffer try { // create four byte placeholders used to evaluate up to a four-byte // window. The FIFO layout looks like: // ------------------------- // in ---> | One | Two |Three|Four | ---> out // ------------------------- byte byteOne = 0x00, // set initial placeholder values byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00; // Create a buffer that will store the sample bytes as they are read ByteBuffer sampleBuffer = ByteBuffer.allocate(getBufferSize()); // Declare sample variables to be used in the response parsing byte[] sampleArray; // create a byte buffer to store bytes from the TCP stream ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize()); // add a channel of data that will be pushed to the server. // Each sample will be sent to the Data Turbine as an rbnb frame. ChannelMap rbnbChannelMap = new ChannelMap(); // while there are bytes to read from the channel ... while (this.channel.read(buffer) != -1 || buffer.position() > 0) { // prepare the buffer for reading buffer.flip(); // while there are unread bytes in the ByteBuffer while (buffer.hasRemaining()) { byteOne = buffer.get(); logger.debug("b1: " + new String(Hex.encodeHex((new byte[] { byteOne }))) + "\t" + "b2: " + new String(Hex.encodeHex((new byte[] { byteTwo }))) + "\t" + "b3: " + new String(Hex.encodeHex((new byte[] { byteThree }))) + "\t" + "b4: " + new String(Hex.encodeHex((new byte[] { byteFour }))) + "\t" + "sample pos: " + sampleBuffer.position() + "\t" + "sample rem: " + sampleBuffer.remaining() + "\t" + "sample cnt: " + sampleByteCount + "\t" + "buffer pos: " + buffer.position() + "\t" + "buffer rem: " + buffer.remaining() + "\t" + "state: " + this.state); // Use a State Machine to process the byte stream. // Start building an rbnb frame for the entire sample, first by // inserting a timestamp into the channelMap. This time is merely // the time of insert into the data turbine, not the time of // observations of the measurements. That time should be parsed out // of the sample in the Sink client code switch (this.state) { case 0: // wake up the instrument // check for instrument metadata fields if (this.enableSendCommands && !this.hasMetadata) { // wake the instrument with an initial '\r\n' command this.command = this.commandSuffix; this.sentCommand = queryInstrument(this.command); this.sentCommand = queryInstrument(this.command); streamingThread.sleep(2000); this.state = 1; break; } else { this.state = 11; break; } case 1: // stop the sampling // be sure the instrument woke (look for S> prompt) //if (byteOne == 0x3E && byteTwo == 0x53 ) { // // sampleByteCount = 0; // sampleBuffer.clear(); // // // send the stop sampling command this.command = this.commandPrefix + this.stopSamplingCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); sampleBuffer.clear(); sampleByteCount = 0; this.state = 2; break; //} else { // // handle instrument hardware response // sampleByteCount++; // add the last byte found to the count // // // add the last byte found to the sample buffer // if ( sampleBuffer.remaining() > 0 ) { // sampleBuffer.put(byteOne); // // } else { // sampleBuffer.compact(); // sampleBuffer.put(byteOne); // // } // // break; // continue reading bytes // //} case 2: // based on outputType, get metadata from the instrument // the response should end in <Executed/> if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { sampleBuffer.clear(); sampleByteCount = 0; this.samplingIsStopped = true; // for newer firmware CTDs, use xml-based query commands if (getOutputType().equals("xml")) { // create the CTD parser instance used to parse CTD output this.ctdParser = new CTDParser(); this.state = 3; break; // otherwise, use text-based query commands } else if (getOutputType().equals("text")) { this.state = 12; // process DS and DCal commands break; } else { logger.info("The CTD output type is not recognized. " + "Please set the output type to either " + "'xml' or 'text'."); failed = true; this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); return !failed; } } // disconnect from the RBNB if (isConnected()) { disconnect(); } return !failed; } } else { // handle instrument hardware response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } break; // continue reading bytes } case 3: // get the instrument status metadata if (!this.ctdParser.getHasStatusMetadata()) { this.command = this.commandPrefix + this.getStatusCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 4; break; } else { // get the configuration metadata this.command = this.commandPrefix + this.getConfigurationCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 5; break; } case 4: // handle instrument status response // command response ends with <Executed/> (so find: ed/>) if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { // handle instrument status response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // set the CTD metadata int executedIndex = this.responseString.indexOf("<Executed/>"); this.responseString = this.responseString.substring(0, executedIndex - 1); this.ctdParser.setMetadata(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // then get the instrument configuration metadata if (!this.ctdParser.getHasConfigurationMetadata()) { this.command = this.commandPrefix + this.getConfigurationCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 5; break; } else { // get the calibration metadata this.command = this.commandPrefix + this.getCalibrationCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 6; break; } } else { break; // continue reading bytes } case 5: // handle the instrument configuration metadata // command response ends with <Executed/> (so find: ed/>) if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { // handle instrument configration response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // set the CTD metadata int executedIndex = this.responseString.indexOf("<Executed/>"); this.responseString = this.responseString.substring(0, executedIndex - 1); this.ctdParser.setMetadata(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // then get the instrument calibration metadata if (!this.ctdParser.getHasCalibrationMetadata()) { this.command = this.commandPrefix + this.getCalibrationCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 6; break; } else { this.command = this.commandPrefix + this.getEventsCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 7; break; } } else { break; // continue reading bytes } case 6: // handle the instrument calibration metadata // command response ends with <Executed/> (so find: ed/>) if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { // handle instrument calibration response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // set the CTD metadata int executedIndex = this.responseString.indexOf("<Executed/>"); this.responseString = this.responseString.substring(0, executedIndex - 1); this.ctdParser.setMetadata(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // then get the instrument event metadata if (!this.ctdParser.getHasEventMetadata()) { this.command = this.commandPrefix + this.getEventsCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 7; break; } else { this.command = this.commandPrefix + this.getHardwareCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 8; break; } } else { break; // continue reading bytes } case 7: // handle instrument event metadata // command response ends with <Executed/> (so find: ed/>) if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { // handle instrument events response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // set the CTD metadata int executedIndex = this.responseString.indexOf("<Executed/>"); this.responseString = this.responseString.substring(0, executedIndex - 1); this.ctdParser.setMetadata(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // then get the instrument hardware metadata if (!this.ctdParser.getHasHardwareMetadata()) { this.command = this.commandPrefix + this.getHardwareCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 8; break; } else { this.state = 9; break; } } else { break; // continue reading bytes } case 8: // handle the instrument hardware response // command response ends with <Executed/> (so find: ed/>) if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { // handle instrument hardware response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // set the CTD metadata int executedIndex = this.responseString.indexOf("<Executed/>"); this.responseString = this.responseString.substring(0, executedIndex - 1); this.ctdParser.setMetadata(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // sync the clock if it is not synced if (!this.clockIsSynced) { this.state = 9; break; } else { this.state = 10; break; } } else { break; // continue reading bytes } case 9: // set the instrument clock // is sampling stopped? if (!this.samplingIsStopped) { // wake the instrument with an initial '\r\n' command this.command = this.commandSuffix; this.sentCommand = queryInstrument(this.command); streamingThread.sleep(2000); // then stop the sampling this.command = this.commandPrefix + this.stopSamplingCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); this.samplingIsStopped = true; } // now set the clock if (this.sentCommand) { this.clockSyncDate = new Date(); DATE_FORMAT.setTimeZone(TZ); String dateAsString = DATE_FORMAT.format(this.clockSyncDate); this.command = this.commandPrefix + this.setDateTimeCommand + dateAsString + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.clockIsSynced = true; logger.info("The instrument clock has bee synced at " + this.clockSyncDate.toString()); this.state = 10; break; } else { break; // try the clock sync again due to failure } case 10: // restart the instrument sampling if (this.samplingIsStopped) { this.hasMetadata = true; this.command = this.commandPrefix + this.startSamplingCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); if (this.sentCommand) { this.state = 11; break; } else { break; // try starting the sampling again due to failure } } else { break; } case 11: // read bytes to the next EOL characters // sample line is terminated by \r\n // note bytes are in reverse order in the FIFO window if (byteOne == 0x0A && byteTwo == 0x0D) { sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract just the length of the sample bytes out of the // sample buffer, and place it in the channel map as a // byte array. Then, send it to the data turbine. sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // test if the sample is not just an instrument message if (this.responseString.matches("^# [0-9].*\r\n") || this.responseString.matches("^# [0-9].*\r\n") || this.responseString.matches("^ [0-9].*\r\n")) { // add the data observations string to the CTDParser object // and populate the CTDParser data fields //this.ctdParser.setData(this.responseString); //this.ctdParser.parse(); // build the channel map with all of the data and metadata channels: int channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutTimeAuto("server"); // add the ASCII sample data field rbnbChannelMap.PutDataAsString(channelIndex, this.responseString); // add other metadata and data fields to the map if metadata was collected if (this.hasMetadata && this.ctdParser != null) { // add the samplingMode field data channelIndex = rbnbChannelMap.Add("samplingMode"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getSamplingMode()); // String // add the temperatureSerialNumber field data channelIndex = rbnbChannelMap.Add("temperatureSerialNumber"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getTemperatureSerialNumber()); // String // add the conductivitySerialNumber field data channelIndex = rbnbChannelMap.Add("conductivitySerialNumber"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getConductivitySerialNumber()); // String // add the mainBatteryVoltage field data channelIndex = rbnbChannelMap.Add("mainBatteryVoltage"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getMainBatteryVoltage() }); // double // add the lithiumBatteryVoltage field data channelIndex = rbnbChannelMap.Add("lithiumBatteryVoltage"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getLithiumBatteryVoltage() }); // double // add the operatingCurrent field data channelIndex = rbnbChannelMap.Add("operatingCurrent"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getOperatingCurrent() }); // double // add the pumpCurrent field data channelIndex = rbnbChannelMap.Add("pumpCurrent"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPumpCurrent() }); // double // add the channels01ExternalCurrent field data channelIndex = rbnbChannelMap.Add("channels01ExternalCurrent"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getChannels01ExternalCurrent() }); // double // add the channels23ExternalCurrent field data channelIndex = rbnbChannelMap.Add("channels23ExternalCurrent"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getChannels23ExternalCurrent() }); // double // add the loggingStatus field data channelIndex = rbnbChannelMap.Add("loggingStatus"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getLoggingStatus()); // String // add the numberOfScansToAverage field data channelIndex = rbnbChannelMap.Add("numberOfScansToAverage"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getNumberOfScansToAverage() }); // int // add the numberOfSamples field data channelIndex = rbnbChannelMap.Add("numberOfSamples"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getNumberOfSamples() }); // int // add the numberOfAvailableSamples field data channelIndex = rbnbChannelMap.Add("numberOfAvailableSamples"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getNumberOfAvailableSamples() }); // int // add the sampleInterval field data channelIndex = rbnbChannelMap.Add("sampleInterval"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getSampleInterval() }); // int // add the measurementsPerSample field data channelIndex = rbnbChannelMap.Add("measurementsPerSample"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getMeasurementsPerSample() }); // int // add the transmitRealtime field data channelIndex = rbnbChannelMap.Add("transmitRealtime"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getTransmitRealtime()); // String // add the numberOfCasts field data channelIndex = rbnbChannelMap.Add("numberOfCasts"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getNumberOfCasts() }); // int // add the minimumConductivityFrequency field data channelIndex = rbnbChannelMap.Add("minimumConductivityFrequency"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getMinimumConductivityFrequency() }); // int // add the pumpDelay field data channelIndex = rbnbChannelMap.Add("pumpDelay"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getPumpDelay() }); // int // add the automaticLogging field data channelIndex = rbnbChannelMap.Add("automaticLogging"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getAutomaticLogging()); // String // add the ignoreMagneticSwitch field data channelIndex = rbnbChannelMap.Add("ignoreMagneticSwitch"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getIgnoreMagneticSwitch()); // String // add the batteryType field data channelIndex = rbnbChannelMap.Add("batteryType"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getBatteryType()); // String // add the batteryCutoff field data channelIndex = rbnbChannelMap.Add("batteryCutoff"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getBatteryCutoff()); // String // add the pressureSensorType field data channelIndex = rbnbChannelMap.Add("pressureSensorType"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getPressureSensorType()); // String // add the pressureSensorRange field data channelIndex = rbnbChannelMap.Add("pressureSensorRange"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getPressureSensorRange()); // String // add the sbe38TemperatureSensor field data channelIndex = rbnbChannelMap.Add("sbe38TemperatureSensor"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getSbe38TemperatureSensor()); // String // add the gasTensionDevice field data channelIndex = rbnbChannelMap.Add("gasTensionDevice"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getGasTensionDevice()); // String // add the externalVoltageChannelZero field data channelIndex = rbnbChannelMap.Add("externalVoltageChannelZero"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getExternalVoltageChannelZero()); // String // add the externalVoltageChannelOne field data channelIndex = rbnbChannelMap.Add("externalVoltageChannelOne"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getExternalVoltageChannelOne()); // String // add the externalVoltageChannelTwo field data channelIndex = rbnbChannelMap.Add("externalVoltageChannelTwo"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getExternalVoltageChannelTwo()); // String // add the externalVoltageChannelThree field data channelIndex = rbnbChannelMap.Add("externalVoltageChannelThree"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getExternalVoltageChannelThree()); // String // add the echoCommands field data channelIndex = rbnbChannelMap.Add("echoCommands"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getEchoCommands()); // String // add the outputFormat field data channelIndex = rbnbChannelMap.Add("outputFormat"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getOutputFormat()); // String // add the temperatureCalibrationDate field data channelIndex = rbnbChannelMap.Add("temperatureCalibrationDate"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getTemperatureCalibrationDate()); // String // add the temperatureCoefficientTA0 field data channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA0() }); // double // add the temperatureCoefficientTA1 field data channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA1"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA1() }); // double // add the temperatureCoefficientTA2 field data channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA2"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA2() }); // double // add the temperatureCoefficientTA3 field data channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA3"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA3() }); // double // add the temperatureOffsetCoefficient field data channelIndex = rbnbChannelMap.Add("temperatureOffsetCoefficient"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getTemperatureOffsetCoefficient() }); // double // add the conductivityCalibrationDate field data channelIndex = rbnbChannelMap.Add("conductivityCalibrationDate"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getConductivityCalibrationDate()); // String // add the conductivityCoefficientG field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientG"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientG() }); // double // add the conductivityCoefficientH field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientH"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientH() }); // double // add the conductivityCoefficientI field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientI"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientI() }); // double // add the conductivityCoefficientJ field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientJ"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientJ() }); // double // add the conductivityCoefficientCF0 field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientCF0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCF0() }); // double // add the conductivityCoefficientCPCOR field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientCPCOR"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCPCOR() }); // double // add the conductivityCoefficientCTCOR field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientCTCOR"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCTCOR() }); // double // add the conductivityCoefficientCSLOPE field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientCSLOPE"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCSLOPE() }); // double // add the pressureSerialNumber field data channelIndex = rbnbChannelMap.Add("pressureSerialNumber"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getPressureSerialNumber()); // String // add the pressureCoefficientPA0 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPA0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA0() }); // double // add the pressureCoefficientPA1 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPA1"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA1() }); // double // add the pressureCoefficientPA2 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPA2"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA2() }); // double // add the pressureCoefficientPTCA0 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCA0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA0() }); // double // add the pressureCoefficientPTCA1 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCA1"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA1() }); // double // add the pressureCoefficientPTCA2 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCA2"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA2() }); // double // add the pressureCoefficientPTCB0 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCB0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB0() }); // double // add the pressureCoefficientPTCB1 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCB1"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB1() }); // double // add the pressureCoefficientPTCB2 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCB2"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB2() }); // double // add the pressureCoefficientPTEMPA0 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTEMPA0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA0() }); // double // add the pressureCoefficientPTEMPA1 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTEMPA1"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA1() }); // double // add the pressureCoefficientPTEMPA2 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTEMPA2"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA2() }); // double // add the pressureOffsetCoefficient field data channelIndex = rbnbChannelMap.Add("pressureOffsetCoefficient"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureOffsetCoefficient() }); // double } // send the sample to the data turbine getSource().Flush(rbnbChannelMap); logger.info("Sent sample to the DataTurbine: " + this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; channelIndex = 0; rbnbChannelMap.Clear(); logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap."); // check if the clock needs syncing (daily) if (this.enableSendCommands) { // get the current datetime Calendar currentCalendar = Calendar.getInstance(); currentCalendar.setTime(new Date()); Calendar lastSyncedCalendar = Calendar.getInstance(); lastSyncedCalendar.setTime(this.clockSyncDate); // round the dates to the day currentCalendar.clear(Calendar.MILLISECOND); currentCalendar.clear(Calendar.SECOND); currentCalendar.clear(Calendar.MINUTE); currentCalendar.clear(Calendar.HOUR); lastSyncedCalendar.clear(Calendar.MILLISECOND); lastSyncedCalendar.clear(Calendar.SECOND); lastSyncedCalendar.clear(Calendar.MINUTE); lastSyncedCalendar.clear(Calendar.HOUR); // sync the clock daily if (currentCalendar.before(lastSyncedCalendar)) { this.state = 8; } } // otherwise stay in state = 11 break; // the sample looks more like an instrument message, don't flush } else { logger.info("This string does not look like a sample, " + "and was not sent to the DataTurbine."); logger.info("Skipping sample: " + this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; //rbnbChannelMap.Clear(); logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap."); this.state = 11; break; } } else { // not 0x0A0D // still in the middle of the sample, keep adding bytes sampleByteCount++; // add each byte found if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); logger.debug("Compacting sampleBuffer ..."); sampleBuffer.put(byteOne); } break; } // end if for 0x0A0D EOL case 12: // alternatively use legacy DS and DCal commands if (this.enableSendCommands) { // start by getting the DS status output this.command = this.commandPrefix + this.displayStatusCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 13; break; } else { this.state = 0; break; } case 13: // handle the DS command response // command should end with the S> prompt if (byteOne == 0x7E && byteTwo == 0x53) { // handle instrument status response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount - 2]; //subtract "S>" sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // then get the instrument calibration metadata this.command = this.commandPrefix + this.displayCalibrationCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 14; break; } else { break; // continue reading bytes } case 14: // handle the DCal command response // command should end with the S> prompt if (byteOne == 0x7E && byteTwo == 0x53) { // handle instrument status response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount - 2]; // subtract "S>" sampleBuffer.flip(); sampleBuffer.get(sampleArray); // append the DCal output to the DS output this.responseString = this.responseString.concat(new String(sampleArray, "US-ASCII")); // and add the data delimiter expected in the CTDParser this.responseString = this.responseString.concat("*END*\r\n\r\n"); // build the CTDParser object with legacy DS and DCal metadata this.ctdParser = new CTDParser(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; this.state = 9; // set the clock and start sampling break; } else { break; // continue reading bytes } } // end switch statement // shift the bytes in the FIFO window byteFour = byteThree; byteThree = byteTwo; byteTwo = byteOne; } //end while (more unread bytes) // prepare the buffer to read in more bytes from the stream buffer.compact(); } // end while (more channel bytes to read) this.channel.close(); } catch (IOException e) { // handle exceptions // In the event of an i/o exception, log the exception, and allow execute() // to return false, which will prompt a retry. failed = true; this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); } } // disconnect from the RBNB if (isConnected()) { disconnect(); } e.printStackTrace(); return !failed; } catch (InterruptedException intde) { // in the event that the streamingThread is interrupted failed = true; this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); } } // disconnect from the RBNB if (isConnected()) { disconnect(); } intde.printStackTrace(); return !failed; } catch (SAPIException sapie) { // In the event of an RBNB communication exception, log the exception, // and allow execute() to return false, which will prompt a retry. //this.channel.close(); failed = true; this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); } } // disconnect from the RBNB if (isConnected()) { disconnect(); } sapie.printStackTrace(); return !failed; } catch (ParseException pe) { failed = true; this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); } } // disconnect from the RBNB if (isConnected()) { disconnect(); } logger.info("There was an error parsing the metadata response. " + "The error message was: " + pe.getMessage()); return !failed; } finally { this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); } } } return !failed; }
From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxSource.java
/** * A method that executes the streaming of data from the source to the RBNB * server after all configuration of settings, connections to hosts, and * thread initiatizing occurs. This method contains the detailed code for * streaming the data and interpreting the stream. *///from w w w . j a va2 s. c o m protected boolean execute() { logger.debug("DavisWxSource.execute() called."); // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; // while data are being sent, read them into the buffer try { this.socketChannel = getSocketConnection(); // create four byte placeholders used to evaluate up to a four-byte // window. The FIFO layout looks like: // ------------------------- // in ---> | One | Two |Three|Four | ---> out // ------------------------- byte byteOne = 0x00, // set initial placeholder values byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00; // Create a buffer that will store the sample bytes as they are read ByteBuffer sampleBuffer = ByteBuffer.allocate(getBufferSize()); // create a byte buffer to store bytes from the TCP stream ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize()); // add a channel of data that will be pushed to the server. // Each sample will be sent to the Data Turbine as an rbnb frame. ChannelMap rbnbChannelMap = new ChannelMap(); int channelIndex = 0; // add the raw binary LOOP packet data //channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); //rbnbChannelMap.PutUserInfo(channelIndex, "units=none"); // add the barTrendAsString field data channelIndex = rbnbChannelMap.Add("barTrendAsString"); // Falling Slowly rbnbChannelMap.PutUserInfo(channelIndex, "units=none"); // add the barometer field data channelIndex = rbnbChannelMap.Add("barometer"); // 29.9 rbnbChannelMap.PutUserInfo(channelIndex, "units=inch Hg"); // add the insideTemperature field data channelIndex = rbnbChannelMap.Add("insideTemperature"); // 83.9 rbnbChannelMap.PutUserInfo(channelIndex, "units=degrees F"); // add the insideHumidity field data channelIndex = rbnbChannelMap.Add("insideHumidity"); // 51 rbnbChannelMap.PutUserInfo(channelIndex, "units=percent"); // add the outsideTemperature field data channelIndex = rbnbChannelMap.Add("outsideTemperature"); // 76.7 rbnbChannelMap.PutUserInfo(channelIndex, "units=degrees F"); // add the windSpeed field data channelIndex = rbnbChannelMap.Add("windSpeed"); // 5 rbnbChannelMap.PutUserInfo(channelIndex, "units=mph"); // add the tenMinuteAverageWindSpeed field data channelIndex = rbnbChannelMap.Add("tenMinuteAverageWindSpeed"); // 4 rbnbChannelMap.PutUserInfo(channelIndex, "units=mph"); // add the windDirection field data channelIndex = rbnbChannelMap.Add("windDirection"); // 80 rbnbChannelMap.PutUserInfo(channelIndex, "units=degrees"); // add the outsideHumidity field data channelIndex = rbnbChannelMap.Add("outsideHumidity"); // 73 rbnbChannelMap.PutUserInfo(channelIndex, "units=percent"); // add the rainRate field data channelIndex = rbnbChannelMap.Add("rainRate"); // 0.0 rbnbChannelMap.PutUserInfo(channelIndex, "units=inch/hour"); // add the uvRadiation field data channelIndex = rbnbChannelMap.Add("uvRadiation"); // 0 rbnbChannelMap.PutUserInfo(channelIndex, "UV index"); // add the solarRadiation field data channelIndex = rbnbChannelMap.Add("solarRadiation"); // 0.0 rbnbChannelMap.PutUserInfo(channelIndex, "watt/m^2"); // add the stormRain field data channelIndex = rbnbChannelMap.Add("stormRain"); // 0.0 rbnbChannelMap.PutUserInfo(channelIndex, "inch"); // add the currentStormStartDate field data channelIndex = rbnbChannelMap.Add("currentStormStartDate"); // -1--1-1999 rbnbChannelMap.PutUserInfo(channelIndex, "units=none"); // add the dailyRain field data channelIndex = rbnbChannelMap.Add("dailyRain"); // 0.0 rbnbChannelMap.PutUserInfo(channelIndex, "units=inch"); // add the monthlyRain field data channelIndex = rbnbChannelMap.Add("monthlyRain"); // 0.0 rbnbChannelMap.PutUserInfo(channelIndex, "units=inch"); // add the yearlyRain field data channelIndex = rbnbChannelMap.Add("yearlyRain"); // 15.0 rbnbChannelMap.PutUserInfo(channelIndex, "units=inch"); // add the dailyEvapoTranspiration field data channelIndex = rbnbChannelMap.Add("dailyEvapoTranspiration"); // 0.0 rbnbChannelMap.PutUserInfo(channelIndex, "units=inch"); // add the monthlyEvapoTranspiration field data channelIndex = rbnbChannelMap.Add("monthlyEvapoTranspiration"); // 0.0 rbnbChannelMap.PutUserInfo(channelIndex, "units=inch"); // add the yearlyEvapoTranspiration field data channelIndex = rbnbChannelMap.Add("yearlyEvapoTranspiration"); // 93.0 rbnbChannelMap.PutUserInfo(channelIndex, "units=inch"); // add the transmitterBatteryStatus field data channelIndex = rbnbChannelMap.Add("transmitterBatteryStatus"); // 0 rbnbChannelMap.PutUserInfo(channelIndex, "units=none"); // add the consoleBatteryVoltage field data channelIndex = rbnbChannelMap.Add("consoleBatteryVoltage"); // 4.681640625 rbnbChannelMap.PutUserInfo(channelIndex, "units=volts"); // add the forecastAsString field data channelIndex = rbnbChannelMap.Add("forecastAsString"); // Partially Cloudy rbnbChannelMap.PutUserInfo(channelIndex, "units=none"); // add the forecastRuleNumberAsString field data //channelIndex = rbnbChannelMap.Add("forecastRuleNumberAsString"); // Increasing clouds with little temperature change. //rbnbChannelMap.PutUserInfo(channelIndex, "units=none"); // add the timeOfSunrise field data channelIndex = rbnbChannelMap.Add("timeOfSunrise"); // 05:49 rbnbChannelMap.PutUserInfo(channelIndex, "units=none"); // add the timeOfSunset field data channelIndex = rbnbChannelMap.Add("timeOfSunset"); // 19:11 rbnbChannelMap.PutUserInfo(channelIndex, "units=none"); channelIndex = rbnbChannelMap.Add("DecimalASCIISampleData"); // sample data as ASCII rbnbChannelMap.PutUserInfo(channelIndex, "units=none"); // register the channel map of variables and units with the DataTurbine getSource().Register(rbnbChannelMap); // reset variables for use with the incoming data rbnbChannelMap.Clear(); channelIndex = 0; // wake the instrument with an initial '\n' command this.command = this.commandSuffix; this.sentCommand = queryInstrument(this.command); // allow time for the instrument response streamingThread.sleep(2000); this.command = this.commandPrefix + this.takeSampleCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); // while there are bytes to read from the socket ... while (this.socketChannel.read(buffer) != -1 || buffer.position() > 0) { // prepare the buffer for reading buffer.flip(); // while there are unread bytes in the ByteBuffer while (buffer.hasRemaining()) { byteOne = buffer.get(); //logger.debug("b1: " + new String(Hex.encodeHex((new byte[]{byteOne}))) + "\t" + // "b2: " + new String(Hex.encodeHex((new byte[]{byteTwo}))) + "\t" + // "b3: " + new String(Hex.encodeHex((new byte[]{byteThree}))) + "\t" + // "b4: " + new String(Hex.encodeHex((new byte[]{byteFour}))) + "\t" + // "sample pos: " + sampleBuffer.position() + "\t" + // "sample rem: " + sampleBuffer.remaining() + "\t" + // "sample cnt: " + sampleByteCount + "\t" + // "buffer pos: " + buffer.position() + "\t" + // "buffer rem: " + buffer.remaining() + "\t" + // "state: " + state //); // Use a State Machine to process the byte stream. // Start building an rbnb frame for the entire sample, first by // inserting a timestamp into the channelMap. This time is merely // the time of insert into the data turbine, not the time of // observations of the measurements. That time should be parsed out // of the sample in the Sink client code switch (state) { case 0: // sample line is begun by "ACK L" (the first part of ACK + "LOOP") // note bytes are in reverse order in the FIFO window if (byteOne == 0x4C && byteTwo == 0x06) { sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // we've found the beginning of a sample, move on state = 1; break; } else { break; } case 1: // read the rest of the bytes to the next EOL characters // sample line is terminated by "\n\r" // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x0A) { sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } state = 3; break; } else { // not 0x0A0D // still in the middle of the sample, keep adding bytes sampleByteCount++; // add each byte found if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); logger.debug("Compacting sampleBuffer ..."); sampleBuffer.put(byteOne); } break; } // end if for 0x0A0D EOL case 3: // At this point, we've found the \n\r delimiter, read the first // of 2 CRC bytes sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } state = 4; break; case 4: // At this point, we've found the \n\r delimiter, read the second // of 2 CRC bytes sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } state = 0; // extract just the length of the sample bytes out of the // sample buffer, and place it in the channel map as a // byte array. Then, send it to the data turbine. byte[] sampleArray = new byte[sampleByteCount]; try { sampleBuffer.flip(); sampleBuffer.get(sampleArray); // parse and send the sample to the data turbine this.davisWxParser = new DavisWxParser(sampleBuffer); } catch (java.lang.Exception e) { logger.info( "There was a problem parsing the binary weather LOOP packet. Skipping this sample."); byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; sampleBuffer.clear(); sampleByteCount = 0; rbnbChannelMap.Clear(); break; } // create a character string to store characters from the TCP stream StringBuilder decimalASCIISampleData = new StringBuilder(); rbnbChannelMap.PutTimeAuto("server"); // add the raw binary LOOP packet data //channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); //rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); //rbnbChannelMap.PutDataAsByteArray(channelIndex, sampleArray); // raw binary LOOP packet // add the barTrendAsString field data channelIndex = rbnbChannelMap.Add("barTrendAsString"); // Falling Slowly rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, davisWxParser.getBarTrendAsString()); decimalASCIISampleData.append( String.format("\"%16s\"", (Object) davisWxParser.getBarTrendAsString()) + ", "); // add the packetType field to the ASCII string only decimalASCIISampleData.append( String.format("%1d", (Object) new Integer(davisWxParser.getPacketType())) + ", "); // add the nextRecord field to the ASCII string only decimalASCIISampleData.append( String.format("%04d", (Object) new Integer(davisWxParser.getNextRecord())) + ", "); // add the barometer field data channelIndex = rbnbChannelMap.Add("barometer"); // 29.9 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getBarometer() }); decimalASCIISampleData.append( String.format("%06.4f", (Object) new Float(davisWxParser.getBarometer())) + ", "); // add the insideTemperature field data channelIndex = rbnbChannelMap.Add("insideTemperature"); // 83.9 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getInsideTemperature() }); decimalASCIISampleData.append( String.format("%05.2f", (Object) new Float(davisWxParser.getInsideTemperature())) + ", "); // add the insideHumidity field data channelIndex = rbnbChannelMap.Add("insideHumidity"); // 51 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { davisWxParser.getInsideHumidity() }); decimalASCIISampleData.append( String.format("%03d", (Object) new Integer(davisWxParser.getInsideHumidity())) + ", "); // add the outsideTemperature field data channelIndex = rbnbChannelMap.Add("outsideTemperature"); // 76.7 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getOutsideTemperature() }); decimalASCIISampleData.append( String.format("%05.2f", (Object) new Float(davisWxParser.getOutsideTemperature())) + ", "); // add the windSpeed field data channelIndex = rbnbChannelMap.Add("windSpeed"); // 5 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { davisWxParser.getWindSpeed() }); decimalASCIISampleData.append( String.format("%03d", (Object) new Integer(davisWxParser.getWindSpeed())) + ", "); // add the tenMinuteAverageWindSpeed field data channelIndex = rbnbChannelMap.Add("tenMinuteAverageWindSpeed"); // 4 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { davisWxParser.getTenMinuteAverageWindSpeed() }); decimalASCIISampleData.append(String.format("%03d", (Object) new Integer(davisWxParser.getTenMinuteAverageWindSpeed())) + ", "); // add the windDirection field data channelIndex = rbnbChannelMap.Add("windDirection"); // 80 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { davisWxParser.getWindDirection() }); decimalASCIISampleData.append( String.format("%03d", (Object) new Integer(davisWxParser.getWindDirection())) + ", "); // add the extraTemperature fields as ASCII only float[] extraTemperatures = davisWxParser.getExtraTemperatures(); for (float temperature : extraTemperatures) { decimalASCIISampleData .append(String.format("%05.2f", (Object) new Float(temperature)) + ", "); } // add the soilTemperature fields as ASCII only float[] soilTemperatures = davisWxParser.getSoilTemperatures(); for (float soil : soilTemperatures) { decimalASCIISampleData.append(String.format("%05.2f", (Object) new Float(soil)) + ", "); } // add the leafTemperature fields as ASCII only float[] leafTemperatures = davisWxParser.getLeafTemperatures(); for (float leaf : leafTemperatures) { decimalASCIISampleData.append(String.format("%05.2f", (Object) new Float(leaf)) + ", "); } // add the outsideHumidity field data channelIndex = rbnbChannelMap.Add("outsideHumidity"); // 73 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { davisWxParser.getOutsideHumidity() }); decimalASCIISampleData.append( String.format("%03d", (Object) new Integer(davisWxParser.getOutsideHumidity())) + ", "); // add the rainRate field data channelIndex = rbnbChannelMap.Add("rainRate"); // 0.0 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getRainRate() }); decimalASCIISampleData.append( String.format("%04.2f", (Object) new Float(davisWxParser.getRainRate())) + ", "); // add the uvRadiation field data channelIndex = rbnbChannelMap.Add("uvRadiation"); // 0 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { davisWxParser.getUvRadiation() }); decimalASCIISampleData.append( String.format("%03d", (Object) new Integer(davisWxParser.getUvRadiation())) + ", "); // add the solarRadiation field data channelIndex = rbnbChannelMap.Add("solarRadiation"); // 0.0 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getSolarRadiation() }); decimalASCIISampleData.append( String.format("%04.1f", (Object) new Float(davisWxParser.getSolarRadiation())) + ", "); // add the stormRain field data channelIndex = rbnbChannelMap.Add("stormRain"); // 0.0 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getStormRain() }); decimalASCIISampleData.append( String.format("%04.2f", (Object) new Float(davisWxParser.getStormRain())) + ", "); // add the currentStormStartDate field data channelIndex = rbnbChannelMap.Add("currentStormStartDate"); // -1--1-1999 rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, davisWxParser.getCurrentStormStartDate()); decimalASCIISampleData.append( String.format("%10s", (Object) davisWxParser.getCurrentStormStartDate()) + ", "); // add the dailyRain field data channelIndex = rbnbChannelMap.Add("dailyRain"); // 0.0 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getDailyRain() }); decimalASCIISampleData.append( String.format("%04.2f", (Object) new Float(davisWxParser.getDailyRain())) + ", "); // add the monthlyRain field data channelIndex = rbnbChannelMap.Add("monthlyRain"); // 0.0 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getMonthlyRain() }); decimalASCIISampleData.append( String.format("%04.2f", (Object) new Float(davisWxParser.getMonthlyRain())) + ", "); // add the yearlyRain field data channelIndex = rbnbChannelMap.Add("yearlyRain"); // 15.0 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getYearlyRain() }); decimalASCIISampleData.append( String.format("%04.2f", (Object) new Float(davisWxParser.getYearlyRain())) + ", "); // add the dailyEvapoTranspiration field data channelIndex = rbnbChannelMap.Add("dailyEvapoTranspiration"); // 0.0 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getDailyEvapoTranspiration() }); decimalASCIISampleData.append(String.format("%04.2f", (Object) new Float(davisWxParser.getDailyEvapoTranspiration())) + ", "); // add the monthlyEvapoTranspiration field data channelIndex = rbnbChannelMap.Add("monthlyEvapoTranspiration"); // 0.0 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getMonthlyEvapoTranspiration() }); decimalASCIISampleData.append(String.format("%04.2f", (Object) new Float(davisWxParser.getMonthlyEvapoTranspiration())) + ", "); // add the yearlyEvapoTranspiration field data channelIndex = rbnbChannelMap.Add("yearlyEvapoTranspiration"); // 93.0 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getYearlyEvapoTranspiration() }); decimalASCIISampleData.append(String.format("%04.2f", (Object) new Float(davisWxParser.getYearlyEvapoTranspiration())) + ", "); // add the consoleBatteryVoltage field data channelIndex = rbnbChannelMap.Add("consoleBatteryVoltage"); // 4.681640625 rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getConsoleBatteryVoltage() }); decimalASCIISampleData.append(String.format("%04.2f", (Object) new Float(davisWxParser.getConsoleBatteryVoltage())) + ", "); // add the forecastAsString field data channelIndex = rbnbChannelMap.Add("forecastAsString"); // Partially Cloudy rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, davisWxParser.getForecastAsString()); decimalASCIISampleData.append( String.format("\"%47s\"", (Object) davisWxParser.getForecastAsString()) + ", "); // add the forecastRuleNumberAsString field data as ASCII only decimalASCIISampleData.append( String.format("\"%167s\"", (Object) davisWxParser.getForecastRuleNumberAsString()) + ", "); // add the timeOfSunrise field data channelIndex = rbnbChannelMap.Add("timeOfSunrise"); // 05:49 rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, davisWxParser.getTimeOfSunrise()); decimalASCIISampleData .append(String.format("%5s", (Object) davisWxParser.getTimeOfSunrise()) + ", "); // add the timeOfSunset field data channelIndex = rbnbChannelMap.Add("timeOfSunset"); // 19:11 rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, davisWxParser.getTimeOfSunset()); decimalASCIISampleData .append(String.format("%5s", (Object) davisWxParser.getTimeOfSunset()) + ", "); // then add a timestamp to the end of the sample DATE_FORMAT.setTimeZone(TZ); String sampleDateAsString = DATE_FORMAT.format(new Date()).toString(); decimalASCIISampleData.append(sampleDateAsString); decimalASCIISampleData.append("\n"); // add the ASCII CSV string of selected fields as a channel channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); // 19:11 rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, decimalASCIISampleData.toString()); // finally, send the channel map of data to the DataTurbine getSource().Flush(rbnbChannelMap); String sampleString = new String(Hex.encodeHex(sampleArray)); logger.info("Sample: " + sampleString); logger.debug("barTrendAsString: " + davisWxParser.getBarTrendAsString()); logger.debug("barometer: " + davisWxParser.getBarometer()); logger.debug("insideTemperature: " + davisWxParser.getInsideTemperature()); logger.debug("insideHumidity: " + davisWxParser.getInsideHumidity()); logger.debug("outsideTemperature: " + davisWxParser.getOutsideTemperature()); logger.debug("windSpeed: " + davisWxParser.getWindSpeed()); logger.debug( "tenMinuteAverageWindSpeed: " + davisWxParser.getTenMinuteAverageWindSpeed()); logger.debug("windDirection: " + davisWxParser.getWindDirection()); logger.debug("outsideHumidity: " + davisWxParser.getOutsideHumidity()); logger.debug("rainRate: " + davisWxParser.getRainRate()); logger.debug("uvRadiation: " + davisWxParser.getUvRadiation()); logger.debug("solarRadiation: " + davisWxParser.getSolarRadiation()); logger.debug("stormRain: " + davisWxParser.getStormRain()); logger.debug("currentStormStartDate: " + davisWxParser.getCurrentStormStartDate()); logger.debug("dailyRain: " + davisWxParser.getDailyRain()); logger.debug("monthlyRain: " + davisWxParser.getMonthlyRain()); logger.debug("yearlyRain: " + davisWxParser.getYearlyRain()); logger.debug( "dailyEvapoTranspiration: " + davisWxParser.getDailyEvapoTranspiration()); logger.debug( "monthlyEvapoTranspiration: " + davisWxParser.getMonthlyEvapoTranspiration()); logger.debug( "yearlyEvapoTranspiration: " + davisWxParser.getYearlyEvapoTranspiration()); logger.debug("transmitterBatteryStatus: " + Arrays.toString(davisWxParser.getTransmitterBatteryStatus())); logger.debug("consoleBatteryVoltage: " + davisWxParser.getConsoleBatteryVoltage()); logger.debug("forecastAsString: " + davisWxParser.getForecastAsString()); //logger.debug("forecastRuleNumberAsString: " + davisWxParser.getForecastRuleNumberAsString()); logger.debug("timeOfSunrise: " + davisWxParser.getTimeOfSunrise()); logger.debug("timeOfSunset: " + davisWxParser.getTimeOfSunset()); logger.info(" flushed data to the DataTurbine. "); byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; sampleBuffer.clear(); sampleByteCount = 0; rbnbChannelMap.Clear(); //logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap."); //state = 0; // Once the sample is flushed, take a new sample // allow time for the instrument response streamingThread.sleep(2000); this.command = this.commandPrefix + this.takeSampleCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); } // end switch statement // shift the bytes in the FIFO window byteFour = byteThree; byteThree = byteTwo; byteTwo = byteOne; } //end while (more unread bytes) // prepare the buffer to read in more bytes from the stream buffer.compact(); } // end while (more socket bytes to read) this.socketChannel.close(); } catch (IOException e) { // handle exceptions // In the event of an i/o exception, log the exception, and allow execute() // to return false, which will prompt a retry. failed = true; e.printStackTrace(); return !failed; } catch (SAPIException sapie) { // In the event of an RBNB communication exception, log the exception, // and allow execute() to return false, which will prompt a retry. failed = true; sapie.printStackTrace(); return !failed; } catch (java.lang.InterruptedException ine) { failed = true; ine.printStackTrace(); return !failed; } return !failed; }