mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[insteon] Use shared jetty http client (#17922)
Signed-off-by: jsetton <jeremy.setton@gmail.com>
This commit is contained in:
parent
5d40a719f4
commit
2ab9822605
@ -20,6 +20,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.openhab.binding.insteon.internal.discovery.InsteonDiscoveryService;
|
import org.openhab.binding.insteon.internal.discovery.InsteonDiscoveryService;
|
||||||
import org.openhab.binding.insteon.internal.discovery.InsteonLegacyDiscoveryService;
|
import org.openhab.binding.insteon.internal.discovery.InsteonLegacyDiscoveryService;
|
||||||
import org.openhab.binding.insteon.internal.handler.InsteonBridgeHandler;
|
import org.openhab.binding.insteon.internal.handler.InsteonBridgeHandler;
|
||||||
@ -29,6 +30,7 @@ import org.openhab.binding.insteon.internal.handler.InsteonLegacyNetworkHandler;
|
|||||||
import org.openhab.binding.insteon.internal.handler.InsteonSceneHandler;
|
import org.openhab.binding.insteon.internal.handler.InsteonSceneHandler;
|
||||||
import org.openhab.binding.insteon.internal.handler.X10DeviceHandler;
|
import org.openhab.binding.insteon.internal.handler.X10DeviceHandler;
|
||||||
import org.openhab.core.config.discovery.DiscoveryService;
|
import org.openhab.core.config.discovery.DiscoveryService;
|
||||||
|
import org.openhab.core.io.net.http.HttpClientFactory;
|
||||||
import org.openhab.core.io.transport.serial.SerialPortManager;
|
import org.openhab.core.io.transport.serial.SerialPortManager;
|
||||||
import org.openhab.core.storage.StorageService;
|
import org.openhab.core.storage.StorageService;
|
||||||
import org.openhab.core.thing.Bridge;
|
import org.openhab.core.thing.Bridge;
|
||||||
@ -56,6 +58,7 @@ import org.osgi.service.component.annotations.Reference;
|
|||||||
@Component(configurationPid = "binding.insteon", service = ThingHandlerFactory.class)
|
@Component(configurationPid = "binding.insteon", service = ThingHandlerFactory.class)
|
||||||
public class InsteonHandlerFactory extends BaseThingHandlerFactory {
|
public class InsteonHandlerFactory extends BaseThingHandlerFactory {
|
||||||
|
|
||||||
|
private final HttpClient httpClient;
|
||||||
private final SerialPortManager serialPortManager;
|
private final SerialPortManager serialPortManager;
|
||||||
private final InsteonStateDescriptionProvider stateDescriptionProvider;
|
private final InsteonStateDescriptionProvider stateDescriptionProvider;
|
||||||
private final StorageService storageService;
|
private final StorageService storageService;
|
||||||
@ -64,10 +67,12 @@ public class InsteonHandlerFactory extends BaseThingHandlerFactory {
|
|||||||
private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
|
private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
|
||||||
|
|
||||||
@Activate
|
@Activate
|
||||||
public InsteonHandlerFactory(final @Reference SerialPortManager serialPortManager,
|
public InsteonHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
|
||||||
|
final @Reference SerialPortManager serialPortManager,
|
||||||
final @Reference InsteonStateDescriptionProvider stateDescriptionProvider,
|
final @Reference InsteonStateDescriptionProvider stateDescriptionProvider,
|
||||||
final @Reference StorageService storageService, final @Reference ThingManager thingManager,
|
final @Reference StorageService storageService, final @Reference ThingManager thingManager,
|
||||||
final @Reference ThingRegistry thingRegistry) {
|
final @Reference ThingRegistry thingRegistry) {
|
||||||
|
this.httpClient = httpClientFactory.getCommonHttpClient();
|
||||||
this.serialPortManager = serialPortManager;
|
this.serialPortManager = serialPortManager;
|
||||||
this.stateDescriptionProvider = stateDescriptionProvider;
|
this.stateDescriptionProvider = stateDescriptionProvider;
|
||||||
this.storageService = storageService;
|
this.storageService = storageService;
|
||||||
@ -86,14 +91,14 @@ public class InsteonHandlerFactory extends BaseThingHandlerFactory {
|
|||||||
|
|
||||||
if (THING_TYPE_HUB1.equals(thingTypeUID) || THING_TYPE_HUB2.equals(thingTypeUID)
|
if (THING_TYPE_HUB1.equals(thingTypeUID) || THING_TYPE_HUB2.equals(thingTypeUID)
|
||||||
|| THING_TYPE_PLM.equals(thingTypeUID)) {
|
|| THING_TYPE_PLM.equals(thingTypeUID)) {
|
||||||
InsteonBridgeHandler handler = new InsteonBridgeHandler((Bridge) thing, serialPortManager, storageService,
|
InsteonBridgeHandler handler = new InsteonBridgeHandler((Bridge) thing, httpClient, serialPortManager,
|
||||||
thingRegistry);
|
storageService, thingRegistry);
|
||||||
InsteonDiscoveryService service = new InsteonDiscoveryService(handler);
|
InsteonDiscoveryService service = new InsteonDiscoveryService(handler);
|
||||||
registerDiscoveryService(handler, service);
|
registerDiscoveryService(handler, service);
|
||||||
return handler;
|
return handler;
|
||||||
} else if (THING_TYPE_LEGACY_NETWORK.equals(thingTypeUID)) {
|
} else if (THING_TYPE_LEGACY_NETWORK.equals(thingTypeUID)) {
|
||||||
InsteonLegacyNetworkHandler handler = new InsteonLegacyNetworkHandler((Bridge) thing, serialPortManager,
|
InsteonLegacyNetworkHandler handler = new InsteonLegacyNetworkHandler((Bridge) thing, httpClient,
|
||||||
thingManager, thingRegistry);
|
serialPortManager, thingManager, thingRegistry);
|
||||||
InsteonLegacyDiscoveryService service = new InsteonLegacyDiscoveryService(handler);
|
InsteonLegacyDiscoveryService service = new InsteonLegacyDiscoveryService(handler);
|
||||||
registerDiscoveryService(handler, service);
|
registerDiscoveryService(handler, service);
|
||||||
return handler;
|
return handler;
|
||||||
|
@ -27,6 +27,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonLegacyChannelConfiguration;
|
import org.openhab.binding.insteon.internal.config.InsteonLegacyChannelConfiguration;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonLegacyNetworkConfiguration;
|
import org.openhab.binding.insteon.internal.config.InsteonLegacyNetworkConfiguration;
|
||||||
import org.openhab.binding.insteon.internal.device.DeviceAddress;
|
import org.openhab.binding.insteon.internal.device.DeviceAddress;
|
||||||
@ -119,13 +120,13 @@ public class InsteonLegacyBinding implements LegacyDriverListener, LegacyPortLis
|
|||||||
private InsteonLegacyNetworkHandler handler;
|
private InsteonLegacyNetworkHandler handler;
|
||||||
|
|
||||||
public InsteonLegacyBinding(InsteonLegacyNetworkHandler handler, InsteonLegacyNetworkConfiguration config,
|
public InsteonLegacyBinding(InsteonLegacyNetworkHandler handler, InsteonLegacyNetworkConfiguration config,
|
||||||
SerialPortManager serialPortManager, ScheduledExecutorService scheduler) {
|
HttpClient httpClient, ScheduledExecutorService scheduler, SerialPortManager serialPortManager) {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
|
|
||||||
String port = config.getRedactedPort();
|
String port = config.getRedactedPort();
|
||||||
logger.debug("port = '{}'", port);
|
logger.debug("port = '{}'", port);
|
||||||
|
|
||||||
driver = new LegacyDriver(config, this, serialPortManager, scheduler);
|
driver = new LegacyDriver(config, this, httpClient, scheduler, serialPortManager);
|
||||||
driver.addPortListener(this);
|
driver.addPortListener(this);
|
||||||
|
|
||||||
Integer devicePollIntervalSeconds = config.getDevicePollIntervalSeconds();
|
Integer devicePollIntervalSeconds = config.getDevicePollIntervalSeconds();
|
||||||
|
@ -21,6 +21,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonBridgeConfiguration;
|
import org.openhab.binding.insteon.internal.config.InsteonBridgeConfiguration;
|
||||||
import org.openhab.binding.insteon.internal.device.database.DatabaseManager;
|
import org.openhab.binding.insteon.internal.device.database.DatabaseManager;
|
||||||
import org.openhab.binding.insteon.internal.device.database.ModemDB;
|
import org.openhab.binding.insteon.internal.device.database.ModemDB;
|
||||||
@ -53,10 +54,10 @@ public class InsteonModem extends BaseDevice<InsteonAddress, InsteonBridgeHandle
|
|||||||
private boolean initialized = false;
|
private boolean initialized = false;
|
||||||
private int msgsReceived = 0;
|
private int msgsReceived = 0;
|
||||||
|
|
||||||
public InsteonModem(InsteonBridgeConfiguration config, ScheduledExecutorService scheduler,
|
public InsteonModem(InsteonBridgeConfiguration config, HttpClient httpClient, ScheduledExecutorService scheduler,
|
||||||
SerialPortManager serialPortManager) {
|
SerialPortManager serialPortManager) {
|
||||||
super(InsteonAddress.UNKNOWN);
|
super(InsteonAddress.UNKNOWN);
|
||||||
this.port = new Port(config, scheduler, serialPortManager);
|
this.port = new Port(config, httpClient, scheduler, serialPortManager);
|
||||||
this.modemDB = new ModemDB(this);
|
this.modemDB = new ModemDB(this);
|
||||||
this.dbm = new DatabaseManager(this, scheduler);
|
this.dbm = new DatabaseManager(this, scheduler);
|
||||||
this.linker = new LinkManager(this, scheduler);
|
this.linker = new LinkManager(this, scheduler);
|
||||||
@ -509,13 +510,14 @@ public class InsteonModem extends BaseDevice<InsteonAddress, InsteonBridgeHandle
|
|||||||
*
|
*
|
||||||
* @param handler the bridge handler
|
* @param handler the bridge handler
|
||||||
* @param config the bridge config
|
* @param config the bridge config
|
||||||
|
* @param httpClient the http client
|
||||||
* @param scheduler the scheduler service
|
* @param scheduler the scheduler service
|
||||||
* @param serialPortManager the serial port manager
|
* @param serialPortManager the serial port manager
|
||||||
* @return the newly created InsteonModem
|
* @return the newly created InsteonModem
|
||||||
*/
|
*/
|
||||||
public static InsteonModem makeModem(InsteonBridgeHandler handler, InsteonBridgeConfiguration config,
|
public static InsteonModem makeModem(InsteonBridgeHandler handler, InsteonBridgeConfiguration config,
|
||||||
ScheduledExecutorService scheduler, SerialPortManager serialPortManager) {
|
HttpClient httpClient, ScheduledExecutorService scheduler, SerialPortManager serialPortManager) {
|
||||||
InsteonModem modem = new InsteonModem(config, scheduler, serialPortManager);
|
InsteonModem modem = new InsteonModem(config, httpClient, scheduler, serialPortManager);
|
||||||
modem.setHandler(handler);
|
modem.setHandler(handler);
|
||||||
return modem;
|
return modem;
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import java.util.stream.Stream;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonBridgeConfiguration;
|
import org.openhab.binding.insteon.internal.config.InsteonBridgeConfiguration;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonHub1Configuration;
|
import org.openhab.binding.insteon.internal.config.InsteonHub1Configuration;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonHub2Configuration;
|
import org.openhab.binding.insteon.internal.config.InsteonHub2Configuration;
|
||||||
@ -63,13 +64,15 @@ public class InsteonBridgeHandler extends InsteonBaseThingHandler implements Bri
|
|||||||
private @Nullable ScheduledFuture<?> reconnectJob;
|
private @Nullable ScheduledFuture<?> reconnectJob;
|
||||||
private @Nullable ScheduledFuture<?> resetJob;
|
private @Nullable ScheduledFuture<?> resetJob;
|
||||||
private @Nullable ScheduledFuture<?> statisticsJob;
|
private @Nullable ScheduledFuture<?> statisticsJob;
|
||||||
|
private HttpClient httpClient;
|
||||||
private SerialPortManager serialPortManager;
|
private SerialPortManager serialPortManager;
|
||||||
private Storage<DeviceCache> storage;
|
private Storage<DeviceCache> storage;
|
||||||
private ThingRegistry thingRegistry;
|
private ThingRegistry thingRegistry;
|
||||||
|
|
||||||
public InsteonBridgeHandler(Bridge bridge, SerialPortManager serialPortManager, StorageService storageService,
|
public InsteonBridgeHandler(Bridge bridge, HttpClient httpClient, SerialPortManager serialPortManager,
|
||||||
ThingRegistry thingRegistry) {
|
StorageService storageService, ThingRegistry thingRegistry) {
|
||||||
super(bridge);
|
super(bridge);
|
||||||
|
this.httpClient = httpClient;
|
||||||
this.serialPortManager = serialPortManager;
|
this.serialPortManager = serialPortManager;
|
||||||
this.storage = storageService.getStorage(bridge.getUID().toString(), DeviceCache.class.getClassLoader());
|
this.storage = storageService.getStorage(bridge.getUID().toString(), DeviceCache.class.getClassLoader());
|
||||||
this.thingRegistry = thingRegistry;
|
this.thingRegistry = thingRegistry;
|
||||||
@ -164,7 +167,7 @@ public class InsteonBridgeHandler extends InsteonBaseThingHandler implements Bri
|
|||||||
legacyHandler.disable();
|
legacyHandler.disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
InsteonModem modem = InsteonModem.makeModem(this, config, scheduler, serialPortManager);
|
InsteonModem modem = InsteonModem.makeModem(this, config, httpClient, scheduler, serialPortManager);
|
||||||
this.modem = modem;
|
this.modem = modem;
|
||||||
|
|
||||||
if (isInitialized()) {
|
if (isInitialized()) {
|
||||||
|
@ -21,6 +21,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.openhab.binding.insteon.internal.InsteonLegacyBinding;
|
import org.openhab.binding.insteon.internal.InsteonLegacyBinding;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonBridgeConfiguration;
|
import org.openhab.binding.insteon.internal.config.InsteonBridgeConfiguration;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonLegacyNetworkConfiguration;
|
import org.openhab.binding.insteon.internal.config.InsteonLegacyNetworkConfiguration;
|
||||||
@ -67,6 +68,7 @@ public class InsteonLegacyNetworkHandler extends BaseBridgeHandler {
|
|||||||
private @Nullable ScheduledFuture<?> reconnectJob = null;
|
private @Nullable ScheduledFuture<?> reconnectJob = null;
|
||||||
private @Nullable ScheduledFuture<?> settleJob = null;
|
private @Nullable ScheduledFuture<?> settleJob = null;
|
||||||
private long lastInsteonDeviceCreatedTimestamp = 0;
|
private long lastInsteonDeviceCreatedTimestamp = 0;
|
||||||
|
private HttpClient httpClient;
|
||||||
private SerialPortManager serialPortManager;
|
private SerialPortManager serialPortManager;
|
||||||
private ThingManager thingManager;
|
private ThingManager thingManager;
|
||||||
private ThingRegistry thingRegistry;
|
private ThingRegistry thingRegistry;
|
||||||
@ -74,9 +76,10 @@ public class InsteonLegacyNetworkHandler extends BaseBridgeHandler {
|
|||||||
private Map<String, String> channelInfo = new ConcurrentHashMap<>();
|
private Map<String, String> channelInfo = new ConcurrentHashMap<>();
|
||||||
private Map<ChannelUID, Configuration> channelConfigs = new ConcurrentHashMap<>();
|
private Map<ChannelUID, Configuration> channelConfigs = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public InsteonLegacyNetworkHandler(Bridge bridge, SerialPortManager serialPortManager, ThingManager thingManager,
|
public InsteonLegacyNetworkHandler(Bridge bridge, HttpClient httpClient, SerialPortManager serialPortManager,
|
||||||
ThingRegistry thingRegistry) {
|
ThingManager thingManager, ThingRegistry thingRegistry) {
|
||||||
super(bridge);
|
super(bridge);
|
||||||
|
this.httpClient = httpClient;
|
||||||
this.serialPortManager = serialPortManager;
|
this.serialPortManager = serialPortManager;
|
||||||
this.thingManager = thingManager;
|
this.thingManager = thingManager;
|
||||||
this.thingRegistry = thingRegistry;
|
this.thingRegistry = thingRegistry;
|
||||||
@ -105,7 +108,7 @@ public class InsteonLegacyNetworkHandler extends BaseBridgeHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
insteonBinding = new InsteonLegacyBinding(this, config, serialPortManager, scheduler);
|
insteonBinding = new InsteonLegacyBinding(this, config, httpClient, scheduler, serialPortManager);
|
||||||
updateStatus(ThingStatus.UNKNOWN);
|
updateStatus(ThingStatus.UNKNOWN);
|
||||||
|
|
||||||
// hold off on starting to poll until devices that already are defined as things are added.
|
// hold off on starting to poll until devices that already are defined as things are added.
|
||||||
@ -136,7 +139,7 @@ public class InsteonLegacyNetworkHandler extends BaseBridgeHandler {
|
|||||||
this.driverInitializedJob = null;
|
this.driverInitializedJob = null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.debug("driver is not initialized yet");
|
logger.trace("driver is not initialized yet");
|
||||||
}
|
}
|
||||||
}, 0, DRIVER_INITIALIZED_TIME_IN_SECONDS, TimeUnit.SECONDS);
|
}, 0, DRIVER_INITIALIZED_TIME_IN_SECONDS, TimeUnit.SECONDS);
|
||||||
} else {
|
} else {
|
||||||
|
@ -12,23 +12,27 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.binding.insteon.internal.transport;
|
package org.openhab.binding.insteon.internal.transport;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
|
import org.eclipse.jetty.client.api.ContentResponse;
|
||||||
|
import org.eclipse.jetty.client.api.Request;
|
||||||
|
import org.eclipse.jetty.http.HttpHeader;
|
||||||
|
import org.eclipse.jetty.http.HttpStatus;
|
||||||
import org.openhab.binding.insteon.internal.utils.HexUtils;
|
import org.openhab.binding.insteon.internal.utils.HexUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -47,11 +51,13 @@ public class HubIOStream extends IOStream {
|
|||||||
|
|
||||||
private static final String BS_START = "<BS>";
|
private static final String BS_START = "<BS>";
|
||||||
private static final String BS_END = "</BS>";
|
private static final String BS_END = "</BS>";
|
||||||
|
private static final int REQUEST_TIMEOUT = 30; // in seconds
|
||||||
|
|
||||||
private String host;
|
private String host;
|
||||||
private int port;
|
private int port;
|
||||||
private String auth;
|
private String auth;
|
||||||
private int pollInterval;
|
private int pollInterval;
|
||||||
|
private HttpClient httpClient;
|
||||||
private ScheduledExecutorService scheduler;
|
private ScheduledExecutorService scheduler;
|
||||||
private @Nullable ScheduledFuture<?> job;
|
private @Nullable ScheduledFuture<?> job;
|
||||||
// index of the last byte we have read in the buffer
|
// index of the last byte we have read in the buffer
|
||||||
@ -65,14 +71,16 @@ public class HubIOStream extends IOStream {
|
|||||||
* @param username hub user name
|
* @param username hub user name
|
||||||
* @param password hub password
|
* @param password hub password
|
||||||
* @param pollInterval hub poll interval (in milliseconds)
|
* @param pollInterval hub poll interval (in milliseconds)
|
||||||
|
* @param httpClient the http client
|
||||||
* @param scheduler the scheduler
|
* @param scheduler the scheduler
|
||||||
*/
|
*/
|
||||||
public HubIOStream(String host, int port, String username, String password, int pollInterval,
|
public HubIOStream(String host, int port, String username, String password, int pollInterval, HttpClient httpClient,
|
||||||
ScheduledExecutorService scheduler) {
|
ScheduledExecutorService scheduler) {
|
||||||
this.host = host;
|
this.host = host;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.auth = Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
|
this.auth = Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
|
||||||
this.pollInterval = pollInterval;
|
this.pollInterval = pollInterval;
|
||||||
|
this.httpClient = httpClient;
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -265,54 +273,33 @@ public class HubIOStream extends IOStream {
|
|||||||
/**
|
/**
|
||||||
* Helper method to fetch url from http server
|
* Helper method to fetch url from http server
|
||||||
*
|
*
|
||||||
* @param resource the url
|
* @param path the url path
|
||||||
* @return contents returned by http server
|
* @return contents returned by http server
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private String getURL(String resource) throws IOException {
|
private String getURL(String path) throws IOException {
|
||||||
String url = "http://" + host + ":" + port + resource;
|
Request request = httpClient.newRequest(host, port).path(path).header(HttpHeader.AUTHORIZATION, "Basic " + auth)
|
||||||
|
.timeout(REQUEST_TIMEOUT, TimeUnit.SECONDS);
|
||||||
|
logger.trace("getting {}", request.getURI());
|
||||||
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
|
||||||
try {
|
try {
|
||||||
connection.setConnectTimeout(30000);
|
ContentResponse response = request.send();
|
||||||
connection.setReadTimeout(10000);
|
|
||||||
connection.setUseCaches(false);
|
|
||||||
connection.setDoInput(true);
|
|
||||||
connection.setDoOutput(false);
|
|
||||||
connection.setRequestProperty("Authorization", "Basic " + auth);
|
|
||||||
|
|
||||||
logger.trace("getting {}", url);
|
int statusCode = response.getStatus();
|
||||||
|
switch (statusCode) {
|
||||||
int responseCode = connection.getResponseCode();
|
case HttpStatus.OK_200:
|
||||||
if (responseCode != 200) {
|
return response.getContentAsString();
|
||||||
if (responseCode == 401) {
|
case HttpStatus.UNAUTHORIZED_401:
|
||||||
throw new IOException(
|
throw new IOException(
|
||||||
"Bad username or password. See the label on the bottom of the hub for the correct login information.");
|
"Bad username or password. See the label on the bottom of the hub for the correct login information.");
|
||||||
} else {
|
default:
|
||||||
throw new IOException(url + " failed with the response code: " + responseCode);
|
throw new IOException("GET " + request.getURI() + " failed with status code: " + statusCode);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
return getData(connection.getInputStream());
|
Thread.currentThread().interrupt();
|
||||||
} finally {
|
throw new IOException("GET " + request.getURI() + " interrupted");
|
||||||
connection.disconnect();
|
} catch (TimeoutException | ExecutionException e) {
|
||||||
}
|
throw new IOException("GET " + request.getURI() + " failed with error: " + e.getMessage());
|
||||||
}
|
|
||||||
|
|
||||||
private String getData(InputStream is) throws IOException {
|
|
||||||
BufferedInputStream bis = new BufferedInputStream(is);
|
|
||||||
try {
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int length = 0;
|
|
||||||
while ((length = bis.read(buffer)) != -1) {
|
|
||||||
baos.write(buffer, 0, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
String s = baos.toString();
|
|
||||||
return s;
|
|
||||||
} finally {
|
|
||||||
bis.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonBridgeConfiguration;
|
import org.openhab.binding.insteon.internal.config.InsteonBridgeConfiguration;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonHub1Configuration;
|
import org.openhab.binding.insteon.internal.config.InsteonHub1Configuration;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonHub2Configuration;
|
import org.openhab.binding.insteon.internal.config.InsteonHub2Configuration;
|
||||||
@ -113,16 +114,17 @@ public abstract class IOStream {
|
|||||||
* Creates an IOStream from an insteon bridge config object
|
* Creates an IOStream from an insteon bridge config object
|
||||||
*
|
*
|
||||||
* @param config
|
* @param config
|
||||||
|
* @param httpClient
|
||||||
* @param scheduler
|
* @param scheduler
|
||||||
* @param serialPortManager
|
* @param serialPortManager
|
||||||
* @return reference to IOStream
|
* @return reference to IOStream
|
||||||
*/
|
*/
|
||||||
public static IOStream create(InsteonBridgeConfiguration config, ScheduledExecutorService scheduler,
|
public static IOStream create(InsteonBridgeConfiguration config, HttpClient httpClient,
|
||||||
SerialPortManager serialPortManager) {
|
ScheduledExecutorService scheduler, SerialPortManager serialPortManager) {
|
||||||
if (config instanceof InsteonHub1Configuration hub1Config) {
|
if (config instanceof InsteonHub1Configuration hub1Config) {
|
||||||
return makeTcpIOStream(hub1Config);
|
return makeTcpIOStream(hub1Config);
|
||||||
} else if (config instanceof InsteonHub2Configuration hub2Config) {
|
} else if (config instanceof InsteonHub2Configuration hub2Config) {
|
||||||
return makeHubIOStream(hub2Config, scheduler);
|
return makeHubIOStream(hub2Config, httpClient, scheduler);
|
||||||
} else if (config instanceof InsteonPLMConfiguration plmConfig) {
|
} else if (config instanceof InsteonPLMConfiguration plmConfig) {
|
||||||
return makeSerialIOStream(plmConfig, serialPortManager);
|
return makeSerialIOStream(plmConfig, serialPortManager);
|
||||||
} else {
|
} else {
|
||||||
@ -130,13 +132,14 @@ public abstract class IOStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HubIOStream makeHubIOStream(InsteonHub2Configuration config, ScheduledExecutorService scheduler) {
|
private static HubIOStream makeHubIOStream(InsteonHub2Configuration config, HttpClient httpClient,
|
||||||
|
ScheduledExecutorService scheduler) {
|
||||||
String host = config.getHostname();
|
String host = config.getHostname();
|
||||||
int port = config.getPort();
|
int port = config.getPort();
|
||||||
String user = config.getUsername();
|
String user = config.getUsername();
|
||||||
String pass = config.getPassword();
|
String pass = config.getPassword();
|
||||||
int pollInterval = config.getHubPollInterval();
|
int pollInterval = config.getHubPollInterval();
|
||||||
return new HubIOStream(host, port, user, pass, pollInterval, scheduler);
|
return new HubIOStream(host, port, user, pass, pollInterval, httpClient, scheduler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SerialIOStream makeSerialIOStream(InsteonPLMConfiguration config,
|
private static SerialIOStream makeSerialIOStream(InsteonPLMConfiguration config,
|
||||||
|
@ -20,6 +20,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonLegacyNetworkConfiguration;
|
import org.openhab.binding.insteon.internal.config.InsteonLegacyNetworkConfiguration;
|
||||||
import org.openhab.binding.insteon.internal.device.InsteonAddress;
|
import org.openhab.binding.insteon.internal.device.InsteonAddress;
|
||||||
import org.openhab.binding.insteon.internal.device.LegacyPollManager;
|
import org.openhab.binding.insteon.internal.device.LegacyPollManager;
|
||||||
@ -44,11 +45,11 @@ public class LegacyDriver {
|
|||||||
private Map<InsteonAddress, LegacyModemDBEntry> modemDBEntries = new HashMap<>();
|
private Map<InsteonAddress, LegacyModemDBEntry> modemDBEntries = new HashMap<>();
|
||||||
private ReentrantLock modemDBEntriesLock = new ReentrantLock();
|
private ReentrantLock modemDBEntriesLock = new ReentrantLock();
|
||||||
|
|
||||||
public LegacyDriver(InsteonLegacyNetworkConfiguration config, LegacyDriverListener listener,
|
public LegacyDriver(InsteonLegacyNetworkConfiguration config, LegacyDriverListener listener, HttpClient httpClient,
|
||||||
SerialPortManager serialPortManager, ScheduledExecutorService scheduler) {
|
ScheduledExecutorService scheduler, SerialPortManager serialPortManager) {
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
|
|
||||||
this.port = new LegacyPort(config, this, serialPortManager, scheduler);
|
this.port = new LegacyPort(config, this, httpClient, scheduler, serialPortManager);
|
||||||
this.poller = new LegacyPollManager(scheduler);
|
this.poller = new LegacyPollManager(scheduler);
|
||||||
this.requester = new LegacyRequestManager(scheduler);
|
this.requester = new LegacyRequestManager(scheduler);
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.openhab.binding.insteon.internal.InsteonLegacyBindingConstants;
|
import org.openhab.binding.insteon.internal.InsteonLegacyBindingConstants;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonLegacyNetworkConfiguration;
|
import org.openhab.binding.insteon.internal.config.InsteonLegacyNetworkConfiguration;
|
||||||
import org.openhab.binding.insteon.internal.device.InsteonAddress;
|
import org.openhab.binding.insteon.internal.device.InsteonAddress;
|
||||||
@ -74,10 +75,10 @@ public class LegacyPort {
|
|||||||
|
|
||||||
private IOStream ioStream;
|
private IOStream ioStream;
|
||||||
private String name;
|
private String name;
|
||||||
private Modem modem;
|
private Modem modem = new Modem();
|
||||||
private ScheduledExecutorService scheduler;
|
private ScheduledExecutorService scheduler;
|
||||||
private IOStreamReader reader;
|
private IOStreamReader reader = new IOStreamReader();
|
||||||
private IOStreamWriter writer;
|
private IOStreamWriter writer = new IOStreamWriter();
|
||||||
private @Nullable ScheduledFuture<?> readJob;
|
private @Nullable ScheduledFuture<?> readJob;
|
||||||
private @Nullable ScheduledFuture<?> writeJob;
|
private @Nullable ScheduledFuture<?> writeJob;
|
||||||
private boolean running = false;
|
private boolean running = false;
|
||||||
@ -94,19 +95,16 @@ public class LegacyPort {
|
|||||||
*
|
*
|
||||||
* @param config the network bridge config
|
* @param config the network bridge config
|
||||||
* @param driver the driver that manages this port
|
* @param driver the driver that manages this port
|
||||||
* @param serialPortManager the serial port manager
|
* @param httpClient the http client
|
||||||
* @param scheduler the scheduler
|
* @param scheduler the scheduler
|
||||||
|
* @param serialPortManager the serial port manager
|
||||||
*/
|
*/
|
||||||
public LegacyPort(InsteonLegacyNetworkConfiguration config, LegacyDriver driver,
|
public LegacyPort(InsteonLegacyNetworkConfiguration config, LegacyDriver driver, HttpClient httpClient,
|
||||||
SerialPortManager serialPortManager, ScheduledExecutorService scheduler) {
|
ScheduledExecutorService scheduler, SerialPortManager serialPortManager) {
|
||||||
this.name = config.getRedactedPort();
|
this.name = config.getRedactedPort();
|
||||||
this.driver = driver;
|
this.driver = driver;
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
this.modem = new Modem();
|
this.ioStream = IOStream.create(config.parse(), httpClient, scheduler, serialPortManager);
|
||||||
addListener(modem);
|
|
||||||
this.ioStream = IOStream.create(config.parse(), scheduler, serialPortManager);
|
|
||||||
this.reader = new IOStreamReader();
|
|
||||||
this.writer = new IOStreamWriter();
|
|
||||||
this.mdbb = new LegacyModemDBBuilder(this, scheduler);
|
this.mdbb = new LegacyModemDBBuilder(this, scheduler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +113,7 @@ public class LegacyPort {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public synchronized boolean isModemDBComplete() {
|
public synchronized boolean isModemDBComplete() {
|
||||||
return (modemDBComplete);
|
return modemDBComplete;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isRunning() {
|
public boolean isRunning() {
|
||||||
@ -496,6 +494,7 @@ public class LegacyPort {
|
|||||||
public void initialize() {
|
public void initialize() {
|
||||||
try {
|
try {
|
||||||
Msg msg = Msg.makeMessage("GetIMInfo");
|
Msg msg = Msg.makeMessage("GetIMInfo");
|
||||||
|
addListener(this);
|
||||||
writeMessage(msg);
|
writeMessage(msg);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.warn("modem init failed!", e);
|
logger.warn("modem init failed!", e);
|
||||||
|
@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.openhab.binding.insteon.internal.config.InsteonBridgeConfiguration;
|
import org.openhab.binding.insteon.internal.config.InsteonBridgeConfiguration;
|
||||||
import org.openhab.binding.insteon.internal.transport.message.Msg;
|
import org.openhab.binding.insteon.internal.transport.message.Msg;
|
||||||
import org.openhab.binding.insteon.internal.transport.message.MsgFactory;
|
import org.openhab.binding.insteon.internal.transport.message.MsgFactory;
|
||||||
@ -64,8 +65,8 @@ public class Port {
|
|||||||
private String name;
|
private String name;
|
||||||
private ScheduledExecutorService scheduler;
|
private ScheduledExecutorService scheduler;
|
||||||
private IOStream ioStream;
|
private IOStream ioStream;
|
||||||
private IOStreamReader reader;
|
private IOStreamReader reader = new IOStreamReader();
|
||||||
private IOStreamWriter writer;
|
private IOStreamWriter writer = new IOStreamWriter();
|
||||||
private @Nullable ScheduledFuture<?> readJob;
|
private @Nullable ScheduledFuture<?> readJob;
|
||||||
private @Nullable ScheduledFuture<?> writeJob;
|
private @Nullable ScheduledFuture<?> writeJob;
|
||||||
private MsgFactory msgFactory = new MsgFactory();
|
private MsgFactory msgFactory = new MsgFactory();
|
||||||
@ -73,13 +74,11 @@ public class Port {
|
|||||||
private LinkedBlockingQueue<Msg> writeQueue = new LinkedBlockingQueue<>();
|
private LinkedBlockingQueue<Msg> writeQueue = new LinkedBlockingQueue<>();
|
||||||
private AtomicBoolean connected = new AtomicBoolean(false);
|
private AtomicBoolean connected = new AtomicBoolean(false);
|
||||||
|
|
||||||
public Port(InsteonBridgeConfiguration config, ScheduledExecutorService scheduler,
|
public Port(InsteonBridgeConfiguration config, HttpClient httpClient, ScheduledExecutorService scheduler,
|
||||||
SerialPortManager serialPortManager) {
|
SerialPortManager serialPortManager) {
|
||||||
this.name = config.getId();
|
this.name = config.getId();
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
this.ioStream = IOStream.create(config, scheduler, serialPortManager);
|
this.ioStream = IOStream.create(config, httpClient, scheduler, serialPortManager);
|
||||||
this.reader = new IOStreamReader();
|
|
||||||
this.writer = new IOStreamWriter();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
Loading…
Reference in New Issue
Block a user