Skip to content

Commit 40d04bc

Browse files
committed
only show up to 8 outlets and don't discover disabled outlets
1 parent d0126ea commit 40d04bc

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/oly/netpowerctrl/DiscoveryThread.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,26 @@ public void parsePacket(final String message, int recevied_port) {
8585
di.RecvPort = recevied_port;
8686
// leave SendPort as default, as we have no way to know.
8787

88-
for (int i=6; i<(msg.length-2); i++) {
89-
String outlet[] = msg[i].split(",");
88+
int disabledOutlets = 0;
89+
int numOutlets = 8; // normally, the device sends info for 8 outlets no matter how many are actually equipped
90+
91+
if (msg.length > 14)
92+
try {disabledOutlets = Integer.parseInt(msg[14]);} catch (NumberFormatException e) {}
93+
94+
if (msg.length < 14)
95+
numOutlets = msg.length-6;
96+
97+
for (int i=0; i<numOutlets; i++) {
98+
String outlet[] = msg[6+i].split(",");
9099
if (outlet.length < 1)
91100
continue;
92101
OutletInfo oi = new OutletInfo();
93-
oi.OutletNumber = i-5; // 1-based
102+
oi.OutletNumber = i+1; // 1-based
94103
oi.Description = outlet[0];
95104
if (outlet.length > 1)
96105
oi.State = outlet[1].equals("1");
106+
oi.Disabled = (disabledOutlets & (1<<i)) != 0;
107+
97108
di.Outlets.add(oi);
98109
}
99110

src/oly/netpowerctrl/NetpowerctrlActivity.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package oly.netpowerctrl;
22

33
import java.util.ArrayList;
4+
import java.util.List;
45

56
import android.annotation.SuppressLint;
67
import android.app.AlertDialog;
@@ -304,6 +305,15 @@ synchronized public void onReceive(Context context, Intent intent) {
304305
}
305306

306307
if (!found) {
308+
// remove it's disabled outlets
309+
List<OutletInfo> remove = new ArrayList<OutletInfo>();
310+
for (OutletInfo oi: device_info.Outlets)
311+
if (oi.Disabled)
312+
remove.add(oi);
313+
314+
for (OutletInfo oi: remove)
315+
device_info.Outlets.remove(oi);
316+
307317
alDiscoveredDevices.add(device_info);
308318
adpDiscoveredDevices.getFilter().filter("");
309319
}

src/oly/netpowerctrl/OutletInfo.java

+5
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ public class OutletInfo implements Parcelable {
88
public int OutletNumber;
99
public String Description;
1010
public boolean State;
11+
public boolean Disabled;
1112

1213
public OutletInfo() {
1314
OutletNumber = -1;
1415
Description = "";
1516
State = false;
17+
Disabled = false;
1618
}
1719

1820
public OutletInfo(OutletInfo other) {
1921
OutletNumber = other.OutletNumber;
2022
Description = other.Description;
2123
State = other.State;
24+
Disabled = other.Disabled;
2225
}
2326

2427
public int describeContents() {
@@ -29,6 +32,7 @@ public void writeToParcel(Parcel dest, int flags) {
2932
dest.writeInt(OutletNumber);
3033
dest.writeString(Description);
3134
dest.writeInt(State ? 1 : 0);
35+
dest.writeInt(Disabled ? 1 : 0);
3236
}
3337

3438
// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
@@ -47,6 +51,7 @@ private OutletInfo(Parcel in) {
4751
OutletNumber = in.readInt();
4852
Description = in.readString();
4953
State = in.readInt() != 0;
54+
Disabled = in.readInt() != 0;
5055
}
5156
}
5257

0 commit comments

Comments
 (0)