Skip to content

Commit bbfdb45

Browse files
author
David Graeff
committed
Executable+IOConnection: missing writer.close(), so write didn't work. Add check and exception to capture this for the future.
Hidding/Showing of executables work. Cache reachability state in executable instead of looking up ioconnections each time.
1 parent ae6fb6a commit bbfdb45

30 files changed

+337
-216
lines changed

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

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Context;
44
import android.content.Intent;
55
import android.net.Uri;
6-
import android.os.AsyncTask;
76
import android.support.annotation.NonNull;
87
import android.support.annotation.Nullable;
98
import android.util.Log;
@@ -25,7 +24,6 @@
2524
import oly.netpowerctrl.data.AbstractBasePlugin;
2625
import oly.netpowerctrl.data.DataService;
2726
import oly.netpowerctrl.devices.Credentials;
28-
import oly.netpowerctrl.devices.DevicesObserver;
2927
import oly.netpowerctrl.executables.Executable;
3028
import oly.netpowerctrl.executables.ExecutableAndCommand;
3129
import oly.netpowerctrl.executables.ExecutableType;
@@ -105,10 +103,10 @@ public static String extractIDFromExecutableUID_s(String uid) {
105103

106104
void fillExecutable(Executable executable, Credentials credentials, String uid, int value) {
107105
executable.ui_type = ExecutableType.TypeToggle;
108-
executable.credentials = credentials;
109106
executable.deviceUID = credentials.getUid();
110107
executable.setUid(uid);
111108
executable.current_value = value;
109+
executable.setCredentials(credentials);
112110
}
113111

114112
Credentials createDefaultCredentials(String MacAddress) {
@@ -140,7 +138,7 @@ private int executeDeviceBatch(@NonNull IOConnection ioConnection,
140138
int success = 0;
141139
// Use Http instead of UDP for sending. For each batch command we will send a single http request
142140
for (ExecutableAndCommand c : command_list) {
143-
boolean ok = executeViaHTTP(ioConnection, c.port, c.command);
141+
boolean ok = executeViaHTTP(ioConnection, c.executable, c.command);
144142
if (ok) ++success;
145143
}
146144
return success;
@@ -175,9 +173,9 @@ private int executeDeviceBatch(@NonNull IOConnection ioConnection,
175173

176174
// Second step: Apply commands
177175
for (ExecutableAndCommand c : command_list) {
178-
c.port.last_command_timecode = System.currentTimeMillis();
176+
c.executable.setExecutionInProgress(true);
179177

180-
int id = extractIDFromExecutableUID(c.port.getUid());
178+
int id = extractIDFromExecutableUID(c.executable.getUid());
181179
if (id >= 10 && id < 20) {
182180
containsIO = true;
183181
} else if (id >= 0) {
@@ -217,13 +215,13 @@ private int executeDeviceBatch(@NonNull IOConnection ioConnection,
217215
data[0] = 'S';
218216
data[1] = 'w';
219217
data[2] = data_outlet;
220-
new UDPSend(dataService, ioConnection, data, requestMessage, UDPErrors.INQUERY_REQUEST);
218+
new UDPSend(ioConnection, data, requestMessage, UDPErrors.INQUERY_REQUEST);
221219
}
222220
if (containsIO) {
223221
data[0] = 'I';
224222
data[1] = 'O';
225223
data[2] = data_io;
226-
new UDPSend(dataService, ioConnection, data, requestMessage, UDPErrors.INQUERY_REQUEST);
224+
new UDPSend(ioConnection, data, requestMessage, UDPErrors.INQUERY_REQUEST);
227225
}
228226

229227
return command_list.size();
@@ -325,7 +323,7 @@ public boolean executeViaHTTP(IOConnection ioConnection, Executable port, int co
325323
*/
326324
@Override
327325
public boolean execute(@NonNull Executable executable, int command, onExecutionFinished callback) {
328-
executable.last_command_timecode = System.currentTimeMillis();
326+
executable.setExecutionInProgress(true);
329327

330328
boolean bValue = false;
331329
if (command == Executable.ON)
@@ -359,14 +357,14 @@ else if (command == Executable.TOGGLE)
359357
// IOS
360358
data = String.format(Locale.US, "%s%d%s%s", bValue ? "IO_on" : "IO_off",
361359
id - 10, credentials.userName, credentials.password).getBytes();
362-
new UDPSend(dataService, ioConnection, data, requestMessage, UDPErrors.INQUERY_REQUEST);
360+
new UDPSend(ioConnection, data, requestMessage, UDPErrors.INQUERY_REQUEST);
363361
if (callback != null) callback.addSuccess();
364362
return true;
365363
} else if (id >= 0) {
366364
// Outlets
367365
data = String.format(Locale.US, "%s%d%s%s", bValue ? "Sw_on" : "Sw_off",
368366
id, credentials.userName, credentials.password).getBytes();
369-
new UDPSend(dataService, ioConnection, data, requestMessage, UDPErrors.INQUERY_REQUEST);
367+
new UDPSend(ioConnection, data, requestMessage, UDPErrors.INQUERY_REQUEST);
370368
if (callback != null) callback.addSuccess();
371369
return true;
372370
} else {
@@ -408,7 +406,7 @@ public void requestData(@NonNull IOConnection ioConnection) {
408406
HttpThreadPool.execute(new HttpThreadPool.HTTPRunner<>((IOConnectionHTTP) ioConnection,
409407
"strg.cfg", "", ioConnection, false, AnelReceiveSendHTTP.receiveCtrlHtml));
410408
} else {
411-
new UDPSend(dataService, ioConnection, requestMessage, UDPErrors.INQUERY_REQUEST);
409+
new UDPSend(ioConnection, requestMessage, UDPErrors.INQUERY_REQUEST);
412410
}
413411
}
414412

@@ -417,22 +415,6 @@ public Credentials createNewDefaultCredentials() {
417415
return createDefaultCredentials(UUID.randomUUID().toString());
418416
}
419417

420-
@Override
421-
public long getUpdatedTime(Executable executable) {
422-
DeviceIOConnections deviceIOConnections = dataService.connections.openDevice(executable.deviceUID);
423-
if (deviceIOConnections == null) return 0;
424-
IOConnection ioConnection = deviceIOConnections.findReachable();
425-
if (ioConnection == null) return 0;
426-
return ioConnection.getLastUsed();
427-
}
428-
429-
@Override
430-
public String getDeviceName(Executable executable) {
431-
Credentials credentials = dataService.credentials.findByUID(executable.deviceUID);
432-
if (credentials == null) throw new RuntimeException();
433-
return credentials.deviceName;
434-
}
435-
436418
@Override
437419
public ReachabilityStates getReachableState(Executable executable) {
438420
DeviceIOConnections deviceIOConnections = dataService.connections.openDevice(executable.deviceUID);
@@ -531,7 +513,7 @@ public void executeTransaction(onExecutionFinished callback) {
531513

532514
// add to tree
533515
for (ExecutableAndCommand executableAndCommand : command_list) {
534-
DeviceIOConnections deviceIOConnections = dataService.connections.openDevice(executableAndCommand.port.deviceUID);
516+
DeviceIOConnections deviceIOConnections = dataService.connections.openDevice(executableAndCommand.executable.deviceUID);
535517
if (deviceIOConnections == null) continue;
536518
IOConnection connection = deviceIOConnections.findReachable();
537519
if (connection == null) continue;
@@ -567,22 +549,22 @@ public String getLocalizedName() {
567549
public void checkDevicesReachabilityAfterNetworkChange() {
568550
Logging.getInstance().logEnergy("Anel: check availability");
569551

570-
startNetworkReceivers(false);
571-
572-
dataService.addDeviceObserver(new DevicesObserver(dataService.credentials.findByPlugin(this), new DevicesObserver.onDevicesObserverFinished() {
573-
@Override
574-
public void onObserverJobFinished(DevicesObserver devicesObserver) {
575-
if (!devicesObserver.isAllTimedOut()) return;
576-
//TODO
577-
new AsyncTask<AnelPlugin, Void, Void>() {
578-
@Override
579-
protected Void doInBackground(AnelPlugin... plugin) {
580-
plugin[0].stopNetwork();
581-
return null;
582-
}
583-
}.execute(AnelPlugin.this);
584-
}
585-
}));
552+
// startNetworkReceivers(false);
553+
//
554+
// dataService.addDeviceObserver(new DevicesObserver(dataService.credentials.findByPlugin(this), new DevicesObserver.onDevicesObserverFinished() {
555+
// @Override
556+
// public void onObserverJobFinished(DevicesObserver devicesObserver) {
557+
// if (!devicesObserver.isAllTimedOut()) return;
558+
// //TODO
559+
// new AsyncTask<AnelPlugin, Void, Void>() {
560+
// @Override
561+
// protected Void doInBackground(AnelPlugin... plugin) {
562+
// plugin[0].stopNetwork();
563+
// return null;
564+
// }
565+
// }.execute(AnelPlugin.this);
566+
// }
567+
// }));
586568
}
587569

588570
@Override

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

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void parsePacket(final byte[] message, int length, int receive_port, Inet
6060

6161
Logging.getInstance().logDetect("UDP Device detected\n" + DeviceName);
6262

63+
// Create or get the credentials (device) object. It consists of a unique id, device name, username, password.
6364
DataService dataService = anelPlugin.getDataService();
6465
Credentials credentials = dataService.credentials.findByUID(MacAddress);
6566
if (credentials == null) {
@@ -70,9 +71,6 @@ public void parsePacket(final byte[] message, int length, int receive_port, Inet
7071
// We will filter out any disabled outlets
7172
int disabledOutlets = 0;
7273
int numOutlets = 8;
73-
74-
credentials.setDeviceName(DeviceName);
75-
7674
int httpPort = 80;
7775

7876
// For old firmwares
@@ -97,8 +95,47 @@ public void parsePacket(final byte[] message, int length, int receive_port, Inet
9795
if (msg.length > 25)
9896
credentials.version = msg[25].trim();
9997

98+
// Only update device name if it is empty, we do not want to overwrite the name given by the user.
99+
if (credentials.deviceName.isEmpty())
100+
credentials.setDeviceName(DeviceName);
101+
102+
// The order is important: First save the credentials, then save the connections, then save the executables.
103+
// Because: connections need the credentials object. Executables need the reachability information of the connections.
104+
100105
dataService.credentials.put(credentials);
101106

107+
// IO Connections
108+
DeviceIOConnections deviceIOConnections = dataService.connections.openCreateDevice(MacAddress);
109+
110+
{
111+
String ioConnection_uid = MacAddress + "UDP";
112+
IOConnectionUDP ioConnection = (IOConnectionUDP) deviceIOConnections.findByUID(ioConnection_uid);
113+
if (ioConnection == null)
114+
ioConnection = new IOConnectionUDP(credentials);
115+
ioConnection.setReceiveAddress(peer);
116+
ioConnection.connectionUID = ioConnection_uid;
117+
ioConnection.hostName = HostName;
118+
ioConnection.PortUDPSend = SharedPrefs.getInstance().getDefaultSendPort();
119+
ioConnection.PortUDPReceive = receive_port;
120+
ioConnection.setStatusMessage(null);
121+
ioConnection.incReceivedPackets();
122+
dataService.connections.put(ioConnection);
123+
}
124+
125+
{
126+
String ioConnection_uid = MacAddress + "HTTP";
127+
IOConnectionHTTP ioConnection = (IOConnectionHTTP) deviceIOConnections.findByUID(ioConnection_uid);
128+
if (ioConnection == null)
129+
ioConnection = new IOConnectionHTTP(credentials);
130+
ioConnection.setReceiveAddress(peer);
131+
ioConnection.connectionUID = ioConnection_uid;
132+
ioConnection.hostName = HostName;
133+
ioConnection.PortHttp = httpPort;
134+
ioConnection.setStatusMessage(null);
135+
dataService.connections.put(ioConnection);
136+
}
137+
138+
102139
// IO ports
103140
if (msg.length > 25) {
104141
// 1-based id. IO output range: 11..19, IO input range is 21..29
@@ -119,6 +156,7 @@ public void parsePacket(final byte[] message, int length, int receive_port, Inet
119156
}
120157
}
121158

159+
// Executables
122160
for (int i = 0; i < numOutlets; i++) {
123161
String outlet[] = msg[6 + i].split(",");
124162
if (outlet.length < 1)
@@ -136,33 +174,5 @@ public void parsePacket(final byte[] message, int length, int receive_port, Inet
136174
dataService.executables.put(executable);
137175
}
138176

139-
DeviceIOConnections deviceIOConnections = dataService.connections.openCreateDevice(MacAddress);
140-
141-
{
142-
String ioConnection_uid = MacAddress + "UDP";
143-
IOConnectionUDP ioConnection = (IOConnectionUDP) deviceIOConnections.findByUID(ioConnection_uid);
144-
if (ioConnection == null)
145-
ioConnection = new IOConnectionUDP(credentials);
146-
ioConnection.setReceiveAddress(peer);
147-
ioConnection.connectionUID = ioConnection_uid;
148-
ioConnection.hostName = HostName;
149-
ioConnection.PortUDPSend = SharedPrefs.getInstance().getDefaultSendPort();
150-
ioConnection.PortUDPReceive = receive_port;
151-
ioConnection.setStatusMessage(null);
152-
ioConnection.incReceivedPackets();
153-
dataService.connections.put(ioConnection);
154-
}
155-
156-
{
157-
String ioConnection_uid = MacAddress + "HTTP";
158-
IOConnectionHTTP ioConnection = (IOConnectionHTTP) deviceIOConnections.findByUID(ioConnection_uid);
159-
if (ioConnection == null)
160-
ioConnection = new IOConnectionHTTP(credentials);
161-
ioConnection.setReceiveAddress(peer);
162-
ioConnection.connectionUID = ioConnection_uid;
163-
ioConnection.hostName = HostName;
164-
ioConnection.PortHttp = httpPort;
165-
dataService.connections.put(ioConnection);
166-
}
167177
}
168178
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.util.Log;
44

5-
import java.net.DatagramSocket;
65
import java.net.InetAddress;
76
import java.net.InterfaceAddress;
87
import java.net.NetworkInterface;
@@ -11,8 +10,8 @@
1110
import java.util.Set;
1211

1312
import oly.netpowerctrl.data.DataService;
14-
import oly.netpowerctrl.main.App;
1513
import oly.netpowerctrl.network.UDPErrors;
14+
import oly.netpowerctrl.network.UDPSend;
1615
import oly.netpowerctrl.utils.Logging;
1716

1817
/**
@@ -22,14 +21,14 @@ public class AnelSendUDPBroadcastJob {
2221
public static void run(DataService dataService) {
2322
Set<Integer> ports = dataService.connections.getAllSendPorts();
2423

25-
DatagramSocket datagramSocket;
26-
try {
27-
datagramSocket = new DatagramSocket();
28-
datagramSocket.setBroadcast(true);
29-
} catch (SocketException e) {
30-
e.printStackTrace();
31-
return;
32-
}
24+
// DatagramSocket datagramSocket;
25+
// try {
26+
// datagramSocket = new DatagramSocket();
27+
// datagramSocket.setBroadcast(true);
28+
// } catch (SocketException e) {
29+
// e.printStackTrace();
30+
// return;
31+
// }
3332

3433
boolean logDetect = Logging.getInstance().mLogDetect;
3534

@@ -55,11 +54,12 @@ public static void run(DataService dataService) {
5554
Logging.getInstance().logDetect("UDP Broadcast on " + broadcast.toString() + " Ports: " + portsString);
5655
}
5756

57+
Log.w("SendUDPBroadcastJob", "Broadcast Query");
58+
5859
//String portString = "";
5960
for (int port : ports) {
61+
new UDPSend(broadcast, port, "wer da?\r\n".getBytes(), UDPErrors.INQUERY_BROADCAST_REQUEST);
6062
//portString += " " + String.valueOf(port);
61-
UDPErrors.sendPacketHandleErrors(App.instance,
62-
datagramSocket, broadcast, port, "wer da?\r\n".getBytes());
6363
}
6464

6565
//Log.w("AnelSendUDPBroadcastJob", "Query " + portString);

app/src/main/java/oly/netpowerctrl/data/AbstractBasePlugin.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,5 @@ public DataService getDataService() {
101101
*/
102102
abstract public void addNewIOConnection(@NonNull Credentials credentials, @NonNull onNewIOConnection callback);
103103

104-
public abstract long getUpdatedTime(Executable executable);
105-
106-
public abstract String getDeviceName(Executable executable);
107-
108104
public abstract ReachabilityStates getReachableState(Executable executable);
109105
}

app/src/main/java/oly/netpowerctrl/data/DataService.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public boolean onPluginsReady(DataService dataService, List<AbstractBasePlugin>
351351
}
352352

353353
observersServiceReady.onServiceReady(dataService);
354-
refreshDevices();
354+
refreshExistingDevices();
355355
return true;
356356
}
357357

@@ -413,8 +413,8 @@ public void clear() {
413413
*/
414414
public void addToConfiguredDevices(Credentials credentials) {
415415
credentials.setConfigured(true);
416-
connections.save(credentials.deviceUID);
417416
this.credentials.put(credentials);
417+
connections.save(credentials.deviceUID);
418418
}
419419

420420
public void showNotificationForNextRefresh(boolean notificationAfterNextRefresh) {
@@ -431,10 +431,26 @@ public boolean onDataQueryFinished(DataService dataService) {
431431
return true;
432432
}
433433

434+
/**
435+
* Refreshes all configured devices.
436+
*/
437+
public void refreshExistingDevices() {
438+
observersStartStopRefresh.onRefreshStateChanged(true);
439+
addDeviceObserver(new DevicesObserver(credentials.getItems().values(), new DevicesObserver.onDevicesObserverFinished() {
440+
@Override
441+
public void onObserverJobFinished(DevicesObserver devicesObserver) {
442+
//timers.checkAlarm(DataService.getService().alarmStartedTime());
443+
Logging.getInstance().logEnergy("...fertig\n" + " Timeout: " + String.valueOf(devicesObserver.timedOutDevices().size()));
444+
observersDataQueryCompleted.onDataQueryFinished(DataService.this);
445+
observersStartStopRefresh.onRefreshStateChanged(false);
446+
}
447+
}));
448+
}
449+
434450
/**
435451
* Refreshes all configured devices (detect unreachable devices) and detect new un-configured devices.
436452
*/
437-
public void refreshDevices() {
453+
public void detectDevices() {
438454
observersStartStopRefresh.onRefreshStateChanged(true);
439455
addDeviceObserver(new DevicesObserver(new DevicesObserver.onDevicesObserverFinished() {
440456
@Override

0 commit comments

Comments
 (0)