use of com.vmware.flowgate.poweriqworker.model.Sensor in project flowgate by vmware.
the class SyncSensorMetaDataJobTest method testAggregatorSensorIdAndSourceForPdu2.
@Test
public void testAggregatorSensorIdAndSourceForPdu2() {
Asset pdu = createAsset1();
HashMap<String, String> justificationfields = new HashMap<String, String>();
String filed = "509" + FlowgateConstant.SEPARATOR + "l9i8728d55368540fcba1692,606" + FlowgateConstant.SEPARATOR + "l9i8728d55368540fcba1692";
justificationfields.put(AssetSubCategory.Humidity.toString(), filed);
pdu.setJustificationfields(justificationfields);
Sensor sensor = new Sensor();
sensor.setId(610);
sensor.setType(PowerIQService.HumiditySensor);
String source = "l9i8728d55368540fcba1692";
pdu = powerIQService.aggregatorSensorIdAndSourceForPdu(pdu, sensor, source);
TestCase.assertEquals(filed + FlowgateConstant.SPILIT_FLAG + sensor.getId() + FlowgateConstant.SEPARATOR + source, pdu.getJustificationfields().get(AssetSubCategory.Humidity.toString()));
}
use of com.vmware.flowgate.poweriqworker.model.Sensor in project flowgate by vmware.
the class PowerIQAPIClient method getSensorById.
public Sensor getSensorById(String id) {
Sensor sensor = new Sensor();
ResponseEntity<SensorResult> sensorResult = this.restTemplate.exchange(getPowerIQServiceEndpoint() + String.format(GetSensorByIdURL, id), HttpMethod.GET, getDefaultEntity(), SensorResult.class);
if (sensorResult != null) {
sensor = sensorResult.getBody().getSensor();
}
return sensor;
}
use of com.vmware.flowgate.poweriqworker.model.Sensor in project flowgate by vmware.
the class PowerIQService method removeSensorMetaData.
public void removeSensorMetaData(FacilitySoftwareConfig powerIQ) {
PowerIQAPIClient client = createClient(powerIQ);
restClient.setServiceKey(serviceKeyConfig.getServiceKey());
List<Asset> sensors = restClient.getAllAssetsBySourceAndType(powerIQ.getId(), AssetCategory.Sensors);
List<Asset> servers = restClient.getAllAssetsByType(AssetCategory.Server);
long currentTime = System.currentTimeMillis();
String expiredTimeRangeValue = template.opsForValue().get(EventMessageUtil.EXPIREDTIMERANGE);
long expiredTimeRange = 0l;
if (expiredTimeRangeValue != null) {
expiredTimeRange = Long.valueOf(expiredTimeRangeValue);
} else {
expiredTimeRange = FlowgateConstant.DEFAULTEXPIREDTIMERANGE;
}
for (Asset sensor : sensors) {
if (!sensor.isExpired(currentTime, expiredTimeRange)) {
continue;
}
Sensor sensorFromPowerIQ = client.getSensorById(String.valueOf(sensor.getAssetNumber()));
if (sensorFromPowerIQ == null) {
List<Asset> needToupdate = updateServer(servers, sensor.getId());
restClient.saveAssets(needToupdate);
restClient.removeAssetByID(sensor.getId());
} else {
sensor.setLastupdate(currentTime);
restClient.saveAssets(sensor);
}
}
}
use of com.vmware.flowgate.poweriqworker.model.Sensor in project flowgate by vmware.
the class PowerIQService method saveSensorAssetsToFlowgate.
public void saveSensorAssetsToFlowgate(Map<String, Asset> exsitingSensorAssets, PowerIQAPIClient client, String assetSource, LocationInfo location) {
List<Asset> pdusFromFlowgate = restClient.getAllAssetsBySourceAndType(assetSource, AssetCategory.PDU);
Map<String, Asset> pduAssetMap = getPDUIDAndAssetMap(pdusFromFlowgate);
List<Sensor> sensors = null;
int limit = 100;
int offset = 0;
List<Asset> newAssetsNeedToSave = null;
List<Asset> oldAssetsNeedToupdate = null;
while ((sensors = client.getSensors(limit, offset)) != null) {
if (sensors.isEmpty()) {
logger.warn(String.format("No sensor data from /api/v2/sensors?limit=%s&offset=%s", limit, offset));
break;
}
newAssetsNeedToSave = new ArrayList<Asset>();
oldAssetsNeedToupdate = new ArrayList<Asset>();
for (Sensor sensor : sensors) {
// Filter AbsoluteHumiditySensor
if (subCategoryMap.get(sensor.getType()) == null) {
continue;
}
Asset asset = new Asset();
Map<String, String> sensorMap = new HashMap<String, String>();
sensorMap.put(FlowgateConstant.SENSOR_ID_FROM_POWERIQ, String.valueOf(sensor.getId()));
sensorMap.put(FlowgateConstant.POSITION, sensor.getPosition());
if (sensor.getPduId() != null) {
Asset pduAsset = pduAssetMap.get(String.valueOf(sensor.getPduId()));
if (pduAsset == null) {
asset = fillLocation(sensor.getParent(), location);
} else {
// If the sensor's has pdu information. Then it can use the PDU's location info.
com.vmware.flowgate.common.model.Parent parent = new com.vmware.flowgate.common.model.Parent();
parent.setParentId(String.valueOf(sensor.getPduId()));
parent.setType(FlowgateConstant.PDU);
asset.setParent(parent);
asset.setRoom(pduAsset.getRoom());
asset.setFloor(pduAsset.getFloor());
asset.setBuilding(pduAsset.getBuilding());
asset.setCity(pduAsset.getCity());
asset.setCountry(pduAsset.getCountry());
asset.setRegion(pduAsset.getRegion());
// Record the pdu_assetId for the sensor.
sensorMap.put(FlowgateConstant.PDU_ASSET_ID, pduAsset.getId());
// Record the sensorId and sensor_source for the pdu.
pduAsset = aggregatorSensorIdAndSourceForPdu(pduAsset, sensor, assetSource);
}
} else {
asset = fillLocation(sensor.getParent(), location);
}
Asset assetToUpdate = exsitingSensorAssets.get(String.valueOf(sensor.getId()));
if (assetToUpdate != null) {
assetToUpdate.setAssetName(sensor.getName());
assetToUpdate.setRow(asset.getRow());
assetToUpdate.setRoom(asset.getRoom());
assetToUpdate.setFloor(asset.getFloor());
assetToUpdate.setBuilding(asset.getBuilding());
assetToUpdate.setCity(asset.getCity());
assetToUpdate.setCountry(asset.getCountry());
assetToUpdate.setRegion(asset.getRegion());
assetToUpdate.setSerialnumber(sensor.getSerialNumber());
assetToUpdate.setParent(asset.getParent());
HashMap<String, String> oldjustificationfields = assetToUpdate.getJustificationfields();
Map<String, String> oldSensorInfoMap = null;
try {
oldSensorInfoMap = getInfoMap(oldjustificationfields.get(FlowgateConstant.SENSOR));
} catch (IOException e) {
logger.error("Format sensor info map error", e);
}
if (oldSensorInfoMap != null) {
oldSensorInfoMap.put(FlowgateConstant.PDU_ASSET_ID, sensorMap.get(FlowgateConstant.PDU_ASSET_ID));
oldSensorInfoMap.put(FlowgateConstant.SENSOR_ID_FROM_POWERIQ, sensorMap.get(FlowgateConstant.SENSOR_ID_FROM_POWERIQ));
oldSensorInfoMap.put(FlowgateConstant.POSITION, sensorMap.get(FlowgateConstant.POSITION));
try {
String sensorInfo = mapper.writeValueAsString(oldSensorInfoMap);
oldjustificationfields.put(FlowgateConstant.SENSOR, sensorInfo);
} catch (JsonProcessingException e) {
logger.error("Format sensor info map error", e);
}
}
assetToUpdate.setJustificationfields(oldjustificationfields);
assetToUpdate.setLastupdate(System.currentTimeMillis());
assetToUpdate.setMountingSide(sensorMountingSide.get(sensor.getPosition().toUpperCase()));
// save
oldAssetsNeedToupdate.add(assetToUpdate);
} else {
HashMap<String, String> justificationfieldsForSensor = new HashMap<String, String>();
try {
justificationfieldsForSensor.put(FlowgateConstant.SENSOR, mapper.writeValueAsString(sensorMap));
} catch (JsonProcessingException e) {
logger.error("Format sensor info map error", e);
}
asset.setAssetName(sensor.getName());
asset.setJustificationfields(justificationfieldsForSensor);
asset.setSerialnumber(sensor.getSerialNumber());
asset.setAssetSource(assetSource);
asset.setCategory(AssetCategory.Sensors);
asset.setSubCategory(subCategoryMap.get(sensor.getType()));
asset.setCreated(System.currentTimeMillis());
if (sensor.getPosition() != null) {
asset.setMountingSide(sensorMountingSide.get(sensor.getPosition().toUpperCase()));
}
// save
newAssetsNeedToSave.add(asset);
}
}
restClient.saveAssets(oldAssetsNeedToupdate);
if (!newAssetsNeedToSave.isEmpty()) {
// We need the assetId of sensor asset, so it should be saved first.
List<Asset> sensorAlreadySaved = new ArrayList<Asset>();
for (Asset asset : newAssetsNeedToSave) {
ResponseEntity<Void> res = restClient.saveAssets(asset);
if (res.getStatusCode().is2xxSuccessful()) {
String assetId = getAssetIdByResponseEntity(res);
asset.setId(assetId);
sensorAlreadySaved.add(asset);
if (AssetSubCategory.Humidity.equals(asset.getSubCategory()) || AssetSubCategory.Temperature.equals(asset.getSubCategory())) {
Map<String, String> metricsFormulas = new HashMap<>(1);
Map<String, String> sensorFormulas = new HashMap<>(1);
sensorFormulas.put(asset.getSubCategory().toString(), asset.getId());
metricsFormulas.put(FlowgateConstant.SENSOR, asset.metricsFormulaToString(sensorFormulas));
asset.setMetricsformulars(metricsFormulas);
// save sensor formula
restClient.saveAssets(asset);
} else if (asset.getSubCategory() == null) {
Map<String, String> metricsFormulas = new HashMap<>(1);
Map<String, String> sensorFormulas = new HashMap<>(2);
sensorFormulas.put(MetricName.HUMIDITY, asset.getId());
sensorFormulas.put(MetricName.TEMPERATURE, asset.getId());
metricsFormulas.put(FlowgateConstant.SENSOR, asset.metricsFormulaToString(sensorFormulas));
asset.setMetricsformulars(metricsFormulas);
// save sensor formula
restClient.saveAssets(asset);
}
}
}
Set<Asset> pduAssetNeedToUpdate = updatePduMetricformular(sensorAlreadySaved, pduAssetMap);
restClient.saveAssets(new ArrayList<Asset>(pduAssetNeedToUpdate));
}
offset += limit;
}
}
use of com.vmware.flowgate.poweriqworker.model.Sensor in project flowgate by vmware.
the class PowerIQService method getSensorRealTimeData.
public List<RealTimeData> getSensorRealTimeData(FacilitySoftwareConfig powerIQ, List<Asset> assets) {
HashMap<AdvanceSettingType, String> advanceSetting = getAdvanceSetting(powerIQ);
List<RealTimeData> realtimeDatas = new ArrayList<RealTimeData>();
String dateFormat = advanceSetting.get(AdvanceSettingType.DateFormat);
String timezone = advanceSetting.get(AdvanceSettingType.TimeZone);
String temperature = advanceSetting.get(AdvanceSettingType.TEMPERATURE_UNIT);
String humidity = advanceSetting.get(AdvanceSettingType.HUMIDITY_UNIT);
PowerIQAPIClient powerIQAPIClient = createClient(powerIQ);
for (Asset asset : assets) {
HashMap<String, String> sensorExtraInfo = asset.getJustificationfields();
String sensorInfo = sensorExtraInfo.get(FlowgateConstant.SENSOR);
Map<String, String> sensorInfoMap = null;
try {
sensorInfoMap = getInfoMap(sensorInfo);
} catch (IOException e2) {
continue;
}
String sensorId = sensorInfoMap.get(FlowgateConstant.SENSOR_ID_FROM_POWERIQ);
Sensor sensor = null;
try {
sensor = powerIQAPIClient.getSensorById(sensorId);
} catch (HttpClientErrorException e) {
logger.error("Failed to query data from PowerIQ", e);
IntegrationStatus integrationStatus = powerIQ.getIntegrationStatus();
if (integrationStatus == null) {
integrationStatus = new IntegrationStatus();
}
integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
integrationStatus.setDetail(e.getMessage());
integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
updateIntegrationStatus(powerIQ);
break;
} catch (ResourceAccessException e1) {
if (e1.getCause().getCause() instanceof ConnectException) {
checkAndUpdateIntegrationStatus(powerIQ, e1.getMessage());
break;
}
break;
}
SensorReading reading = sensor.getReading();
if (reading == null || reading.getId() == 0) {
continue;
}
RealTimeData realTimeData = new RealTimeData();
String valueDateTime = reading.getReadingTime();
long recordedTime = WormholeDateFormat.getLongTime(valueDateTime, dateFormat, timezone);
if (recordedTime == -1) {
logger.error("Failed to translate the time string: " + valueDateTime + ".And the dateformat is " + dateFormat);
continue;
}
List<ValueUnit> values = new ArrayList<ValueUnit>();
ValueUnit value = new ValueUnit();
value.setTime(recordedTime);
value.setKey(sensorAndMetricMap.get(sensor.getType()));
String unit = reading.getUom();
MetricUnit sourceUnit = null, targetUnit = null;
switch(sensorAndMetricMap.get(sensor.getType())) {
case MetricName.HUMIDITY:
if (unit != null && !unit.isEmpty()) {
if (unit.equals("%")) {
sourceUnit = MetricUnit.percent;
} else {
sourceUnit = MetricUnit.valueOf(unit);
}
} else {
if (humidity.equals("%")) {
sourceUnit = MetricUnit.percent;
} else {
sourceUnit = MetricUnit.valueOf(humidity);
}
}
targetUnit = MetricUnit.percent;
break;
case MetricName.TEMPERATURE:
if (unit != null && !unit.isEmpty()) {
sourceUnit = MetricUnit.valueOf(unit.toUpperCase());
} else {
sourceUnit = MetricUnit.valueOf(temperature.toUpperCase());
}
targetUnit = MetricUnit.C;
break;
default:
break;
}
Double metricsValue = reading.getValue();
if (metricsValue == null) {
continue;
}
try {
value.setValueNum(value.translateUnit(metricsValue, sourceUnit, targetUnit));
} catch (WormholeException e) {
logger.error("Cannot translate Unit", e);
}
if (targetUnit.toString().equals(MetricUnit.percent.toString())) {
value.setUnit("%");
} else {
value.setUnit(targetUnit.toString());
}
values.add(value);
realTimeData.setAssetID(asset.getId());
realTimeData.setTime(recordedTime);
realTimeData.setValues(values);
realTimeData.setId(asset.getId() + "_" + recordedTime);
realtimeDatas.add(realTimeData);
}
return realtimeDatas;
}
Aggregations