Skip to content

Commit

Permalink
some review on exception handling
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent ARNAL <[email protected]>
  • Loading branch information
lo92fr committed Dec 20, 2024
1 parent 57ff83c commit 9e553d2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,83 +71,82 @@ public void setSmartthingsTypeRegistry(SmartthingsTypeRegistry typeRegistry) {
*/
@Override
public void startScan() {
doScan(true);
try {
doScan(true);
} catch (SmartthingsException ex) {
logger.error("Error during device scan:", ex.toString());
}
}

public void doScan(Boolean addDevice) {
public void doScan(Boolean addDevice) throws SmartthingsException {
SmartthingsBridgeHandler bridge = smartthingsBridgeHandler;
if (bridge == null) {
return;
}
SmartthingsApi api = bridge.getSmartthingsApi();

try {
SmartthingsDevice[] devices = api.getAllDevices();

for (SmartthingsDevice device : devices) {
SmartthingsDevice[] devices = api.getAllDevices();

String name = device.name;
String label = device.label;
for (SmartthingsDevice device : devices) {

logger.debug("Device");
String name = device.name;
String label = device.label;

if (device.components == null || device.components.length == 0) {
return;
}
logger.debug("Device");

Boolean enabled = false;
// if ("Four".equals(label)) {
// enabled = true;
// }
// if ("Petrole".equals(label)) {
// enabled = true;
// }
if (label.contains("cuisson")) {
enabled = true;
}
if (device.components == null || device.components.length == 0) {
return;
}

Boolean enabled = false;
// if ("Four".equals(label)) {
// enabled = true;
// }
// if ("Petrole".equals(label)) {
// enabled = true;
// }
if (label.contains("cuisson")) {
enabled = true;
}

if (!enabled) {
continue;
}
enabled = true;

if (!enabled) {
continue;
}

String deviceType = null;
for (SmartthingsComponent component : device.components) {
String compId = component.id;
String deviceType = null;
for (SmartthingsComponent component : device.components) {
String compId = component.id;

if (component.categories != null && component.categories.length > 0) {
for (SmartthingsCategory cat : component.categories) {
String catId = cat.name;
if (component.categories != null && component.categories.length > 0) {
for (SmartthingsCategory cat : component.categories) {
String catId = cat.name;

if ("main".equals(compId)) {
deviceType = catId;
}
if ("main".equals(compId)) {
deviceType = catId;
}
}
}
}

if (deviceType == null) {
logger.info("unknow device, bypass");
continue;
}

if ("white-and-color-ambiance".equals(name)) {
continue;
}
if (deviceType == null) {
logger.info("unknow device, bypass");
continue;
}

deviceType = deviceType.toLowerCase();
if (this.typeRegistry != null) {
this.typeRegistry.register(deviceType, device);
}
if (addDevice) {
createDevice(deviceType, Objects.requireNonNull(device));
}
if ("white-and-color-ambiance".equals(name)) {
continue;
}

deviceType = deviceType.toLowerCase();
if (this.typeRegistry != null) {
this.typeRegistry.register(deviceType, device);
}
} catch (SmartthingsException ex) {
logger.error("Unable to get devices !!");
return;
if (addDevice) {
createDevice(deviceType, Objects.requireNonNull(device));
}

}

logger.debug("End Discovery");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,37 @@ public void handleCommand(ChannelUID channelUID, Command command) {
public void initialize() {
super.initialize();

updateStatus(ThingStatus.UNKNOWN);

scheduler.submit(() -> {
try {
initRegistry();
updateStatus(ThingStatus.ONLINE);
} catch (SmartthingsException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
}
});
}

public void initRegistry() throws SmartthingsException {
initCapabilites();
discoService.doScan(false);

updateStatus(ThingStatus.ONLINE);
}

public void initCapabilites() {
public void initCapabilites() throws SmartthingsException {
logger.info("Start init capa");
SmartthingsApi api = this.getSmartthingsApi();
typeRegistry.setCloudBridgeHandler(this);

try {
SmartthingsCapabilitie[] capabilitiesList = api.getAllCapabilities();
SmartthingsCapabilitie[] capabilitiesList = api.getAllCapabilities();

for (SmartthingsCapabilitie cap : capabilitiesList) {
String capId = cap.id;
String capVersion = cap.version;
SmartthingsCapabilitie capa = api.getCapabilitie(capId, capVersion);
typeRegistry.registerCapabilities(capa);
}
} catch (SmartthingsException ex) {
logger.error("Error during initCapabilities!!");
for (SmartthingsCapabilitie cap : capabilitiesList) {
String capId = cap.id;
String capVersion = cap.version;
SmartthingsCapabilitie capa = api.getCapabilitie(capId, capVersion);
typeRegistry.registerCapabilities(capa);
}

logger.info("End init capa");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void createChannelTypes(SmartthingsCapabilitie capa) {
continue;
}

logger.info("capa: {} <> {}", capa.id, key);
logger.trace("capa: {} <> {}", capa.id, key);

if (attr == null) {
continue;
Expand Down Expand Up @@ -343,7 +343,7 @@ private void generateThingsType(String deviceId, String deviceLabel, String devi
}
}

logger.info("capa: {}", cap.id);
logger.trace("capa: {}", cap.id);
for (String key : capa.attributes.keySet()) {
if (key.indexOf("Range") >= 0) {
continue;
Expand Down

0 comments on commit 9e553d2

Please sign in to comment.