Search in sources :

Example 1 with AssetQuery

use of org.openremote.model.query.AssetQuery in project openremote by openremote.

the class EnergyOptimisationService method runOptimisation.

/**
 * Runs the optimisation routine for the specified time; it is important that this method does not throw an
 * exception as it will cancel the scheduled task thus stopping future optimisations.
 */
protected void runOptimisation(String optimisationAssetId, Instant optimisationTime) {
    OptimisationInstance optimisationInstance = assetOptimisationInstanceMap.get(optimisationAssetId);
    if (optimisationInstance == null) {
        return;
    }
    LOG.finer(getLogPrefix(optimisationAssetId) + "Running for time '" + formatter.format(optimisationTime));
    EnergyOptimiser optimiser = optimisationInstance.energyOptimiser;
    int intervalCount = optimiser.get24HourIntervalCount();
    double intervalSize = optimiser.getIntervalSize();
    LOG.finest(getLogPrefix(optimisationAssetId) + "Fetching child assets of type '" + ElectricitySupplierAsset.class.getSimpleName() + "'");
    List<ElectricitySupplierAsset> supplierAssets = assetStorageService.findAll(new AssetQuery().types(ElectricitySupplierAsset.class).recursive(true).parents(optimisationAssetId)).stream().filter(asset -> asset.hasAttribute(ElectricitySupplierAsset.TARIFF_IMPORT)).map(asset -> (ElectricitySupplierAsset) asset).collect(Collectors.toList());
    if (supplierAssets.size() != 1) {
        LOG.warning(getLogPrefix(optimisationAssetId) + "Expected exactly one " + ElectricitySupplierAsset.class.getSimpleName() + " asset with a '" + ElectricitySupplierAsset.TARIFF_IMPORT.getName() + "' attribute but found: " + supplierAssets.size());
        return;
    }
    double[] powerNets = new double[intervalCount];
    ElectricitySupplierAsset supplierAsset = supplierAssets.get(0);
    if (LOG.isLoggable(Level.FINEST)) {
        LOG.finest(getLogPrefix(optimisationAssetId) + "Found child asset of type '" + ElectricitySupplierAsset.class.getSimpleName() + "': " + supplierAsset);
    }
    // Do some basic validation
    if (supplierAsset.getTariffImport().isPresent()) {
        LOG.warning(getLogPrefix(optimisationAssetId) + ElectricitySupplierAsset.class.getSimpleName() + " asset '" + ElectricitySupplierAsset.TARIFF_IMPORT.getName() + "' attribute has no value");
    }
    LOG.finest(getLogPrefix(optimisationAssetId) + "Fetching optimisable child assets of type '" + ElectricityStorageAsset.class.getSimpleName() + "'");
    List<ElectricityStorageAsset> optimisableStorageAssets = assetStorageService.findAll(new AssetQuery().recursive(true).parents(optimisationAssetId).types(ElectricityStorageAsset.class).attributes(new LogicGroup<>(LogicGroup.Operator.AND, Collections.singletonList(new LogicGroup<>(LogicGroup.Operator.OR, new AttributePredicate(ElectricityStorageAsset.SUPPORTS_IMPORT.getName(), new BooleanPredicate(true)), new AttributePredicate(ElectricityStorageAsset.SUPPORTS_EXPORT.getName(), new BooleanPredicate(true)))), new AttributePredicate().name(new StringPredicate(ElectricityAsset.POWER_SETPOINT.getName()))))).stream().map(asset -> (ElectricityStorageAsset) asset).collect(Collectors.toList());
    List<ElectricityStorageAsset> finalOptimisableStorageAssets = optimisableStorageAssets;
    optimisableStorageAssets = optimisableStorageAssets.stream().filter(asset -> {
        // Exclude force charged assets (so we don't mess with the setpoint)
        if (forceChargeAssetIds.contains(asset.getId())) {
            LOG.finest("Optimisable asset was requested to force charge so it won't be optimised: " + asset.getId());
            @SuppressWarnings("OptionalGetWithoutIsPresent") Attribute<Double> powerAttribute = asset.getAttribute(ElectricityAsset.POWER).get();
            double[] powerLevels = get24HAttributeValues(asset.getId(), powerAttribute, optimiser.getIntervalSize(), intervalCount, optimisationTime);
            IntStream.range(0, intervalCount).forEach(i -> powerNets[i] += powerLevels[i]);
            double currentEnergyLevel = asset.getEnergyLevel().orElse(0d);
            double maxEnergyLevel = getElectricityStorageAssetEnergyLevelMax(asset);
            if (currentEnergyLevel >= maxEnergyLevel) {
                LOG.info("Force charged asset has reached maxEnergyLevelPercentage so stopping charging: " + asset.getId());
                forceChargeAssetIds.remove(asset.getId());
                assetProcessingService.sendAttributeEvent(new AttributeEvent(asset.getId(), ElectricityStorageAsset.POWER_SETPOINT, 0d));
                assetProcessingService.sendAttributeEvent(new AttributeEvent(asset.getId(), ElectricityStorageAsset.FORCE_CHARGE, AttributeExecuteStatus.COMPLETED));
            }
            return false;
        }
        if (asset instanceof ElectricityChargerAsset) {
            // Check if it has a child vehicle asset
            return finalOptimisableStorageAssets.stream().noneMatch(a -> {
                if (a instanceof ElectricVehicleAsset && a.getParentId().equals(asset.getId())) {
                    // Take the lowest power max from vehicle or charger
                    double vehiclePowerImportMax = a.getPowerImportMax().orElse(Double.MAX_VALUE);
                    double vehiclePowerExportMax = a.getPowerExportMax().orElse(Double.MAX_VALUE);
                    double chargerPowerImportMax = asset.getPowerImportMax().orElse(Double.MAX_VALUE);
                    double chargerPowerExportMax = asset.getPowerExportMax().orElse(Double.MAX_VALUE);
                    double smallestPowerImportMax = Math.min(vehiclePowerImportMax, chargerPowerImportMax);
                    double smallestPowerExportMax = Math.min(vehiclePowerExportMax, chargerPowerExportMax);
                    if (smallestPowerImportMax < vehiclePowerImportMax) {
                        LOG.fine("Reducing vehicle power import max due to connected charger limit: vehicle=" + a.getId() + ", oldPowerImportMax=" + vehiclePowerImportMax + ", newPowerImportMax=" + smallestPowerImportMax);
                        a.setPowerImportMax(smallestPowerImportMax);
                    }
                    if (smallestPowerExportMax < vehiclePowerExportMax) {
                        LOG.fine("Reducing vehicle power Export max due to connected charger limit: vehicle=" + a.getId() + ", oldPowerExportMax=" + vehiclePowerExportMax + ", newPowerExportMax=" + smallestPowerExportMax);
                        a.setPowerExportMax(smallestPowerExportMax);
                    }
                    LOG.finest("Excluding charger from optimisable assets and child vehicle will be used instead: " + asset.getId());
                    return true;
                }
                return false;
            });
        }
        return true;
    }).sorted(Comparator.comparingInt(asset -> asset.getEnergyLevelSchedule().map(schedule -> 0).orElse(1))).collect(Collectors.toList());
    if (optimisableStorageAssets.isEmpty()) {
        LOG.warning(getLogPrefix(optimisationAssetId) + "Expected at least one optimisable '" + ElectricityStorageAsset.class.getSimpleName() + " asset with a '" + ElectricityAsset.POWER_SETPOINT.getName() + "' attribute but found none");
        return;
    }
    if (LOG.isLoggable(Level.FINEST)) {
        LOG.finest(getLogPrefix(optimisationAssetId) + "Found optimisable child assets of type '" + ElectricityStorageAsset.class.getSimpleName() + "': " + optimisableStorageAssets.stream().map(Asset::getId).collect(Collectors.joining(", ")));
    }
    LOG.finest(getLogPrefix(optimisationAssetId) + "Fetching plain consumer and producer child assets of type '" + ElectricityProducerAsset.class.getSimpleName() + "', '" + ElectricityConsumerAsset.class.getSimpleName() + "', '" + ElectricityStorageAsset.class.getSimpleName() + "'");
    AtomicInteger count = new AtomicInteger(0);
    assetStorageService.findAll(new AssetQuery().recursive(true).parents(optimisationAssetId).types(ElectricityConsumerAsset.class, ElectricityProducerAsset.class).attributes(new AttributePredicate().name(new StringPredicate(ElectricityAsset.POWER.getName())))).forEach(asset -> {
        @SuppressWarnings("OptionalGetWithoutIsPresent") Attribute<Double> powerAttribute = asset.getAttribute(ElectricityAsset.POWER).get();
        double[] powerLevels = get24HAttributeValues(asset.getId(), powerAttribute, optimiser.getIntervalSize(), intervalCount, optimisationTime);
        IntStream.range(0, intervalCount).forEach(i -> powerNets[i] += powerLevels[i]);
        count.incrementAndGet();
    });
    // Get power of storage assets that don't support neither import or export (treat them as plain consumers/producers)
    List<ElectricityStorageAsset> plainStorageAssets = assetStorageService.findAll(new AssetQuery().recursive(true).parents(optimisationAssetId).types(ElectricityStorageAsset.class).attributes(new AttributePredicate().name(new StringPredicate(ElectricityAsset.POWER.getName())), new AttributePredicate(ElectricityStorageAsset.SUPPORTS_IMPORT.getName(), new BooleanPredicate(true), true, null), new AttributePredicate(ElectricityStorageAsset.SUPPORTS_EXPORT.getName(), new BooleanPredicate(true), true, null))).stream().map(asset -> (ElectricityStorageAsset) asset).collect(Collectors.toList());
    // Exclude chargers with a power value != 0 and a child vehicle with a power value != 0 (avoid double counting - vehicle takes priority)
    plainStorageAssets.stream().filter(asset -> {
        if (asset instanceof ElectricityChargerAsset) {
            // Check if it has a child vehicle asset also check optimisable assets as child vehicle could be in there
            return plainStorageAssets.stream().noneMatch(a -> {
                if (a instanceof ElectricVehicleAsset && a.getParentId().equals(asset.getId())) {
                    LOG.finest("Excluding charger from plain consumer/producer calculations to avoid double counting power: " + asset.getId());
                    return true;
                }
                return false;
            }) && finalOptimisableStorageAssets.stream().noneMatch(a -> {
                if (a instanceof ElectricVehicleAsset && a.getParentId().equals(asset.getId())) {
                    LOG.finest("Excluding charger from plain consumer/producer calculations to avoid double counting power: " + asset.getId());
                    return true;
                }
                return false;
            });
        }
        return true;
    }).forEach(asset -> {
        @SuppressWarnings("OptionalGetWithoutIsPresent") Attribute<Double> powerAttribute = asset.getAttribute(ElectricityAsset.POWER).get();
        double[] powerLevels = get24HAttributeValues(asset.getId(), powerAttribute, optimiser.getIntervalSize(), intervalCount, optimisationTime);
        IntStream.range(0, intervalCount).forEach(i -> powerNets[i] += powerLevels[i]);
        count.incrementAndGet();
    });
    if (LOG.isLoggable(Level.FINER)) {
        LOG.finer(getLogPrefix(optimisationAssetId) + "Found plain consumer and producer child assets count=" + count.get());
        LOG.finer("Calculated net power of consumers and producers: " + Arrays.toString(powerNets));
    }
    // Get supplier costs for each interval
    double financialWeightingImport = optimiser.getFinancialWeighting();
    double financialWeightingExport = optimiser.getFinancialWeighting();
    if (financialWeightingImport < 1d && !supplierAsset.getCarbonImport().isPresent()) {
        financialWeightingImport = 1d;
    }
    if (financialWeightingExport < 1d && !supplierAsset.getCarbonExport().isPresent()) {
        financialWeightingExport = 1d;
    }
    double[] costsImport = get24HAttributeValues(supplierAsset.getId(), supplierAsset.getAttribute(ElectricitySupplierAsset.TARIFF_IMPORT).orElse(null), optimiser.getIntervalSize(), intervalCount, optimisationTime);
    double[] costsExport = get24HAttributeValues(supplierAsset.getId(), supplierAsset.getAttribute(ElectricitySupplierAsset.TARIFF_EXPORT).orElse(null), optimiser.getIntervalSize(), intervalCount, optimisationTime);
    if (financialWeightingImport < 1d || financialWeightingExport < 1d) {
        double[] carbonImport = get24HAttributeValues(supplierAsset.getId(), supplierAsset.getAttribute(ElectricitySupplierAsset.CARBON_IMPORT).orElse(null), optimiser.getIntervalSize(), intervalCount, optimisationTime);
        double[] carbonExport = get24HAttributeValues(supplierAsset.getId(), supplierAsset.getAttribute(ElectricitySupplierAsset.CARBON_EXPORT).orElse(null), optimiser.getIntervalSize(), intervalCount, optimisationTime);
        LOG.finer(getLogPrefix(optimisationAssetId) + "Adjusting costs to include some carbon weighting, financialWeightingImport=" + financialWeightingImport + ", financialWeightingExport=" + financialWeightingExport);
        for (int i = 0; i < costsImport.length; i++) {
            costsImport[i] = (financialWeightingImport * costsImport[i]) + ((1 - financialWeightingImport) * carbonImport[i]);
            costsExport[i] = (financialWeightingExport * costsExport[i]) + ((1 - financialWeightingExport) * carbonExport[i]);
        }
    }
    if (LOG.isLoggable(Level.FINER)) {
        LOG.finer(getLogPrefix(optimisationAssetId) + "Import costs: " + Arrays.toString(costsImport));
        LOG.finer(getLogPrefix(optimisationAssetId) + "Export costs: " + Arrays.toString(costsExport));
    }
    // Savings variables
    List<String> obsoleteUnoptimisedAssetIds = new ArrayList<>(optimisationInstance.unoptimisedStorageAssetEnergyLevels.keySet());
    double unoptimisedPower = powerNets[0];
    double financialCost = 0d;
    double carbonCost = 0d;
    double unoptimisedFinancialCost = 0d;
    double unoptimisedCarbonCost = 0d;
    // Optimise storage assets with priority on storage assets with an energy schedule (already sorted above)
    double importPowerMax = supplierAsset.getPowerImportMax().orElse(Double.MAX_VALUE);
    double exportPowerMax = -1 * supplierAsset.getPowerExportMax().orElse(Double.MAX_VALUE);
    double[] importPowerMaxes = new double[intervalCount];
    double[] exportPowerMaxes = new double[intervalCount];
    Arrays.fill(importPowerMaxes, importPowerMax);
    Arrays.fill(exportPowerMaxes, exportPowerMax);
    long periodSeconds = (long) (optimiser.getIntervalSize() * 60 * 60);
    for (ElectricityStorageAsset storageAsset : optimisableStorageAssets) {
        boolean hasSetpoint = storageAsset.hasAttribute(ElectricityStorageAsset.POWER_SETPOINT);
        boolean supportsExport = storageAsset.isSupportsExport().orElse(false);
        boolean supportsImport = storageAsset.isSupportsImport().orElse(false);
        LOG.finer(getLogPrefix(optimisationAssetId) + "Optimising power set points for storage asset: " + storageAsset);
        if (!supportsExport && !supportsImport) {
            LOG.finest(getLogPrefix(optimisationAssetId) + "Storage asset doesn't support import or export: " + storageAsset.getId());
            continue;
        }
        if (!hasSetpoint) {
            LOG.info(getLogPrefix(optimisationAssetId) + "Storage asset has no '" + ElectricityStorageAsset.POWER_SETPOINT.getName() + "' attribute so cannot be controlled: " + storageAsset.getId());
            continue;
        }
        double energyCapacity = storageAsset.getEnergyCapacity().orElse(0d);
        double energyLevel = Math.min(energyCapacity, storageAsset.getEnergyLevel().orElse(-1d));
        if (energyCapacity <= 0d || energyLevel < 0) {
            LOG.info(getLogPrefix(optimisationAssetId) + "Storage asset has no capacity or energy level so cannot import or export energy: " + storageAsset.getId());
            continue;
        }
        double energyLevelMax = Math.min(energyCapacity, ((double) storageAsset.getEnergyLevelPercentageMax().orElse(100) / 100) * energyCapacity);
        double energyLevelMin = Math.min(energyCapacity, ((double) storageAsset.getEnergyLevelPercentageMin().orElse(0) / 100) * energyCapacity);
        double[] energyLevelMins = new double[intervalCount];
        double[] energyLevelMaxs = new double[intervalCount];
        Arrays.fill(energyLevelMins, energyLevelMin);
        Arrays.fill(energyLevelMaxs, energyLevelMax);
        // Does the storage support import and have an energy level schedule
        Optional<Integer[][]> energyLevelScheduleOptional = storageAsset.getEnergyLevelSchedule();
        boolean hasEnergyMinRequirement = energyLevelMin > 0 || energyLevelScheduleOptional.isPresent();
        double powerExportMax = storageAsset.getPowerExportMax().map(power -> -1 * power).orElse(Double.MIN_VALUE);
        double powerImportMax = storageAsset.getPowerImportMax().orElse(Double.MAX_VALUE);
        int[][] energySchedule = energyLevelScheduleOptional.map(dayArr -> Arrays.stream(dayArr).map(hourArr -> Arrays.stream(hourArr).mapToInt(i -> i != null ? i : 0).toArray()).toArray(int[][]::new)).orElse(null);
        if (energySchedule != null) {
            LOG.finer(getLogPrefix(optimisationAssetId) + "Applying energy schedule for storage asset: " + storageAsset.getId());
            optimiser.applyEnergySchedule(energyLevelMins, energyLevelMaxs, energyCapacity, energySchedule, LocalDateTime.ofInstant(Instant.ofEpochMilli(timerService.getCurrentTimeMillis()), ZoneId.systemDefault()));
        }
        double maxEnergyLevelMin = Arrays.stream(energyLevelMins).max().orElse(0);
        boolean isConnected = storageAssetConnected(storageAsset);
        // TODO: Make these a function of energy level
        Function<Integer, Double> powerImportMaxCalculator = interval -> interval == 0 && !isConnected ? 0 : powerImportMax;
        Function<Integer, Double> powerExportMaxCalculator = interval -> interval == 0 && !isConnected ? 0 : powerExportMax;
        if (hasEnergyMinRequirement) {
            LOG.finer(getLogPrefix(optimisationAssetId) + "Normalising min energy requirements for storage asset: " + storageAsset.getId());
            optimiser.normaliseEnergyMinRequirements(energyLevelMins, powerImportMaxCalculator, powerExportMaxCalculator, energyLevel);
            if (LOG.isLoggable(Level.FINEST)) {
                LOG.finest(getLogPrefix(optimisationAssetId) + "Min energy requirements for storage asset '" + storageAsset.getId() + "': " + Arrays.toString(energyLevelMins));
            }
        }
        // Calculate the power setpoints for this asset and update power net values for each interval
        double[] setpoints = getStoragePowerSetpoints(optimisationInstance, storageAsset, energyLevelMins, energyLevelMaxs, powerNets, importPowerMaxes, exportPowerMaxes, costsImport, costsExport);
        if (setpoints != null) {
            // Assume these setpoints will be applied so update the power net values with these
            for (int i = 0; i < powerNets.length; i++) {
                if (i == 0) {
                    if (!storageAssetConnected(storageAsset)) {
                        LOG.finer("Optimised storage asset not connected so interval 0 will not be counted or actioned: " + storageAsset.getId());
                        setpoints[i] = 0;
                        continue;
                    }
                    // Update savings/cost data with costs specific to this asset
                    if (setpoints[i] > 0) {
                        financialCost += storageAsset.getTariffImport().orElse(0d) * setpoints[i] * intervalSize;
                    } else {
                        financialCost += storageAsset.getTariffExport().orElse(0d) * -1 * setpoints[i] * intervalSize;
                    }
                }
                powerNets[i] += setpoints[i];
            }
            // Push the setpoints into the prediction service for the storage asset's setpoint attribute and set current setpoint
            List<Pair<?, LocalDateTime>> valuesAndTimestamps = IntStream.range(1, setpoints.length).mapToObj(i -> new Pair<>(setpoints[i], LocalDateTime.ofInstant(optimisationTime.plus(periodSeconds * i, ChronoUnit.SECONDS), ZoneId.systemDefault()))).collect(Collectors.toList());
            assetPredictedDatapointService.updateValues(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT.getName(), valuesAndTimestamps);
        }
        assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT, setpoints != null ? setpoints[0] : null));
        // Update unoptimised power for this asset
        obsoleteUnoptimisedAssetIds.remove(storageAsset.getId());
        double assetUnoptimisedPower = getStorageUnoptimisedImportPower(optimisationInstance, optimisationAssetId, storageAsset, maxEnergyLevelMin, Math.max(0, powerImportMax - unoptimisedPower));
        unoptimisedPower += assetUnoptimisedPower;
        unoptimisedFinancialCost += storageAsset.getTariffImport().orElse(0d) * assetUnoptimisedPower * intervalSize;
    }
    // Clear out un-optimised data for not found assets
    obsoleteUnoptimisedAssetIds.forEach(optimisationInstance.unoptimisedStorageAssetEnergyLevels.keySet()::remove);
    // Calculate and store savings data
    carbonCost = (powerNets[0] >= 0 ? supplierAsset.getCarbonImport().orElse(0d) : -1 * supplierAsset.getCarbonExport().orElse(0d)) * powerNets[0] * intervalSize;
    financialCost += (powerNets[0] >= 0 ? supplierAsset.getTariffImport().orElse(0d) : -1 * supplierAsset.getTariffExport().orElse(0d)) * powerNets[0] * intervalSize;
    unoptimisedCarbonCost = (unoptimisedPower >= 0 ? supplierAsset.getCarbonImport().orElse(0d) : -1 * supplierAsset.getCarbonExport().orElse(0d)) * unoptimisedPower * intervalSize;
    unoptimisedFinancialCost += (unoptimisedPower >= 0 ? supplierAsset.getTariffImport().orElse(0d) : -1 * supplierAsset.getTariffExport().orElse(0d)) * unoptimisedPower * intervalSize;
    double financialSaving = unoptimisedFinancialCost - financialCost;
    double carbonSaving = unoptimisedCarbonCost - carbonCost;
    LOG.info(getLogPrefix(optimisationAssetId) + "Current interval financial saving = " + financialSaving);
    LOG.info(getLogPrefix(optimisationAssetId) + "Current interval carbon saving = " + carbonSaving);
    financialSaving += optimisationInstance.optimisationAsset.getFinancialSaving().orElse(0d);
    carbonSaving += optimisationInstance.optimisationAsset.getCarbonSaving().orElse(0d);
    // Update in memory asset
    optimisationInstance.optimisationAsset.setFinancialSaving(financialSaving);
    optimisationInstance.optimisationAsset.setCarbonSaving(carbonSaving);
    // Push new values into the DB
    assetProcessingService.sendAttributeEvent(new AttributeEvent(optimisationAssetId, EnergyOptimisationAsset.FINANCIAL_SAVING, financialSaving));
    assetProcessingService.sendAttributeEvent(new AttributeEvent(optimisationAssetId, EnergyOptimisationAsset.CARBON_SAVING, carbonSaving));
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) IntStream(java.util.stream.IntStream) GatewayService.isNotForGateway(org.openremote.manager.gateway.GatewayService.isNotForGateway) java.util(java.util) DatapointInterval(org.openremote.model.datapoint.DatapointInterval) ScheduledFuture(java.util.concurrent.ScheduledFuture) AssetDescriptor(org.openremote.model.asset.AssetDescriptor) BiFunction(java.util.function.BiFunction) AttributeRef(org.openremote.model.attribute.AttributeRef) AssetProcessingService(org.openremote.manager.asset.AssetProcessingService) ValueUtil(org.openremote.model.util.ValueUtil) Function(java.util.function.Function) Level(java.util.logging.Level) java.time(java.time) BooleanPredicate(org.openremote.model.query.filter.BooleanPredicate) Attribute(org.openremote.model.attribute.Attribute) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeEvent(org.openremote.model.attribute.AttributeEvent) org.openremote.model.asset.impl(org.openremote.model.asset.impl) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PersistenceEvent(org.openremote.model.PersistenceEvent) LogicGroup(org.openremote.model.query.LogicGroup) MessageBrokerService(org.openremote.container.message.MessageBrokerService) AssetPredictedDatapointService(org.openremote.manager.datapoint.AssetPredictedDatapointService) Asset(org.openremote.model.asset.Asset) MetaItemType(org.openremote.model.value.MetaItemType) HOURS(java.time.temporal.ChronoUnit.HOURS) AssetQuery(org.openremote.model.query.AssetQuery) Pair(org.openremote.model.util.Pair) ContainerService(org.openremote.model.ContainerService) AttributePredicate(org.openremote.model.query.filter.AttributePredicate) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Container(org.openremote.model.Container) ValueDatapoint(org.openremote.model.datapoint.ValueDatapoint) TimeUnit(java.util.concurrent.TimeUnit) ClientEventService(org.openremote.manager.event.ClientEventService) ChronoUnit(java.time.temporal.ChronoUnit) StringPredicate(org.openremote.model.query.filter.StringPredicate) RouteBuilder(org.apache.camel.builder.RouteBuilder) TimerService(org.openremote.container.timer.TimerService) PERSISTENCE_TOPIC(org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC) DateTimeFormatter(java.time.format.DateTimeFormatter) PersistenceService.isPersistenceEventForEntityType(org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType) GatewayService(org.openremote.manager.gateway.GatewayService) AttributeExecuteStatus(org.openremote.model.attribute.AttributeExecuteStatus) StringPredicate(org.openremote.model.query.filter.StringPredicate) Attribute(org.openremote.model.attribute.Attribute) AttributePredicate(org.openremote.model.query.filter.AttributePredicate) AttributeEvent(org.openremote.model.attribute.AttributeEvent) BooleanPredicate(org.openremote.model.query.filter.BooleanPredicate) Pair(org.openremote.model.util.Pair) AssetQuery(org.openremote.model.query.AssetQuery) ValueDatapoint(org.openremote.model.datapoint.ValueDatapoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 2 with AssetQuery

use of org.openremote.model.query.AssetQuery in project openremote by openremote.

the class AssetStorageService method prepareAssetQuery.

/**
 * Prepares an {@link AssetQuery} by validating it against security constraints and/or applying default options to the query
 * based on security constraints.
 */
public AssetQuery prepareAssetQuery(AssetQuery query, AuthContext authContext, String requestRealm) throws IllegalStateException {
    if (query == null) {
        query = new AssetQuery();
    }
    boolean isAnonymous = authContext == null;
    boolean isSuperUser = authContext != null && authContext.isSuperUser();
    boolean isRestricted = identityService.getIdentityProvider().isRestrictedUser(authContext);
    // Take realm from query, requestRealm or lastly auth context (super users can query with no realm)
    String realm = query.tenant != null ? query.tenant.realm : requestRealm != null ? requestRealm : (!isSuperUser && authContext != null ? authContext.getAuthenticatedRealm() : null);
    if (!isSuperUser) {
        if (TextUtil.isNullOrEmpty(realm)) {
            String msg = "Realm must be specified to read assets";
            LOG.finer(msg);
            throw new IllegalStateException(msg);
        }
        if (isAnonymous) {
            if (query.access != null && query.access != PUBLIC) {
                String msg = "Only public access allowed for anonymous requests";
                LOG.finer(msg);
                throw new IllegalStateException(msg);
            }
            query.access = PUBLIC;
        } else if (isRestricted) {
            if (query.access == PRIVATE) {
                String msg = "Only public or restricted access allowed for restricted requests";
                LOG.finer(msg);
                throw new IllegalStateException(msg);
            }
            if (query.access == null) {
                query.access = PROTECTED;
            }
        }
        if (query.access != PUBLIC && !authContext.hasResourceRole(ClientRole.READ_ASSETS.getValue(), authContext.getClientId())) {
            String msg = "User must have '" + ClientRole.READ_ASSETS.getValue() + "' role to read non public assets";
            LOG.fine(msg);
            throw new IllegalStateException(msg);
        }
        if (query.access != PUBLIC && !realm.equals(authContext.getAuthenticatedRealm())) {
            String msg = "Realm must match authenticated realm for non public access queries";
            LOG.finer(msg);
            throw new IllegalStateException(msg);
        }
        query.tenant = new TenantPredicate(realm);
        if (query.access == PROTECTED) {
            query.userIds(authContext.getUserId());
        }
    }
    if (!identityService.getIdentityProvider().isTenantActiveAndAccessible(authContext, realm)) {
        String msg = "Realm is not present or is inactive";
        LOG.finer(msg);
        throw new IllegalStateException(msg);
    }
    return query;
}
Also used : AssetQuery(org.openremote.model.query.AssetQuery)

Example 3 with AssetQuery

use of org.openremote.model.query.AssetQuery in project openremote by openremote.

the class AssetStorageService method publishModificationEvents.

protected void publishModificationEvents(PersistenceEvent<Asset<?>> persistenceEvent) {
    Asset<?> asset = persistenceEvent.getEntity();
    switch(persistenceEvent.getCause()) {
        case CREATE:
            // Fully load the asset
            Asset<?> loadedAsset = find(new AssetQuery().ids(asset.getId()));
            if (loadedAsset == null) {
                return;
            }
            if (LOG.isLoggable(Level.FINER)) {
                LOG.finer("Asset created: " + loadedAsset.toStringAll());
            } else {
                LOG.fine("Asset created: " + loadedAsset);
            }
            clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.CREATE, loadedAsset, null));
            // ));
            break;
        case UPDATE:
            String[] updatedProperties = persistenceEvent.getPropertyNames();
            boolean attributesChanged = Arrays.asList(updatedProperties).contains("attributes");
            // String[] updatedProperties = Arrays.stream(persistenceEvent.getPropertyNames()).filter(propertyName -> {
            // Object oldValue = persistenceEvent.getPreviousState(propertyName);
            // Object newValue = persistenceEvent.getCurrentState(propertyName);
            // return !Objects.deepEquals(oldValue, newValue);
            // }).toArray(String[]::new);
            // Fully load the asset
            loadedAsset = find(new AssetQuery().ids(asset.getId()));
            if (loadedAsset == null) {
                return;
            }
            LOG.finer("Asset updated: " + persistenceEvent);
            clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.UPDATE, loadedAsset, updatedProperties));
            // Did any attributes change if so raise attribute events on the event bus
            if (attributesChanged) {
                AttributeMap oldAttributes = persistenceEvent.getPreviousState("attributes");
                AttributeMap newAttributes = persistenceEvent.getCurrentState("attributes");
                // Get removed attributes and raise an attribute event with deleted flag in attribute state
                oldAttributes.stream().filter(oldAttribute -> newAttributes.stream().noneMatch(newAttribute -> oldAttribute.getName().equals(newAttribute.getName()))).forEach(obsoleteAttribute -> clientEventService.publishEvent(AttributeEvent.deletedAttribute(asset.getId(), obsoleteAttribute.getName())));
                // Get new or modified attributes
                getAddedOrModifiedAttributes(oldAttributes.values(), newAttributes.values()).forEach(newOrModifiedAttribute -> publishAttributeEvent(asset, newOrModifiedAttribute));
            }
            break;
        case DELETE:
            if (LOG.isLoggable(Level.FINER)) {
                LOG.finer("Asset deleted: " + asset.toStringAll());
            } else {
                LOG.fine("Asset deleted: " + asset);
            }
            clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.DELETE, asset, null));
            // Raise attribute event with deleted flag for each attribute
            AttributeMap deletedAttributes = asset.getAttributes();
            deletedAttributes.forEach(obsoleteAttribute -> clientEventService.publishEvent(AttributeEvent.deletedAttribute(asset.getId(), obsoleteAttribute.getName())));
            break;
    }
}
Also used : ValuePredicate.asPredicateOrTrue(org.openremote.model.query.filter.ValuePredicate.asPredicateOrTrue) ClientRole(org.openremote.model.security.ClientRole) AuthContext(org.openremote.container.security.AuthContext) NoResultException(javax.persistence.NoResultException) ValueUtil(org.openremote.model.util.ValueUtil) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeEvent(org.openremote.model.attribute.AttributeEvent) GroupAsset(org.openremote.model.asset.impl.GroupAsset) TextUtil(org.openremote.model.util.TextUtil) ConstraintViolation(javax.validation.ConstraintViolation) LogicGroup(org.openremote.model.query.LogicGroup) Attribute.getAddedOrModifiedAttributes(org.openremote.model.attribute.Attribute.getAddedOrModifiedAttributes) Predicate(java.util.function.Predicate) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) ACCESS_RESTRICTED_READ(org.openremote.model.value.MetaItemType.ACCESS_RESTRICTED_READ) Query(javax.persistence.Query) StringArrayType(com.vladmihalcea.hibernate.type.array.StringArrayType) org.openremote.model(org.openremote.model) RouteBuilder(org.apache.camel.builder.RouteBuilder) PERSISTENCE_TOPIC(org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC) AssetInfo(org.openremote.model.event.shared.AssetInfo) EventSubscription(org.openremote.model.event.shared.EventSubscription) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) SharedEvent(org.openremote.model.event.shared.SharedEvent) GatewayService(org.openremote.manager.gateway.GatewayService) java.sql(java.sql) IntStream(java.util.stream.IntStream) AttributeMap(org.openremote.model.attribute.AttributeMap) EventSubscriptionAuthorizer(org.openremote.manager.event.EventSubscriptionAuthorizer) java.util(java.util) ACCESS_PUBLIC_READ(org.openremote.model.value.MetaItemType.ACCESS_PUBLIC_READ) CLIENT_EVENT_TOPIC(org.openremote.manager.event.ClientEventService.CLIENT_EVENT_TOPIC) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Session(org.hibernate.Session) TypedQuery(javax.persistence.TypedQuery) Supplier(java.util.function.Supplier) Level(java.util.logging.Level) AbstractReturningWork(org.hibernate.jdbc.AbstractReturningWork) PGobject(org.postgresql.util.PGobject) Attribute(org.openremote.model.attribute.Attribute) PersistenceService(org.openremote.container.persistence.PersistenceService) org.openremote.model.query.filter(org.openremote.model.query.filter) BiConsumer(java.util.function.BiConsumer) ManagerWebService(org.openremote.manager.web.ManagerWebService) org.openremote.model.asset(org.openremote.model.asset) MessageBrokerService(org.openremote.container.message.MessageBrokerService) User(org.openremote.model.security.User) Access(org.openremote.model.query.AssetQuery.Access) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) MetaItemType(org.openremote.model.value.MetaItemType) AssetQuery(org.openremote.model.query.AssetQuery) Pair(org.openremote.model.util.Pair) EntityManager(javax.persistence.EntityManager) Consumer(java.util.function.Consumer) EventRequestResponseWrapper(org.openremote.model.event.shared.EventRequestResponseWrapper) ClientEventService(org.openremote.manager.event.ClientEventService) ConstraintViolationException(javax.validation.ConstraintViolationException) TimerService(org.openremote.container.timer.TimerService) ConsoleResourceImpl(org.openremote.manager.asset.console.ConsoleResourceImpl) PredicateBuilder.or(org.apache.camel.builder.PredicateBuilder.or) PersistenceService.isPersistenceEventForEntityType(org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType) AttributeMap(org.openremote.model.attribute.AttributeMap) AssetQuery(org.openremote.model.query.AssetQuery)

Example 4 with AssetQuery

use of org.openremote.model.query.AssetQuery in project openremote by openremote.

the class AssetStorageService method appendWhereClause.

@SuppressWarnings("unchecked")
protected static boolean appendWhereClause(StringBuilder sb, AssetQuery query, int level, List<ParameterBinder> binders, Supplier<Long> timeProvider) {
    // level = 1 is main query
    // level = 2 is union
    // level = 3 is CTE
    boolean containsCalendarPredicate = false;
    boolean recursive = query.recursive;
    sb.append(" where true");
    if (level == 2) {
        return false;
    }
    if (level == 1 && query.ids != null && query.ids.length > 0) {
        final int pos = binders.size() + 1;
        sb.append(" and A.ID = ANY(?").append(pos).append(")");
        binders.add((em, st) -> st.setParameter(pos, query.ids, StringArrayType.INSTANCE));
    }
    if (level == 1 && query.names != null && query.names.length > 0) {
        sb.append(" and (");
        boolean isFirst = true;
        for (StringPredicate pred : query.names) {
            if (!isFirst) {
                sb.append(" or ");
            }
            isFirst = false;
            final int pos = binders.size() + 1;
            sb.append(pred.caseSensitive ? "A.NAME " : "upper(A.NAME)");
            sb.append(buildMatchFilter(pred, pos));
            binders.add((em, st) -> st.setParameter(pos, pred.prepareValue()));
        }
        sb.append(")");
    }
    if (query.parents != null && query.parents.length > 0) {
        sb.append(" and (");
        boolean isFirst = true;
        for (ParentPredicate pred : query.parents) {
            if (!isFirst) {
                sb.append(" or (");
            } else {
                sb.append("(");
            }
            isFirst = false;
            if (level == 1 && pred.id != null) {
                final int pos = binders.size() + 1;
                sb.append("A.PARENT_ID = ?").append(pos);
                binders.add((em, st) -> st.setParameter(pos, pred.id));
            } else if (level == 1) {
                sb.append("A.PARENT_ID is null");
            } else {
                sb.append("true");
            }
            sb.append(")");
        }
        sb.append(")");
    }
    if (level == 1 && query.paths != null && query.paths.length > 0) {
        sb.append(" and (");
        Arrays.stream(query.paths).map(p -> String.join(".", p.path)).forEach(lqueryStr -> {
            int pos = binders.size() + 1;
            sb.append("A.PATH ~ lquery(?").append(pos).append(") or ");
            binders.add((em, st) -> st.setParameter(pos, "*." + lqueryStr + ".*"));
        });
        sb.append("false)");
    }
    if (!recursive || level == 3) {
        if (query.tenant != null && !TextUtil.isNullOrEmpty(query.tenant.realm)) {
            final int pos = binders.size() + 1;
            sb.append(" and A.REALM = ?").append(pos);
            binders.add((em, st) -> st.setParameter(pos, query.tenant.realm));
        }
        if (query.userIds != null && query.userIds.length > 0) {
            final int pos = binders.size() + 1;
            sb.append(" and UA.USER_ID = ANY(?").append(pos).append(")");
            binders.add((em, st) -> st.setParameter(pos, query.userIds, StringArrayType.INSTANCE));
        }
        if (level == 1 && query.access == Access.PUBLIC) {
            sb.append(" and A.ACCESS_PUBLIC_READ is true");
        }
        if (query.types != null && query.types.length > 0) {
            String[] resolvedTypes = getResolvedAssetTypes(query.types);
            final int pos = binders.size() + 1;
            sb.append(" and A.TYPE = ANY(?").append(pos).append(")");
            binders.add((em, st) -> st.setParameter(pos, resolvedTypes, StringArrayType.INSTANCE));
        }
        if (query.attributes != null) {
            sb.append(" and A.id in (select A.id from ");
            AtomicInteger offset = new AtomicInteger(sb.length());
            Consumer<String> selectInserter = (str) -> sb.insert(offset.getAndAdd(str.length()), str);
            sb.append(" where true AND ");
            containsCalendarPredicate = addAttributePredicateGroupQuery(sb, binders, 0, selectInserter, query.attributes, timeProvider);
            sb.append(")");
        }
    }
    return containsCalendarPredicate;
}
Also used : ValuePredicate.asPredicateOrTrue(org.openremote.model.query.filter.ValuePredicate.asPredicateOrTrue) ClientRole(org.openremote.model.security.ClientRole) AuthContext(org.openremote.container.security.AuthContext) NoResultException(javax.persistence.NoResultException) ValueUtil(org.openremote.model.util.ValueUtil) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeEvent(org.openremote.model.attribute.AttributeEvent) GroupAsset(org.openremote.model.asset.impl.GroupAsset) TextUtil(org.openremote.model.util.TextUtil) ConstraintViolation(javax.validation.ConstraintViolation) LogicGroup(org.openremote.model.query.LogicGroup) Attribute.getAddedOrModifiedAttributes(org.openremote.model.attribute.Attribute.getAddedOrModifiedAttributes) Predicate(java.util.function.Predicate) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) ACCESS_RESTRICTED_READ(org.openremote.model.value.MetaItemType.ACCESS_RESTRICTED_READ) Query(javax.persistence.Query) StringArrayType(com.vladmihalcea.hibernate.type.array.StringArrayType) org.openremote.model(org.openremote.model) RouteBuilder(org.apache.camel.builder.RouteBuilder) PERSISTENCE_TOPIC(org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC) AssetInfo(org.openremote.model.event.shared.AssetInfo) EventSubscription(org.openremote.model.event.shared.EventSubscription) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) SharedEvent(org.openremote.model.event.shared.SharedEvent) GatewayService(org.openremote.manager.gateway.GatewayService) java.sql(java.sql) IntStream(java.util.stream.IntStream) AttributeMap(org.openremote.model.attribute.AttributeMap) EventSubscriptionAuthorizer(org.openremote.manager.event.EventSubscriptionAuthorizer) java.util(java.util) ACCESS_PUBLIC_READ(org.openremote.model.value.MetaItemType.ACCESS_PUBLIC_READ) CLIENT_EVENT_TOPIC(org.openremote.manager.event.ClientEventService.CLIENT_EVENT_TOPIC) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Session(org.hibernate.Session) TypedQuery(javax.persistence.TypedQuery) Supplier(java.util.function.Supplier) Level(java.util.logging.Level) AbstractReturningWork(org.hibernate.jdbc.AbstractReturningWork) PGobject(org.postgresql.util.PGobject) Attribute(org.openremote.model.attribute.Attribute) PersistenceService(org.openremote.container.persistence.PersistenceService) org.openremote.model.query.filter(org.openremote.model.query.filter) BiConsumer(java.util.function.BiConsumer) ManagerWebService(org.openremote.manager.web.ManagerWebService) org.openremote.model.asset(org.openremote.model.asset) MessageBrokerService(org.openremote.container.message.MessageBrokerService) User(org.openremote.model.security.User) Access(org.openremote.model.query.AssetQuery.Access) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) MetaItemType(org.openremote.model.value.MetaItemType) AssetQuery(org.openremote.model.query.AssetQuery) Pair(org.openremote.model.util.Pair) EntityManager(javax.persistence.EntityManager) Consumer(java.util.function.Consumer) EventRequestResponseWrapper(org.openremote.model.event.shared.EventRequestResponseWrapper) ClientEventService(org.openremote.manager.event.ClientEventService) ConstraintViolationException(javax.validation.ConstraintViolationException) TimerService(org.openremote.container.timer.TimerService) ConsoleResourceImpl(org.openremote.manager.asset.console.ConsoleResourceImpl) PredicateBuilder.or(org.apache.camel.builder.PredicateBuilder.or) PersistenceService.isPersistenceEventForEntityType(org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 5 with AssetQuery

use of org.openremote.model.query.AssetQuery in project openremote by openremote.

the class EmailNotificationHandler method getTargets.

@Override
public List<Notification.Target> getTargets(Notification.Source source, String sourceId, List<Notification.Target> targets, AbstractNotificationMessage message) {
    List<Notification.Target> mappedTargets = new ArrayList<>();
    if (targets != null) {
        targets.forEach(target -> {
            Notification.TargetType targetType = target.getType();
            String targetId = target.getId();
            switch(targetType) {
                case TENANT:
                case USER:
                    // Find all users in this tenant or by id
                    User[] users = targetType == Notification.TargetType.TENANT ? managerIdentityService.getIdentityProvider().queryUsers(new UserQuery().tenant(new TenantPredicate(targetId))) : managerIdentityService.getIdentityProvider().queryUsers(new UserQuery().ids(targetId));
                    if (users.length == 0) {
                        if (targetType == Notification.TargetType.USER) {
                            LOG.info("User not found: " + targetId);
                        } else {
                            LOG.info("No users found in target realm: " + targetId);
                        }
                        return;
                    }
                    mappedTargets.addAll(Arrays.stream(users).filter(user -> !Boolean.parseBoolean(user.getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED, Collections.singletonList("false")).get(0))).map(user -> {
                        Notification.Target userAssetTarget = new Notification.Target(Notification.TargetType.USER, user.getId());
                        userAssetTarget.setData(new EmailNotificationMessage.Recipient(user.getFullName(), user.getEmail()));
                        return userAssetTarget;
                    }).collect(Collectors.toList()));
                    break;
                case CUSTOM:
                    // Nothing to do here
                    mappedTargets.add(new Notification.Target(targetType, targetId));
                    break;
                case ASSET:
                    // Find descendant assets with email attribute
                    List<Asset<?>> assets = assetStorageService.findAll(new AssetQuery().select(new AssetQuery.Select().attributes(Asset.EMAIL.getName())).paths(new PathPredicate(targetId)).attributes(new AttributePredicate(new StringPredicate(Asset.EMAIL.getName()), new ValueEmptyPredicate().negate(true))));
                    if (assets.isEmpty()) {
                        LOG.fine("No assets with email attribute descendants of target asset");
                        return;
                    }
                    mappedTargets.addAll(assets.stream().map(asset -> {
                        Notification.Target assetTarget = new Notification.Target(Notification.TargetType.ASSET, asset.getId());
                        assetTarget.setData(new EmailNotificationMessage.Recipient(asset.getName(), asset.getEmail().orElse(null)));
                        return assetTarget;
                    }).collect(Collectors.toList()));
                    break;
            }
        });
    }
    EmailNotificationMessage email = (EmailNotificationMessage) message;
    // Map to/cc/bcc into a custom target for traceability in sent notifications
    List<String> addresses = new ArrayList<>();
    if (email.getTo() != null) {
        addresses.addAll(email.getTo().stream().map(EmailNotificationMessage.Recipient::getAddress).map(address -> "to:" + address).collect(Collectors.toList()));
        email.setTo((List<EmailNotificationMessage.Recipient>) null);
    }
    if (email.getCc() != null) {
        addresses.addAll(email.getCc().stream().map(EmailNotificationMessage.Recipient::getAddress).map(address -> "cc:" + address).collect(Collectors.toList()));
        email.setCc((List<EmailNotificationMessage.Recipient>) null);
    }
    if (email.getBcc() != null) {
        addresses.addAll(email.getBcc().stream().map(EmailNotificationMessage.Recipient::getAddress).map(address -> "bcc:" + address).collect(Collectors.toList()));
        email.setBcc((List<EmailNotificationMessage.Recipient>) null);
    }
    if (!addresses.isEmpty()) {
        mappedTargets.add(new Notification.Target(Notification.TargetType.CUSTOM, String.join(";", addresses)));
    }
    return mappedTargets;
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) Arrays(java.util.Arrays) AbstractNotificationMessage(org.openremote.model.notification.AbstractNotificationMessage) KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED(org.openremote.manager.security.ManagerKeycloakIdentityProvider.KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED) EmailPopulatingBuilder(org.simplejavamail.email.EmailPopulatingBuilder) TransportStrategy(org.simplejavamail.mailer.config.TransportStrategy) MapAccess.getInteger(org.openremote.container.util.MapAccess.getInteger) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Notification(org.openremote.model.notification.Notification) UserQuery(org.openremote.model.query.UserQuery) Recipient(org.simplejavamail.email.Recipient) org.openremote.model.query.filter(org.openremote.model.query.filter) EmailNotificationMessage(org.openremote.model.notification.EmailNotificationMessage) MapAccess.getBoolean(org.openremote.container.util.MapAccess.getBoolean) TextUtil(org.openremote.model.util.TextUtil) NotificationSendResult(org.openremote.model.notification.NotificationSendResult) User(org.openremote.model.security.User) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Asset(org.openremote.model.asset.Asset) MailerBuilder(org.simplejavamail.mailer.MailerBuilder) AssetQuery(org.openremote.model.query.AssetQuery) Mailer(org.simplejavamail.mailer.Mailer) ContainerService(org.openremote.model.ContainerService) Logger(java.util.logging.Logger) Constants(org.openremote.model.Constants) Collectors(java.util.stream.Collectors) Container(org.openremote.model.Container) List(java.util.List) EmailBuilder(org.simplejavamail.email.EmailBuilder) Email(org.simplejavamail.email.Email) Collections(java.util.Collections) EmailNotificationMessage(org.openremote.model.notification.EmailNotificationMessage) User(org.openremote.model.security.User) AssetQuery(org.openremote.model.query.AssetQuery) ArrayList(java.util.ArrayList) Recipient(org.simplejavamail.email.Recipient) Notification(org.openremote.model.notification.Notification) UserQuery(org.openremote.model.query.UserQuery) Asset(org.openremote.model.asset.Asset)

Aggregations

AssetQuery (org.openremote.model.query.AssetQuery)17 Asset (org.openremote.model.asset.Asset)9 Level (java.util.logging.Level)8 Logger (java.util.logging.Logger)8 Collectors (java.util.stream.Collectors)8 ManagerIdentityService (org.openremote.manager.security.ManagerIdentityService)7 java.util (java.util)6 RouteBuilder (org.apache.camel.builder.RouteBuilder)6 MessageBrokerService (org.openremote.container.message.MessageBrokerService)6 AssetStorageService (org.openremote.manager.asset.AssetStorageService)6 Container (org.openremote.model.Container)6 TextUtil (org.openremote.model.util.TextUtil)6 EntityManager (javax.persistence.EntityManager)5 PERSISTENCE_TOPIC (org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC)5 PersistenceService.isPersistenceEventForEntityType (org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType)5 TimerService (org.openremote.container.timer.TimerService)5 ClientEventService (org.openremote.manager.event.ClientEventService)5 GatewayService (org.openremote.manager.gateway.GatewayService)5 ContainerService (org.openremote.model.ContainerService)5 AttributeEvent (org.openremote.model.attribute.AttributeEvent)5