1
1
package oly .netpowerctrl .anelservice ;
2
2
3
3
import android .content .Context ;
4
- import android .util .Log ;
5
4
6
5
import java .io .IOException ;
7
6
import java .net .DatagramPacket ;
8
7
import java .net .DatagramSocket ;
9
8
import java .net .InetAddress ;
10
- import java .net . UnknownHostException ;
9
+ import java .util . Collection ;
11
10
import java .util .Locale ;
12
- import java .util .Map ;
13
- import java .util .TreeMap ;
14
11
15
12
import oly .netpowerctrl .R ;
13
+ import oly .netpowerctrl .datastructure .DeviceCommand ;
16
14
import oly .netpowerctrl .datastructure .DeviceInfo ;
17
- import oly .netpowerctrl .datastructure .OutletCommand ;
18
- import oly .netpowerctrl .datastructure .OutletCommandGroup ;
19
15
import oly .netpowerctrl .utils .ShowToast ;
20
16
21
17
public class DeviceSend {
22
- /**
23
- * This class extracts the destination ip, user access data
24
- * and provides outlet manipulation methods (on/off/toggle).
25
- * The resulting data byte variable can be used to send a
26
- * bulk-change udp packet.
27
- */
28
- static final public class DeviceSwitch {
29
- // build bulk change byte, see: www.anel-elektronik.de/forum_neu/viewtopic.php?f=16&t=207
30
- // “Sw” + Steckdosen + User + Passwort
31
- // Steckdosen = Zustand aller Steckdosen binär
32
- // LSB = Steckdose 1, MSB (Bit 8)= Steckdose 8 (PRO, POWER), Bit 2 = Steckdose 3 (HOME).
33
- // Soll nur 1 & 5 eingeschaltet werden=00010001 = 17 = 0x11 (HEX)
34
- private byte data ;
35
- InetAddress dest ;
36
- int port ;
37
- String access ;
38
-
39
- public DeviceSwitch (DeviceInfo di ) {
40
- this .data = 0 ;
41
- this .port = di .SendPort ;
42
- try {
43
- this .dest = InetAddress .getByName (di .HostName );
44
- } catch (UnknownHostException e ) {
45
- this .dest = null ;
46
- }
47
- this .access = di .UserName + di .Password ;
48
- for (int i = 0 ; i < di .Outlets .size (); ++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 );
53
- }
54
-
55
- }
56
-
57
- void switchOn (int outletNumber ) {
58
- data |= ((byte ) (1 << outletNumber - 1 ));
59
- }
60
-
61
- void switchOff (int outletNumber ) {
62
- data &= ~((byte ) (1 << outletNumber - 1 ));
63
- }
64
-
65
- void toggle (int outletNumber ) {
66
- if ((data & ((byte ) (1 << outletNumber - 1 ))) > 0 ) {
67
- switchOff (outletNumber );
68
- } else {
69
- switchOn (outletNumber );
70
- }
71
- }
72
-
73
- byte getSwitchByte () {
74
- return data ;
75
- }
76
- }
77
-
78
18
/**
79
19
* Bulk version of sendOutlet. Send changes for each device in only one packet per device.
80
20
*
81
21
* @param context The context of the activity for showing toast messages and
82
22
* getResources
83
- * @param og The OutletCommandGroup
84
- * @return Return true if all fields have been found
23
+ * @param device_commands Bulk command per device
85
24
*/
86
- static public void sendOutlet (final Context context , final OutletCommandGroup og ) {
25
+ static public void sendOutlet (final Context context , final Collection < DeviceCommand > device_commands , final boolean requestNewValuesAfterSend ) {
87
26
// udp sending in own thread
88
27
new Thread (new Runnable () {
89
28
public void run () {
90
29
try {
91
30
DatagramSocket s = new DatagramSocket ();
92
31
93
- TreeMap <String , DeviceSwitch > devices = new TreeMap <String , DeviceSwitch >();
94
- for (OutletCommand c : og .commands ) {
95
- if (!devices .containsKey (c .device_mac )) {
96
- devices .put (c .device_mac , new DeviceSwitch (c .outletinfo .device ));
97
- }
98
-
99
- //Log.w("sendOutlet",c.device_mac+" "+ Integer.valueOf(c.outletNumber).toString()+" "+Integer.valueOf(c.state).toString());
100
-
101
- switch (c .state ) {
102
- case 0 :
103
- devices .get (c .device_mac ).switchOn (c .outletNumber );
104
- break ;
105
- case 1 :
106
- devices .get (c .device_mac ).switchOff (c .outletNumber );
107
- break ;
108
- case 2 :
109
- devices .get (c .device_mac ).toggle (c .outletNumber );
110
- break ;
111
- }
112
-
113
- }
114
-
115
- for (Map .Entry <String , DeviceSwitch > c : devices .entrySet ()) {
116
- sendAllOutlets (c .getValue (), s );
32
+ for (DeviceCommand c : device_commands ) {
33
+ sendAllOutlets (c , s );
117
34
}
118
35
s .close ();
119
36
120
- // wait 100ms
121
- try {
122
- Thread .sleep (100 );
123
- } catch (InterruptedException ignored ) {
124
- }
37
+ if (requestNewValuesAfterSend ) {
38
+ // wait 100ms
39
+ try {
40
+ Thread .sleep (100 );
41
+ } catch (InterruptedException ignored ) {
42
+ }
125
43
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 );
44
+ // request new values from each device
45
+
46
+ for (DeviceCommand device_command : device_commands ) {
47
+ DeviceQuery .sendQuery (context , device_command .dest .getHostAddress (), device_command .port );
48
+ }
129
49
}
130
50
131
51
} catch (final IOException e ) {
@@ -142,14 +62,25 @@ public void run() {
142
62
* @param context The context
143
63
* @param device The device and state
144
64
*/
145
- static public void sendAllOutlets (final Context context , final DeviceSwitch device ) {
65
+ static public void sendAllOutlets (final Context context , final DeviceCommand device , final boolean requestNewValuesAfterSend ) {
146
66
// udp sending in own thread
147
67
new Thread (new Runnable () {
148
68
public void run () {
149
69
try {
150
70
DatagramSocket s = new DatagramSocket ();
151
71
sendAllOutlets (device , s );
152
72
s .close ();
73
+
74
+ if (requestNewValuesAfterSend ) {
75
+ // wait 100ms
76
+ try {
77
+ Thread .sleep (100 );
78
+ } catch (InterruptedException ignored ) {
79
+ }
80
+
81
+ // request new values
82
+ DeviceQuery .sendQuery (context , device .dest .getHostAddress (), device .port );
83
+ }
153
84
} catch (final IOException e ) {
154
85
ShowToast .FromOtherThread (context , context .getResources ().getString (R .string .error_sending_inquiry ) + ": "
155
86
+ e .getMessage ());
@@ -158,7 +89,7 @@ public void run() {
158
89
}).start ();
159
90
}
160
91
161
- static private void sendAllOutlets (final DeviceSwitch device , DatagramSocket s ) throws IOException {
92
+ static private void sendAllOutlets (final DeviceCommand device , DatagramSocket s ) throws IOException {
162
93
if (device .dest != null ) {
163
94
byte [] data = new byte [3 + device .access .length ()];
164
95
data [0 ] = 'S' ;
0 commit comments