Skip to content

Commit d5253aa

Browse files
author
David Gräff
committed
Master/Slave for outlets
Master/Slave for scenes Fix crashes
1 parent 7e59299 commit d5253aa

37 files changed

+855
-484
lines changed

.idea/workspace.xml

+363-405
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/AndroidManifest.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="oly.netpowerctrl"
4-
android:versionCode="44"
5-
android:versionName="2.11">
4+
android:versionCode="46"
5+
android:versionName="3.0">
66

77
<!-- Api level 14: Android 4.0 15. Dezember 2011 -->
88
<!-- Api level 16: Android 4.1 27. Juni 2012 -->

app/src/main/java/oly/netpowerctrl/anel/AnelBroadcastSendJob.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Set;
1313

1414
import oly.netpowerctrl.application_state.NetpowerctrlApplication;
15+
import oly.netpowerctrl.application_state.NetpowerctrlService;
1516
import oly.netpowerctrl.datastructure.DeviceInfo;
1617
import oly.netpowerctrl.network.DeviceSend;
1718

@@ -74,10 +75,13 @@ public void process(DeviceSend deviceSend) {
7475
// Toast.LENGTH_SHORT).show();
7576

7677
// Query all existing anel devices directly
78+
NetpowerctrlService service = NetpowerctrlApplication.getService();
79+
if (service == null)
80+
return;
7781
List<DeviceInfo> devices = NetpowerctrlApplication.getDataController().configuredDevices;
7882
for (DeviceInfo di : devices) {
7983
if (di.pluginID.equals(AnelPlugin.PLUGIN_ID))
80-
di.getPluginInterface().requestData(di);
84+
di.getPluginInterface(service).requestData(di);
8185
}
8286
}
8387
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import oly.netpowerctrl.R;
2424
import oly.netpowerctrl.application_state.NetpowerctrlApplication;
25+
import oly.netpowerctrl.application_state.NetpowerctrlService;
2526
import oly.netpowerctrl.application_state.PluginInterface;
2627
import oly.netpowerctrl.application_state.RuntimeDataController;
2728
import oly.netpowerctrl.datastructure.DeviceInfo;
@@ -84,10 +85,10 @@ public void startDiscoveryThreads(int additional_port) {
8485
}
8586
}
8687

87-
public void stopDiscoveryThreads() {
88+
public void stopDiscoveryThreads(NetpowerctrlService service) {
8889
RuntimeDataController d = NetpowerctrlApplication.getDataController();
8990
for (DeviceInfo di : d.configuredDevices) {
90-
if (di.getPluginInterface() == this) {
91+
if (this.equals(di.getPluginInterface(service))) {
9192
di.setNotReachable("Energiesparmodus");
9293
d.onDeviceUpdated(di);
9394
}

app/src/main/java/oly/netpowerctrl/anel/ConfigureDeviceFragment.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void testDevice() {
5353

5454
test_state = TestStates.TEST_REACHABLE;
5555

56-
PluginInterface pi = device.getPluginInterface();
56+
PluginInterface pi = device.getPluginInterface(NetpowerctrlApplication.getService());
5757
assert pi != null;
5858
pi.prepareForDevices(device);
5959

@@ -80,7 +80,7 @@ public void onClick(DialogInterface dialog, int whichButton) {
8080
}
8181

8282
private void saveAndFinish() {
83-
PluginInterface pi = device.getPluginInterface();
83+
PluginInterface pi = device.getPluginInterface(NetpowerctrlApplication.getService());
8484
assert pi != null;
8585
pi.prepareForDevices(device);
8686

@@ -201,7 +201,7 @@ public void onDeviceUpdated(DeviceInfo di, boolean willBeRemoved) {
201201
test_state = TestStates.TEST_ACCESS;
202202
// Just send the current value of the first device port as target value.
203203
// Should change nothing but we will get a feedback if the credentials are working.
204-
PluginInterface pi = device.getPluginInterface();
204+
PluginInterface pi = device.getPluginInterface(NetpowerctrlApplication.getService());
205205
assert pi != null;
206206
if (deviceQuery != null) {
207207
deviceQuery.addDevice(device, false);

app/src/main/java/oly/netpowerctrl/application_state/NetpowerctrlService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void finish() {
123123
PluginInterface first = plugins.get(0);
124124
plugins.clear();
125125
plugins.add(first);
126-
((AnelPlugin) first).stopDiscoveryThreads();
126+
((AnelPlugin) first).stopDiscoveryThreads(this);
127127

128128
isNetworkReducedMode = true;
129129
// Stop send and listen threads

app/src/main/java/oly/netpowerctrl/application_state/RuntimeDataController.java

+24-6
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void clearNewDevices() {
173173
public void addToConfiguredDevices(DeviceInfo current_device, boolean write_to_disk) {
174174
// Already in configured devices?
175175
for (int i = configuredDevices.size() - 1; i >= 0; --i) {
176-
if (current_device.UniqueDeviceID.equals(configuredDevices.get(i).UniqueDeviceID)) {
176+
if (current_device.equalsByUniqueID(configuredDevices.get(i))) {
177177
configuredDevices.set(i, current_device);
178178
if (write_to_disk) {
179179
saveConfiguredDevices(true);
@@ -189,7 +189,7 @@ public void addToConfiguredDevices(DeviceInfo current_device, boolean write_to_d
189189

190190
// Remove from new devices list
191191
for (int i = 0; i < newDevices.size(); ++i) {
192-
if (newDevices.get(i).UniqueDeviceID.equals(current_device.UniqueDeviceID)) {
192+
if (newDevices.get(i).equalsByUniqueID(current_device)) {
193193
newDevices.remove(i);
194194
notifyNewDeviceObservers(current_device, true);
195195
break;
@@ -326,7 +326,7 @@ public void rename(DevicePort port, String new_name, DevicePortRenamed callback)
326326
if (callback != null)
327327
callback.devicePort_start_rename(port);
328328

329-
PluginInterface remote = port.device.getPluginInterface();
329+
PluginInterface remote = port.device.getPluginInterface(NetpowerctrlApplication.getService());
330330
if (remote != null) {
331331
remote.rename(port, new_name, callback);
332332
if (callback != null)
@@ -346,7 +346,7 @@ public void execute(Scene scene, ExecutionFinished callback) {
346346
if (p == null)
347347
continue;
348348

349-
PluginInterface remote = p.device.getPluginInterface();
349+
PluginInterface remote = p.device.getPluginInterface(NetpowerctrlApplication.getService());
350350
if (remote == null)
351351
continue;
352352

@@ -361,9 +361,27 @@ public void execute(Scene scene, ExecutionFinished callback) {
361361
}
362362

363363
public void execute(final DevicePort port, final int command, final ExecutionFinished callback) {
364-
PluginInterface remote = port.device.getPluginInterface();
364+
PluginInterface remote = port.device.getPluginInterface(NetpowerctrlApplication.getService());
365365
if (remote != null) {
366366
remote.execute(port, command, callback);
367+
368+
// Support for slaves of an outlet.
369+
List<UUID> slaves = port.getSlaves();
370+
if (slaves.size() > 0) {
371+
boolean bValue = false;
372+
if (command == DevicePort.ON)
373+
bValue = true;
374+
else if (command == DevicePort.OFF)
375+
bValue = false;
376+
else if (command == DevicePort.TOGGLE)
377+
bValue = port.current_value <= 0;
378+
379+
for (UUID slave_uuid : slaves) {
380+
DevicePort p = NetpowerctrlApplication.getDataController().findDevicePort(slave_uuid);
381+
if (p != null)
382+
execute(p, bValue ? DevicePort.ON : DevicePort.OFF, null);
383+
}
384+
}
367385
return;
368386
}
369387

@@ -390,7 +408,7 @@ public void setDevicePortBitmap(Context context, DevicePort port, Bitmap bitmap)
390408
public int countNetworkDevices() {
391409
int i = 0;
392410
for (DeviceInfo di : configuredDevices)
393-
if (di.isNetworkDevice()) ++i;
411+
if (di.isNetworkDevice(NetpowerctrlApplication.getService())) ++i;
394412
return i;
395413
}
396414
}

app/src/main/java/oly/netpowerctrl/datastructure/DeviceInfo.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import oly.netpowerctrl.R;
1818
import oly.netpowerctrl.anel.AnelPlugin;
1919
import oly.netpowerctrl.application_state.NetpowerctrlApplication;
20+
import oly.netpowerctrl.application_state.NetpowerctrlService;
2021
import oly.netpowerctrl.application_state.PluginInterface;
2122
import oly.netpowerctrl.preferences.SharedPrefs;
2223
import oly.netpowerctrl.utils.JSONHelper;
@@ -215,11 +216,11 @@ public int compareTo(DeviceInfo deviceInfo) {
215216
*
216217
* @return
217218
*/
218-
public PluginInterface getPluginInterface() {
219+
public PluginInterface getPluginInterface(NetpowerctrlService service) {
219220
PluginInterface pi = pluginInterface != null ? pluginInterface.get() : null;
220221

221222
if (pi == null) {
222-
pi = NetpowerctrlApplication.getService().getPluginInterface(this);
223+
pi = service.getPluginInterface(this);
223224
pluginInterface = new WeakReference<PluginInterface>(pi);
224225
}
225226

@@ -424,8 +425,8 @@ public void remove(Integer id) {
424425
DevicePorts.remove(id);
425426
}
426427

427-
public boolean isNetworkDevice() {
428-
PluginInterface pi = getPluginInterface();
428+
public boolean isNetworkDevice(NetpowerctrlService service) {
429+
PluginInterface pi = getPluginInterface(service);
429430
if (pi == null)
430431
return false;
431432
return pi.isNetworkPlugin();

app/src/main/java/oly/netpowerctrl/datastructure/DevicePort.java

+21
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public final class DevicePort implements Comparable {
3737
public DeviceInfo device;
3838
protected DevicePortType ui_type;
3939
private String Description = "";
40+
private List<UUID> slaves = new ArrayList<UUID>();
4041

4142

4243
public DevicePort(DeviceInfo di, DevicePortType ui_type) {
@@ -54,6 +55,7 @@ public DevicePort(DevicePort other) {
5455
positionRequest = other.positionRequest;
5556
ui_type = other.ui_type;
5657
device = other.device;
58+
slaves = other.slaves;
5759
id = other.id;
5860
// do not copy last_command_timecode! This value is set by the execute(..) methods
5961
}
@@ -92,6 +94,13 @@ public static DevicePort fromJSON(JsonReader reader, DeviceInfo di)
9294
oi.id = reader.nextInt();
9395
} else if (name.equals("uuid")) {
9496
oi.uuid = UUID.fromString(reader.nextString());
97+
} else if (name.equals("slaves")) {
98+
oi.slaves.clear();
99+
reader.beginArray();
100+
while (reader.hasNext()) {
101+
oi.slaves.add(UUID.fromString(reader.nextString()));
102+
}
103+
reader.endArray();
95104
} else if (name.equals("groups")) {
96105
oi.groups.clear();
97106
reader.beginArray();
@@ -164,6 +173,10 @@ public void toJSON(JsonWriter writer) throws IOException {
164173
for (UUID group_uuid : groups)
165174
writer.value(group_uuid.toString());
166175
writer.endArray();
176+
writer.name("slaves").beginArray();
177+
for (UUID slave_uuid : slaves)
178+
writer.value(slave_uuid.toString());
179+
writer.endArray();
167180
writer.endObject();
168181
}
169182

@@ -193,6 +206,14 @@ public void addToGroup(UUID uuid) {
193206
groups.add(uuid);
194207
}
195208

209+
public void setSlaves(List<UUID> slaves) {
210+
this.slaves = slaves;
211+
}
212+
213+
public List<UUID> getSlaves() {
214+
return slaves;
215+
}
216+
196217
public enum DevicePortType {
197218
TypeUnknown, TypeRangedValue, TypeToggle, TypeButton
198219
}

app/src/main/java/oly/netpowerctrl/datastructure/Scene.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public boolean isFavourite() {
2626
return favourite;
2727
}
2828

29+
public void setMaster(DevicePort master) {
30+
uuid_master = master.uuid;
31+
}
32+
33+
public UUID getMasterUUid() {
34+
return uuid_master;
35+
}
36+
2937

3038
public static class SceneItem {
3139
public UUID uuid = UUID.randomUUID();
@@ -149,7 +157,7 @@ public static Scene fromJSON(JsonReader reader) throws IOException {
149157
scene.sceneName = reader.nextString();
150158
} else if (name.equals("uuid")) {
151159
scene.uuid = UUID.fromString(reader.nextString());
152-
} else if (name.equals("master")) {
160+
} else if (name.equals("uuid_master")) {
153161
scene.uuid_master = UUID.fromString(reader.nextString());
154162
} else if (name.equals("favourite")) {
155163
scene.favourite = reader.nextBoolean();

app/src/main/java/oly/netpowerctrl/listadapter/DevicePortsAvailableAdapter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public DevicePortsAvailableAdapter(Context context) {
1616

1717
@Override
1818
public View getView(int position, View convertView, ViewGroup parent) {
19-
outlet_res_id = R.layout.create_scene_outlet_list_item;
19+
outlet_res_id = R.layout.available_outlet_list_item;
2020
return super.getView(position, convertView, parent);
2121
}
2222

app/src/main/java/oly/netpowerctrl/listadapter/DevicePortsBaseAdapter.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected static class ViewHolder implements View.OnClickListener, IconDeferredL
6262
seekBar = (SeekBar) convertView.findViewById(R.id.item_seekbar);
6363
//mainTextView = (LinearLayout) convertView.findViewById(R.id.outlet_list_text);
6464
entry = convertView.findViewById(R.id.item_layout);
65-
title = (TextView) convertView.findViewById(R.id.title);
65+
title = (TextView) convertView.findViewById(R.id.text1);
6666
subtitle = (TextView) convertView.findViewById(R.id.subtitle);
6767
}
6868

@@ -149,18 +149,32 @@ protected DevicePortsBaseAdapter(Context context, ListItemMenu mListContextMenu,
149149

150150
/**
151151
* Call this to load AnelDeviceSwitch data from a scene.
152+
* This will not update the view.
152153
*
153154
* @param scene
154155
*/
155-
public void loadByScene(Scene scene) {
156+
public void loadItemsOfScene(Scene scene) {
156157
for (Scene.SceneItem sceneItem : scene.sceneItems) {
157158
DevicePort port = NetpowerctrlApplication.getDataController().findDevicePort(sceneItem.uuid);
158159
if (port == null) {
159160
continue;
160161
}
161162
addItem(port, sceneItem.command);
162163
}
163-
notifyDataSetChanged();
164+
}
165+
166+
protected int getItemPositionByUUid(UUID uuid) {
167+
if (uuid == null)
168+
return -1;
169+
170+
int i = 0;
171+
for (DevicePortListItem info : all_outlets) {
172+
if (info.port.uuid.equals(uuid))
173+
return i;
174+
++i;
175+
}
176+
177+
return -1;
164178
}
165179

166180
public List<Scene.SceneItem> getScene() {

0 commit comments

Comments
 (0)