Skip to content

Commit 6eb0b24

Browse files
author
David Gräff
committed
Fix crashes: Adding new device. Execute a scene.
Fix bug: Execute a scene: Transaction was not cleared after executed
1 parent cc8e1c0 commit 6eb0b24

File tree

14 files changed

+503
-314
lines changed

14 files changed

+503
-314
lines changed

.idea/workspace.xml

Lines changed: 368 additions & 221 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/app.iml

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
<component name="FacetManager">
44
<facet type="android" name="Android">
55
<configuration>
6-
<option name="SELECTED_BUILD_VARIANT" value="debug" />
7-
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
8-
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugJava" />
9-
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugTest" />
10-
<option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
6+
<option name="SELECTED_BUILD_VARIANT" value="release" />
7+
<option name="ASSEMBLE_TASK_NAME" value="assembleRelease" />
8+
<option name="COMPILE_JAVA_TASK_NAME" value="compileReleaseJava" />
9+
<option name="SOURCE_GEN_TASK_NAME" value="generateReleaseSources" />
1110
<option name="ALLOW_USER_CONFIGURATION" value="false" />
1211
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
1312
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
@@ -22,26 +21,21 @@
2221
</facet>
2322
</component>
2423
<component name="NewModuleRootManager" inherit-compiler-output="false">
25-
<output url="file://$MODULE_DIR$/build/classes/debug" />
24+
<output url="file://$MODULE_DIR$/build/classes/release" />
2625
<exclude-output />
2726
<content url="file://$MODULE_DIR$">
28-
<sourceFolder url="file://$MODULE_DIR$/build/source/r/debug" isTestSource="false" generated="true" />
29-
<sourceFolder url="file://$MODULE_DIR$/build/source/aidl/debug" isTestSource="false" generated="true" />
30-
<sourceFolder url="file://$MODULE_DIR$/build/source/buildConfig/debug" isTestSource="false" generated="true" />
31-
<sourceFolder url="file://$MODULE_DIR$/build/source/rs/debug" isTestSource="false" generated="true" />
32-
<sourceFolder url="file://$MODULE_DIR$/build/res/rs/debug" type="java-resource" />
33-
<sourceFolder url="file://$MODULE_DIR$/build/source/r/test/debug" isTestSource="true" generated="true" />
34-
<sourceFolder url="file://$MODULE_DIR$/build/source/aidl/test/debug" isTestSource="true" generated="true" />
35-
<sourceFolder url="file://$MODULE_DIR$/build/source/buildConfig/test/debug" isTestSource="true" generated="true" />
36-
<sourceFolder url="file://$MODULE_DIR$/build/source/rs/test/debug" isTestSource="true" generated="true" />
37-
<sourceFolder url="file://$MODULE_DIR$/build/res/rs/test/debug" type="java-test-resource" />
38-
<sourceFolder url="file://$MODULE_DIR$/src/debug/res" type="java-resource" />
39-
<sourceFolder url="file://$MODULE_DIR$/src/debug/resources" type="java-resource" />
40-
<sourceFolder url="file://$MODULE_DIR$/src/debug/aidl" isTestSource="false" />
41-
<sourceFolder url="file://$MODULE_DIR$/src/debug/assets" isTestSource="false" />
42-
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" />
43-
<sourceFolder url="file://$MODULE_DIR$/src/debug/jni" isTestSource="false" />
44-
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
27+
<sourceFolder url="file://$MODULE_DIR$/build/source/r/release" isTestSource="false" generated="true" />
28+
<sourceFolder url="file://$MODULE_DIR$/build/source/aidl/release" isTestSource="false" generated="true" />
29+
<sourceFolder url="file://$MODULE_DIR$/build/source/buildConfig/release" isTestSource="false" generated="true" />
30+
<sourceFolder url="file://$MODULE_DIR$/build/source/rs/release" isTestSource="false" generated="true" />
31+
<sourceFolder url="file://$MODULE_DIR$/build/res/rs/release" type="java-resource" />
32+
<sourceFolder url="file://$MODULE_DIR$/src/release/res" type="java-resource" />
33+
<sourceFolder url="file://$MODULE_DIR$/src/release/resources" type="java-resource" />
34+
<sourceFolder url="file://$MODULE_DIR$/src/release/aidl" isTestSource="false" />
35+
<sourceFolder url="file://$MODULE_DIR$/src/release/assets" isTestSource="false" />
36+
<sourceFolder url="file://$MODULE_DIR$/src/release/java" isTestSource="false" />
37+
<sourceFolder url="file://$MODULE_DIR$/src/release/jni" isTestSource="false" />
38+
<sourceFolder url="file://$MODULE_DIR$/src/release/rs" isTestSource="false" />
4539
<sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" />
4640
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
4741
<sourceFolder url="file://$MODULE_DIR$/src/main/aidl" isTestSource="false" />

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
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="37"
5-
android:versionName="2.8a">
4+
android:versionCode="38"
5+
android:versionName="2.8b">
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/AnelPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public void run() {
313313

314314
@Override
315315
public void addToTransaction(DevicePort port, int command) {
316-
command_list.add(new Scene.PortAndCommand());
316+
command_list.add(new Scene.PortAndCommand(port, command));
317317
}
318318

319319
@Override
@@ -333,6 +333,8 @@ public void executeTransaction(ExecutionFinished callback) {
333333
for (TreeMap.Entry<DeviceInfo, List<Scene.PortAndCommand>> entry : commands_grouped_by_devices.entrySet()) {
334334
executeDeviceBatch(entry.getKey(), entry.getValue(), callback);
335335
}
336+
337+
command_list.clear();
336338
}
337339

338340
public static final String PLUGIN_ID = "org.anel.outlets_and_io";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import oly.netpowerctrl.network.ExecutionFinished;
77

88
/**
9-
* Created by david on 10.04.14.
9+
* This interface defines a plugin
1010
*/
1111
public interface PluginInterface {
1212
void finish();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ public void addToTransaction(DevicePort port, int command) {
183183
public void executeTransaction(ExecutionFinished callback) {
184184
if (callback != null)
185185
callback.onExecutionFinished(transaction_counter);
186+
187+
transaction_counter = 0;
186188
}
187189

188190
@Override

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import android.content.Context;
44
import android.graphics.Bitmap;
5+
import android.util.Log;
56

67
import java.util.ArrayList;
78
import java.util.Collections;
89
import java.util.HashSet;
910
import java.util.Iterator;
1011
import java.util.List;
1112
import java.util.Set;
12-
import java.util.TreeSet;
1313
import java.util.UUID;
1414
import java.util.WeakHashMap;
1515

@@ -133,6 +133,16 @@ public void addUpdateDeviceState(DeviceQuery o) {
133133
updateDeviceStateList.add(o);
134134
}
135135

136+
private void notifyDeviceQueries(DeviceInfo target) {
137+
// notify observers who are using the DeviceQuery class
138+
Iterator<DeviceQuery> it = updateDeviceStateList.iterator();
139+
while (it.hasNext()) {
140+
// Return true if the DeviceQuery object has finished its task.
141+
if (it.next().notifyObservers(target))
142+
it.remove();
143+
}
144+
}
145+
136146
public void clear() {
137147
// Log.w("stopUseListener","ObserverConfigured: "+Integer.valueOf(observersStateChanged.size()).toString() +
138148
// " ObserverNew: "+Integer.valueOf(observersNew.size()).toString()+
@@ -218,8 +228,8 @@ public void addToConfiguredDevices(DeviceInfo current_device, boolean write_to_d
218228
// Remove from new devices list
219229
for (int i = 0; i < newDevices.size(); ++i) {
220230
if (newDevices.get(i).UniqueDeviceID.equals(current_device.UniqueDeviceID)) {
221-
notifyNewDeviceObservers(newDevices.get(i), true);
222231
newDevices.remove(i);
232+
notifyNewDeviceObservers(current_device, true);
223233
break;
224234
}
225235
}
@@ -252,27 +262,23 @@ public void onDeviceUpdated(DeviceInfo device_info) {
252262
for (DeviceInfo target : configuredDevices) {
253263
if (!device_info.UniqueDeviceID.equals(target.UniqueDeviceID))
254264
continue;
255-
target.copyFreshValues(device_info);
256-
notifyConfiguredDeviceChangeObservers(device_info, false);
257-
258-
// notify observers who are using the DeviceQuery class
259-
Iterator<DeviceQuery> it = updateDeviceStateList.iterator();
260-
while (it.hasNext()) {
261-
// Return true if the DeviceQuery object has finished its task.
262-
if (it.next().notifyObservers(target))
263-
it.remove();
265+
266+
if (!target.copyFreshValues(device_info)) {
267+
Log.w("RuntimeDataController", "same values: " + device_info.DeviceName);
268+
notifyDeviceQueries(target);
269+
return;
264270
}
265271

272+
Log.w("onDeviceUpdated", target.DeviceName + " " + String.valueOf(target.getHash()));
273+
274+
notifyConfiguredDeviceChangeObservers(target, false);
275+
notifyDeviceQueries(target);
276+
266277
return;
267278
}
268279

269280
// notify observers who are using the DeviceQuery class
270-
Iterator<DeviceQuery> it = updateDeviceStateList.iterator();
271-
while (it.hasNext()) {
272-
// Return true if the DeviceQuery object has finished its task.
273-
if (it.next().notifyObservers(device_info))
274-
it.remove();
275-
}
281+
notifyDeviceQueries(device_info);
276282

277283
// Do we have this new device already in the list?
278284
for (DeviceInfo target : newDevices) {
@@ -359,7 +365,7 @@ public void rename(DevicePort port, String new_name, DevicePortRenamed callback)
359365
}
360366

361367
public void execute(Scene scene, ExecutionFinished callback) {
362-
Set<PluginInterface> pluginInterfaces = new TreeSet<PluginInterface>();
368+
List<PluginInterface> pluginInterfaces = new ArrayList<PluginInterface>();
363369
for (Scene.SceneItem item : scene.sceneItems) {
364370
DevicePort p = NetpowerctrlApplication.getDataController().findDevicePort(item.uuid);
365371
if (p == null)
@@ -370,7 +376,8 @@ public void execute(Scene scene, ExecutionFinished callback) {
370376
continue;
371377

372378
remote.addToTransaction(p, item.command);
373-
pluginInterfaces.add(remote);
379+
if (!pluginInterfaces.contains(remote))
380+
pluginInterfaces.add(remote);
374381
}
375382

376383
for (PluginInterface p : pluginInterfaces) {

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,17 @@ public DeviceInfo(DeviceInfo other) {
135135
}
136136
}
137137

138-
public void copyFreshValues(DeviceInfo other) {
138+
/**
139+
* @param other Another DeviceInfo object from where to copy data from.
140+
* @return Return true if this object changed because of the values of "other".
141+
*/
142+
public boolean copyFreshValues(DeviceInfo other) {
139143
if (other.equals(this)) {
140-
return;
144+
return false;
141145
}
146+
147+
int hash_before = getHash();
148+
142149
// Add all devicePorts from DeviceInfo other to a new list (so that we can modify)
143150
//List<DevicePort> new_devicePorts = new ArrayList<DevicePort>();
144151
//new_devicePorts.addAll(other.DevicePorts);
@@ -175,6 +182,8 @@ public void copyFreshValues(DeviceInfo other) {
175182
reachable = other.reachable;
176183
not_reachable_reason = other.not_reachable_reason;
177184
updated = other.updated;
185+
186+
return getHash() != hash_before;
178187
}
179188

180189
/**
@@ -325,4 +334,14 @@ public boolean updatedAfter(long current_time) {
325334
public long getUpdatedTime() {
326335
return updated;
327336
}
337+
338+
public int getHash() {
339+
int h = DeviceName.hashCode() + 512 * ReceivePort + 1024 * SendPort + UniqueDeviceID.hashCode();
340+
if (reachable) h += 1000;
341+
342+
for (DevicePort port : DevicePorts) {
343+
h += port.current_value + port.getDescription().hashCode();
344+
}
345+
return h;
346+
}
328347
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,10 @@ private SceneItem getSceneItem(UUID uuid) {
203203
public static class PortAndCommand {
204204
public DevicePort port;
205205
public Integer command;
206+
207+
public PortAndCommand(DevicePort port, Integer command) {
208+
this.port = port;
209+
this.command = command;
210+
}
206211
}
207212
}

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

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,6 @@ public void onResume() {
3838
}
3939
}
4040

41-
private View getView(int position, View convertView, List<DeviceInfo> devices) {
42-
43-
if (convertView == null)
44-
convertView = inflater.inflate(R.layout.device_list_item, null);
45-
assert convertView != null;
46-
47-
DeviceInfo di = devices.get(position);
48-
TextView tvName = (TextView) convertView.findViewById(R.id.device_name);
49-
tvName.setText(di.DeviceName);
50-
if (di.isReachable())
51-
tvName.setPaintFlags(tvName.getPaintFlags() & ~(Paint.STRIKE_THRU_TEXT_FLAG));
52-
else
53-
tvName.setPaintFlags(tvName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
54-
55-
TextView tvIP = (TextView) convertView.findViewById(R.id.device_ip);
56-
String subtext = di.HostName;
57-
if (di.Temperature.length() > 0)
58-
subtext += ", " + di.Temperature;
59-
if (di.FirmwareVersion.length() > 0)
60-
subtext += ", " + di.FirmwareVersion;
61-
if (!di.isReachable())
62-
subtext += ", " + di.not_reachable_reason;
63-
tvIP.setText(subtext);
64-
65-
tvIP.setTag(position);
66-
67-
convertView.setTag(position);
68-
return convertView;
69-
}
70-
7141
@Override
7242
public void onDeviceUpdated(DeviceInfo di, boolean willBeRemoved) {
7343
notifyDataSetChanged();
@@ -88,8 +58,9 @@ public int getChildrenCount(int groupPosition) {
8858
if (NetpowerctrlApplication.getDataController().configuredDevices.size() == 0 && groupPosition == 0)
8959
groupPosition = 1;
9060

91-
return (groupPosition == 0) ? NetpowerctrlApplication.getDataController().configuredDevices.size() :
61+
int r = (groupPosition == 0) ? NetpowerctrlApplication.getDataController().configuredDevices.size() :
9262
NetpowerctrlApplication.getDataController().newDevices.size();
63+
return r;
9364
}
9465

9566
@Override
@@ -148,15 +119,43 @@ public View getGroupView(int groupPosition, boolean isExpanded, View convertView
148119
}
149120

150121
@Override
151-
public View getChildView(int groupPosition, int position, boolean b, View view, ViewGroup viewGroup) {
122+
public View getChildView(int groupPosition, int position, boolean b, View convertView, ViewGroup viewGroup) {
152123
// The groupPosition of new_devices is always 1
153124
if (NetpowerctrlApplication.getDataController().configuredDevices.size() == 0 && groupPosition == 0)
154125
groupPosition = 1;
155126

156-
return getView(position, view,
157-
(groupPosition == 0) ? NetpowerctrlApplication.getDataController().configuredDevices :
158-
NetpowerctrlApplication.getDataController().newDevices
159-
);
127+
List<DeviceInfo> devices = (groupPosition == 0) ? NetpowerctrlApplication.getDataController().configuredDevices :
128+
NetpowerctrlApplication.getDataController().newDevices;
129+
130+
if (convertView == null)
131+
convertView = inflater.inflate(R.layout.device_list_item, null);
132+
assert convertView != null;
133+
134+
if (position == 1)
135+
position = 1;
136+
DeviceInfo di = devices.get(position);
137+
TextView tvName = (TextView) convertView.findViewById(R.id.device_name);
138+
tvName.setText(di.DeviceName);
139+
if (di.isReachable())
140+
tvName.setPaintFlags(tvName.getPaintFlags() & ~(Paint.STRIKE_THRU_TEXT_FLAG));
141+
else
142+
tvName.setPaintFlags(tvName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
143+
144+
TextView tvIP = (TextView) convertView.findViewById(R.id.device_ip);
145+
String subtext = di.HostName;
146+
if (di.Temperature.length() > 0)
147+
subtext += ", " + di.Temperature;
148+
if (di.FirmwareVersion.length() > 0)
149+
subtext += ", " + di.FirmwareVersion;
150+
if (!di.isReachable())
151+
subtext += ", " + di.not_reachable_reason;
152+
tvIP.setText(subtext);
153+
154+
tvIP.setTag(position);
155+
156+
convertView.setTag(position);
157+
return convertView;
158+
160159
}
161160

162161
@Override

0 commit comments

Comments
 (0)