Skip to content

Commit

Permalink
Replace anonymous Runnables by by lambda expressions / method references
Browse files Browse the repository at this point in the history
  • Loading branch information
ViToni committed Mar 23, 2023
1 parent 42885a1 commit acf69e2
Show file tree
Hide file tree
Showing 20 changed files with 493 additions and 662 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,21 +284,18 @@ public void commandReceived(ZigBeeCommand command) {
}
});

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
shutdown = true;
try {
System.in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
mainThread.interrupt();
mainThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
shutdown = true;
try {
System.in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
mainThread.interrupt();
mainThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}));
}
Expand Down Expand Up @@ -945,74 +942,62 @@ public boolean process(final ZigBeeApi zigbeeApi, final String[] args, final Pri
return false;
}

new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (true) {
print("STRESSING 1 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(167);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
int cnt = 0;
while (true) {
print("STRESSING 1 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(167);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();

new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (true) {
print("STRESSING 2 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(107);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
int cnt = 0;
while (true) {
print("STRESSING 2 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(107);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();

new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (true) {
print("STRESSING 3 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(131);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
int cnt = 0;
while (true) {
print("STRESSING 3 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(131);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();

new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (true) {
print("STRESSING 4 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(187);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
int cnt = 0;
while (true) {
print("STRESSING 4 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(187);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
Expand Down Expand Up @@ -1089,46 +1074,42 @@ public String getSyntax() {
*/
@Override
public boolean process(final ZigBeeApi zigbeeApi, final String[] args, final PrintStream out) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
int cnts = 1000;
int errors = 0;
int cnt = 0;
List<Integer> lqi = new ArrayList<>();
while (cnt < cnts) {
print("LQI Poll CNT: " + cnt++, out);
final ManagementLqiRequest neighborRequest = new ManagementLqiRequest(0);
neighborRequest.setDestinationAddress(new ZigBeeEndpointAddress(0));

CommandResult response;
try {
response = networkManager.sendTransaction(neighborRequest, neighborRequest).get();
final ManagementLqiResponse neighborResponse = response.getResponse();

if (neighborResponse == null || neighborResponse.getStatus() != ZdoStatus.SUCCESS) {
errors++;
continue;
}
if (neighborResponse.getNeighborTableList().isEmpty()) {
print("No neighbors", out);
continue;
}
lqi.add(neighborResponse.getNeighborTableList().get(0).getLqi());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
new Thread(() -> {
int cnts = 1000;
int errors = 0;
int cnt = 0;
List<Integer> lqi = new ArrayList<>();
while (cnt < cnts) {
print("LQI Poll CNT: " + cnt++, out);
final ManagementLqiRequest neighborRequest = new ManagementLqiRequest(0);
neighborRequest.setDestinationAddress(new ZigBeeEndpointAddress(0));

CommandResult response;
try {
response = networkManager.sendTransaction(neighborRequest, neighborRequest).get();
final ManagementLqiResponse neighborResponse = response.getResponse();

if (neighborResponse == null || neighborResponse.getStatus() != ZdoStatus.SUCCESS) {
errors++;
continue;
}
if (neighborResponse.getNeighborTableList().isEmpty()) {
print("No neighbors", out);
continue;
}
lqi.add(neighborResponse.getNeighborTableList().get(0).getLqi());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
IntSummaryStatistics stats = lqi.stream().mapToInt((x) -> x).summaryStatistics();

print("LQI Polling Complete", out);
print("Errors: " + errors, out);
print("Min : " + stats.getMin(), out);
print("Max : " + stats.getMax(), out);
print("Avg : " + stats.getAverage(), out);

}
IntSummaryStatistics stats = lqi.stream().mapToInt((x) -> x).summaryStatistics();

print("LQI Polling Complete", out);
print("Errors: " + errors, out);
print("Min : " + stats.getMin(), out);
print("Max : " + stats.getMax(), out);
print("Avg : " + stats.getAverage(), out);
}).start();

return true;
Expand Down Expand Up @@ -1157,12 +1138,7 @@ public String getSyntax() {
*/
@Override
public boolean process(final ZigBeeApi zigbeeApi, final String[] args, final PrintStream out) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
networkManager.reinitialize();
}
}).start();
new Thread(networkManager::reinitialize).start();

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,17 +630,14 @@ private void scheduleNetworkStatePolling() {
return;
}

pollingTimer = executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
// Don't poll the state if the network is down
// or we've sent a command to the dongle within the pollRate
if (!networkStateUp || (lastSendCommand + pollRate > System.currentTimeMillis())) {
return;
}
// Don't wait for the response. This is running in a single thread scheduler
frameHandler.queueFrame(new EzspNetworkStateRequest());
pollingTimer = executorService.scheduleWithFixedDelay(() -> {
// Don't poll the state if the network is down
// or we've sent a command to the dongle within the pollRate
if (!networkStateUp || (lastSendCommand + pollRate > System.currentTimeMillis())) {
return;
}
// Don't wait for the response. This is running in a single thread scheduler
frameHandler.queueFrame(new EzspNetworkStateRequest());
}, pollRate, pollRate, TimeUnit.MILLISECONDS);
}

Expand Down Expand Up @@ -808,40 +805,37 @@ public void sendCommand(final int msgTag, final ZigBeeApsFrame apsFrame) {

// The response from the SendXxxcast messages returns the network layer sequence number
// We need to correlate this with the messageTag
executorService.execute(new Runnable() {
@Override
public void run() {
synchronized (isConfiguredSync) {
if (!isConfigured) {
logger.debug("EZSP Dongle is not configured. Frame not sent: {}", apsFrame);
return;
}
executorService.execute(() -> {
synchronized (isConfiguredSync) {
if (!isConfigured) {
logger.debug("EZSP Dongle is not configured. Frame not sent: {}", apsFrame);
return;
}

lastSendCommand = System.currentTimeMillis();
frameHandler.sendEzspTransaction(transaction);

EmberStatus status = null;
if (transaction.getResponse() instanceof EzspSendUnicastResponse) {
fragmentationApsCounters.put(msgTag,
((EzspSendUnicastResponse) transaction.getResponse()).getSequence());
status = ((EzspSendUnicastResponse) transaction.getResponse()).getStatus();
} else if (transaction.getResponse() instanceof EzspSendBroadcastResponse) {
status = ((EzspSendBroadcastResponse) transaction.getResponse()).getStatus();
} else if (transaction.getResponse() instanceof EzspSendMulticastResponse) {
status = ((EzspSendMulticastResponse) transaction.getResponse()).getStatus();
} else {
logger.debug("Unable to get response from {} :: {}", transaction.getRequest(),
transaction.getResponse());
return;
}
lastSendCommand = System.currentTimeMillis();
frameHandler.sendEzspTransaction(transaction);

EmberStatus status = null;
if (transaction.getResponse() instanceof EzspSendUnicastResponse) {
fragmentationApsCounters.put(msgTag,
((EzspSendUnicastResponse) transaction.getResponse()).getSequence());
status = ((EzspSendUnicastResponse) transaction.getResponse()).getStatus();
} else if (transaction.getResponse() instanceof EzspSendBroadcastResponse) {
status = ((EzspSendBroadcastResponse) transaction.getResponse()).getStatus();
} else if (transaction.getResponse() instanceof EzspSendMulticastResponse) {
status = ((EzspSendMulticastResponse) transaction.getResponse()).getStatus();
} else {
logger.debug("Unable to get response from {} :: {}", transaction.getRequest(),
transaction.getResponse());
return;
}

// If this is EMBER_SUCCESS, then do nothing as the command is still not transmitted.
// If there was an error, then we let the system know we've failed already!
if (status == EmberStatus.EMBER_SUCCESS) {
return;
}
zigbeeTransportReceive.receiveCommandState(msgTag, ZigBeeTransportProgressState.TX_NAK);
// If this is EMBER_SUCCESS, then do nothing as the command is still not transmitted.
// If there was an error, then we let the system know we've failed already!
if (status == EmberStatus.EMBER_SUCCESS) {
return;
}
zigbeeTransportReceive.receiveCommandState(msgTag, ZigBeeTransportProgressState.TX_NAK);
}
});
}
Expand Down Expand Up @@ -941,18 +935,15 @@ public void handlePacket(EzspFrame response) {

// Message has been completed by the NCP
if (response instanceof EzspMessageSentHandler) {
executorService.execute(new Runnable() {
@Override
public void run() {
EzspMessageSentHandler sentHandler = (EzspMessageSentHandler) response;
ZigBeeTransportProgressState sentHandlerState;
if (sentHandler.getStatus() == EmberStatus.EMBER_SUCCESS) {
sentHandlerState = ZigBeeTransportProgressState.RX_ACK;
} else {
sentHandlerState = ZigBeeTransportProgressState.RX_NAK;
}
zigbeeTransportReceive.receiveCommandState(sentHandler.getMessageTag(), sentHandlerState);
executorService.execute(() -> {
EzspMessageSentHandler sentHandler = (EzspMessageSentHandler) response;
ZigBeeTransportProgressState sentHandlerState;
if (sentHandler.getStatus() == EmberStatus.EMBER_SUCCESS) {
sentHandlerState = ZigBeeTransportProgressState.RX_ACK;
} else {
sentHandlerState = ZigBeeTransportProgressState.RX_NAK;
}
zigbeeTransportReceive.receiveCommandState(sentHandler.getMessageTag(), sentHandlerState);
});
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,9 @@ public class EzspNeighborTable {
public EzspNeighborTable(AshFrameHandler ashHandler, int updatePeriod) {
this.ashHandler = ashHandler;

Runnable neighborUpdateThread = new Runnable() {
@Override
public void run() {
updateNeighbors();
}
};

routingEntries = getConfiguration(EzspConfigId.EZSP_CONFIG_SOURCE_ROUTE_TABLE_SIZE);

scheduler.scheduleAtFixedRate(neighborUpdateThread, updatePeriod, updatePeriod, TimeUnit.SECONDS);
scheduler.scheduleAtFixedRate(this::updateNeighbors, updatePeriod, updatePeriod, TimeUnit.SECONDS);
}

private void updateNeighbors() {
Expand Down
Loading

0 comments on commit acf69e2

Please sign in to comment.