5
5
import java .util .ArrayList ;
6
6
import java .util .Collection ;
7
7
import java .util .Iterator ;
8
+ import java .util .List ;
8
9
9
10
import oly .netpowerctrl .datastructure .DeviceInfo ;
10
11
import oly .netpowerctrl .main .NetpowerctrlApplication ;
16
17
* all scenes to query.
17
18
*/
18
19
public class DeviceQuery {
19
- private Collection <DeviceInfo > devices_to_observe ;
20
+ private List <DeviceInfo > devices_to_observe ;
20
21
private DeviceUpdateStateOrTimeout target ;
21
22
private Handler timeoutHandler = new Handler ();
22
- private boolean foundBroudcastQueries = false ;
23
23
24
24
private Runnable timeoutRunnable = new Runnable () {
25
25
@ Override
@@ -29,26 +29,17 @@ public void run() {
29
29
return ;
30
30
31
31
if (devices_to_observe .isEmpty ()) {
32
- target .onDeviceQueryFinished (devices_to_observe .size ());
32
+ target .onDeviceQueryFinished (devices_to_observe );
33
+ NetpowerctrlApplication .instance .removeUpdateDeviceState (DeviceQuery .this );
33
34
return ;
34
35
}
35
36
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 );
51
40
}
41
+ NetpowerctrlApplication .instance .removeUpdateDeviceState (DeviceQuery .this );
42
+ target .onDeviceQueryFinished (devices_to_observe );
52
43
}
53
44
};
54
45
@@ -60,7 +51,7 @@ public DeviceQuery(DeviceUpdateStateOrTimeout target, DeviceInfo device_to_obser
60
51
// Register on main application object to receive device updates
61
52
NetpowerctrlApplication .instance .addUpdateDeviceState (this );
62
53
63
- DeviceSend .instance ().sendQuery (device_to_observe . HostName , device_to_observe . SendPort );
54
+ DeviceSend .instance ().sendQuery (device_to_observe );
64
55
timeoutHandler .postDelayed (timeoutRunnable , 1200 );
65
56
}
66
57
@@ -75,7 +66,7 @@ public DeviceQuery(DeviceUpdateStateOrTimeout target, Collection<DeviceInfo> dev
75
66
76
67
// Send out broadcast
77
68
for (DeviceInfo di : devices_to_observe )
78
- DeviceSend .instance ().sendQuery (di . HostName , di . SendPort );
69
+ DeviceSend .instance ().sendQuery (di );
79
70
}
80
71
81
72
/**
@@ -92,7 +83,7 @@ public DeviceQuery(DeviceUpdateStateOrTimeout target) {
92
83
NetpowerctrlApplication .instance .addUpdateDeviceState (this );
93
84
94
85
timeoutHandler .postDelayed (timeoutRunnable , 1200 );
95
- foundBroudcastQueries = DeviceSend .instance ().sendBroadcastQuery ();
86
+ DeviceSend .instance ().sendBroadcastQuery ();
96
87
}
97
88
98
89
/**
@@ -114,9 +105,23 @@ public boolean notifyObservers(DeviceInfo received_data) {
114
105
if (devices_to_observe .isEmpty ()) {
115
106
timeoutHandler .removeCallbacks (timeoutRunnable );
116
107
if (target != null )
117
- target .onDeviceQueryFinished (devices_to_observe . size () );
108
+ target .onDeviceQueryFinished (devices_to_observe );
118
109
return true ;
119
110
}
120
111
return false ;
121
112
}
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
+ }
122
127
}
0 commit comments