Skip to content

Commit a91bb7f

Browse files
authored
[folderwatcher] Add more debugging (#20360)
* Add more debugging Signed-off-by: Alexandr Salamatov <goopilot@gmail.com>
1 parent f8e4fbb commit a91bb7f

6 files changed

Lines changed: 189 additions & 22 deletions

File tree

bundles/org.openhab.binding.folderwatcher/src/main/java/org/openhab/binding/folderwatcher/internal/FolderWatcherHandlerFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.osgi.service.component.annotations.Activate;
3232
import org.osgi.service.component.annotations.Component;
3333
import org.osgi.service.component.annotations.Reference;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
3436

3537
/**
3638
* The {@link FolderWatcherHandlerFactory} is responsible for creating things and thing
@@ -41,7 +43,7 @@
4143
@NonNullByDefault
4244
@Component(configurationPid = "binding.folderwatcher", service = ThingHandlerFactory.class)
4345
public class FolderWatcherHandlerFactory extends BaseThingHandlerFactory {
44-
46+
private static final Logger logger = LoggerFactory.getLogger(FolderWatcherHandlerFactory.class);
4547
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_FTPFOLDER,
4648
THING_TYPE_LOCALFOLDER, THING_TYPE_S3BUCKET, THING_TYPE_AZUREBLOB);
4749
private HttpClientFactory httpClientFactory;
@@ -59,16 +61,22 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
5961
@Override
6062
protected @Nullable ThingHandler createHandler(Thing thing) {
6163
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
64+
logger.debug("Creating handler for thing: {} of type: {}", thing.getUID(), thingTypeUID);
6265

6366
if (THING_TYPE_FTPFOLDER.equals(thingTypeUID)) {
67+
logger.debug("Creating FTP Folder Watcher handler");
6468
return new FtpFolderWatcherHandler(thing);
6569
} else if (THING_TYPE_LOCALFOLDER.equals(thingTypeUID)) {
70+
logger.debug("Creating Local Folder Watcher handler");
6671
return new LocalFolderWatcherHandler(thing);
6772
} else if (THING_TYPE_S3BUCKET.equals(thingTypeUID)) {
73+
logger.debug("Creating S3 Bucket Watcher handler");
6874
return new S3BucketWatcherHandler(thing, httpClientFactory);
6975
} else if (THING_TYPE_AZUREBLOB.equals(thingTypeUID)) {
76+
logger.debug("Creating Azure Blob Watcher handler");
7077
return new AzureBlobWatcherHandler(thing, httpClientFactory);
7178
}
79+
logger.debug("Unsupported thing type: {}", thingTypeUID);
7280
return null;
7381
}
7482
}

bundles/org.openhab.binding.folderwatcher/src/main/java/org/openhab/binding/folderwatcher/internal/common/WatcherCommon.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.List;
2121

2222
import org.eclipse.jdt.annotation.NonNullByDefault;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
2325

2426
/**
2527
* The {@link WatcherCommon} class contains commonly used methods.
@@ -28,37 +30,52 @@
2830
*/
2931
@NonNullByDefault
3032
public class WatcherCommon {
33+
private static final Logger logger = LoggerFactory.getLogger(WatcherCommon.class);
3134

3235
private static void initFile(File file, String watchDir) throws IOException {
36+
logger.debug("Initializing file {} with watch directory: {}", file.getAbsolutePath(), watchDir);
3337
try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file))) {
3438
fileWriter.write(watchDir);
3539
fileWriter.newLine();
40+
logger.debug("File {} initialized successfully", file.getAbsolutePath());
3641
}
3742
}
3843

3944
public static List<String> initStorage(File file, String watchDir) throws IOException {
45+
logger.debug("Initializing storage from file: {}, watch directory: {}", file.getAbsolutePath(), watchDir);
4046
List<String> returnList = List.of();
4147
List<String> currentFileListing = List.of();
4248
if (!file.exists()) {
49+
logger.debug("Listing file does not exist, creating parent directories and initializing file");
4350
Files.createDirectories(file.toPath().getParent());
4451
initFile(file, watchDir);
4552
} else {
53+
logger.debug("Listing file exists, reading existing entries");
4654
currentFileListing = Files.readAllLines(file.toPath().toAbsolutePath());
47-
if (currentFileListing.get(0).equals(watchDir)) {
55+
if (currentFileListing.isEmpty()) {
56+
logger.debug("File is empty, initializing with watch directory");
57+
initFile(file, watchDir);
58+
} else if (currentFileListing.get(0).equals(watchDir)) {
59+
logger.debug("File contains {} entries for matching watch directory", currentFileListing.size());
4860
returnList = currentFileListing;
4961
} else {
62+
logger.debug("Watch directory mismatch in file, reinitializing. Previous: {}, Current: {}",
63+
currentFileListing.get(0), watchDir);
5064
initFile(file, watchDir);
5165
}
5266
}
5367
return returnList;
5468
}
5569

5670
public static void saveNewListing(List<String> newList, File listingFile) throws IOException {
71+
logger.debug("Saving {} new entries to listing file: {}", newList.size(), listingFile.getAbsolutePath());
5772
try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(listingFile, true))) {
5873
for (String newFile : newList) {
5974
fileWriter.write(newFile);
6075
fileWriter.newLine();
76+
logger.trace("Saved entry: {}", newFile);
6177
}
78+
logger.debug("Successfully saved {} entries to listing file", newList.size());
6279
}
6380
}
6481
}

bundles/org.openhab.binding.folderwatcher/src/main/java/org/openhab/binding/folderwatcher/internal/handler/AzureBlobWatcherHandler.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,68 +70,97 @@ public void handleCommand(ChannelUID channelUID, Command command) {
7070
@Override
7171
public void initialize() {
7272
config = getConfigAs(AzureBlobWatcherConfiguration.class);
73+
logger.debug(
74+
"Initializing Azure Blob Watcher handler for {} with account: {}, container: {}, anonymous: {}, poll interval: {}s",
75+
thing.getUID(), config.azureAccountName, config.azureContainerName, config.azureAnonymous,
76+
config.pollIntervalAzure);
7377
if (config.azureAccountName.isBlank() || config.azureContainerName.isBlank()) {
78+
logger.debug("Azure configuration invalid: account={}, container={}", config.azureAccountName,
79+
config.azureContainerName);
7480
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
7581
"Account name or container configuration is invalid");
7682
return;
77-
} else if (config.pollIntervalAzure == 0) {
83+
} else if (config.pollIntervalAzure <= 0) {
84+
logger.debug("Polling interval is zero or negative");
7885
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
7986
"Polling interval must be greater than 0 seconds");
8087
return;
8188
}
8289

8390
if (config.azureAnonymous) {
91+
logger.debug("Creating Azure connection with anonymous access");
8492
azure = new AzureActions(httpClientFactory, config.azureAccountName, config.azureContainerName);
8593
} else {
94+
logger.debug("Creating Azure connection with access key");
8695
azure = new AzureActions(httpClientFactory, config.azureAccountName, config.azureContainerName,
8796
config.azureAccessKey);
8897
}
98+
logger.debug("Starting Azure blob refresh with {} second interval", config.pollIntervalAzure);
8999
updateStatus(ThingStatus.UNKNOWN);
90100
executionJob = scheduler.scheduleWithFixedDelay(this::refreshAzureBlobInformation, 0, config.pollIntervalAzure,
91101
TimeUnit.SECONDS);
92102
}
93103

94104
private boolean refreshAzureBlobInformation() {
105+
logger.debug("Refreshing Azure blob container information for {}/{}", config.azureAccountName,
106+
config.azureContainerName);
95107
if (previousBlobListing.isEmpty()) {
96108
try {
109+
logger.debug("Initializing Azure listing file for account: {}, container: {}", config.azureAccountName,
110+
config.azureContainerName);
97111
previousBlobListing = WatcherCommon.initStorage(currentBlobListingFile,
98112
config.azureAccountName + "-" + config.azureContainerName);
113+
logger.debug("Loaded {} previous Azure files from storage", previousBlobListing.size());
99114
} catch (Exception e) {
115+
logger.debug("Exception initializing Azure listing file: {}", e.getMessage());
100116
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
101117
"Local storage initialization error: " + e.getMessage());
102118
logger.debug("Can't write file {}: {}", currentBlobListingFile, e.getMessage());
103-
executionJob.cancel(false);
119+
ScheduledFuture<?> executionJob = this.executionJob;
120+
if (executionJob != null) {
121+
executionJob.cancel(false);
122+
}
104123
return false;
105124
}
106125
}
107126

108127
List<String> currentBlobListing = new ArrayList<>();
109128
try {
110129
currentBlobListing = azure.listContainer(config.containerPath);
130+
logger.debug("Azure container scan found {} total files", currentBlobListing.size());
111131
updateStatus(ThingStatus.ONLINE);
112132
List<String> difBlobListing = new ArrayList<>(currentBlobListing);
113133
difBlobListing.removeAll(previousBlobListing);
114-
difBlobListing.forEach(file -> triggerChannel(CHANNEL_NEWFILE, file));
134+
logger.debug("Detected {} new Azure files since last refresh", difBlobListing.size());
135+
difBlobListing.forEach(file -> {
136+
logger.trace("Triggering CHANNEL_NEWFILE with: {}", file);
137+
triggerChannel(CHANNEL_NEWFILE, file);
138+
});
115139

116140
if (!difBlobListing.isEmpty()) {
141+
logger.debug("Saving {} new files to listing file", difBlobListing.size());
117142
WatcherCommon.saveNewListing(difBlobListing, currentBlobListingFile);
118143
}
144+
logger.debug("Azure refresh completed, updated previous listing from {} to {} files",
145+
previousBlobListing.size(), currentBlobListing.size());
119146
previousBlobListing = new ArrayList<>(currentBlobListing);
120147
} catch (Exception e) {
148+
logger.debug("Exception connecting to Azure container: {}", e.getMessage(), e);
121149
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
122-
"Can't connect to the contaner: " + e.getMessage());
123-
logger.debug("Can't connect to the contaner: {}", e.getMessage());
150+
"Can't connect to the container: " + e.getMessage());
124151
return false;
125152
}
126153
return true;
127154
}
128155

129156
@Override
130157
public void dispose() {
158+
logger.debug("Disposing Azure Blob Watcher handler for {}", thing.getUID());
131159
ScheduledFuture<?> executionJob = this.executionJob;
132160
if (executionJob != null) {
133161
executionJob.cancel(true);
134162
this.executionJob = null;
163+
logger.debug("Cancelled execution job");
135164
}
136165
}
137166
}

0 commit comments

Comments
 (0)