Search in sources :

Example 1 with TradfriCoapClient

use of org.eclipse.smarthome.binding.tradfri.internal.TradfriCoapClient in project smarthome by eclipse.

the class TradfriGatewayHandler method establishConnection.

private void establishConnection() {
    TradfriGatewayConfig configuration = getConfigAs(TradfriGatewayConfig.class);
    this.gatewayURI = "coaps://" + configuration.host + ":" + configuration.port + "/" + DEVICES;
    this.gatewayInfoURI = "coaps://" + configuration.host + ":" + configuration.port + "/" + GATEWAY + "/" + GATEWAY_DETAILS;
    try {
        URI uri = new URI(gatewayURI);
        deviceClient = new TradfriCoapClient(uri);
    } catch (URISyntaxException e) {
        logger.error("Illegal gateway URI '{}': {}", gatewayURI, e.getMessage());
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
        return;
    }
    DtlsConnectorConfig.Builder builder = new DtlsConnectorConfig.Builder(new InetSocketAddress(0));
    builder.setPskStore(new StaticPskStore(configuration.identity, configuration.preSharedKey.getBytes()));
    dtlsConnector = new DTLSConnector(builder.build());
    endPoint = new TradfriCoapEndpoint(dtlsConnector, NetworkConfig.getStandard());
    deviceClient.setEndpoint(endPoint);
    updateStatus(ThingStatus.UNKNOWN);
    // schedule a new scan every minute
    scanJob = scheduler.scheduleWithFixedDelay(this::startScan, 0, 1, TimeUnit.MINUTES);
}
Also used : StaticPskStore(org.eclipse.californium.scandium.dtls.pskstore.StaticPskStore) TradfriGatewayConfig(org.eclipse.smarthome.binding.tradfri.internal.config.TradfriGatewayConfig) InetSocketAddress(java.net.InetSocketAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) TradfriCoapEndpoint(org.eclipse.smarthome.binding.tradfri.internal.TradfriCoapEndpoint) TradfriCoapClient(org.eclipse.smarthome.binding.tradfri.internal.TradfriCoapClient)

Example 2 with TradfriCoapClient

use of org.eclipse.smarthome.binding.tradfri.internal.TradfriCoapClient in project smarthome by eclipse.

the class TradfriThingHandler method initialize.

@Override
public synchronized void initialize() {
    Bridge tradfriGateway = getBridge();
    this.id = getConfigAs(TradfriDeviceConfig.class).id;
    TradfriGatewayHandler handler = (TradfriGatewayHandler) tradfriGateway.getHandler();
    String uriString = handler.getGatewayURI() + "/" + id;
    try {
        URI uri = new URI(uriString);
        coapClient = new TradfriCoapClient(uri);
        coapClient.setEndpoint(handler.getEndpoint());
    } catch (URISyntaxException e) {
        logger.debug("Illegal device URI `{}`: {}", uriString, e.getMessage());
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
        return;
    }
    active = true;
    updateStatus(ThingStatus.UNKNOWN);
    switch(tradfriGateway.getStatus()) {
        case ONLINE:
            scheduler.schedule(() -> {
                coapClient.startObserve(this);
            }, 3, TimeUnit.SECONDS);
            break;
        case OFFLINE:
        default:
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, String.format("Gateway offline '%s'", tradfriGateway.getStatusInfo()));
            break;
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Bridge(org.eclipse.smarthome.core.thing.Bridge) TradfriCoapClient(org.eclipse.smarthome.binding.tradfri.internal.TradfriCoapClient)

Aggregations

URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 TradfriCoapClient (org.eclipse.smarthome.binding.tradfri.internal.TradfriCoapClient)2 InetSocketAddress (java.net.InetSocketAddress)1 DTLSConnector (org.eclipse.californium.scandium.DTLSConnector)1 DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)1 StaticPskStore (org.eclipse.californium.scandium.dtls.pskstore.StaticPskStore)1 TradfriCoapEndpoint (org.eclipse.smarthome.binding.tradfri.internal.TradfriCoapEndpoint)1 TradfriGatewayConfig (org.eclipse.smarthome.binding.tradfri.internal.config.TradfriGatewayConfig)1 Bridge (org.eclipse.smarthome.core.thing.Bridge)1