55import java .util .ArrayList ;
66import java .util .Collection ;
77import java .util .Iterator ;
8+ import java .util .List ;
89
910import oly .netpowerctrl .datastructure .DeviceInfo ;
1011import oly .netpowerctrl .main .NetpowerctrlApplication ;
1617 * all scenes to query.
1718 */
1819public class DeviceQuery {
19- private Collection <DeviceInfo > devices_to_observe ;
20+ private List <DeviceInfo > devices_to_observe ;
2021 private DeviceUpdateStateOrTimeout target ;
2122 private Handler timeoutHandler = new Handler ();
22- private boolean foundBroudcastQueries = false ;
2323
2424 private Runnable timeoutRunnable = new Runnable () {
2525 @ Override
@@ -29,26 +29,17 @@ public void run() {
2929 return ;
3030
3131 if (devices_to_observe .isEmpty ()) {
32- target .onDeviceQueryFinished (devices_to_observe .size ());
32+ target .onDeviceQueryFinished (devices_to_observe );
33+ NetpowerctrlApplication .instance .removeUpdateDeviceState (DeviceQuery .this );
3334 return ;
3435 }
3536
36- // Special case: We are able to send broadcasts, but nevertheless not all
37- // configured devices responded, we will send specific queries now
38- if (foundBroudcastQueries ) {
39- foundBroudcastQueries = false ;
40- for (DeviceInfo di : devices_to_observe ) {
41- DeviceSend .instance ().sendQuery (di .HostName , di .SendPort );
42- }
43- // New timeout
44- timeoutHandler .postDelayed (timeoutRunnable , 1200 );
45- } else {
46- for (DeviceInfo di : devices_to_observe ) {
47- di .reachable = false ;
48- target .onDeviceTimeout (di );
49- }
50- target .onDeviceQueryFinished (devices_to_observe .size ());
37+ for (DeviceInfo di : devices_to_observe ) {
38+ di .reachable = false ;
39+ target .onDeviceTimeout (di );
5140 }
41+ NetpowerctrlApplication .instance .removeUpdateDeviceState (DeviceQuery .this );
42+ target .onDeviceQueryFinished (devices_to_observe );
5243 }
5344 };
5445
@@ -60,7 +51,7 @@ public DeviceQuery(DeviceUpdateStateOrTimeout target, DeviceInfo device_to_obser
6051 // Register on main application object to receive device updates
6152 NetpowerctrlApplication .instance .addUpdateDeviceState (this );
6253
63- DeviceSend .instance ().sendQuery (device_to_observe . HostName , device_to_observe . SendPort );
54+ DeviceSend .instance ().sendQuery (device_to_observe );
6455 timeoutHandler .postDelayed (timeoutRunnable , 1200 );
6556 }
6657
@@ -75,7 +66,7 @@ public DeviceQuery(DeviceUpdateStateOrTimeout target, Collection<DeviceInfo> dev
7566
7667 // Send out broadcast
7768 for (DeviceInfo di : devices_to_observe )
78- DeviceSend .instance ().sendQuery (di . HostName , di . SendPort );
69+ DeviceSend .instance ().sendQuery (di );
7970 }
8071
8172 /**
@@ -92,7 +83,7 @@ public DeviceQuery(DeviceUpdateStateOrTimeout target) {
9283 NetpowerctrlApplication .instance .addUpdateDeviceState (this );
9384
9485 timeoutHandler .postDelayed (timeoutRunnable , 1200 );
95- foundBroudcastQueries = DeviceSend .instance ().sendBroadcastQuery ();
86+ DeviceSend .instance ().sendBroadcastQuery ();
9687 }
9788
9889 /**
@@ -114,9 +105,23 @@ public boolean notifyObservers(DeviceInfo received_data) {
114105 if (devices_to_observe .isEmpty ()) {
115106 timeoutHandler .removeCallbacks (timeoutRunnable );
116107 if (target != null )
117- target .onDeviceQueryFinished (devices_to_observe . size () );
108+ target .onDeviceQueryFinished (devices_to_observe );
118109 return true ;
119110 }
120111 return false ;
121112 }
113+
114+ /**
115+ * Called right before this object is removed from the Application list
116+ * of DeviceQueries because the listener service has been shutdown. All
117+ * remaining device queries of this object have to timeout now.
118+ */
119+ public void finishWithTimeouts () {
120+ timeoutHandler .removeCallbacks (timeoutRunnable );
121+ for (DeviceInfo di : devices_to_observe ) {
122+ di .reachable = false ;
123+ target .onDeviceTimeout (di );
124+ }
125+ target .onDeviceQueryFinished (devices_to_observe );
126+ }
122127}
0 commit comments