/** * Convert the value from a source unit to a target unit Note: if the value is an age, the method * adjusts the value to return the right age * * @param activeInstrumentRunService * @param targetInstrumentRunValue * @param sourceInstrumentRunValue */ @SuppressWarnings("unchecked") public void convert( ActiveInstrumentRunService activeInstrumentRunService, InstrumentRunValue targetInstrumentRunValue, InstrumentRunValue sourceInstrumentRunValue) { InstrumentParameter sourceParameter = activeInstrumentRunService .getInstrumentType() .getInstrumentParameter(sourceInstrumentRunValue.getInstrumentParameter()); InstrumentParameter targetParameter = activeInstrumentRunService .getInstrumentType() .getInstrumentParameter(targetInstrumentRunValue.getInstrumentParameter()); log.debug( "Converting parameters from source {} to target {}", sourceParameter, targetParameter); Unit sourceUnit = Unit.valueOf(sourceParameter.getMeasurementUnit()); Unit targetUnit = Unit.valueOf(targetParameter.getMeasurementUnit()); log.debug( "Converting units from source {} to target {}", sourceUnit.toString(), targetUnit.toString()); double sourceValue; // Extract the source value and convert it to a double try { sourceValue = Double.parseDouble( sourceInstrumentRunValue.getData(sourceParameter.getDataType()).getValueAsString()); } catch (NumberFormatException e) { Data sourceData = sourceInstrumentRunValue.getData(sourceParameter.getDataType()); log.error( "Error converting between measurement units. Original value {} of type {} cannot be converted to a double, which is required to convert between measurement units.", sourceData.getValueAsString(), sourceData.getType()); throw e; } double newValue = sourceUnit.getConverterTo(targetUnit).convert(sourceValue); switch (activeInstrumentRunService .getInstrumentType() .getInstrumentParameter(targetInstrumentRunValue.getInstrumentParameter()) .getDataType()) { case DECIMAL: targetInstrumentRunValue.setData(DataBuilder.buildDecimal(newValue)); break; case INTEGER: if (targetUnit.toString().equalsIgnoreCase("year")) newValue = Math.floor(newValue); targetInstrumentRunValue.setData(DataBuilder.buildInteger(Math.round(newValue))); break; } }
public FXMesureAreaHandler() { super(); uiArea.setMaxHeight(Double.MAX_VALUE); uiUnit.setItems(FXCollections.observableArrayList(Unit.valueOf("km2"), SI.SQUARE_METRE)); uiUnit.getSelectionModel().selectFirst(); pane.setAlignment(Pos.CENTER); pane.setBackground( new Background(new BackgroundFill(Color.WHITE, new CornerRadii(10), Insets.EMPTY))); pane.setPadding(new Insets(10, 10, 10, 10)); deco.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); deco.getColumnConstraints() .add( new ColumnConstraints( 0, HBox.USE_COMPUTED_SIZE, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true)); deco.getColumnConstraints() .add( new ColumnConstraints( 0, HBox.USE_COMPUTED_SIZE, Double.MAX_VALUE, Priority.NEVER, HPos.CENTER, true)); deco.getColumnConstraints() .add( new ColumnConstraints( 0, HBox.USE_COMPUTED_SIZE, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true)); uiUnit .valueProperty() .addListener( (ObservableValue<? extends Unit> observable, Unit oldValue, Unit newValue) -> { updateGeometry(); }); deco.add(pane, 1, 0); }
static { productMap = new HashMap<String, HDF4ProductFieldType.Product>(); // TODO add more products productMap.put( "mcsst", new HDF4ProductFieldType.Product("mcsst", SI.CELSIUS, "sea surface temperature")); productMap.put( "sst", new HDF4ProductFieldType.Product("sst", SI.CELSIUS, "sea surface temperature")); productMap.put("sst4", new HDF4ProductFieldType.Product("sst4", SI.CELSIUS, "temperature")); productMap.put("lowcloud", new HDF4ProductFieldType.Product("lowcloud", Unit.ONE, "lowcloud")); productMap.put( "K_490", new HDF4ProductFieldType.Product("K_490", Unit.valueOf("m^-1"), "k_490")); }
/** * Creates the needed JEVis structure * * @param buildingId building node id, where the structure has to be created * <p>TODO Enable mysql server TODO Add units */ public void createStructure(long buildingId) { ObjectAndBoolean dsd = createObjectCheckNameExistance( buildingId, "Data Source Directory", "Data Source Directory"); ObjectAndBoolean mysqlServer = createObjectCheckNameExistance( dsd.getJEVisObject().getID(), "MySQL Server", "MySQL Server"); if (mysqlServer.isNew) { long id = mysqlServer.getJEVisObject().getID(); writeToJEVis(id, "Schema", _schema); writeToJEVis(id, "User", _dbUser); writeToJEVis(id, "Port", _port); writeToJEVis(id, "Host", _host); writeToJEVis(id, "Password", _dbPW); writeToJEVis(id, "Enabled", true); } ObjectAndBoolean dataDirectory = createObjectCheckNameExistance(buildingId, "Data Directory", "Data Directory"); for (Sensor sensor : _result) { ObjectAndBoolean sqlChannelDir = createObjectCheckNameExistance( mysqlServer.getJEVisObject().getID(), "SQL Channel Directory", sensor.getName() + "_" + sensor.getSymbol()); ObjectAndBoolean channel = createObjectCheckNameExistance( sqlChannelDir.getJEVisObject().getID(), "SQL Channel", "SQL Channel"); if (channel.isNew) { long id = channel.getJEVisObject().getID(); writeToJEVis(id, "Column Timestamp", "time"); writeToJEVis(id, "Column Value", "value"); writeToJEVis(id, "Table", sensor.getTable()); writeToJEVis(id, "Timestamp Format", "yyyy-MM-dd HH:mm:ss.s"); } ObjectAndBoolean sqlDPD = createObjectCheckNameExistance( channel.getJEVisObject().getID(), "SQL Data Point Directory", "DPD"); ObjectAndBoolean sqlDP = createObjectCheckNameExistance(sqlDPD.getJEVisObject().getID(), "SQL Data Point", "DP"); ObjectAndBoolean device = createObjectCheckNameExistance( dataDirectory.getJEVisObject().getID(), "Device", sensor.getName()); if (device.isNew) { long id = device.getJEVisObject().getID(); writeToJEVis(id, "MAC", sensor.getName()); } ObjectAndBoolean data = createObjectCheckNameExistance( device.getJEVisObject().getID(), "Data", sensor.getSymbol()); try { JEVisAttribute attributeValue = data.getJEVisObject().getAttribute("Value"); attributeValue.setDisplayUnit( new JEVisUnitImp(Unit.valueOf(sensor.getUnit()), "", JEVisUnit.Prefix.NONE)); attributeValue.setInputUnit( new JEVisUnitImp(Unit.valueOf(sensor.getUnit()), "", JEVisUnit.Prefix.NONE)); attributeValue.commit(); } catch (JEVisException ex) { Logger.getLogger(WiotechStructureCreator.class.getName()).log(Level.SEVERE, null, ex); } if (data.isNew) { writeToJEVis( sqlDP.getJEVisObject().getID(), "Target", data.getJEVisObject().getID().toString()); } } }