Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
package com.espressif.idf.core.tools.watcher;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

Expand All @@ -28,8 +37,8 @@ public class EimJsonWatchService extends Thread
private final List<EimJsonChangeListener> eimJsonChangeListeners = new CopyOnWriteArrayList<>();
private volatile boolean running = true;
private volatile boolean paused = false;

private volatile Instant lastModifiedTime;

private EimJsonWatchService() throws IOException
{
String directoryPathString = Platform.getOS().equals(Platform.OS_WIN32) ? EimConstants.EIM_WIN_DIR
Expand Down Expand Up @@ -80,38 +89,38 @@ public void addEimJsonChangeListener(EimJsonChangeListener listener)
eimJsonChangeListeners.add(listener);
}
}

public void removeAllListeners()
{
eimJsonChangeListeners.clear();
}

public static void withPausedListeners(Runnable task)
{
EimJsonWatchService watchService = getInstance();
boolean wasPaused = watchService.paused;
watchService.pauseListeners();

try
{
task.run();
task.run();
}
catch (Exception e)
{
Logger.log(e);
}
finally {
} finally
{
if (!wasPaused)
watchService.unpauseListeners();
}
}

public void pauseListeners()
{
Logger.log("Listeners are paused"); //$NON-NLS-1$
paused = true;
}

public void unpauseListeners()
{
Logger.log("Listeners are resumed"); //$NON-NLS-1$
Expand Down Expand Up @@ -150,9 +159,26 @@ public void run()
if (context instanceof Path path && path.toString().equals(EimConstants.EIM_JSON))
{
Path fullPath = watchDirectoryPath.resolve(path);
for (EimJsonChangeListener listener : eimJsonChangeListeners)
try
{
Instant currentModified = Files.getLastModifiedTime(fullPath).toInstant()
.truncatedTo(ChronoUnit.SECONDS);

if (lastModifiedTime != null && currentModified.compareTo(lastModifiedTime) <= 0)
{
continue; // skip duplicate or same-second event
}

lastModifiedTime = currentModified;

for (EimJsonChangeListener listener : eimJsonChangeListeners)
{
listener.onJsonFileChanged(fullPath, paused);
}
}
catch (IOException e)
{
listener.onJsonFileChanged(fullPath, paused);
Logger.log(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.espressif.idf.core.tools.EimLoader;
import com.espressif.idf.core.tools.ToolInitializer;
import com.espressif.idf.core.tools.vo.EimJson;
import com.espressif.idf.core.tools.watcher.EimJsonStateChecker;
import com.espressif.idf.core.tools.watcher.EimJsonWatchService;
import com.espressif.idf.core.util.IDFUtil;
import com.espressif.idf.core.util.StringUtil;
Expand Down Expand Up @@ -79,9 +78,7 @@ public void earlyStartup()
idfEnvironmentVariables = new IDFEnvironmentVariables();
eimLoader = new EimLoader(new StartupClassDownloadEimDownloadListener(), standardConsoleStream,
errorConsoleStream, Display.getDefault());
EimJsonStateChecker stateChecker = new EimJsonStateChecker(preferences);
eimJsonUiChangeHandler = new EimJsonUiChangeHandler(preferences);
stateChecker.updateLastSeenTimestamp();
EimJsonWatchService.getInstance().addEimJsonChangeListener(eimJsonUiChangeHandler);

if (!toolInitializer.isEimInstalled() && !toolInitializer.isEimIdfJsonPresent())
Expand Down Expand Up @@ -121,10 +118,6 @@ else if (toolInitializer.isEimIdfJsonPresent() && !toolInitializer.isEspIdfSet()
toolInitializer.findAndSetEimPath();
}

if (stateChecker.wasModifiedSinceLastRun())
{
showEimJsonStateChangeNotification();
}
}

private boolean checkIfEimPathMacOsIsInApplications()
Expand Down Expand Up @@ -217,7 +210,7 @@ private void displayInformationMessageBox(String messageTitle, String message)
}, ignored -> {
});
}

private void closeEspIdfManager()
{
Display.getDefault().asyncExec(() -> {
Expand All @@ -244,12 +237,6 @@ private void closeEspIdfManager()
});
}


private void showEimJsonStateChangeNotification()
{
eimJsonUiChangeHandler.displayMessageToUser();
}

private void notifyMissingTools()
{
GlobalModalLock.showModal(() -> MessageDialog.openQuestion(Display.getDefault().getActiveShell(),
Expand Down
Loading