55package com .espressif .idf .core .tools .watcher ;
66
77import java .io .IOException ;
8- import java .nio .file .*;
9- import java .nio .file .attribute .FileAttribute ;
8+ import java .nio .file .ClosedWatchServiceException ;
9+ import java .nio .file .FileSystems ;
10+ import java .nio .file .Files ;
11+ import java .nio .file .Path ;
12+ import java .nio .file .Paths ;
13+ import java .nio .file .StandardWatchEventKinds ;
14+ import java .nio .file .WatchEvent ;
15+ import java .nio .file .WatchKey ;
16+ import java .nio .file .WatchService ;
17+ import java .time .Instant ;
18+ import java .time .temporal .ChronoUnit ;
1019import java .util .List ;
1120import java .util .concurrent .CopyOnWriteArrayList ;
1221
@@ -28,8 +37,8 @@ public class EimJsonWatchService extends Thread
2837 private final List <EimJsonChangeListener > eimJsonChangeListeners = new CopyOnWriteArrayList <>();
2938 private volatile boolean running = true ;
3039 private volatile boolean paused = false ;
31-
32-
40+ private volatile Instant lastModifiedTime ;
41+
3342 private EimJsonWatchService () throws IOException
3443 {
3544 String directoryPathString = Platform .getOS ().equals (Platform .OS_WIN32 ) ? EimConstants .EIM_WIN_DIR
@@ -80,38 +89,38 @@ public void addEimJsonChangeListener(EimJsonChangeListener listener)
8089 eimJsonChangeListeners .add (listener );
8190 }
8291 }
83-
92+
8493 public void removeAllListeners ()
8594 {
8695 eimJsonChangeListeners .clear ();
8796 }
88-
97+
8998 public static void withPausedListeners (Runnable task )
9099 {
91100 EimJsonWatchService watchService = getInstance ();
92101 boolean wasPaused = watchService .paused ;
93102 watchService .pauseListeners ();
94-
103+
95104 try
96105 {
97- task .run ();
106+ task .run ();
98107 }
99108 catch (Exception e )
100109 {
101110 Logger .log (e );
102- }
103- finally {
111+ } finally
112+ {
104113 if (!wasPaused )
105114 watchService .unpauseListeners ();
106115 }
107116 }
108-
117+
109118 public void pauseListeners ()
110119 {
111120 Logger .log ("Listeners are paused" ); //$NON-NLS-1$
112121 paused = true ;
113122 }
114-
123+
115124 public void unpauseListeners ()
116125 {
117126 Logger .log ("Listeners are resumed" ); //$NON-NLS-1$
@@ -150,9 +159,26 @@ public void run()
150159 if (context instanceof Path path && path .toString ().equals (EimConstants .EIM_JSON ))
151160 {
152161 Path fullPath = watchDirectoryPath .resolve (path );
153- for (EimJsonChangeListener listener : eimJsonChangeListeners )
162+ try
163+ {
164+ Instant currentModified = Files .getLastModifiedTime (fullPath ).toInstant ()
165+ .truncatedTo (ChronoUnit .SECONDS );
166+
167+ if (lastModifiedTime != null && currentModified .compareTo (lastModifiedTime ) <= 0 )
168+ {
169+ continue ; // skip duplicate or same-second event
170+ }
171+
172+ lastModifiedTime = currentModified ;
173+
174+ for (EimJsonChangeListener listener : eimJsonChangeListeners )
175+ {
176+ listener .onJsonFileChanged (fullPath , paused );
177+ }
178+ }
179+ catch (IOException e )
154180 {
155- listener . onJsonFileChanged ( fullPath , paused );
181+ Logger . log ( e );
156182 }
157183 }
158184 }
0 commit comments