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

+1
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

+5-1
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

+2-1
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

+1-1
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

+2-1
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

+10-11
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

+5
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

+18-4
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

+1-1
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

+5-6
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

app/src/main/java/oly/netpowerctrl/groups/Group.java

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public boolean hasChanged() {
9090
return last_changeCode != name.hashCode();
9191
}
9292

93+
@Override
94+
public void setHasChanged() {
95+
last_changeCode = 0;
96+
}
97+
9398
@Override
9499
public void resetChanged() {
95100
last_changeCode = name.hashCode();

app/src/main/java/oly/netpowerctrl/ioconnection/IOConnection.java

+5
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ public boolean hasChanged() {
230230
return lastChangedCode != computeHash();
231231
}
232232

233+
@Override
234+
public void setHasChanged() {
235+
lastChangedCode = 0;
236+
}
237+
233238
@Override
234239
public void resetChanged() {
235240
lastChangedCode = computeHash();

app/src/main/java/oly/netpowerctrl/ioconnection/adapter/AdapterItemHeader.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ public class AdapterItemHeader extends AdapterItem implements onCollectionUpdate
1818

1919
public AdapterItemHeader(Credentials credentials, final DataService service, IOConnectionAdapter adapter) {
2020
super(adapter);
21-
if (!credentials.isConfigured())
22-
this.title = App.getAppString(R.string.device_new, credentials.getDeviceName());
23-
else
24-
this.title = credentials.getDeviceName();
2521

2622
this.credentials = credentials;
2723
this.isConfigured = credentials.isConfigured();
2824
this.deviceUID = credentials.deviceUID;
2925
this.UID = credentials.getUid();
26+
updateTitle();
3027

3128
/**
3229
* The following is done in a runnable, because AdapterItemConnection uses IOConnectionCollection.registerObserver and
@@ -41,6 +38,13 @@ public void run() {
4138

4239
}
4340

41+
private void updateTitle() {
42+
if (!credentials.isConfigured())
43+
this.title = App.getAppString(R.string.device_new, credentials.getDeviceName());
44+
else
45+
this.title = credentials.getDeviceName();
46+
}
47+
4448
@Override
4549
public void destroy() {
4650
App.getMainThreadHandler().post(new Runnable() {
@@ -60,6 +64,7 @@ public String getSubtitle() {
6064
public boolean updated(@NonNull CredentialsCollection credentialsCollection, @Nullable Credentials credentials, @NonNull ObserverUpdateActions action) {
6165
if ((action == ObserverUpdateActions.UpdateAction || action == ObserverUpdateActions.UpdateReachableAction) && this.credentials.equals(credentials)) {
6266
this.credentials = credentials;
67+
updateTitle();
6368
last_known_position = adapter.notifyItemChanged(this, last_known_position);
6469
}
6570

app/src/main/java/oly/netpowerctrl/plugin_anel/AnelPlugin.java

+9-13
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ void fillExecutable(Executable executable, Credentials credentials, String uid,
103103
executable.ui_type = ExecutableType.TypeToggle;
104104
executable.deviceUID = credentials.getUid();
105105
executable.setUid(uid);
106+
executable.min_value = 0;
107+
executable.max_value = 1;
106108
executable.current_value = value;
107109
executable.setCredentials(credentials, dataService.connections);
108110
}
@@ -181,21 +183,21 @@ private int executeDeviceBatch(@NonNull IOConnection ioConnection,
181183
containsOutlets = true;
182184
}
183185
switch (c.command) {
184-
case Executable.OFF:
186+
case ExecutableAndCommand.OFF:
185187
if (id >= 10 && id < 20) {
186188
data_io = switchOff(data_io, id - 10);
187189
} else if (id >= 0) {
188190
data_outlet = switchOff(data_outlet, id);
189191
}
190192
break;
191-
case Executable.ON:
193+
case ExecutableAndCommand.ON:
192194
if (id >= 10 && id < 20) {
193195
data_io = switchOn(data_io, id - 10);
194196
} else if (id >= 0) {
195197
data_outlet = switchOn(data_outlet, id);
196198
}
197199
break;
198-
case Executable.TOGGLE:
200+
case ExecutableAndCommand.TOGGLE:
199201
if (id >= 10 && id < 20) {
200202
data_io = toggle(data_io, id - 10);
201203
} else if (id >= 0) {
@@ -251,7 +253,7 @@ public boolean executeViaHTTP(IOConnectionHTTP ioConnection, Executable port, in
251253
}
252254
// The http interface can only toggle. If the current state is the same as the command state
253255
// then we request values instead of sending a command.
254-
if (command == Executable.TOGGLE && port.current_value == command)
256+
if (command == ExecutableAndCommand.TOGGLE && port.current_value == command)
255257
HttpThreadPool.execute(new HttpThreadPool.HTTPRunner<>(ioConnection, "strg.cfg",
256258
"", ioConnection, false, AnelReceiveSendHTTP.receiveCtrlHtml));
257259
else {
@@ -274,13 +276,7 @@ public boolean executeViaHTTP(IOConnectionHTTP ioConnection, Executable port, in
274276
public boolean execute(@NonNull Executable executable, int command, onExecutionFinished callback) {
275277
executable.setExecutionInProgress(true);
276278

277-
boolean bValue = false;
278-
if (command == Executable.ON)
279-
bValue = true;
280-
else if (command == Executable.OFF)
281-
bValue = false;
282-
else if (command == Executable.TOGGLE)
283-
bValue = executable.current_value <= 0;
279+
int value = (command == ExecutableAndCommand.TOGGLE) ? executable.getCurrentValueToggled() : command;
284280

285281
DeviceIOConnections deviceIOConnections = dataService.connections.openDevice(executable.deviceUID);
286282
final IOConnection ioConnection = deviceIOConnections != null ? deviceIOConnections.findReachable() : null;
@@ -305,15 +301,15 @@ else if (command == Executable.TOGGLE)
305301
byte[] data;
306302
if (id >= 10 && id < 20) {
307303
// IOS
308-
data = String.format(Locale.US, "%s%d%s%s", bValue ? "IO_on" : "IO_off",
304+
data = String.format(Locale.US, "%s%d%s%s", value > 0 ? "IO_on" : "IO_off",
309305
id - 10, credentials.userName, credentials.password).getBytes();
310306
UDPSend.sendMessage(connectionUDP, data);
311307
UDPSend.sendMessage(connectionUDP, requestMessage);
312308
if (callback != null) callback.addSuccess();
313309
return true;
314310
} else if (id >= 0) {
315311
// Outlets
316-
data = String.format(Locale.US, "%s%d%s%s", bValue ? "Sw_on" : "Sw_off",
312+
data = String.format(Locale.US, "%s%d%s%s", value > 0 ? "Sw_on" : "Sw_off",
317313
id, credentials.userName, credentials.password).getBytes();
318314
UDPSend.sendMessage(connectionUDP, data);
319315
UDPSend.sendMessage(connectionUDP, requestMessage);

app/src/main/java/oly/netpowerctrl/plugin_anel/AnelReceiveSendHTTP.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import oly.netpowerctrl.data.DataService;
1717
import oly.netpowerctrl.devices.Credentials;
1818
import oly.netpowerctrl.executables.Executable;
19-
import oly.netpowerctrl.executables.ExecutableType;
19+
import oly.netpowerctrl.executables.ExecutableAndCommand;
2020
import oly.netpowerctrl.ioconnection.IOConnection;
2121
import oly.netpowerctrl.ioconnection.IOConnectionHTTP;
2222
import oly.netpowerctrl.network.HttpThreadPool;
@@ -67,17 +67,18 @@ public void httpResponse(IOConnection ioConnection, boolean callback_success, St
6767

6868
// Update executables
6969
for (int i = 0; i < 8; ++i) {
70+
boolean disabled = data[30 + i].equals("1");
71+
if (disabled)
72+
continue;
73+
7074
String executable_uid = AnelPlugin.makeExecutableUID(ioConnection.deviceUID, i + 1);
7175
Executable executable = dataService.executables.findByUID(executable_uid);
7276
if (executable == null) {
7377
executable = new Executable();
74-
executable.ui_type = ExecutableType.TypeToggle;
7578
}
76-
anelPlugin.fillExecutable(executable, credentials, executable_uid, data[20 + i].equals("1") ? Executable.ON : Executable.OFF);
79+
anelPlugin.fillExecutable(executable, credentials, executable_uid, data[20 + i].equals("1") ? ExecutableAndCommand.ON : ExecutableAndCommand.OFF);
7780
executable.title = data[10 + i];
78-
boolean disabled = data[30 + i].equals("1");
79-
if (!disabled)
80-
dataService.executables.put(executable);
81+
dataService.executables.put(executable);
8182
}
8283
}
8384
}

0 commit comments

Comments
 (0)