Search in sources :

Example 6 with Sensor

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()));
}
Also used : HashMap(java.util.HashMap) Asset(com.vmware.flowgate.common.model.Asset) Sensor(com.vmware.flowgate.poweriqworker.model.Sensor) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with Sensor

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;
}
Also used : SensorResult(com.vmware.flowgate.poweriqworker.model.SensorResult) Sensor(com.vmware.flowgate.poweriqworker.model.Sensor)

Example 8 with 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);
        }
    }
}
Also used : PowerIQAPIClient(com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient) Asset(com.vmware.flowgate.common.model.Asset) Sensor(com.vmware.flowgate.poweriqworker.model.Sensor)

Example 9 with 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;
    }
}
Also used : HashMap(java.util.HashMap) Parent(com.vmware.flowgate.poweriqworker.model.Parent) ArrayList(java.util.ArrayList) Asset(com.vmware.flowgate.common.model.Asset) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) Sensor(com.vmware.flowgate.poweriqworker.model.Sensor)

Example 10 with Sensor

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;
}
Also used : RealTimeData(com.vmware.flowgate.common.model.RealTimeData) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) ArrayList(java.util.ArrayList) WormholeException(com.vmware.flowgate.common.exception.WormholeException) IOException(java.io.IOException) ResourceAccessException(org.springframework.web.client.ResourceAccessException) SensorReading(com.vmware.flowgate.poweriqworker.model.SensorReading) PowerIQAPIClient(com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient) Asset(com.vmware.flowgate.common.model.Asset) MetricUnit(com.vmware.flowgate.common.model.ValueUnit.MetricUnit) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) AdvanceSettingType(com.vmware.flowgate.common.model.FacilitySoftwareConfig.AdvanceSettingType) Sensor(com.vmware.flowgate.poweriqworker.model.Sensor) ConnectException(java.net.ConnectException)

Aggregations

Sensor (com.vmware.flowgate.poweriqworker.model.Sensor)13 Asset (com.vmware.flowgate.common.model.Asset)10 Test (org.junit.Test)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 RealTimeData (com.vmware.flowgate.common.model.RealTimeData)4 SensorReading (com.vmware.flowgate.poweriqworker.model.SensorReading)3 PowerIQAPIClient (com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient)2 Parent (com.vmware.flowgate.poweriqworker.model.Parent)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 WormholeException (com.vmware.flowgate.common.exception.WormholeException)1 AdvanceSettingType (com.vmware.flowgate.common.model.FacilitySoftwareConfig.AdvanceSettingType)1 IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)1 ValueUnit (com.vmware.flowgate.common.model.ValueUnit)1 MetricUnit (com.vmware.flowgate.common.model.ValueUnit.MetricUnit)1 LocationInfo (com.vmware.flowgate.poweriqworker.model.LocationInfo)1 SensorResult (com.vmware.flowgate.poweriqworker.model.SensorResult)1