[miio] Avoid excessive MessageSenderThread (#11455)
* [miio] Avoid excessive MessageSenderThread Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com>
This commit is contained in:
parent
03e2303800
commit
dfc9a5cb92
|
@ -90,6 +90,7 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
|
||||||
protected @Nullable MiIoAsyncCommunication miioCom;
|
protected @Nullable MiIoAsyncCommunication miioCom;
|
||||||
protected CloudConnector cloudConnector;
|
protected CloudConnector cloudConnector;
|
||||||
protected String cloudServer = "";
|
protected String cloudServer = "";
|
||||||
|
protected String deviceId = "";
|
||||||
protected int lastId;
|
protected int lastId;
|
||||||
|
|
||||||
protected Map<Integer, String> cmds = new ConcurrentHashMap<>();
|
protected Map<Integer, String> cmds = new ConcurrentHashMap<>();
|
||||||
|
@ -153,6 +154,7 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.cloudServer = configuration.cloudServer;
|
this.cloudServer = configuration.cloudServer;
|
||||||
|
this.deviceId = configuration.deviceId;
|
||||||
isIdentified = false;
|
isIdentified = false;
|
||||||
deviceVariables.put(TIMESTAMP, Instant.now().getEpochSecond());
|
deviceVariables.put(TIMESTAMP, Instant.now().getEpochSecond());
|
||||||
deviceVariables.put(PROPERTY_DID, configuration.deviceId);
|
deviceVariables.put(PROPERTY_DID, configuration.deviceId);
|
||||||
|
@ -372,64 +374,63 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
|
||||||
if (configuration.host.isBlank()) {
|
if (configuration.host.isBlank()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@Nullable
|
if (deviceId.isBlank() && !getCloudServer().isBlank()) {
|
||||||
String deviceId = configuration.deviceId;
|
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]+.*$")) {
|
if (deviceId.length() == 8 && deviceId.matches("^.*[a-zA-Z]+.*$")) {
|
||||||
logger.warn(
|
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 '{}'",
|
"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));
|
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()) {
|
if (getCloudServer().isBlank()) {
|
||||||
logger.debug("Ping Mi deviceId '{}' at {}", deviceId, configuration.host);
|
deviceId = "";
|
||||||
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 {
|
} else {
|
||||||
miioCom.close();
|
final String id = Utils.fromHEX(deviceId);
|
||||||
|
deviceId = id;
|
||||||
|
miIoScheduler.execute(() -> updateDeviceIdConfig(id));
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
miioCom.registerListener(this);
|
|
||||||
this.miioCom = miioCom;
|
|
||||||
return miioCom;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
} else {
|
||||||
logger.debug("No deviceId defined. Retrieving Mi deviceId");
|
logger.debug("No deviceId defined. Retrieving Mi deviceId");
|
||||||
final MiIoAsyncCommunication miioCom = new MiIoAsyncCommunication(configuration.host, token, "", lastId,
|
final MiIoAsyncCommunication miioComF = new MiIoAsyncCommunication(configuration.host, token, "", lastId,
|
||||||
configuration.timeout, cloudConnector);
|
configuration.timeout, cloudConnector);
|
||||||
Message miIoResponse = miioCom.sendPing(configuration.host);
|
try {
|
||||||
|
Message miIoResponse = miioComF.sendPing(configuration.host);
|
||||||
if (miIoResponse != null) {
|
if (miIoResponse != null) {
|
||||||
deviceId = Utils.fromHEX(Utils.getHex(miIoResponse.getDeviceId()));
|
deviceId = Utils.fromHEX(Utils.getHex(miIoResponse.getDeviceId()));
|
||||||
logger.debug("Ping response from deviceId '{}' at {}. Time stamp: {}, OH time {}, delta {}",
|
logger.debug("Ping response from deviceId '{}' at {}. Time stamp: {}, OH time {}, delta {}",
|
||||||
deviceId, configuration.host, miIoResponse.getTimestamp(), LocalDateTime.now(),
|
deviceId, configuration.host, miIoResponse.getTimestamp(), LocalDateTime.now(),
|
||||||
miioCom.getTimeDelta());
|
miioComF.getTimeDelta());
|
||||||
miioCom.setDeviceId(deviceId);
|
miioComF.setDeviceId(deviceId);
|
||||||
logger.debug("Using retrieved Mi deviceId: {}", deviceId);
|
logger.debug("Using retrieved Mi deviceId: {}", deviceId);
|
||||||
updateDeviceIdConfig(deviceId);
|
miioComF.registerListener(this);
|
||||||
miioCom.registerListener(this);
|
this.miioCom = miioComF;
|
||||||
this.miioCom = miioCom;
|
final String id = deviceId;
|
||||||
return miioCom;
|
miIoScheduler.execute(() -> updateDeviceIdConfig(id));
|
||||||
|
return miioComF;
|
||||||
} else {
|
} else {
|
||||||
miioCom.close();
|
miioComF.close();
|
||||||
}
|
logger.debug("Ping response from deviceId '{}' at {} FAILED", configuration.deviceId,
|
||||||
}
|
configuration.host);
|
||||||
logger.debug("Ping response from deviceId '{}' at {} FAILED", configuration.deviceId, configuration.host);
|
|
||||||
disconnectedNoResponse();
|
disconnectedNoResponse();
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
miioComF.close();
|
||||||
logger.debug("Could not connect to {} at {}", getThing().getUID().toString(), configuration.host);
|
logger.debug("Could not connect to {} at {}", getThing().getUID().toString(), configuration.host);
|
||||||
disconnected(e.getMessage());
|
disconnected(e.getMessage());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateDeviceIdConfig(String deviceId) {
|
private void updateDeviceIdConfig(String deviceId) {
|
||||||
|
|
|
@ -93,7 +93,6 @@ public class MiIoAsyncCommunication {
|
||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
this.cloudConnector = cloudConnector;
|
this.cloudConnector = cloudConnector;
|
||||||
setId(id);
|
setId(id);
|
||||||
startReceiver();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<MiIoMessageListener> getListeners() {
|
protected List<MiIoMessageListener> getListeners() {
|
||||||
|
@ -249,7 +248,7 @@ public class MiIoAsyncCommunication {
|
||||||
public synchronized void startReceiver() {
|
public synchronized void startReceiver() {
|
||||||
MessageSenderThread senderThread = this.senderThread;
|
MessageSenderThread senderThread = this.senderThread;
|
||||||
if (senderThread == null || !senderThread.isAlive()) {
|
if (senderThread == null || !senderThread.isAlive()) {
|
||||||
senderThread = new MessageSenderThread(deviceId);
|
senderThread = new MessageSenderThread(deviceId.isBlank() ? "?" + ip : deviceId);
|
||||||
senderThread.start();
|
senderThread.start();
|
||||||
this.senderThread = senderThread;
|
this.senderThread = senderThread;
|
||||||
}
|
}
|
||||||
|
@ -435,7 +434,7 @@ public class MiIoAsyncCommunication {
|
||||||
if (socket == null || socket.isClosed()) {
|
if (socket == null || socket.isClosed()) {
|
||||||
socket = new DatagramSocket();
|
socket = new DatagramSocket();
|
||||||
socket.setSoTimeout(timeout);
|
socket.setSoTimeout(timeout);
|
||||||
logger.debug("Opening socket on port: {} ", socket.getLocalPort());
|
logger.debug("Opening socket on port: {} ({} {})", socket.getLocalPort(), deviceId, ip);
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
return socket;
|
return socket;
|
||||||
} else {
|
} else {
|
||||||
|
@ -497,6 +496,10 @@ public class MiIoAsyncCommunication {
|
||||||
|
|
||||||
public void setDeviceId(String deviceId) {
|
public void setDeviceId(String deviceId) {
|
||||||
this.deviceId = deviceId;
|
this.deviceId = deviceId;
|
||||||
|
MessageSenderThread senderThread = this.senderThread;
|
||||||
|
if (senderThread != null) {
|
||||||
|
senderThread.setName("OH-binding-miio-MessageSenderThread-" + deviceId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getQueueLength() {
|
public int getQueueLength() {
|
||||||
|
|
Loading…
Reference in New Issue