32
32
import kong .unirest .json .JSONObject ;
33
33
34
34
public class shelly {
35
- private static class ListCmd implements Function <List <String >, Integer > {
35
+ private abstract static class BaseListCmd implements Function <List <String >, Integer > {
36
36
private static int timeout = 10 ;
37
37
38
- private static Map <String , JSONObject > result ;
39
-
40
- private static class ShellyServiceListener implements ServiceListener {
38
+ private class ShellyServiceListener implements ServiceListener {
41
39
@ Override
42
40
public void serviceAdded (ServiceEvent event ) {
43
41
}
@@ -54,7 +52,7 @@ public void serviceResolved(ServiceEvent event) {
54
52
}
55
53
}
56
54
57
- public static void handleShelly (ServiceInfo info ) {
55
+ public void handleShelly (ServiceInfo info ) {
58
56
if (info .getHostAddresses () != null && info .getHostAddresses ().length > 0 ) {
59
57
String gen = info .getPropertyString ("gen" );
60
58
if (gen == null ) {
@@ -69,17 +67,57 @@ public static void handleShelly(ServiceInfo info) {
69
67
}
70
68
}
71
69
72
- public static void requestInfoGen1 (ServiceInfo info ) {
70
+ public void requestInfoGen1 (ServiceInfo info ) {
73
71
String url = "http://" + info .getHostAddresses ()[0 ] + "/settings" ;
74
- request (url , obj -> printShellyGen1 (info , obj ));
72
+ request (url , obj -> handleShellyGen1Info (info , obj ));
75
73
}
76
74
77
- public static void requestInfoGen2 (ServiceInfo info ) {
78
- String url = "http://" + info .getHostAddresses ()[0 ] + "/rpc/Shelly.GetStatus " ;
79
- request (url , obj -> printShellyGen2 (info , obj ));
75
+ public void requestInfoGen2 (ServiceInfo info ) {
76
+ String url = "http://" + info .getHostAddresses ()[0 ] + "/rpc/Shelly.GetConfig " ;
77
+ request (url , obj -> handleShellyGen2Info (info , obj ));
80
78
}
81
79
82
- private static synchronized void printShellyGen1 (ServiceInfo info , JSONObject settings ) {
80
+ public abstract void handleShellyGen1Info (ServiceInfo info , JSONObject settings );
81
+
82
+ public abstract void handleShellyGen2Info (ServiceInfo info , JSONObject settings );
83
+
84
+ @ Override
85
+ public Integer apply (List <String > args ) {
86
+ Args res = ArgsParser .create ().needsValue ("--timeout" , "-t" ).parse (args );
87
+ for (Option opt : res .options ()) {
88
+ switch (opt .name ()) {
89
+ case "-t" :
90
+ case "--timeout" :
91
+ timeout = Integer .parseInt (opt .values ().get (0 ));
92
+ break ;
93
+ case "-h" :
94
+ case "--help" :
95
+ System .err .println ("Usage: shelly list [-t|--timeout <seconds>]" );
96
+ return 1 ;
97
+ }
98
+ }
99
+
100
+ try (JmDNS jmdns = JmDNS .create (InetAddress .getLocalHost ())) {
101
+ jmdns .addServiceListener ("_http._tcp.local." , new ShellyServiceListener ());
102
+ // Wait for timeout or user interrupt
103
+ if (timeout > 0 ) {
104
+ Thread .sleep (timeout * 1000 );
105
+ } else {
106
+ Thread .currentThread ().join ();
107
+ }
108
+ } catch (IOException e ) {
109
+ System .err .println (e .getMessage ());
110
+ } catch (InterruptedException e ) {
111
+ System .err .println (e .getMessage ());
112
+ }
113
+ Unirest .shutDown ();
114
+ return 0 ;
115
+ }
116
+ }
117
+
118
+ private static class ListCmd extends BaseListCmd {
119
+ @ Override
120
+ public synchronized void handleShellyGen1Info (ServiceInfo info , JSONObject settings ) {
83
121
String type = get (settings , "device" ).getString ("type" );
84
122
String mode = settings .optString ("mode" );
85
123
String name = settings .optString ("name" );
@@ -143,7 +181,8 @@ private static synchronized void printShellyGen1(ServiceInfo info, JSONObject se
143
181
}
144
182
}
145
183
146
- private static synchronized void printShellyGen2 (ServiceInfo info , JSONObject settings ) {
184
+ @ Override
185
+ public synchronized void handleShellyGen2Info (ServiceInfo info , JSONObject settings ) {
147
186
StringBuilder str = new StringBuilder ();
148
187
str .append (String .format ("%s %s" ,
149
188
info .getName (),
@@ -153,36 +192,7 @@ private static synchronized void printShellyGen2(ServiceInfo info, JSONObject se
153
192
154
193
@ Override
155
194
public Integer apply (List <String > args ) {
156
- Args res = ArgsParser .create ().needsValue ("--timeout" , "-t" ).parse (args );
157
- for (Option opt : res .options ()) {
158
- switch (opt .name ()) {
159
- case "-t" :
160
- case "--timeout" :
161
- timeout = Integer .parseInt (opt .values ().get (0 ));
162
- break ;
163
- case "-h" :
164
- case "--help" :
165
- System .err .println ("Usage: shelly list [-t|--timeout <seconds>]" );
166
- return 1 ;
167
- }
168
- }
169
-
170
- result = new ConcurrentHashMap <>();
171
- try (JmDNS jmdns = JmDNS .create (InetAddress .getLocalHost ())) {
172
- jmdns .addServiceListener ("_http._tcp.local." , new ShellyServiceListener ());
173
- // Wait for timeout or user interrupt
174
- if (timeout > 0 ) {
175
- Thread .sleep (timeout * 1000 );
176
- } else {
177
- Thread .currentThread ().join ();
178
- }
179
- } catch (IOException e ) {
180
- System .err .println (e .getMessage ());
181
- } catch (InterruptedException e ) {
182
- System .err .println (e .getMessage ());
183
- }
184
- Unirest .shutDown ();
185
- return 0 ;
195
+ return super .apply (args );
186
196
}
187
197
}
188
198
@@ -243,7 +253,7 @@ protected String url() {
243
253
244
254
@ Override
245
255
protected void help () {
246
- System .err .println ("Usage: shelly settings <ip>" );
256
+ System .err .println ("Usage: shelly config <ip>" );
247
257
}
248
258
}
249
259
@@ -266,6 +276,33 @@ protected void help() {
266
276
}
267
277
}
268
278
279
+ private static class DumpCmd extends BaseListCmd {
280
+ private JSONArray result = new JSONArray ();
281
+
282
+ @ Override
283
+ public synchronized void handleShellyGen1Info (ServiceInfo info , JSONObject settings ) {
284
+ String name = info .getName ();
285
+ System .err .println ("Found Gen 1 device: " + name );
286
+ result .put (settings );
287
+ }
288
+
289
+ @ Override
290
+ public synchronized void handleShellyGen2Info (ServiceInfo info , JSONObject settings ) {
291
+ String name = info .getName ();
292
+ System .err .println ("Found Gen 2 device: " + name );
293
+ result .put (settings );
294
+ }
295
+
296
+ @ Override
297
+ public Integer apply (List <String > args ) {
298
+ int res = super .apply (args );
299
+ if (res == 0 ) {
300
+ System .out .println (result .toString (1 ));
301
+ }
302
+ return res ;
303
+ }
304
+ }
305
+
269
306
private static abstract class ActionCmd implements Function <List <String >, Integer > {
270
307
271
308
protected abstract String url (String ip , int index );
@@ -386,6 +423,9 @@ public static void main(String... args) throws InterruptedException {
386
423
case "config" :
387
424
System .exit (new ConfigCmd ().apply (res .rest ()));
388
425
break ;
426
+ case "dump" :
427
+ System .exit (new DumpCmd ().apply (res .rest ()));
428
+ break ;
389
429
case "status" :
390
430
System .exit (new StatusCmd ().apply (res .rest ()));
391
431
break ;
@@ -402,7 +442,7 @@ public static void main(String... args) throws InterruptedException {
402
442
}
403
443
404
444
private static void help () {
405
- System .err .println ("Usage: shelly list|config|status|switch|light ..." );
445
+ System .err .println ("Usage: shelly list|config|dump| status|switch|light ..." );
406
446
}
407
447
408
448
private static CompletableFuture <HttpResponse <JsonNode >> request (String url , Consumer <JSONObject > func ) {
0 commit comments