Skip to content

Commit 5691b55

Browse files
committed
feat: added dump command to shelly.java
1 parent 5f9de78 commit 5691b55

File tree

1 file changed

+84
-44
lines changed

1 file changed

+84
-44
lines changed

experimental/shelly.java

+84-44
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@
3232
import kong.unirest.json.JSONObject;
3333

3434
public class shelly {
35-
private static class ListCmd implements Function<List<String>, Integer> {
35+
private abstract static class BaseListCmd implements Function<List<String>, Integer> {
3636
private static int timeout = 10;
3737

38-
private static Map<String, JSONObject> result;
39-
40-
private static class ShellyServiceListener implements ServiceListener {
38+
private class ShellyServiceListener implements ServiceListener {
4139
@Override
4240
public void serviceAdded(ServiceEvent event) {
4341
}
@@ -54,7 +52,7 @@ public void serviceResolved(ServiceEvent event) {
5452
}
5553
}
5654

57-
public static void handleShelly(ServiceInfo info) {
55+
public void handleShelly(ServiceInfo info) {
5856
if (info.getHostAddresses() != null && info.getHostAddresses().length > 0) {
5957
String gen = info.getPropertyString("gen");
6058
if (gen == null) {
@@ -69,17 +67,57 @@ public static void handleShelly(ServiceInfo info) {
6967
}
7068
}
7169

72-
public static void requestInfoGen1(ServiceInfo info) {
70+
public void requestInfoGen1(ServiceInfo info) {
7371
String url = "http://" + info.getHostAddresses()[0] + "/settings";
74-
request(url, obj -> printShellyGen1(info, obj));
72+
request(url, obj -> handleShellyGen1Info(info, obj));
7573
}
7674

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));
8078
}
8179

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) {
83121
String type = get(settings, "device").getString("type");
84122
String mode = settings.optString("mode");
85123
String name = settings.optString("name");
@@ -143,7 +181,8 @@ private static synchronized void printShellyGen1(ServiceInfo info, JSONObject se
143181
}
144182
}
145183

146-
private static synchronized void printShellyGen2(ServiceInfo info, JSONObject settings) {
184+
@Override
185+
public synchronized void handleShellyGen2Info(ServiceInfo info, JSONObject settings) {
147186
StringBuilder str = new StringBuilder();
148187
str.append(String.format("%s %s",
149188
info.getName(),
@@ -153,36 +192,7 @@ private static synchronized void printShellyGen2(ServiceInfo info, JSONObject se
153192

154193
@Override
155194
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);
186196
}
187197
}
188198

@@ -243,7 +253,7 @@ protected String url() {
243253

244254
@Override
245255
protected void help() {
246-
System.err.println("Usage: shelly settings <ip>");
256+
System.err.println("Usage: shelly config <ip>");
247257
}
248258
}
249259

@@ -266,6 +276,33 @@ protected void help() {
266276
}
267277
}
268278

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+
269306
private static abstract class ActionCmd implements Function<List<String>, Integer> {
270307

271308
protected abstract String url(String ip, int index);
@@ -386,6 +423,9 @@ public static void main(String... args) throws InterruptedException {
386423
case "config":
387424
System.exit(new ConfigCmd().apply(res.rest()));
388425
break;
426+
case "dump":
427+
System.exit(new DumpCmd().apply(res.rest()));
428+
break;
389429
case "status":
390430
System.exit(new StatusCmd().apply(res.rest()));
391431
break;
@@ -402,7 +442,7 @@ public static void main(String... args) throws InterruptedException {
402442
}
403443

404444
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 ...");
406446
}
407447

408448
private static CompletableFuture<HttpResponse<JsonNode>> request(String url, Consumer<JSONObject> func) {

0 commit comments

Comments
 (0)