예제 #1
0
 @Override
 public SensorData toProtoSensor() {
   SensorData.Builder sdb = SensorData.newBuilder();
   sdb.setRecordTime(getTimestamp());
   sdb.addValueFloat(getGyrX());
   sdb.addValueFloat(getGyrY());
   sdb.addValueFloat(getGyrZ());
   return sdb.build();
 }
예제 #2
0
 public SensorDescGyroscope(SensorData sensorData) {
   super(sensorData);
   this.gyrX = sensorData.getValueFloat(0);
   this.gyrY = sensorData.getValueFloat(1);
   this.gyrZ = sensorData.getValueFloat(2);
 }
  @Override
  public void run() {
    InputStream is;
    try {
      is = socket.getInputStream();
      boolean connected = true;
      while (connected) {
        connected &= !socket.isClosed();
        try {
          // Parse
          SensorUpload su = SensorUpload.parseDelimitedFrom(is);
          if (su != null) {
            long huuid = su.getHuuid();
            long luuid = su.getLuuid();

            byte[] uuid = UUID.toByteArray(huuid, luuid);

            long sensorId = su.getSensorId();
            System.out.println(sensorId);
            long uploadTime = su.getUploadTime();
            List<SensorData> sensorValues = su.getSensorValuesList();

            // TODO, audit data, correct data timestamps?

            // Insert into the Database
            try {
              // Insert transaction
              PreparedStatement transactstmt = sqlse.getTransactionInsertStatement(connection);
              transactstmt.setBytes(1, uuid);
              transactstmt.setLong(2, uploadTime);
              transactstmt.execute();
              transactstmt.close();

              // Insert data
              PreparedStatement datastmt = sqlse.getSensorInsertStatement(connection, sensorId);

              if (datastmt != null) {
                List<Integer> types = sqlse.getArgumentExpectation(sensorId);

                for (SensorData sd : sensorValues) {
                  try {
                    datastmt.setBytes(1, uuid);
                    datastmt.setLong(2, sd.getRecordTime());

                    Iterator<Boolean> iterBool = sd.getValueBoolList().iterator();
                    Iterator<Integer> iterInteger = sd.getValueInt32List().iterator();
                    Iterator<Long> iterLong = sd.getValueInt64List().iterator();
                    Iterator<Float> iterFloat = sd.getValueFloatList().iterator();
                    Iterator<Double> iterDouble = sd.getValueDoubleList().iterator();
                    Iterator<String> iterString = sd.getValueStringList().iterator();

                    int counter = 3;
                    for (Integer type : types) {
                      switch (type) {
                        case SqlSetup.TYPE_BOOL:
                          datastmt.setBoolean(counter, iterBool.next());
                          break;
                        case SqlSetup.TYPE_INT32:
                          datastmt.setInt(counter, iterInteger.next());
                          break;
                        case SqlSetup.TYPE_INT64:
                          datastmt.setLong(counter, iterLong.next());
                          break;
                        case SqlSetup.TYPE_FLOAT:
                          datastmt.setFloat(counter, iterFloat.next());
                          break;
                        case SqlSetup.TYPE_DOUBLE:
                          datastmt.setDouble(counter, iterDouble.next());
                          break;
                        case SqlSetup.TYPE_STRING:
                          datastmt.setString(counter, iterString.next());
                          break;
                        default:
                          break;
                      }
                      counter++;
                    }
                    datastmt.addBatch();
                  } catch (NoSuchElementException e) {
                    Log.getInstance()
                        .append(Log.FLAG_WARNING, "Sensor data type mismatch with database");
                  }
                }
                // Add sensor data to the database
                datastmt.executeBatch();
                datastmt.close();
              }
            } catch (SQLException e) {
              Log.getInstance()
                  .append(Log.FLAG_WARNING, "Submitting sensor data chunk to database failed");
            }
          } else {
            connected = false;
          }
        } catch (IOException e) {
          Log.getInstance().append(Log.FLAG_WARNING, "Parsing protobuf SensorUpload failed");
          connected = false;
        }
      }
    } catch (IOException e) {
      Log.getInstance().append(Log.FLAG_WARNING, "Opening data stream from socket failed");
    } catch (Exception e) {
      Log.getInstance().append(Log.FLAG_WARNING, "Generic error");
    } finally {
      cleanup();
    }
  }