[miio] Avoid excessive MessageSenderThread (#11455)

* [miio] Avoid excessive MessageSenderThread

Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com>
This commit is contained in:
Marcel 2021-10-27 21:18:54 +02:00 committed by GitHub
parent 03e2303800
commit dfc9a5cb92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 47 deletions

View File

@ -90,6 +90,7 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
protected @Nullable MiIoAsyncCommunication miioCom;
protected CloudConnector cloudConnector;
protected String cloudServer = "";
protected String deviceId = "";
protected int lastId;
protected Map<Integer, String> cmds = new ConcurrentHashMap<>();
@ -153,6 +154,7 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
return;
}
this.cloudServer = configuration.cloudServer;
this.deviceId = configuration.deviceId;
isIdentified = false;
deviceVariables.put(TIMESTAMP, Instant.now().getEpochSecond());
deviceVariables.put(PROPERTY_DID, configuration.deviceId);
@ -372,63 +374,62 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
if (configuration.host.isBlank()) {
return null;
}
@Nullable
String deviceId = configuration.deviceId;
if (deviceId.isBlank() && !getCloudServer().isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
"Cloud communication requires defined deviceId in the config");
return null;
}
if (deviceId.length() == 8 && deviceId.matches("^.*[a-zA-Z]+.*$")) {
logger.warn(
"As per openHAB version 3.2 the deviceId is no longer a string with hexadecimals, instead it is a string with the numeric respresentation of the deviceId. If you continue seeing this message, update deviceId in your thing configuration. Expected change for thing '{}': Update current deviceId: '{}' to '{}'",
getThing().getUID(), deviceId, Utils.fromHEX(deviceId));
deviceId = "";
}
try {
if (!deviceId.isBlank() && tokenCheckPass(configuration.token)) {
final MiIoAsyncCommunication miioCom = new MiIoAsyncCommunication(configuration.host, token, deviceId,
lastId, configuration.timeout, cloudConnector);
if (getCloudServer().isBlank()) {
logger.debug("Ping Mi deviceId '{}' at {}", deviceId, configuration.host);
Message miIoResponse = miioCom.sendPing(configuration.host);
if (miIoResponse != null) {
logger.debug("Ping response from deviceId '{}' at {}. Time stamp: {}, OH time {}, delta {}",
Utils.fromHEX(Utils.getHex(miIoResponse.getDeviceId())), configuration.host,
miIoResponse.getTimestamp(), LocalDateTime.now(), miioCom.getTimeDelta());
miioCom.registerListener(this);
this.miioCom = miioCom;
return miioCom;
} else {
miioCom.close();
}
} else {
miioCom.registerListener(this);
this.miioCom = miioCom;
return miioCom;
}
if (getCloudServer().isBlank()) {
deviceId = "";
} else {
logger.debug("No deviceId defined. Retrieving Mi deviceId");
final MiIoAsyncCommunication miioCom = new MiIoAsyncCommunication(configuration.host, token, "", lastId,
configuration.timeout, cloudConnector);
Message miIoResponse = miioCom.sendPing(configuration.host);
final String id = Utils.fromHEX(deviceId);
deviceId = id;
miIoScheduler.execute(() -> updateDeviceIdConfig(id));
}
}
if (!deviceId.isBlank() && (tokenCheckPass(configuration.token) || !getCloudServer().isBlank())) {
final MiIoAsyncCommunication miioComF = new MiIoAsyncCommunication(configuration.host, token, deviceId,
lastId, configuration.timeout, cloudConnector);
miioComF.registerListener(this);
this.miioCom = miioComF;
return miioComF;
} else {
logger.debug("No deviceId defined. Retrieving Mi deviceId");
final MiIoAsyncCommunication miioComF = new MiIoAsyncCommunication(configuration.host, token, "", lastId,
configuration.timeout, cloudConnector);
try {
Message miIoResponse = miioComF.sendPing(configuration.host);
if (miIoResponse != null) {
deviceId = Utils.fromHEX(Utils.getHex(miIoResponse.getDeviceId()));
logger.debug("Ping response from deviceId '{}' at {}. Time stamp: {}, OH time {}, delta {}",
deviceId, configuration.host, miIoResponse.getTimestamp(), LocalDateTime.now(),
miioCom.getTimeDelta());
miioCom.setDeviceId(deviceId);
miioComF.getTimeDelta());
miioComF.setDeviceId(deviceId);
logger.debug("Using retrieved Mi deviceId: {}", deviceId);
updateDeviceIdConfig(deviceId);
miioCom.registerListener(this);
this.miioCom = miioCom;
return miioCom;
miioComF.registerListener(this);
this.miioCom = miioComF;
final String id = deviceId;
miIoScheduler.execute(() -> updateDeviceIdConfig(id));
return miioComF;
} else {
miioCom.close();
miioComF.close();
logger.debug("Ping response from deviceId '{}' at {} FAILED", configuration.deviceId,
configuration.host);
disconnectedNoResponse();
return null;
}
} catch (IOException e) {
miioComF.close();
logger.debug("Could not connect to {} at {}", getThing().getUID().toString(), configuration.host);
disconnected(e.getMessage());
return null;
}
logger.debug("Ping response from deviceId '{}' at {} FAILED", configuration.deviceId, configuration.host);
disconnectedNoResponse();
return null;
} catch (IOException e) {
logger.debug("Could not connect to {} at {}", getThing().getUID().toString(), configuration.host);
disconnected(e.getMessage());
return null;
}
}

View File

@ -93,7 +93,6 @@ public class MiIoAsyncCommunication {
this.timeout = timeout;
this.cloudConnector = cloudConnector;
setId(id);
startReceiver();
}
protected List<MiIoMessageListener> getListeners() {
@ -249,7 +248,7 @@ public class MiIoAsyncCommunication {
public synchronized void startReceiver() {
MessageSenderThread senderThread = this.senderThread;
if (senderThread == null || !senderThread.isAlive()) {
senderThread = new MessageSenderThread(deviceId);
senderThread = new MessageSenderThread(deviceId.isBlank() ? "?" + ip : deviceId);
senderThread.start();
this.senderThread = senderThread;
}
@ -435,7 +434,7 @@ public class MiIoAsyncCommunication {
if (socket == null || socket.isClosed()) {
socket = new DatagramSocket();
socket.setSoTimeout(timeout);
logger.debug("Opening socket on port: {} ", socket.getLocalPort());
logger.debug("Opening socket on port: {} ({} {})", socket.getLocalPort(), deviceId, ip);
this.socket = socket;
return socket;
} else {
@ -497,6 +496,10 @@ public class MiIoAsyncCommunication {
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
MessageSenderThread senderThread = this.senderThread;
if (senderThread != null) {
senderThread.setName("OH-binding-miio-MessageSenderThread-" + deviceId);
}
}
public int getQueueLength() {