Search in sources :

Example 6 with ServerAsset

use of org.openremote.manager.asset.ServerAsset in project openremote by openremote.

the class ManagerDemoSetup method onStart.

@Override
public void onStart() throws Exception {
    KeycloakDemoSetup keycloakDemoSetup = setupService.getTaskOfType(KeycloakDemoSetup.class);
    Tenant masterTenant = keycloakDemoSetup.masterTenant;
    Tenant customerATenant = keycloakDemoSetup.customerATenant;
    masterRealmId = masterTenant.getId();
    customerARealmId = customerATenant.getId();
    // ################################ Demo assets for 'master' realm ###################################
    ServerAsset smartOffice = new ServerAsset();
    smartOffice.setRealmId(masterTenant.getId());
    smartOffice.setName("Smart Office");
    smartOffice.setLocation(geometryFactory.createPoint(new Coordinate(5.460315214821094, 51.44541688237109)));
    smartOffice.setType(BUILDING);
    List<AssetAttribute> smartOfficeAttributes = Arrays.asList(new AssetAttribute("geoStreet", STRING, Values.create("Torenallee 20")).setMeta(new MetaItem(LABEL, Values.create("Street")), new MetaItem(ABOUT, Values.create("http://project-haystack.org/tag/geoStreet"))), new AssetAttribute("geoPostalCode", AttributeType.NUMBER, Values.create(5617)).setMeta(new MetaItem(LABEL, Values.create("Postal Code")), new MetaItem(ABOUT, Values.create("http://project-haystack.org/tag/geoPostalCode"))), new AssetAttribute("geoCity", STRING, Values.create("Eindhoven")).setMeta(new MetaItem(LABEL, Values.create("City")), new MetaItem(ABOUT, Values.create("http://project-haystack.org/tag/geoCity"))), new AssetAttribute("geoCountry", STRING, Values.create("Netherlands")).setMeta(new MetaItem(LABEL, Values.create("Country")), new MetaItem(ABOUT, Values.create("http://project-haystack.org/tag/geoCountry"))));
    smartOffice.setAttributes(smartOfficeAttributes);
    smartOffice = assetStorageService.merge(smartOffice);
    smartOfficeId = smartOffice.getId();
    ServerAsset groundFloor = new ServerAsset("Ground Floor", FLOOR, smartOffice);
    groundFloor.setLocation(smartOffice.getLocation());
    groundFloor = assetStorageService.merge(groundFloor);
    groundFloorId = groundFloor.getId();
    ServerAsset lobby = new ServerAsset("Lobby", ROOM, groundFloor);
    lobby.setLocation(groundFloor.getLocation());
    lobby = assetStorageService.merge(lobby);
    lobbyId = lobby.getId();
    ServerAsset agent = new ServerAsset("Demo Agent", AGENT, lobby);
    agent.setLocation(lobby.getLocation());
    agent.setAttributes(initProtocolConfiguration(new AssetAttribute(agentProtocolConfigName), SimulatorProtocol.PROTOCOL_NAME).addMeta(new MetaItem(SimulatorProtocol.CONFIG_MODE, Values.create(SimulatorProtocol.Mode.WRITE_THROUGH_DELAYED.toString())), new MetaItem(SimulatorProtocol.CONFIG_WRITE_DELAY_MILLISECONDS, Values.create(500))));
    agent = assetStorageService.merge(agent);
    agentId = agent.getId();
    ServerAsset thing = new ServerAsset("Demo Thing", THING, agent);
    thing.setLocation(agent.getLocation());
    thing.setAttributes(new AssetAttribute(thingLightToggleAttributeName, BOOLEAN, Values.create(true)).setMeta(new Meta(new MetaItem(LABEL, Values.create("Light 1 Toggle")), new MetaItem(DESCRIPTION, Values.create("Switch for living room light")), new MetaItem(STORE_DATA_POINTS, Values.create(true)), new MetaItem(AGENT_LINK, new AttributeRef(agent.getId(), agentProtocolConfigName).toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(SwitchSimulatorElement.ELEMENT_NAME)))), // No initial value!
    new AssetAttribute("light1Dimmer", PERCENTAGE).setMeta(new Meta(new MetaItem(LABEL, Values.create("Light 1 Dimmer")), new MetaItem(DESCRIPTION, Values.create("Dimmer for living room light")), new MetaItem(RANGE_MIN, Values.create(0)), new MetaItem(RANGE_MAX, Values.create(100)), new MetaItem(AGENT_LINK, new AttributeRef(agent.getId(), agentProtocolConfigName).toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME_RANGE)), new MetaItem(SimulatorProtocol.CONFIG_MODE, Values.create(SimulatorProtocol.Mode.WRITE_THROUGH_DELAYED.toString())))), new AssetAttribute("light1Color", COLOR_RGB, new ColorRGB(88, 123, 88).asArrayValue()).setMeta(new Meta(new MetaItem(LABEL, Values.create("Light 1 Color")), new MetaItem(DESCRIPTION, Values.create("Color of living room light")), new MetaItem(AGENT_LINK, new AttributeRef(agent.getId(), agentProtocolConfigName).toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(ColorSimulatorElement.ELEMENT_NAME)))), new AssetAttribute("light1PowerConsumption", ENERGY_KWH, Values.create(12.345)).setMeta(new Meta(new MetaItem(LABEL, Values.create("Light 1 Usage")), new MetaItem(DESCRIPTION, Values.create("Total energy consumption of living room light")), new MetaItem(READ_ONLY, Values.create(true)), new MetaItem(FORMAT, Values.create("%3d kWh")), new MetaItem(AGENT_LINK, new AttributeRef(agent.getId(), agentProtocolConfigName).toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)), new MetaItem(STORE_DATA_POINTS, Values.create(true)))));
    thing = assetStorageService.merge(thing);
    thingId = thing.getId();
    // Some sample datapoints
    final ServerAsset finalThing = assetStorageService.find(thingId, true);
    ZonedDateTime now = LocalDateTime.now().atZone(ZoneId.systemDefault());
    AssetAttribute light1PowerConsumptionAttribute = thing.getAttribute("light1PowerConsumption").orElseThrow(() -> new RuntimeException("Invalid test data"));
    persistenceService.doTransaction(em -> {
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(0.11), now.minusDays(80).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(1.22), now.minusDays(40).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(2.33), now.minusDays(20).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(3.44), now.minusDays(10).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(4.55), now.minusDays(8).toEpochSecond() * 1000);
        light1PowerConsumptionAttribute.setValue(Values.create(5.66), now.minusDays(6).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(6.77), now.minusDays(3).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(7.88), now.minusDays(1).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(8.99), now.minusHours(10).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(9.11), now.minusHours(5).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(10.22), now.minusHours(2).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(11.33), now.minusHours(1).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(11.44), now.minusMinutes(30).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(12.00), now.minusMinutes(5).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(12.11), now.minusSeconds(5).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
        light1PowerConsumptionAttribute.setValue(Values.create(12.22), now.minusSeconds(1).toEpochSecond() * 1000);
        assetDatapointService.processAssetUpdate(em, finalThing, light1PowerConsumptionAttribute, AttributeEvent.Source.SENSOR);
    });
    // ################################ Demo assets for 'customerA' realm ###################################
    ServerAsset smartHome = new ServerAsset();
    smartHome.setRealmId(customerATenant.getId());
    smartHome.setName("Smart Home");
    smartHome.setLocation(geometryFactory.createPoint(new Coordinate(5.470945, 51.438000)));
    smartHome.setType(BUILDING);
    smartHome.setAttributes(new AssetAttribute("geoStreet", STRING, Values.create("Wilhelminaplein 21C")).setMeta(new MetaItem(LABEL, Values.create("Street")), new MetaItem(ABOUT, Values.create("http://project-haystack.org/tag/geoStreet"))), new AssetAttribute("geoPostalCode", AttributeType.NUMBER, Values.create(5611)).setMeta(new MetaItem(LABEL, Values.create("Postal Code")), new MetaItem(ABOUT, Values.create("http://project-haystack.org/tag/geoPostalCode"))), new AssetAttribute("geoCity", STRING, Values.create("Eindhoven")).setMeta(new MetaItem(LABEL, Values.create("City")), new MetaItem(ABOUT, Values.create("http://project-haystack.org/tag/geoCity"))), new AssetAttribute("geoCountry", STRING, Values.create("Netherlands")).setMeta(new MetaItem(LABEL, Values.create("Country")), new MetaItem(ABOUT, Values.create("http://project-haystack.org/tag/geoCountry"))));
    smartHome = assetStorageService.merge(smartHome);
    smartHomeId = smartHome.getId();
    // The "Apartment 1" is the demo apartment with complex scenes
    ServerAsset apartment1 = createDemoApartment(smartHome, "Apartment 1");
    apartment1 = assetStorageService.merge(apartment1);
    apartment1Id = apartment1.getId();
    ServerAsset apartment1ServiceAgent = new ServerAsset("Service Agent (Simulator)", AGENT, apartment1);
    apartment1ServiceAgent.setAttributes(initProtocolConfiguration(new AssetAttribute("apartmentSimulator"), SimulatorProtocol.PROTOCOL_NAME).addMeta(new MetaItem(SimulatorProtocol.CONFIG_MODE, Values.create(SimulatorProtocol.Mode.WRITE_THROUGH_IMMEDIATE.toString()))));
    apartment1ServiceAgent = assetStorageService.merge(apartment1ServiceAgent);
    apartment1ServiceAgentId = apartment1ServiceAgent.getId();
    /* ############################ ROOMS ############################## */
    ServerAsset apartment1Livingroom = createDemoApartmentRoom(apartment1, "Living Room");
    addDemoApartmentRoomMotionSensor(apartment1Livingroom, true, () -> new MetaItem[] { new MetaItem(AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)) });
    addDemoApartmentRoomCO2Sensor(apartment1Livingroom, true, () -> new MetaItem[] { new MetaItem(AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)) });
    addDemoApartmentRoomHumiditySensor(apartment1Livingroom, true, () -> new MetaItem[] { new MetaItem(AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)) });
    addDemoApartmentRoomThermometer(apartment1Livingroom, true, () -> new MetaItem[] { new MetaItem(AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)) });
    addDemoApartmentTemperatureControl(apartment1Livingroom, true, () -> new MetaItem[] { new MetaItem(AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)) });
    apartment1Livingroom = assetStorageService.merge(apartment1Livingroom);
    apartment1LivingroomId = apartment1Livingroom.getId();
    ServerAsset apartment1Kitchen = createDemoApartmentRoom(apartment1, "Kitchen");
    addDemoApartmentRoomMotionSensor(apartment1Kitchen, true, () -> new MetaItem[] { new MetaItem(AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)) });
    for (String switchName : new String[] { "A", "B", "C" }) {
        addDemoApartmentSmartSwitch(apartment1Kitchen, switchName, true, attributeIndex -> {
            switch(attributeIndex) {
                case 2:
                    return new MetaItem[] { new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)) };
                case 3:
                    return new MetaItem[] { new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)) };
                case 4:
                    return new MetaItem[] { new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)) };
            }
            return null;
        });
    }
    apartment1Kitchen = assetStorageService.merge(apartment1Kitchen);
    apartment1KitchenId = apartment1Kitchen.getId();
    ServerAsset apartment1Hallway = createDemoApartmentRoom(apartment1, "Hallway");
    addDemoApartmentRoomMotionSensor(apartment1Hallway, true, () -> new MetaItem[] { new MetaItem(AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)) });
    apartment1Hallway = assetStorageService.merge(apartment1Hallway);
    apartment1HallwayId = apartment1Hallway.getId();
    addDemoApartmentVentilation(apartment1, true, () -> new MetaItem[] { new MetaItem(AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()), new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME)) });
    apartment1 = assetStorageService.merge(apartment1);
    if (importDemoScenes) {
        Scene[] scenes = new Scene[] { new Scene("homeScene", "Home scene", "HOME", "0 0 7 ? *", false, 21d), new Scene("awayScene", "Away scene", "AWAY", "0 30 8 ? *", true, 15d), new Scene("eveningScene", "Evening scene", "EVENING", "0 30 17 ? *", false, 22d), new Scene("nightScene", "Night scene", "NIGHT", "0 0 22 ? *", true, 19d) };
        ServerAsset demoApartmentSceneAgent = createDemoApartmentSceneAgent(apartment1, scenes, apartment1Livingroom, apartment1Kitchen, apartment1Hallway);
        demoApartmentSceneAgent = assetStorageService.merge(demoApartmentSceneAgent);
        linkDemoApartmentWithSceneAgent(apartment1, demoApartmentSceneAgent, scenes);
        apartment1 = assetStorageService.merge(apartment1);
    }
    ServerAsset apartment2 = new ServerAsset("Apartment 2", RESIDENCE, smartHome);
    apartment2.setLocation(smartHome.getLocation());
    apartment2.setAttributes(new AssetAttribute("allLightsOffSwitch", AttributeType.BOOLEAN, Values.create(true)).setMeta(new MetaItem(LABEL, Values.create("All Lights Off Switch")), new MetaItem(DESCRIPTION, Values.create("When triggered, turns all lights in the apartment off")), new MetaItem(RULE_EVENT, Values.create(true)), new MetaItem(RULE_EVENT_EXPIRES, Values.create("3s"))));
    apartment2 = assetStorageService.merge(apartment2);
    apartment2Id = apartment2.getId();
    ServerAsset apartment2Livingroom = new ServerAsset("Living Room", ROOM, apartment2);
    apartment2Livingroom.setLocation(apartment2.getLocation());
    apartment2Livingroom.setAttributes(new AssetAttribute("motionSensor", AttributeType.BOOLEAN, Values.create(false)).setMeta(new MetaItem(LABEL, Values.create("Motion Sensor")), new MetaItem(DESCRIPTION, Values.create("PIR sensor that sends 'true' when motion is sensed")), new MetaItem(RULE_STATE, Values.create(true)), new MetaItem(RULE_EVENT, Values.create(true))), new AssetAttribute("presenceDetected", AttributeType.BOOLEAN, Values.create(false)).setMeta(new MetaItem(LABEL, Values.create("Presence Detected")), new MetaItem(DESCRIPTION, Values.create("Someone is currently present in the room")), new MetaItem(RULE_STATE, Values.create(true))), new AssetAttribute("firstPresenceDetected", AttributeType.TIMESTAMP_MILLIS).setMeta(new MetaItem(LABEL, Values.create("First Presence Timestamp")), new MetaItem(DESCRIPTION, Values.create("Timestamp of the first detected presence")), new MetaItem(RULE_STATE, Values.create(true))), new AssetAttribute("lastPresenceDetected", AttributeType.TIMESTAMP_MILLIS).setMeta(new MetaItem(LABEL, Values.create("Last Presence Timestamp")), new MetaItem(DESCRIPTION, Values.create("Timestamp of last detected presence")), new MetaItem(RULE_STATE, Values.create(true))), new AssetAttribute("co2Level", AttributeType.CO2_PPM, Values.create(350)).setMeta(new MetaItem(LABEL, Values.create("CO2 Level")), new MetaItem(RULE_STATE, Values.create(true))), new AssetAttribute("lightSwitch", AttributeType.BOOLEAN, Values.create(true)).setMeta(new MetaItem(LABEL, Values.create("Light Switch")), new MetaItem(RULE_STATE, Values.create(true))), new AssetAttribute("windowOpen", AttributeType.BOOLEAN, Values.create(false)).setMeta(new MetaItem(ACCESS_RESTRICTED_READ, Values.create(true))));
    apartment2Livingroom = assetStorageService.merge(apartment2Livingroom);
    apartment2LivingroomId = apartment2Livingroom.getId();
    ServerAsset apartment2Bathroom = new ServerAsset("Bathroom", ROOM, apartment2);
    apartment2Bathroom.setLocation(apartment2.getLocation());
    apartment2Bathroom.setAttributes(new AssetAttribute("motionSensor", AttributeType.BOOLEAN, Values.create(false)).setMeta(new MetaItem(LABEL, Values.create("Motion Sensor")), new MetaItem(DESCRIPTION, Values.create("PIR sensor that sends 'true' when motion is sensed")), new MetaItem(RULE_STATE, Values.create(true)), new MetaItem(RULE_EVENT, Values.create(true))), new AssetAttribute("presenceDetected", AttributeType.BOOLEAN, Values.create(false)).setMeta(new MetaItem(LABEL, Values.create("Presence Detected")), new MetaItem(DESCRIPTION, Values.create("Someone is currently present in the room")), new MetaItem(RULE_STATE, Values.create(true))), new AssetAttribute("firstPresenceDetected", AttributeType.TIMESTAMP_MILLIS).setMeta(new MetaItem(LABEL, Values.create("First Presence Timestamp")), new MetaItem(DESCRIPTION, Values.create("Timestamp of the first detected presence")), new MetaItem(RULE_STATE, Values.create(true))), new AssetAttribute("lastPresenceDetected", AttributeType.TIMESTAMP_MILLIS).setMeta(new MetaItem(LABEL, Values.create("Last Presence Timestamp")), new MetaItem(DESCRIPTION, Values.create("Timestamp of last detected presence")), new MetaItem(RULE_STATE, Values.create(true))), new AssetAttribute("lightSwitch", AttributeType.BOOLEAN, Values.create(true)).setMeta(new MetaItem(LABEL, Values.create("Light Switch")), new MetaItem(RULE_STATE, Values.create(true))));
    apartment2Bathroom = assetStorageService.merge(apartment2Bathroom);
    apartment2BathroomId = apartment2Bathroom.getId();
    ServerAsset apartment3 = new ServerAsset("Apartment 3", RESIDENCE, smartHome);
    apartment3.setLocation(smartHome.getLocation());
    apartment3 = assetStorageService.merge(apartment3);
    apartment3Id = apartment3.getId();
    ServerAsset apartment3Livingroom = new ServerAsset("Living Room", ROOM, apartment3);
    apartment3Livingroom.setLocation(apartment3.getLocation());
    apartment3Livingroom.addAttributes(new AssetAttribute("lightSwitch", AttributeType.BOOLEAN));
    apartment3Livingroom = assetStorageService.merge(apartment3Livingroom);
    apartment3LivingroomId = apartment3Livingroom.getId();
    // ################################ Link demo users and assets ###################################
    assetStorageService.storeUserAsset(new UserAsset(keycloakDemoSetup.customerATenant.getId(), keycloakDemoSetup.testuser3Id, apartment1Id));
    assetStorageService.storeUserAsset(new UserAsset(keycloakDemoSetup.customerATenant.getId(), keycloakDemoSetup.testuser3Id, apartment1LivingroomId));
    assetStorageService.storeUserAsset(new UserAsset(keycloakDemoSetup.customerATenant.getId(), keycloakDemoSetup.testuser3Id, apartment1KitchenId));
    assetStorageService.storeUserAsset(new UserAsset(keycloakDemoSetup.customerATenant.getId(), keycloakDemoSetup.testuser3Id, apartment1HallwayId));
    assetStorageService.storeUserAsset(new UserAsset(keycloakDemoSetup.customerATenant.getId(), keycloakDemoSetup.testuser3Id, apartment2Id));
}
Also used : AssetMeta(org.openremote.model.asset.AssetMeta) UserAsset(org.openremote.model.asset.UserAsset) ServerAsset(org.openremote.manager.asset.ServerAsset) Tenant(org.openremote.model.security.Tenant) Coordinate(com.vividsolutions.jts.geom.Coordinate) ZonedDateTime(java.time.ZonedDateTime) AssetAttribute(org.openremote.model.asset.AssetAttribute)

Example 7 with ServerAsset

use of org.openremote.manager.asset.ServerAsset in project openremote by openremote.

the class AssetDatapointResourceImpl method getNumberDatapoints.

@Override
public NumberDatapoint[] getNumberDatapoints(@BeanParam RequestParams requestParams, String assetId, String attributeName, DatapointInterval interval, long timestamp) {
    try {
        if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), assetId)) {
            throw new WebApplicationException(Response.Status.FORBIDDEN);
        }
        ServerAsset asset = assetStorageService.find(assetId, true);
        if (asset == null) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        if (!isTenantActiveAndAccessible(asset)) {
            LOG.fine("Forbidden access for user '" + getUsername() + "': " + asset);
            throw new WebApplicationException(Response.Status.FORBIDDEN);
        }
        AssetAttribute attribute = asset.getAttribute(attributeName).orElseThrow(() -> new WebApplicationException(Response.Status.NOT_FOUND));
        if (!Datapoint.isDatapointsCapable(attribute) || !attribute.isStoreDatapoints()) {
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }
        return assetDatapointService.aggregateDatapoints(attribute, interval, timestamp);
    } catch (IllegalStateException ex) {
        throw new WebApplicationException(ex, Response.Status.BAD_REQUEST);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ServerAsset(org.openremote.manager.asset.ServerAsset) AssetAttribute(org.openremote.model.asset.AssetAttribute)

Example 8 with ServerAsset

use of org.openremote.manager.asset.ServerAsset in project openremote by openremote.

the class AssetsFacade method query.

@Override
public Assets.RestrictedQuery query() {
    Assets.RestrictedQuery query = new Assets.RestrictedQuery() {

        @Override
        public Assets.RestrictedQuery select(Select select) {
            throw new IllegalArgumentException("Overriding query projection is not allowed in this rules scope");
        }

        @Override
        public Assets.RestrictedQuery id(String id) {
            throw new IllegalArgumentException("Overriding query restriction is not allowed in this rules scope");
        }

        @Override
        public Assets.RestrictedQuery tenant(TenantPredicate tenantPredicate) {
            if (GlobalRuleset.class.isAssignableFrom(rulesEngineId.getScope()))
                return super.tenant(tenantPredicate);
            throw new IllegalArgumentException("Overriding query restriction is not allowed in this rules scope");
        }

        @Override
        public Assets.RestrictedQuery userId(String userId) {
            throw new IllegalArgumentException("Overriding query restriction is not allowed in this rules scope");
        }

        @Override
        public Assets.RestrictedQuery orderBy(OrderBy orderBy) {
            throw new IllegalArgumentException("Overriding query result order is not allowed in this rules scope");
        }

        @Override
        public String getResult() {
            ServerAsset asset = assetStorageService.find(this);
            return asset != null ? asset.getId() : null;
        }

        @Override
        public Stream<String> stream() {
            if (this.select == null)
                this.select = new Select();
            Include oldValue = this.select.include;
            this.select.include = Include.ONLY_ID_AND_NAME;
            try {
                return assetStorageService.findAll(this).stream().map(Asset::getId);
            } finally {
                this.select.include = oldValue;
            }
        }
    };
    if (TenantRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {
        query.tenant = new TenantPredicate(rulesEngineId.getRealmId().orElseThrow(() -> new IllegalArgumentException("Realm ID missing: " + rulesEngineId)));
    }
    if (AssetRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {
        ServerAsset restrictedAsset = assetStorageService.find(rulesEngineId.getAssetId().orElseThrow(() -> new IllegalStateException("Asset ID missing: " + rulesEngineId)), true);
        if (restrictedAsset == null) {
            throw new IllegalStateException("Asset is no longer available: " + rulesEngineId);
        }
        query.path = new PathPredicate(restrictedAsset.getPath());
    }
    return query;
}
Also used : PathPredicate(org.openremote.model.asset.AssetQuery.PathPredicate) ServerAsset(org.openremote.manager.asset.ServerAsset) TenantPredicate(org.openremote.model.asset.AssetQuery.TenantPredicate) Asset(org.openremote.model.asset.Asset) ServerAsset(org.openremote.manager.asset.ServerAsset)

Example 9 with ServerAsset

use of org.openremote.manager.asset.ServerAsset in project openremote by openremote.

the class AbstractManagerSetup method createDemoApartmentSceneAgent.

protected ServerAsset createDemoApartmentSceneAgent(ServerAsset apartment, Scene[] scenes, ServerAsset... rooms) {
    ServerAsset agent = new ServerAsset("Scene Agent", AGENT, apartment);
    agent.setLocation(apartment.getLocation());
    for (Scene scene : scenes) {
        agent.addAttributes(scene.createMacroAttribute(apartment, rooms));
    }
    for (Scene scene : scenes) {
        agent.addAttributes(scene.createTimerAttributes(apartment));
    }
    addDemoApartmentSceneEnableDisableTimer(apartment, agent, scenes);
    return agent;
}
Also used : ServerAsset(org.openremote.manager.asset.ServerAsset)

Aggregations

ServerAsset (org.openremote.manager.asset.ServerAsset)9 AssetAttribute (org.openremote.model.asset.AssetAttribute)4 Asset (org.openremote.model.asset.Asset)2 AssetMeta (org.openremote.model.asset.AssetMeta)2 Tenant (org.openremote.model.security.Tenant)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 ZonedDateTime (java.time.ZonedDateTime)1 Consumer (java.util.function.Consumer)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 PathPredicate (org.openremote.model.asset.AssetQuery.PathPredicate)1 TenantPredicate (org.openremote.model.asset.AssetQuery.TenantPredicate)1 UserAsset (org.openremote.model.asset.UserAsset)1 MetaItem (org.openremote.model.attribute.MetaItem)1 UserQuery (org.openremote.model.user.UserQuery)1