Search in sources :

Example 26 with Configuration

use of org.openhab.core.config.core.Configuration in project openhab-addons by openhab.

the class SmartherBridgeHandler method registerNotification.

@Override
public synchronized void registerNotification(String plantId) throws SmartherGatewayException {
    if (!config.isUseNotifications()) {
        return;
    }
    final ExpiringCache<List<Location>> localLocationCache = this.locationCache;
    if (localLocationCache != null) {
        List<Location> locations = localLocationCache.getValue();
        if (locations != null) {
            final Optional<Location> maybeLocation = locations.stream().filter(l -> l.getPlantId().equals(plantId)).findFirst();
            if (maybeLocation.isPresent()) {
                Location location = maybeLocation.get();
                if (!location.hasSubscription()) {
                    // Validate notification Url (must be non-null and https)
                    final String notificationUrl = config.getNotificationUrl();
                    if (isValidNotificationUrl(notificationUrl)) {
                        // Call gateway to register plant subscription
                        String subscriptionId = subscribePlant(plantId, config.getNotificationUrl());
                        logger.debug("Bridge[{}] Notification registered: [plantId={}, subscriptionId={}]", thing.getUID(), plantId, subscriptionId);
                        // Add the new subscription to notifications list
                        List<String> notifications = config.addNotification(subscriptionId);
                        // Save the updated notifications list back to bridge config
                        Configuration configuration = editConfiguration();
                        configuration.put(PROPERTY_NOTIFICATIONS, notifications);
                        updateConfiguration(configuration);
                        // Update the local locationCache with the added data
                        locations.stream().forEach(l -> {
                            if (l.getPlantId().equals(plantId)) {
                                l.setSubscription(subscriptionId, config.getNotificationUrl());
                            }
                        });
                        localLocationCache.putValue(locations);
                    } else {
                        logger.warn("Bridge[{}] Invalid notification Url [{}]: must be non-null, public https address", thing.getUID(), notificationUrl);
                    }
                }
            }
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) SmartherGatewayException(org.openhab.binding.bticinosmarther.internal.api.exception.SmartherGatewayException) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.openhab.core.library.types.OnOffType) Plant(org.openhab.binding.bticinosmarther.internal.api.dto.Plant) Program(org.openhab.binding.bticinosmarther.internal.api.dto.Program) StringUtil(org.openhab.binding.bticinosmarther.internal.util.StringUtil) InetAddress(java.net.InetAddress) HttpClient(org.eclipse.jetty.client.HttpClient) Location(org.openhab.binding.bticinosmarther.internal.api.dto.Location) Future(java.util.concurrent.Future) AccessTokenRefreshListener(org.openhab.core.auth.client.oauth2.AccessTokenRefreshListener) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) Duration(java.time.Duration) Subscription(org.openhab.binding.bticinosmarther.internal.api.dto.Subscription) URI(java.net.URI) Notification(org.openhab.binding.bticinosmarther.internal.api.dto.Notification) SmartherNotificationHandler(org.openhab.binding.bticinosmarther.internal.account.SmartherNotificationHandler) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) SmartherBindingConstants(org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants) ModuleStatus(org.openhab.binding.bticinosmarther.internal.api.dto.ModuleStatus) OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Channel(org.openhab.core.thing.Channel) List(java.util.List) SmartherAuthorizationException(org.openhab.binding.bticinosmarther.internal.api.exception.SmartherAuthorizationException) SmartherBridgeConfiguration(org.openhab.binding.bticinosmarther.internal.config.SmartherBridgeConfiguration) Optional(java.util.Optional) BaseBridgeHandler(org.openhab.core.thing.binding.BaseBridgeHandler) ExpiringCache(org.openhab.core.cache.ExpiringCache) BridgeStatus(org.openhab.binding.bticinosmarther.internal.model.BridgeStatus) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) ThingHandlerService(org.openhab.core.thing.binding.ThingHandlerService) SmartherAccountHandler(org.openhab.binding.bticinosmarther.internal.account.SmartherAccountHandler) ArrayList(java.util.ArrayList) SmartherApi(org.openhab.binding.bticinosmarther.internal.api.SmartherApi) ModuleSettings(org.openhab.binding.bticinosmarther.internal.model.ModuleSettings) ChannelUID(org.openhab.core.thing.ChannelUID) DecimalType(org.openhab.core.library.types.DecimalType) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) ThingUID(org.openhab.core.thing.ThingUID) State(org.openhab.core.types.State) RefreshType(org.openhab.core.types.RefreshType) IOException(java.io.IOException) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) UnknownHostException(java.net.UnknownHostException) TimeUnit(java.util.concurrent.TimeUnit) SmartherModuleDiscoveryService(org.openhab.binding.bticinosmarther.internal.discovery.SmartherModuleDiscoveryService) Sender(org.openhab.binding.bticinosmarther.internal.api.dto.Sender) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) Module(org.openhab.binding.bticinosmarther.internal.api.dto.Module) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) Collections(java.util.Collections) Bridge(org.openhab.core.thing.Bridge) Configuration(org.openhab.core.config.core.Configuration) SmartherBridgeConfiguration(org.openhab.binding.bticinosmarther.internal.config.SmartherBridgeConfiguration) List(java.util.List) ArrayList(java.util.ArrayList) Location(org.openhab.binding.bticinosmarther.internal.api.dto.Location)

Example 27 with Configuration

use of org.openhab.core.config.core.Configuration in project openhab-addons by openhab.

the class DmxBridgeHandlerTest method setUp.

@BeforeEach
public void setUp() {
    bridgeProperties = new HashMap<>();
    bridge = BridgeBuilder.create(THING_TYPE_TEST_BRIDGE, "testbridge").withLabel("Test Bridge").withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build()).withConfiguration(new Configuration(bridgeProperties)).build();
    ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
    doAnswer(answer -> {
        ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
        return null;
    }).when(mockCallback).statusUpdated(any(), any());
    bridgeHandler = Mockito.spy(new DmxBridgeHandlerImpl(bridge));
    bridgeHandler.getThing().setHandler(bridgeHandler);
    bridgeHandler.setCallback(mockCallback);
    bridgeHandler.initialize();
}
Also used : Configuration(org.openhab.core.config.core.Configuration) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Thing(org.openhab.core.thing.Thing) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 28 with Configuration

use of org.openhab.core.config.core.Configuration in project openhab-addons by openhab.

the class Lib485BridgeHandlerTest method setUp.

@BeforeEach
public void setUp() {
    bridgeProperties = new HashMap<>();
    bridgeProperties.put(CONFIG_ADDRESS, TEST_ADDRESS);
    bridge = BridgeBuilder.create(THING_TYPE_LIB485_BRIDGE, "lib485bridge").withLabel("Lib485 Bridge").withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build()).withConfiguration(new Configuration(bridgeProperties)).build();
    ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
    doAnswer(answer -> {
        ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
        return null;
    }).when(mockCallback).statusUpdated(any(), any());
    bridgeHandler = new Lib485BridgeHandler(bridge) {

        @Override
        protected void validateConfigurationParameters(Map<String, Object> configurationParameters) {
        }
    };
    bridgeHandler.getThing().setHandler(bridgeHandler);
    bridgeHandler.setCallback(mockCallback);
    bridgeHandler.initialize();
}
Also used : Configuration(org.openhab.core.config.core.Configuration) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Thing(org.openhab.core.thing.Thing) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 29 with Configuration

use of org.openhab.core.config.core.Configuration in project openhab-addons by openhab.

the class ArtnetBridgeHandlerTest method setUp.

@BeforeEach
public void setUp() {
    bridgeProperties = new HashMap<>();
    bridgeProperties.put(CONFIG_ADDRESS, TEST_ADDRESS);
    bridgeProperties.put(CONFIG_UNIVERSE, TEST_UNIVERSE);
    bridge = BridgeBuilder.create(THING_TYPE_ARTNET_BRIDGE, "artnetbridge").withLabel("Artnet Bridge").withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build()).withConfiguration(new Configuration(bridgeProperties)).build();
    ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
    doAnswer(answer -> {
        ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
        return null;
    }).when(mockCallback).statusUpdated(any(), any());
    bridgeHandler = new ArtnetBridgeHandler(bridge) {

        @Override
        protected void validateConfigurationParameters(Map<String, Object> configurationParameters) {
        }
    };
    bridgeHandler.getThing().setHandler(bridgeHandler);
    bridgeHandler.setCallback(mockCallback);
    bridgeHandler.initialize();
}
Also used : Configuration(org.openhab.core.config.core.Configuration) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Thing(org.openhab.core.thing.Thing) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 30 with Configuration

use of org.openhab.core.config.core.Configuration in project openhab-addons by openhab.

the class EnOceanBaseSensorHandler method packetReceived.

@Override
public void packetReceived(BasePacket packet) {
    ERP1Message msg = (ERP1Message) packet;
    EEPType receivingEEPType = receivingEEPTypes.get(msg.getRORG());
    if (receivingEEPType == null) {
        return;
    }
    EEP eep = EEPFactory.buildEEP(receivingEEPType, (ERP1Message) packet);
    logger.debug("ESP Packet payload {} for {} received", HexUtils.bytesToHex(packet.getPayload()), HexUtils.bytesToHex(msg.getSenderId()));
    if (eep.isValid()) {
        byte[] senderId = msg.getSenderId();
        // try to interpret received message for all linked or trigger channels
        getThing().getChannels().stream().filter(channelFilter(receivingEEPType, senderId)).sorted(// handle state channels first
        Comparator.comparing(Channel::getKind)).forEachOrdered(channel -> {
            ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
            String channelTypeId = (channelTypeUID != null) ? channelTypeUID.getId() : "";
            String channelId = channel.getUID().getId();
            Configuration channelConfig = channel.getConfiguration();
            switch(channel.getKind()) {
                case STATE:
                    State result = eep.convertToState(channelId, channelTypeId, channelConfig, this::getCurrentState);
                    // if message can be interpreted (result != UnDefType.UNDEF) => update item state
                    if (result != null && result != UnDefType.UNDEF) {
                        updateState(channelId, result);
                    }
                    break;
                case TRIGGER:
                    String lastEvent = lastEvents.get(channelId);
                    String event = eep.convertToEvent(channelId, channelTypeId, lastEvent, channelConfig);
                    if (event != null) {
                        triggerChannel(channel.getUID(), event);
                        lastEvents.put(channelId, event);
                    }
                    break;
            }
        });
        if (receivingEEPType.getRequstesResponse()) {
            // fire trigger for receive
            triggerChannel(prepareAnswer, "requestAnswer");
            // Send response after 100ms
            if (responseFuture == null || responseFuture.isDone()) {
                responseFuture = scheduler.schedule(this::sendRequestResponse, 100, TimeUnit.MILLISECONDS);
            }
        }
    }
}
Also used : EEPType(org.openhab.binding.enocean.internal.eep.EEPType) EEP(org.openhab.binding.enocean.internal.eep.EEP) ERP1Message(org.openhab.binding.enocean.internal.messages.ERP1Message) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) Configuration(org.openhab.core.config.core.Configuration) State(org.openhab.core.types.State) Channel(org.openhab.core.thing.Channel)

Aggregations

Configuration (org.openhab.core.config.core.Configuration)498 Test (org.junit.jupiter.api.Test)193 Thing (org.openhab.core.thing.Thing)97 Channel (org.openhab.core.thing.Channel)62 HashMap (java.util.HashMap)60 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)59 ChannelUID (org.openhab.core.thing.ChannelUID)50 ThingUID (org.openhab.core.thing.ThingUID)50 Bridge (org.openhab.core.thing.Bridge)49 Nullable (org.eclipse.jdt.annotation.Nullable)39 BeforeEach (org.junit.jupiter.api.BeforeEach)39 BigDecimal (java.math.BigDecimal)35 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)32 IOException (java.io.IOException)31 ArrayList (java.util.ArrayList)26 Rule (org.openhab.core.automation.Rule)24 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)24 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)22 Action (org.openhab.core.automation.Action)19 Condition (org.openhab.core.automation.Condition)19