Skip to content

Commit

Permalink
Now retrieving /zones via HTTP (~100ms faster) (#178, #324)
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed Sep 4, 2024
1 parent c502911 commit d6934ce
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class HccCLI implements CommandLineRunner {
private static final String COMMAND_MDNS_SCAN = "mdns-scan";
private static final String COMMAND_GET_META = "get-meta";
private static final String COMMAND_GET_ZONES = "get-zones";
private static final String COMMAND_GET_ZONES_HTTP = "get-zones-http";
private static final String COMMAND_GET_ZONES_RSOCKET = "get-zones-rsocket";

private final ObjectMapper objectMapper = new ObjectMapper();
private final HttpClient httpClient = new HttpClient();
Expand All @@ -40,13 +42,17 @@ public class CommandGetMeta extends CommandBase {

}

public class CommandGetZonesHTTP extends CommandBase {

}

public abstract class CommandRSocket extends CommandBase {

@Parameter(names = { "--serialization" }, description = "Serialization method")
String serialization = "JSON";
}

public class CommandGetZones extends CommandRSocket {
public class CommandGetZonesRSocket extends CommandRSocket {

}

Expand Down Expand Up @@ -74,13 +80,15 @@ public void run(String... args) throws Exception {

var commandMdnsScan = new CommandMdnsScan();
var commandGetMeta = new CommandGetMeta();
var commandGetZones = new CommandGetZones();
var commandGetZonesHTTP = new CommandGetZonesHTTP();
var commandGetZonesRSocket = new CommandGetZonesRSocket();

var jc = JCommander
.newBuilder()
.addCommand(COMMAND_MDNS_SCAN, commandMdnsScan)
.addCommand(COMMAND_GET_META, commandGetMeta)
.addCommand(COMMAND_GET_ZONES, commandGetZones)
.addCommand(COMMAND_GET_ZONES, commandGetZonesHTTP)
.addCommand(COMMAND_GET_ZONES_RSOCKET, commandGetZonesRSocket)
.build();

try {
Expand All @@ -95,8 +103,9 @@ public void run(String... args) throws Exception {

case COMMAND_MDNS_SCAN -> mdnsScan();
case COMMAND_GET_META -> getMeta(commandGetMeta.url);
case COMMAND_GET_ZONES -> getZones(commandGetZones.url, commandGetZones.serialization);

case COMMAND_GET_ZONES -> getZonesHTTP(commandGetZonesHTTP.url);
case COMMAND_GET_ZONES_HTTP -> getZonesHTTP(commandGetZonesHTTP.url);
case COMMAND_GET_ZONES_RSOCKET -> getZonesRSocket(commandGetZonesRSocket.url, commandGetZonesRSocket.serialization);
}

} catch (ParameterException ex) {
Expand Down Expand Up @@ -131,8 +140,24 @@ private void getMeta(String url) throws IOException {
}
}

private void getZones(String url, String serialization) throws IOException {
ThreadContext.push("getZones");
private void getZonesHTTP(String url) throws IOException {
ThreadContext.push("getZonesHTTP");
try {
logger.info("url={}", url);

var httpUrl = new URL(url);
// First need to get this to determine the host and port to connect RSocket to
var zoneMap = httpClient.getZones(httpUrl);

logger.info("ZONES:\n{}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(zoneMap));

} finally {
ThreadContext.pop();
}
}

private void getZonesRSocket(String url, String serialization) throws IOException {
ThreadContext.push("getZonesRSocket");
try {
logger.info("url={}", url);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.homeclimatecontrol.hcc.meta.EndpointMeta;
import net.sf.dz3r.instrumentation.Marker;
import net.sf.dz3r.signal.hvac.ZoneStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
Expand All @@ -12,6 +13,7 @@

import java.io.IOException;
import java.net.URL;
import java.util.Map;

/**
* HCC remote client using HTTP protocol.
Expand Down Expand Up @@ -58,7 +60,18 @@ private org.apache.http.client.HttpClient createClient() {

public EndpointMeta getMeta(URL targetUrl) throws IOException {

var m = new Marker("getMeta");
return objectMapper.readValue(get(targetUrl, "getMeta"), EndpointMeta.class);
}

public Map<String, ZoneStatus> getZones(URL targetUrl) throws IOException {

// VT: FIXME: This returns a map of maps :O Will deal with this in a short bit.
return objectMapper.readValue(get(targetUrl, "getZones"), Map.class);
}

private String get(URL targetUrl, String marker) throws IOException {

var m = new Marker(marker);
var get = new HttpGet(targetUrl.toString());

try {
Expand All @@ -76,8 +89,9 @@ public EndpointMeta getMeta(URL targetUrl) throws IOException {

var response = EntityUtils.toString(rsp.getEntity());

logger.trace("META/raw: {}", response);
return objectMapper.readValue(response, EndpointMeta.class);
logger.trace("{}/raw: {}", marker, response);

return response;

} finally {
get.releaseConnection();
Expand Down

0 comments on commit d6934ce

Please sign in to comment.