Skip to content

Commit

Permalink
[miio] Avoid excessive MessageSenderThread (openhab#11455)
Browse files Browse the repository at this point in the history
* [miio] Avoid excessive MessageSenderThread

Signed-off-by: Marcel Verpaalen <[email protected]>
  • Loading branch information
marcelrv authored Oct 27, 2021
1 parent f59d0d7 commit fc90fe4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();
Expand Down Expand Up @@ -153,6 +154,7 @@ public void initialize() {
return;
}
this.cloudServer = configuration.cloudServer;
this.deviceId = configuration.deviceId;
isIdentified = false;
deviceVariables.put(TIMESTAMP, Instant.now().getEpochSecond());
deviceVariables.put(PROPERTY_DID, configuration.deviceId);
Expand Down Expand Up @@ -372,63 +374,62 @@ protected void disconnected(@Nullable String message) {
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;

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public MiIoAsyncCommunication(String ip, byte[] token, String did, int id, int t
this.timeout = timeout;
this.cloudConnector = cloudConnector;
setId(id);
startReceiver();
}

protected List<MiIoMessageListener> getListeners() {
Expand Down Expand Up @@ -249,7 +248,7 @@ MiIoSendCommand sendMiIoSendCommand(MiIoSendCommand miIoSendCommand) {
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;
}
Expand Down Expand Up @@ -435,7 +434,7 @@ private DatagramSocket getSocket() throws SocketException {
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 {
Expand Down Expand Up @@ -497,6 +496,10 @@ public String getDeviceId() {

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() {
Expand Down

0 comments on commit fc90fe4

Please sign in to comment.