Skip to content

Commit a97b70c

Browse files
author
David Graeff
committed
Move OFF,ON,TOGGLE defines from Executable to ExecutableAndCommand. If an executable is of type toggle, a toggle switches value between value_min and value_max (not only between two states anymore).
If a command is send, and the remote device responds but the state hasn't changed, we still will still update the gui (remove grey-out text flag) now. Credential changes are saved now.
1 parent 4479a4c commit a97b70c

31 files changed

+138
-84
lines changed

app/src/main/java/oly/netpowerctrl/data/DataService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ public void addToConfiguredDevices(Credentials credentials) {
335335
credentials.setConfigured(true);
336336
this.credentials.put(credentials);
337337
connections.save(credentials.deviceUID);
338+
executables.save(credentials.deviceUID);
338339
}
339340

340341
public void showNotificationForNextRefresh(boolean notificationAfterNextRefresh) {

app/src/main/java/oly/netpowerctrl/devices/Credentials.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public void resetChanged() {
5353
mLastHashCode = computeChangedCode();
5454
}
5555

56+
public void setHasChanged() {
57+
mLastHashCode = 0;
58+
}
59+
5660
public boolean isConfigured() {
5761
return mConfigured;
5862
}
@@ -61,7 +65,6 @@ public void setConfigured(boolean configured) {
6165
if (configured && deviceUID == null)
6266
throw new RuntimeException("Credential cannot be set to configured without an UID!");
6367
this.mConfigured = configured;
64-
resetChanged();
6568
}
6669

6770
@Override
@@ -179,4 +182,5 @@ public String getDeviceName() {
179182
public void setDeviceName(String deviceName) {
180183
this.deviceName = deviceName;
181184
}
185+
182186
}

app/src/main/java/oly/netpowerctrl/devices/CredentialsCollection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public void put(Credentials credentials) {
5252
changed |= credentials.hasChanged();
5353
if (!changed) return;
5454
items.put(credentials.getUid(), credentials);
55-
storage.save(credentials);
55+
if (credentials.isConfigured())
56+
storage.save(credentials);
5657
notifyObservers(credentials, ObserverUpdateActions.UpdateAction);
5758
return;
5859
}

app/src/main/java/oly/netpowerctrl/devices/DeviceQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected void doAction(Credentials credentials, int attempt) {
8383
DeviceIOConnections l = dataService.connections.openDevice(credentials.deviceUID);
8484

8585
// No connections for this device. Nothing to do.
86-
if (l == null) {
86+
if (l == null || l.size() == 0) {
8787
// remove from list of devices to observe and notify observers
8888
deviceFailed(credentials);
8989
return;

app/src/main/java/oly/netpowerctrl/devices/TestCredentials.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import oly.netpowerctrl.App;
99
import oly.netpowerctrl.data.DataService;
1010
import oly.netpowerctrl.executables.Executable;
11+
import oly.netpowerctrl.executables.ExecutableAndCommand;
1112
import oly.netpowerctrl.executables.ExecutableCollection;
1213
import oly.netpowerctrl.utils.ObserverUpdateActions;
1314
import oly.netpowerctrl.utils.onCollectionUpdated;
@@ -53,7 +54,7 @@ public void run() {
5354
// This should not change the actual device state, but will cause an update signal of the
5455
// received and "changed" executable (in updated(...)).
5556
observedExecutable.current_value = observedExecutable.getCurrentValueToggled();
56-
observedExecutable.execute(dataService, Executable.TOGGLE, null);
57+
observedExecutable.execute(dataService, ExecutableAndCommand.TOGGLE, null);
5758
} else {
5859
test_state = TestStates.TEST_INIT;
5960
if (listener != null)

app/src/main/java/oly/netpowerctrl/executables/Executable.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@
2929
*/
3030
public class Executable implements Comparable, IOInterface {
3131
// Some value constants
32-
public static final int OFF = 0;
33-
public static final int ON = 1;
34-
public static final int TOGGLE = -1;
35-
public static final int INVALID = -2;
3632

3733
// Values
38-
public int min_value = OFF;
39-
public int max_value = ON;
34+
public int min_value = 0;
35+
public int max_value = 0;
4036
public int current_value = 0;
4137
// The device this port belongs to.
4238
public String deviceUID;
@@ -87,10 +83,6 @@ public void execute(@NonNull final DataService dataService, final int command, @
8783
}
8884
}
8985

90-
public void execute(@NonNull final DataService dataService, @Nullable final onExecutionFinished callback) {
91-
execute(dataService, TOGGLE, callback);
92-
}
93-
9486
public final Set<String> getGroupUIDs() {
9587
return group_uids;
9688
}
@@ -124,7 +116,9 @@ public final int getMinimumValue() {
124116
}
125117

126118
public int getCurrentValueToggled() {
127-
return current_value > min_value ? min_value : max_value;
119+
int c = (current_value + 1) % (max_value + 1);
120+
if (c < min_value) c = min_value;
121+
return c;
128122
}
129123

130124
/**
@@ -239,6 +233,11 @@ public boolean hasChanged() {
239233
return isSaveable && last_hash_code != computeChangedCode();
240234
}
241235

236+
@Override
237+
public void setHasChanged() {
238+
last_hash_code = 0;
239+
}
240+
242241
@Override
243242
public void resetChanged() {
244243
last_hash_code = computeChangedCode();

app/src/main/java/oly/netpowerctrl/executables/ExecutableAndCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
* Created by david on 21.04.15.
55
*/
66
public class ExecutableAndCommand {
7+
public static final int OFF = 0;
8+
public static final int ON = 1;
9+
public static final int TOGGLE = -1;
10+
public static final int INVALID = -2;
11+
712
public final Executable executable;
813
public final Integer command;
914

app/src/main/java/oly/netpowerctrl/executables/ExecutableCollection.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,20 @@ public void put(Executable executable) {
6666
return;
6767
}
6868

69-
executable.setExecutionInProgress(false);
70-
7169
Executable existed = items.get(executable.getUid());
7270
if (existed != null) {
73-
if (executable == existed && !executable.hasChanged()) return;
71+
if (executable == existed && !executable.hasChanged() && !executable.executionInProgress())
72+
return;
73+
74+
executable.setExecutionInProgress(false);
7475

7576
items.put(executable.getUid(), executable);
7677
if (!executable.needCredentials() || executable.getCredentials().isConfigured())
7778
storage.save(executable);
7879
notifyObservers(executable, ObserverUpdateActions.UpdateAction);
7980
return;
80-
}
81+
} else
82+
executable.setExecutionInProgress(false);
8183

8284
if (executable.isSaveable()) {
8385
items.put(executable.getUid(), executable);
@@ -169,6 +171,18 @@ public List<Executable> filterExecutables(Credentials credentials) {
169171
return list;
170172
}
171173

174+
/**
175+
* Save all connections that belong to the same device ID as the given credential object.
176+
* This will not issue updated notifications even if the objects have changed since last save.
177+
*/
178+
public void save(String deviceUID) {
179+
for (Executable executable : items.values()) {
180+
if (!executable.deviceUID.equals(deviceUID)) continue;
181+
executable.setHasChanged();
182+
put(executable);
183+
}
184+
}
185+
172186
public interface PredicateExecutable {
173187
boolean accept(Executable e);
174188
}

app/src/main/java/oly/netpowerctrl/executables/ExecutablesFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ public boolean onItemClick(View view, final int position, boolean isLongClick) {
377377
((ExecutableViewHolder) mRecyclerView.findViewHolderForAdapterPosition(position)).animate();
378378
adapter.notifyItemChanged(position);
379379

380-
item.getExecutable().execute(dataService, null);
380+
item.getExecutable().execute(dataService, ExecutableAndCommand.TOGGLE, null);
381381
return true;
382382
}
383383

app/src/main/java/oly/netpowerctrl/executables/adapter/InputDemo.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
import java.util.List;
55

66
import oly.netpowerctrl.data.DataService;
7-
import oly.netpowerctrl.executables.Executable;
7+
import oly.netpowerctrl.executables.ExecutableAndCommand;
88
import oly.netpowerctrl.network.ReachabilityStates;
99
import oly.netpowerctrl.scenes.Scene;
1010

11-
;
12-
1311
/**
14-
* Created by david on 07.07.14.
12+
* Adapter with some demo executables.
1513
*/
1614
public class InputDemo extends AdapterInput {
1715
private List<Scene> demoItems = new ArrayList<>();
@@ -24,7 +22,8 @@ public InputDemo() {
2422
private Scene createScene(String name, ReachabilityStates isReachable, boolean isOn) {
2523
Scene scene = Scene.createNewScene();
2624
scene.title = name;
27-
scene.max_value = (1);
25+
scene.min_value = 0;
26+
scene.max_value = 1;
2827
scene.current_value = (isOn ? 1 : 0);
2928
scene.setReachable(isReachable);
3029
return scene;
@@ -33,7 +32,7 @@ private Scene createScene(String name, ReachabilityStates isReachable, boolean i
3332
@Override
3433
public void doUpdateNow() {
3534
for (Scene scene : demoItems)
36-
adapterSource.addItem(scene, Executable.TOGGLE);
35+
adapterSource.addItem(scene, ExecutableAndCommand.TOGGLE);
3736
}
3837

3938
@Override

0 commit comments

Comments
 (0)