Changeset 2540


Ignore:
Timestamp:
Jul 3, 2014, 2:27:20 PM (9 years ago)
Author:
olle
Message:

Refs #615. Lab environment extension updated to handle odd lab sensor output and improve debug logging:

  1. Class\file LabSensorUtil.java in src/net/sf/basedb/labenv/util/ updated:
    a. Public method LabEnvironmentData createLabEnvironmentData(String sensorUrl, String sensorName) updated to check lengths of byte arrays before using data.
    b. Private method String fetchDataFromUrl(String url) updated in debug output with more info concerning current method and more complete stack trace after an exception is thrown (by using ',' instead of '+' before exception output in log.debug() call).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.labenv/trunk/src/net/sf/basedb/labenv/util/LabSensorUtil.java

    r2342 r2540  
    186186        }
    187187        // Get raw variable values from Base64-decoded byte array
    188         int ch1 = (d20ByteArray[26] & 0xFF) | ((d20ByteArray[27] & 0xFF) << 8);
    189         int ch2 = (d20ByteArray[42] & 0xFF) | ((d20ByteArray[43] & 0xFF) << 8);
    190         // Variable nowTime is the number of seconds since 1070-01-01 00:00:00
    191         long nowTime = (d20ByteArray[6] & 0xFF) | ((d20ByteArray[7] & 0xFF) << 8) | ((d20ByteArray[8] & 0xFF) << 16) | ((d20ByteArray[9] & 0xFF) << 24);
    192         // Get variable values from raw variable values
    193         temperature = (ch1 - 1000)/10.0;
    194         humidity = (ch2 - 1000)/10.0;
    195         // Convert nowTime to number of ms since 1070-01-01 00:00:00 before getting dateTime
    196         dateTime = new Date(nowTime*1000);
    197         labEnvironmentData = new LabEnvironmentData(sensorUrl, sensorName, serialNumber, dateTime, temperature, humidity);
     188        if (d20ByteArray.length >= 44)
     189        {         
     190          int ch1 = (d20ByteArray[26] & 0xFF) | ((d20ByteArray[27] & 0xFF) << 8);
     191          int ch2 = (d20ByteArray[42] & 0xFF) | ((d20ByteArray[43] & 0xFF) << 8);
     192          // Variable nowTime is the number of seconds since 1070-01-01 00:00:00
     193          long nowTime = (d20ByteArray[6] & 0xFF) | ((d20ByteArray[7] & 0xFF) << 8) | ((d20ByteArray[8] & 0xFF) << 16) | ((d20ByteArray[9] & 0xFF) << 24);
     194          // Get variable values from raw variable values
     195          temperature = (ch1 - 1000)/10.0;
     196          humidity = (ch2 - 1000)/10.0;
     197          // Convert nowTime to number of ms since 1070-01-01 00:00:00 before getting dateTime
     198          dateTime = new Date(nowTime*1000);
     199          labEnvironmentData = new LabEnvironmentData(sensorUrl, sensorName, serialNumber, dateTime, temperature, humidity);
     200        }
     201        else
     202        {
     203          log.debug("createLabEnvironmentData(" + sensorUrl + ", " + sensorName + "): d20ByteArray.length = " + d20ByteArray.length + " < 44");
     204        }
    198205      }
    199206      catch (ParseException e)
     
    231238      if (statusCode != HttpStatus.SC_OK)
    232239      {
    233         log.debug("Error when trying to execute GetMethod: " + getMethod.getStatusLine());
     240        log.debug("fetchDataFromUrl(" + url + "): Error when trying to execute GetMethod: " + getMethod.getStatusLine());
    234241      }
    235242      // Display response
     
    239246    catch (IOException e)
    240247    {
    241       log.debug("IOException when trying to execute HttpClient method : " + e);
     248      log.debug("fetchDataFromUrl(" + url + "): IOException when trying to execute HttpClient method : ", e);
    242249      ioErrorString = e.getMessage();
    243250    }
    244251    catch (Exception e1)
    245252    {
    246       log.debug("Exception when trying to execute HttpClient method : " + e1);
     253      log.debug("fetchDataFromUrl(" + url + "): Exception when trying to execute HttpClient method : ", e1);
    247254      ioErrorString = e1.getMessage();
    248255    }
     
    256263      else
    257264      {
    258         log.debug("Connection to GetMethod already closed.");
    259       }
    260     }
     265        log.debug("fetchDataFromUrl(" + url + "): Connection to GetMethod already closed.");
     266      }
     267    }
     268    //log.debug("fetchDataFromUrl(" + url + "): content = " + content);
    261269    return content;
    262270  }
Note: See TracChangeset for help on using the changeset viewer.