List of usage examples for java.nio ByteBuffer remaining
public final int remaining()
From source file:com.mobiperf.measurements.HttpTask.java
/** Runs the HTTP measurement task. Will acquire power lock to ensure wifi is not turned off */ @Override/*from w ww . ja v a2 s . com*/ public MeasurementResult call() throws MeasurementError { int statusCode = HttpTask.DEFAULT_STATUS_CODE; long duration = 0; long originalHeadersLen = 0; long originalBodyLen; String headers = null; ByteBuffer body = ByteBuffer.allocate(HttpTask.MAX_BODY_SIZE_TO_UPLOAD); boolean success = false; String errorMsg = ""; InputStream inputStream = null; // This is not the ideal way of doing things, as we can pick up data // from other processes and be overly cautious in running measurements. // However, taking the packet sizes is completely inaccurate, and it's hard to // come up with a consistent expected value, unlike with DNS RRCTask.PacketMonitor packetmonitor = new RRCTask.PacketMonitor(); packetmonitor.setBySize(); packetmonitor.readCurrentPacketValues(); try { // set the download URL, a URL that points to a file on the Internet // this is the file to be downloaded HttpDesc task = (HttpDesc) this.measurementDesc; String urlStr = task.url; // TODO(Wenjie): Need to set timeout for the HTTP methods httpClient = AndroidHttpClient.newInstance(Util.prepareUserAgent(this.parent)); HttpRequestBase request = null; if (task.method.compareToIgnoreCase("head") == 0) { request = new HttpHead(urlStr); } else if (task.method.compareToIgnoreCase("get") == 0) { request = new HttpGet(urlStr); } else if (task.method.compareToIgnoreCase("post") == 0) { request = new HttpPost(urlStr); HttpPost postRequest = (HttpPost) request; postRequest.setEntity(new StringEntity(task.body)); } else { // Use GET by default request = new HttpGet(urlStr); } if (task.headers != null && task.headers.trim().length() > 0) { for (String headerLine : task.headers.split("\r\n")) { String tokens[] = headerLine.split(":"); if (tokens.length == 2) { request.addHeader(tokens[0], tokens[1]); } else { throw new MeasurementError("Incorrect header line: " + headerLine); } } } byte[] readBuffer = new byte[HttpTask.READ_BUFFER_SIZE]; int readLen; int totalBodyLen = 0; long startTime = System.currentTimeMillis(); HttpResponse response = httpClient.execute(request); /* TODO(Wenjie): HttpClient does not automatically handle the following codes * 301 Moved Permanently. HttpStatus.SC_MOVED_PERMANENTLY * 302 Moved Temporarily. HttpStatus.SC_MOVED_TEMPORARILY * 303 See Other. HttpStatus.SC_SEE_OTHER * 307 Temporary Redirect. HttpStatus.SC_TEMPORARY_REDIRECT * * We may want to fetch instead from the redirected page. */ StatusLine statusLine = response.getStatusLine(); if (statusLine != null) { statusCode = statusLine.getStatusCode(); success = (statusCode == 200); } /* For HttpClient to work properly, we still want to consume the entire response even if * the status code is not 200 */ HttpEntity responseEntity = response.getEntity(); originalBodyLen = responseEntity.getContentLength(); long expectedResponseLen = HttpTask.MAX_HTTP_RESPONSE_SIZE; // getContentLength() returns negative number if body length is unknown if (originalBodyLen > 0) { expectedResponseLen = originalBodyLen; } if (responseEntity != null) { inputStream = responseEntity.getContent(); while ((readLen = inputStream.read(readBuffer)) > 0 && totalBodyLen <= HttpTask.MAX_HTTP_RESPONSE_SIZE) { totalBodyLen += readLen; // Fill in the body to report up to MAX_BODY_SIZE if (body.remaining() > 0) { int putLen = body.remaining() < readLen ? body.remaining() : readLen; body.put(readBuffer, 0, putLen); } this.progress = (int) (100 * totalBodyLen / expectedResponseLen); this.progress = Math.min(Config.MAX_PROGRESS_BAR_VALUE, progress); broadcastProgressForUser(this.progress); } duration = System.currentTimeMillis() - startTime; } Header[] responseHeaders = response.getAllHeaders(); if (responseHeaders != null) { headers = ""; for (Header hdr : responseHeaders) { /* * TODO(Wenjie): There can be preceding and trailing white spaces in * each header field. I cannot find internal methods that return the * number of bytes in a header. The solution here assumes the encoding * is one byte per character. */ originalHeadersLen += hdr.toString().length(); headers += hdr.toString() + "\r\n"; } } PhoneUtils phoneUtils = PhoneUtils.getPhoneUtils(); MeasurementResult result = new MeasurementResult(phoneUtils.getDeviceInfo().deviceId, phoneUtils.getDeviceProperty(), HttpTask.TYPE, System.currentTimeMillis() * 1000, success, this.measurementDesc); result.addResult("code", statusCode); dataConsumed = packetmonitor.getPacketsSentDiff(); if (success) { result.addResult("time_ms", duration); result.addResult("headers_len", originalHeadersLen); result.addResult("body_len", totalBodyLen); result.addResult("headers", headers); result.addResult("body", Base64.encodeToString(body.array(), Base64.DEFAULT)); } Logger.i(MeasurementJsonConvertor.toJsonString(result)); return result; } catch (MalformedURLException e) { errorMsg += e.getMessage() + "\n"; Logger.e(e.getMessage()); } catch (IOException e) { errorMsg += e.getMessage() + "\n"; Logger.e(e.getMessage()); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { Logger.e("Fails to close the input stream from the HTTP response"); } } if (httpClient != null) { httpClient.close(); } } throw new MeasurementError("Cannot get result from HTTP measurement because " + errorMsg); }
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 ww w .j a va 2 s.c o 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:org.apache.cassandra.db.CounterColumnTest.java
@Test public void testReconcile() throws UnknownHostException { IColumn left;// ww w . jav a2s .co m IColumn right; IColumn reconciled; ByteBuffer context; // tombstone + tombstone left = new DeletedColumn(ByteBufferUtil.bytes("x"), 1, 1L); right = new DeletedColumn(ByteBufferUtil.bytes("x"), 2, 2L); assert left.reconcile(right).getMarkedForDeleteAt() == right.getMarkedForDeleteAt(); assert right.reconcile(left).getMarkedForDeleteAt() == right.getMarkedForDeleteAt(); // tombstone > live left = new DeletedColumn(ByteBufferUtil.bytes("x"), 1, 2L); right = new CounterColumn(ByteBufferUtil.bytes("x"), 0L, 1L); assert left.reconcile(right) == left; // tombstone < live last delete left = new DeletedColumn(ByteBufferUtil.bytes("x"), 1, 1L); right = new CounterColumn(ByteBufferUtil.bytes("x"), 0L, 4L, 2L); assert left.reconcile(right) == right; // tombstone == live last delete left = new DeletedColumn(ByteBufferUtil.bytes("x"), 1, 2L); right = new CounterColumn(ByteBufferUtil.bytes("x"), 0L, 4L, 2L); assert left.reconcile(right) == right; // tombstone > live last delete left = new DeletedColumn(ByteBufferUtil.bytes("x"), 1, 4L); right = new CounterColumn(ByteBufferUtil.bytes("x"), 0L, 9L, 1L); reconciled = left.reconcile(right); assert reconciled.name() == right.name(); assert reconciled.value() == right.value(); assert reconciled.timestamp() == right.timestamp(); assert ((CounterColumn) reconciled).timestampOfLastDelete() == left.getMarkedForDeleteAt(); // live < tombstone left = new CounterColumn(ByteBufferUtil.bytes("x"), 0L, 1L); right = new DeletedColumn(ByteBufferUtil.bytes("x"), 1, 2L); assert left.reconcile(right) == right; // live last delete > tombstone left = new CounterColumn(ByteBufferUtil.bytes("x"), 0L, 4L, 2L); right = new DeletedColumn(ByteBufferUtil.bytes("x"), 1, 1L); assert left.reconcile(right) == left; // live last delete == tombstone left = new CounterColumn(ByteBufferUtil.bytes("x"), 0L, 4L, 2L); right = new DeletedColumn(ByteBufferUtil.bytes("x"), 1, 2L); assert left.reconcile(right) == left; // live last delete < tombstone left = new CounterColumn(ByteBufferUtil.bytes("x"), 0L, 9L, 1L); right = new DeletedColumn(ByteBufferUtil.bytes("x"), 1, 4L); reconciled = left.reconcile(right); assert reconciled.name() == left.name(); assert reconciled.value() == left.value(); assert reconciled.timestamp() == left.timestamp(); assert ((CounterColumn) reconciled).timestampOfLastDelete() == right.getMarkedForDeleteAt(); // live < live last delete left = new CounterColumn(ByteBufferUtil.bytes("x"), cc.create(NodeId.fromInt(1), 2L, 3L, false), 1L, Long.MIN_VALUE); right = new CounterColumn(ByteBufferUtil.bytes("x"), cc.create(NodeId.fromInt(1), 1L, 1L, false), 4L, 3L); assert left.reconcile(right) == right; // live last delete > live left = new CounterColumn(ByteBufferUtil.bytes("x"), cc.create(NodeId.fromInt(1), 2L, 3L, false), 6L, 5L); right = new CounterColumn(ByteBufferUtil.bytes("x"), cc.create(NodeId.fromInt(1), 1L, 1L, false), 4L, 3L); assert left.reconcile(right) == left; // live + live left = new CounterColumn(ByteBufferUtil.bytes("x"), cc.create(NodeId.fromInt(1), 1L, 1L, false), 4L, Long.MIN_VALUE); right = new CounterColumn(ByteBufferUtil.bytes("x"), cc.create(NodeId.fromInt(1), 2L, 3L, false), 1L, Long.MIN_VALUE); reconciled = left.reconcile(right); assert reconciled.name().equals(left.name()); assert ((CounterColumn) reconciled).total() == 3L; assert reconciled.timestamp() == 4L; left = reconciled; right = new CounterColumn(ByteBufferUtil.bytes("x"), cc.create(NodeId.fromInt(2), 1L, 5L, false), 2L, Long.MIN_VALUE); reconciled = left.reconcile(right); assert reconciled.name().equals(left.name()); assert ((CounterColumn) reconciled).total() == 8L; assert reconciled.timestamp() == 4L; left = reconciled; right = new CounterColumn(ByteBufferUtil.bytes("x"), cc.create(NodeId.fromInt(2), 2L, 2L, false), 6L, Long.MIN_VALUE); reconciled = left.reconcile(right); assert reconciled.name().equals(left.name()); assert ((CounterColumn) reconciled).total() == 5L; assert reconciled.timestamp() == 6L; context = reconciled.value(); int hd = 2; // header assert hd + 2 * stepLength == context.remaining(); assert Util.equalsNodeId(NodeId.fromInt(1), context, hd + 0 * stepLength); assert 2L == context.getLong(hd + 0 * stepLength + idLength); assert 3L == context.getLong(hd + 0 * stepLength + idLength + clockLength); assert Util.equalsNodeId(NodeId.fromInt(2), context, hd + 1 * stepLength); assert 2L == context.getLong(hd + 1 * stepLength + idLength); assert 2L == context.getLong(hd + 1 * stepLength + idLength + clockLength); assert ((CounterColumn) reconciled).timestampOfLastDelete() == Long.MIN_VALUE; }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
private GBDeviceEvent[] decodeAction(ByteBuffer buf) { buf.order(ByteOrder.LITTLE_ENDIAN); byte command = buf.get(); if (command == NOTIFICATIONACTION_INVOKE) { int id;//from w ww . j a v a2s. com UUID uuid = new UUID(0, 0); if (mFwMajor >= 3) { uuid = getUUID(buf); id = (int) (uuid.getLeastSignificantBits() & 0xffffffffL); } else { id = buf.getInt(); } byte action = buf.get(); if (action >= 0x00 && action <= 0x05) { GBDeviceEventNotificationControl devEvtNotificationControl = new GBDeviceEventNotificationControl(); devEvtNotificationControl.handle = id; String caption = "undefined"; int icon_id = 1; boolean needsAck2x = true; switch (action) { case 0x01: devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.OPEN; caption = "Opened"; icon_id = PebbleIconID.DURING_PHONE_CALL; break; case 0x02: devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.DISMISS; caption = "Dismissed"; icon_id = PebbleIconID.RESULT_DISMISSED; needsAck2x = false; break; case 0x03: devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.DISMISS_ALL; caption = "All dismissed"; icon_id = PebbleIconID.RESULT_DISMISSED; needsAck2x = false; break; case 0x04: devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.MUTE; caption = "Muted"; icon_id = PebbleIconID.RESULT_MUTE; break; case 0x05: case 0x00: boolean failed = true; byte attribute_count = buf.get(); if (attribute_count > 0) { byte attribute = buf.get(); if (attribute == 0x01) { // reply string is in attribute 0x01 short length = buf.getShort(); if (length > 64) length = 64; byte[] reply = new byte[length]; buf.get(reply); devEvtNotificationControl.phoneNumber = null; if (buf.remaining() > 1 && buf.get() == 0x0c) { short phoneNumberLength = buf.getShort(); byte[] phoneNumberBytes = new byte[phoneNumberLength]; buf.get(phoneNumberBytes); devEvtNotificationControl.phoneNumber = new String(phoneNumberBytes); } devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.REPLY; devEvtNotificationControl.reply = new String(reply); caption = "SENT"; icon_id = PebbleIconID.RESULT_SENT; failed = false; } } if (failed) { caption = "FAILED"; icon_id = PebbleIconID.RESULT_FAILED; devEvtNotificationControl = null; // error } break; } GBDeviceEventSendBytes sendBytesAck = null; if (mFwMajor >= 3 || needsAck2x) { sendBytesAck = new GBDeviceEventSendBytes(); if (mFwMajor >= 3) { sendBytesAck.encodedBytes = encodeActionResponse(uuid, icon_id, caption); } else { sendBytesAck.encodedBytes = encodeActionResponse2x(id, action, 6, caption); } } return new GBDeviceEvent[] { sendBytesAck, devEvtNotificationControl }; } LOG.info("unexpected action: " + action); } return null; }
From source file:edu.umass.cs.gigapaxos.paxospackets.RequestPacket.java
/** * The weird constant above is to try to avoid mistakes in the painful (but * totally worth it) byte'ification method below. Using bytes as opposed to * json strings makes a non-trivial difference (~2x over json-smart and >4x * over org.json. So we just chuck json libraries and use our own byte[] * serializer for select packets.//from w ww . j a v a2 s.com * * The serialization overhead really matters most for RequestPacket and * AcceptPacket. Every request, even with batching, must be deserialized by * the coordinator and must be serialized back while sending out the * AcceptPacket. The critical path is the following at a coordinator and is * incurred at least in part even with batching for every request: (1) * receive request, (2) send accept, (3) receive accept_replies, (4) send * commit Accordingly, we use byteification for {@link RequestPacket}, * {@link AcceptPacket}, {@link BatchedAcceptReply} and * {@link BatchedCommit}. * * */ protected byte[] toBytes(boolean instrument) { // return cached value if already present if ((this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT) && this.byteifiedSelf != null && !instrument) return this.byteifiedSelf; // check if we can use byteification at all; if not, use toString() if (!((BYTEIFICATION && IntegerMap.allInt()) || instrument)) { try { if (this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT) return this.byteifiedSelf = this.toString().getBytes(CHARSET); // cache return this.toString().getBytes(CHARSET); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); return null; } } // else byteify try { int exactLength = 0; byte[] array = new byte[this.lengthEstimate()]; ByteBuffer bbuf = ByteBuffer.wrap(array); assert (bbuf.position() == 0); // paxospacket stuff super.toBytes(bbuf); int ppPos = bbuf.position(); // for assertion assert (bbuf.position() == ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get() + SIZEOF_PAXOSPACKET_FIXED) : bbuf.position() + " != " + ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get() + SIZEOF_PAXOSPACKET_FIXED; exactLength += (bbuf.position()); bbuf.putLong(this.requestID); bbuf.put(this.stop ? (byte) 1 : (byte) 0); exactLength += (Long.BYTES + 1); // addresses /* Note: 0 is ambiguous with wildcard address, but that's okay * because an incoming packet will never come with a wildcard * address. */ bbuf.put(this.clientAddress != null ? this.clientAddress.getAddress().getAddress() : new byte[4]); // 0 (not -1) means invalid port bbuf.putShort(this.clientAddress != null ? (short) this.clientAddress.getPort() : 0); /* Note: 0 is an ambiguous wildcard address that could also be a * legitimate value of the listening socket address. If the request * happens to have no listening address, we will end up assuming it * was received on the wildcard address. At worst, the matching for * the corresponding response back to the client can fail. */ bbuf.put(this.listenAddress != null ? this.listenAddress.getAddress().getAddress() : new byte[4]); // 0 (not -1) means invalid port bbuf.putShort(this.listenAddress != null ? (short) this.listenAddress.getPort() : 0); exactLength += 2 * (Integer.BYTES + Short.BYTES); // other non-final fields bbuf.putInt(this.entryReplica); bbuf.putLong(this.entryTime); bbuf.put(this.shouldReturnRequestValue ? (byte) 1 : (byte) 0); bbuf.putInt(this.forwardCount); exactLength += (Integer.BYTES + Long.BYTES + 1 + Integer.BYTES); // digest related fields: broadcasted, digest // whether this request was already broadcasted bbuf.put(this.broadcasted ? (byte) 1 : (byte) 0); exactLength += 1; assert (exactLength == // where parent left us off ppPos + SIZEOF_REQUEST_FIXED // for the three int fields not yet filled - 4 * Integer.BYTES) : exactLength + " != [" + ppPos + " + " + SIZEOF_REQUEST_FIXED + " - " + 4 * Integer.BYTES + "]"; // digest length and digest iteself bbuf.putInt(this.digest != null ? this.digest.length : 0); exactLength += Integer.BYTES; if (this.digest != null) bbuf.put(this.digest); exactLength += (this.digest != null ? this.digest.length : 0); // /////////// end of digest related fields ////////// // highly variable length fields // requestValue byte[] reqValBytes = this.requestValue != null ? this.requestValue.getBytes(CHARSET) : new byte[0]; bbuf.putInt(reqValBytes != null ? reqValBytes.length : 0); bbuf.put(reqValBytes); exactLength += (4 + reqValBytes.length); // responseValue byte[] respValBytes = this.responseValue != null ? this.responseValue.getBytes(CHARSET) : new byte[0]; bbuf.putInt(respValBytes != null ? respValBytes.length : 0); bbuf.put(respValBytes); exactLength += (4 + respValBytes.length); // batched requests batchSize|(length:batchedReqBytes)+ bbuf.putInt(this.batchSize()); exactLength += (4); if (this.batchSize() > 0) for (RequestPacket req : this.batched) { byte[] element = req.toBytes(); bbuf.putInt(element.length); bbuf.put(element); exactLength += (4 + element.length); } // bbuf.array() was a generous allocation byte[] exactBytes = new byte[exactLength]; bbuf.flip(); assert (bbuf.remaining() == exactLength) : bbuf.remaining() + " != " + exactLength; bbuf.get(exactBytes); if (this.getType() == PaxosPacketType.REQUEST) this.byteifiedSelf = exactBytes; return exactBytes; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
From source file:edu.hawaii.soest.kilonalu.ctd.SBE37Source.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 ww .j ava 2 s .com protected boolean execute() { logger.debug("SBE37Source.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()); // create a character string to store characters from the TCP stream StringBuilder responseString = new StringBuilder(); // 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 = rbnbChannelMap.Add(getRBNBChannelName()); // wake the instrument with an initial take sample command this.command = this.commandPrefix + getInstrumentID() + "TS" + this.commandSuffix; this.sentCommand = queryInstrument(this.command); // verify the instrument ID is correct while (getInstrumentID() == null) { // allow time for the instrument response streamingThread.sleep(2000); buffer.clear(); // send the command and update the sentCommand status this.sentCommand = queryInstrument(this.command); // read the response into the buffer. Note that the streamed bytes // are 8-bit, not 16-bit Unicode characters. Use the US-ASCII // encoding instead. while (this.socketChannel.read(buffer) != -1 || buffer.position() > 0) { buffer.flip(); while (buffer.hasRemaining()) { String nextCharacter = new String(new byte[] { buffer.get() }, "US-ASCII"); responseString.append(nextCharacter); } // look for the command line ending if (responseString.toString().indexOf("S>") > 0) { // parse the ID from the idCommand response int idStartIndex = responseString.indexOf("=") + 2; int idStopIndex = responseString.indexOf("=") + 4; String idString = responseString.substring(idStartIndex, idStopIndex); // test that the ID is a valid number and set the instrument ID if ((new Integer(idString)).intValue() > 0) { setInstrumentID(idString); buffer.clear(); logger.debug("Instrument ID is " + getInstrumentID() + "."); break; } else { logger.debug("Instrument ID \"" + idString + "\" was not set."); } } else { break; } buffer.compact(); if (getInstrumentID() != null) { break; } } } // instrumentID is set // allow time for the instrument response streamingThread.sleep(5000); this.command = this.commandPrefix + getInstrumentID() + 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 S> // note bytes are in reverse order in the FIFO window if (byteOne == 0x3E && byteTwo == 0x53) { // 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 S> // note bytes are in reverse order in the FIFO window if (byteOne == 0x3E && byteTwo == 0x53) { 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 (less 2 bytes // to exclude the 'S>' prompt characters) 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 - 2]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); // send the sample to the data turbine rbnbChannelMap.PutTimeAuto("server"); String sampleString = new String(sampleArray, "US-ASCII"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, sampleString); getSource().Flush(rbnbChannelMap); logger.info("Sample: " + sampleString); 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 if (getInstrumentID() != null) { // allow time for the instrument response streamingThread.sleep(2000); this.command = this.commandPrefix + getInstrumentID() + this.takeSampleCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); } } 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 } // 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 ie) { ie.printStackTrace(); } return !failed; }
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 ww w . j ava2 s. 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: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 ww.j av a 2 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; }