Skip to content

Commit dffc969

Browse files
author
davidgraeff
committed
Widgets, fix bulk send
Still missing / bugs: * Save scenes and configured devices as json files * NFC * Testing * Widget Updates, Update time configurable * Scenes no grid only list
1 parent 0f699fa commit dffc969

File tree

18 files changed

+526
-378
lines changed

18 files changed

+526
-378
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

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

app/app.iml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
<output url="file://$MODULE_DIR$/build/classes/debug" />
2626
<exclude-output />
2727
<content url="file://$MODULE_DIR$">
28-
<sourceFolder url="file://$MODULE_DIR$/build/source/r/debug" isTestSource="false" />
29-
<sourceFolder url="file://$MODULE_DIR$/build/source/aidl/debug" isTestSource="false" />
30-
<sourceFolder url="file://$MODULE_DIR$/build/source/buildConfig/debug" isTestSource="false" />
31-
<sourceFolder url="file://$MODULE_DIR$/build/source/rs/debug" isTestSource="false" />
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" />
3232
<sourceFolder url="file://$MODULE_DIR$/build/res/rs/debug" type="java-resource" />
3333
<sourceFolder url="file://$MODULE_DIR$/build/source/r/test/debug" isTestSource="true" />
3434
<sourceFolder url="file://$MODULE_DIR$/build/source/aidl/test/debug" isTestSource="true" />
@@ -68,8 +68,9 @@
6868
<excludeFolder url="file://$MODULE_DIR$/build/symbols" />
6969
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
7070
</content>
71-
<orderEntry type="jdk" jdkName="Android API 19 Platform" jdkType="Android SDK" />
71+
<orderEntry type="jdk" jdkName="Android API 16 Platform" jdkType="Android SDK" />
7272
<orderEntry type="sourceFolder" forTests="false" />
73+
<orderEntry type="library" exported="" name="support-v4-19.0.0" level="project" />
7374
</component>
7475
</module>
7576

app/src/main/java/oly/netpowerctrl/anelservice/DeviceSend.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package oly.netpowerctrl.anelservice;
22

33
import android.content.Context;
4+
import android.util.Log;
45

56
import java.io.IOException;
67
import java.net.DatagramPacket;
@@ -45,21 +46,24 @@ public DeviceSwitch(DeviceInfo di) {
4546
}
4647
this.access = di.UserName + di.Password;
4748
for (int i = 0; i < di.Outlets.size(); ++i) {
48-
if (di.Outlets.get(i).State)
49-
switchOn(i);
49+
Log.w("DeviceSwitch", Integer.valueOf(i).toString() + " " + di.DeviceName + " " + Integer.valueOf(di.Outlets.get(i).OutletNumber).toString() + " " + Boolean.valueOf(di.Outlets.get(i).State).toString());
50+
51+
if (!di.Outlets.get(i).Disabled && di.Outlets.get(i).State)
52+
switchOn(di.Outlets.get(i).OutletNumber);
5053
}
54+
5155
}
5256

5357
void switchOn(int outletNumber) {
54-
data |= ((byte) (1 << outletNumber));
58+
data |= ((byte) (1 << outletNumber - 1));
5559
}
5660

5761
void switchOff(int outletNumber) {
58-
data &= ~((byte) (1 << outletNumber));
62+
data &= ~((byte) (1 << outletNumber - 1));
5963
}
6064

6165
void toggle(int outletNumber) {
62-
if ((data & ((byte) (1 << outletNumber))) > 0) {
66+
if ((data & ((byte) (1 << outletNumber - 1))) > 0) {
6367
switchOff(outletNumber);
6468
} else {
6569
switchOn(outletNumber);
@@ -91,6 +95,9 @@ public void run() {
9195
if (!devices.containsKey(c.device_mac)) {
9296
devices.put(c.device_mac, new DeviceSwitch(c.outletinfo.device));
9397
}
98+
99+
//Log.w("sendOutlet",c.device_mac+" "+ Integer.valueOf(c.outletNumber).toString()+" "+Integer.valueOf(c.state).toString());
100+
94101
switch (c.state) {
95102
case 0:
96103
devices.get(c.device_mac).switchOn(c.outletNumber);
@@ -109,6 +116,18 @@ public void run() {
109116
sendAllOutlets(c.getValue(), s);
110117
}
111118
s.close();
119+
120+
// wait 100ms
121+
try {
122+
Thread.sleep(100);
123+
} catch (InterruptedException ignored) {
124+
}
125+
126+
// request new values from each device
127+
for (Map.Entry<String, DeviceSwitch> device : devices.entrySet()) {
128+
DeviceQuery.sendQuery(context, device.getValue().dest.getHostAddress(), device.getValue().port);
129+
}
130+
112131
} catch (final IOException e) {
113132
ShowToast.FromOtherThread(context, context.getResources().getString(R.string.error_sending_inquiry) + ": "
114133
+ e.getMessage());
@@ -141,8 +160,15 @@ public void run() {
141160

142161
static private void sendAllOutlets(final DeviceSwitch device, DatagramSocket s) throws IOException {
143162
if (device.dest != null) {
144-
String messageStr = String.format(Locale.US, "Sw%d%s", device.getSwitchByte(), device.access);
145-
s.send(new DatagramPacket(messageStr.getBytes(), messageStr.length(), device.dest, device.port));
163+
byte[] data = new byte[3 + device.access.length()];
164+
data[0] = 'S';
165+
data[1] = 'w';
166+
data[2] = device.getSwitchByte();
167+
System.arraycopy(device.access.getBytes(), 0, data, 3, device.access.length());
168+
169+
//Log.w("sendAllOutlets", String.valueOf( (char)(device.getSwitchByte()+'0') ));
170+
171+
s.send(new DatagramPacket(data, data.length, device.dest, device.port));
146172
// wait for 20ms trying not to congest the line
147173
try {
148174
Thread.sleep(20);

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
public class OutletCommand {
88
// This field is not saved and is filled by fromOutletInfo for cache purposes only
9-
public String description;
9+
public String description = "";
1010

11-
public String device_mac;
11+
public String device_mac = "";
1212
public int outletNumber;
13-
public int state; //0:off;1:on;2:toggle
13+
public int state = -1; //0:off;1:on;2:toggle
1414
public boolean enabled = false;
1515
public OutletInfo outletinfo = null;
1616

@@ -21,6 +21,12 @@ public String toString() {
2121
"§" + Integer.valueOf(outletNumber).toString() + "§" + Integer.valueOf(state).toString();
2222
}
2323

24+
@Override
25+
public boolean equals(Object o) {
26+
OutletCommand other = (OutletCommand) o;
27+
return other.device_mac.equals(device_mac) && other.outletNumber == outletNumber;
28+
}
29+
2430
public static OutletCommand fromString(String source) {
2531
OutletCommand c = new OutletCommand();
2632
String src[] = source.split("§");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010

1111
public class OutletCommandGroup {
12-
public String sceneName;
13-
public String sceneDetails;
12+
public String sceneName = "";
13+
public String sceneDetails = "";
1414
private String reserved = "";
1515
private UUID uuid;
1616
public ArrayList<OutletCommand> commands;

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

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,52 @@
1616
import java.util.List;
1717

1818
import oly.netpowerctrl.R;
19-
import oly.netpowerctrl.anelservice.DeviceUpdated;
2019
import oly.netpowerctrl.datastructure.DeviceInfo;
2120
import oly.netpowerctrl.datastructure.OutletCommand;
22-
import oly.netpowerctrl.datastructure.OutletCommandGroup;
2321
import oly.netpowerctrl.datastructure.OutletInfo;
2422
import oly.netpowerctrl.main.NetpowerctrlApplication;
2523
import oly.netpowerctrl.utils.ListItemMenu;
2624

27-
public class OutletListAdapter extends BaseAdapter implements ListAdapter, OnItemSelectedListener, DeviceUpdated {
25+
public class OutletListAdapter extends BaseAdapter implements ListAdapter, OnItemSelectedListener {
2826
private List<OutletCommand> all_outlets;
2927
private LayoutInflater inflater;
3028
private ArrayAdapter<CharSequence> spinner_adapter;
3129
private ListItemMenu listItemMenu = null;
3230

33-
public OutletListAdapter(Context context) {
31+
public static OutletListAdapter createByConfiguredDevices(Context context) {
32+
OutletListAdapter o = new OutletListAdapter(context);
33+
for (DeviceInfo device : NetpowerctrlApplication.instance.configuredDevices) {
34+
for (OutletInfo oi : device.Outlets) {
35+
oi.device = device;
36+
o.all_outlets.add(OutletCommand.fromOutletInfo(oi, false));
37+
}
38+
}
39+
return o;
40+
}
41+
42+
public static OutletListAdapter createByOutletCommands(Context context, List<OutletCommand> commands) {
43+
OutletListAdapter o = new OutletListAdapter(context);
44+
for (DeviceInfo device : NetpowerctrlApplication.instance.configuredDevices) {
45+
for (OutletInfo oi : device.Outlets) {
46+
oi.device = device;
47+
OutletCommand c = OutletCommand.fromOutletInfo(oi, false);
48+
int i = commands.indexOf(c);
49+
if (i != -1) {
50+
c.enabled = true;
51+
c.state = commands.get(i).state;
52+
}
53+
o.all_outlets.add(c);
54+
}
55+
}
56+
return o;
57+
}
58+
59+
private OutletListAdapter(Context context) {
3460
inflater = LayoutInflater.from(context);
3561
all_outlets = new ArrayList<OutletCommand>();
36-
NetpowerctrlApplication.instance.registerConfiguredObserver(this);
3762

3863
spinner_adapter = ArrayAdapter.createFromResource(context, R.array.shortcutchoices, android.R.layout.simple_spinner_item);
3964
spinner_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
40-
update();
4165
}
4266

4367
public int getCount() {
@@ -76,28 +100,16 @@ public View getView(int position, View convertView, ViewGroup parent) {
76100
return convertView;
77101
}
78102

79-
void update() {
80-
all_outlets.clear();
81-
for (DeviceInfo device : NetpowerctrlApplication.instance.configuredDevices) {
82-
for (OutletInfo oi : device.Outlets) {
83-
oi.device = device;
84-
all_outlets.add(OutletCommand.fromOutletInfo(oi, false));
85-
}
86-
}
87-
notifyDataSetChanged();
88-
}
89-
90-
public OutletCommandGroup getCheckedItems() {
91-
OutletCommandGroup og = new OutletCommandGroup();
103+
public ArrayList<OutletCommand> getCheckedItems() {
104+
ArrayList<OutletCommand> output = new ArrayList<OutletCommand>();
92105
for (OutletCommand c : all_outlets) {
93106
if (c.enabled) {
94-
og.add(c);
107+
output.add(c);
95108
}
96109
}
97-
return og;
110+
return output;
98111
}
99112

100-
101113
@Override
102114
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
103115
Spinner sp = (Spinner) parent;
@@ -118,9 +130,4 @@ public void onNothingSelected(AdapterView<?> parent) {
118130
public void setListItemMenu(ListItemMenu dce) {
119131
listItemMenu = dce;
120132
}
121-
122-
@Override
123-
public void onDeviceUpdated(DeviceInfo di) {
124-
update();
125-
}
126133
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ public void addScene(OutletCommandGroup data) {
9191
if (data == null)
9292
return;
9393

94-
scenes.add(data);
94+
int i = scenes.indexOf(data);
95+
if (i != -1) {
96+
scenes.set(i, data);
97+
} else
98+
scenes.add(data);
99+
95100
SharedPrefs.SaveGroups(scenes, context);
96101
notifyDataSetChanged();
97102
}

app/src/main/java/oly/netpowerctrl/main/NetpowerctrlActivity.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ protected void onCreate(Bundle savedInstanceState) {
7373
mTitle = mDrawerTitle = getTitle();
7474
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
7575
mDrawerList = (ListView) findViewById(R.id.left_drawer_list);
76-
mDrawerView = (View) findViewById(R.id.left_drawer);
76+
mDrawerView = findViewById(R.id.left_drawer);
7777

7878
try {
79+
//noinspection ConstantConditions
7980
((TextView) findViewById(R.id.version)).setText(getResources().getText(R.string.Version) + " " +
8081
getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
8182
} catch (PackageManager.NameNotFoundException ignored) {
@@ -192,11 +193,11 @@ public void onResume() {
192193
String intentAction = getIntent().getAction();
193194
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intentAction)) {
194195
Intent intent = getIntent();
195-
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
196+
Parcelable[] rawMessages = intent.getParcelableArrayExtra(
196197
NfcAdapter.EXTRA_NDEF_MESSAGES);
197198
// only one message sent during the beam
198-
assert rawMsgs != null;
199-
NdefMessage msg = (NdefMessage) rawMsgs[0];
199+
assert rawMessages != null;
200+
NdefMessage msg = (NdefMessage) rawMessages[0];
200201
@SuppressWarnings("unused")
201202
String beamedDeviceConfigurations = new String(msg.getRecords()[1].getPayload());
202203
//TODO nfc read
@@ -277,7 +278,7 @@ protected void onPostCreate(Bundle savedInstanceState) {
277278
@Override
278279
public void onConfigurationChanged(Configuration newConfig) {
279280
super.onConfigurationChanged(newConfig);
280-
// Pass any configuration change to the drawer toggls
281+
// Pass any configuration change to the drawer toggle
281282
mDrawerToggle.onConfigurationChanged(newConfig);
282283
}
283284
}

app/src/main/java/oly/netpowerctrl/preferences/DevicePreferencesFragment.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ public void onDeviceUpdated(DeviceInfo di) {
213213
if (di.HostName.equals(testDevice.HostName)) {
214214
testDevice.MacAddress = di.MacAddress;
215215
testDevice.DeviceName = di.DeviceName;
216+
testDevice.Outlets.clear();
217+
testDevice.Outlets.addAll(di.Outlets);
216218
test_state = TestStates.TEST_ACCESS;
217219
Handler handler = new Handler();
218220
handler.postDelayed(new Runnable() {
@@ -224,7 +226,8 @@ public void run() {
224226
}
225227
}
226228
}, 2000);
227-
DeviceSend.sendAllOutlets(getActivity(), new DeviceSend.DeviceSwitch(testDevice));
229+
DeviceSend.DeviceSwitch ds = new DeviceSend.DeviceSwitch(testDevice);
230+
DeviceSend.sendAllOutlets(getActivity(), ds);
228231
}
229232
} else if (test_state == TestStates.TEST_ACCESS) {
230233
Toast.makeText(getActivity(), "Test OK", Toast.LENGTH_SHORT).show();

0 commit comments

Comments
 (0)