diff --git a/pom.xml b/pom.xml index d4fbbffc..ef58bcab 100755 --- a/pom.xml +++ b/pom.xml @@ -69,7 +69,7 @@ io.github.soniccloudorg sonic-driver-core - 1.1.16 + 1.1.17 diff --git a/src/main/java/org/cloud/sonic/agent/tools/PHCTool.java b/src/main/java/org/cloud/sonic/agent/tools/PHCTool.java new file mode 100755 index 00000000..cce95ac3 --- /dev/null +++ b/src/main/java/org/cloud/sonic/agent/tools/PHCTool.java @@ -0,0 +1,48 @@ +package org.cloud.sonic.agent.tools; + +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +@Component +@Slf4j +public class PHCTool { + private static String baseUrl = "http://127.0.0.1:7531"; + private static RestTemplate restTemplate; + + @Autowired + public void setRestTemplate(RestTemplate restTemplate) { + PHCTool.restTemplate = restTemplate; + } + + public static void setPosition(int position, String type) { + if (!isSupport()) return; + log.info("set hub position: {} {}", position, type); + JSONObject re = new JSONObject(); + re.put("position", position); + re.put("type", type); + restTemplate.postForEntity(baseUrl + "/control", re, String.class); + } + + public static boolean isSupport() { + try { + ResponseEntity responseEntity = + restTemplate.getForEntity(baseUrl + "/ping", String.class); + if (responseEntity.getStatusCode() == HttpStatus.OK) { + if ("pong".equals(responseEntity.getBody())) { + log.info("hub is ready."); + return true; + } + } + log.info("hub is not ready."); + return false; + }catch (Exception e){ + log.info("hub is not ready. ignore..."); + return false; + } + } +} diff --git a/src/main/java/org/cloud/sonic/agent/transport/TransportClient.java b/src/main/java/org/cloud/sonic/agent/transport/TransportClient.java index dac13473..186d0774 100755 --- a/src/main/java/org/cloud/sonic/agent/transport/TransportClient.java +++ b/src/main/java/org/cloud/sonic/agent/transport/TransportClient.java @@ -44,6 +44,7 @@ import org.cloud.sonic.agent.tests.ios.IOSTestTaskBootThread; import org.cloud.sonic.agent.tools.AgentManagerTool; import org.cloud.sonic.agent.tools.BytesTool; +import org.cloud.sonic.agent.tools.PHCTool; import org.cloud.sonic.agent.tools.SpringTool; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; @@ -99,6 +100,7 @@ public void onMessage(String s) { agentInfo.put("version", "v" + version); agentInfo.put("systemType", System.getProperty("os.name")); agentInfo.put("host", host); + agentInfo.put("hasHub", PHCTool.isSupport() ? 1 : 0); TransportWorker.client.send(agentInfo.toJSONString()); if (isEnableAndroid) { IDevice[] iDevices = AndroidDeviceBridgeTool.getRealOnLineDevices(); @@ -151,6 +153,9 @@ public void onMessage(String s) { heartBeat.put("status", "alive"); TransportWorker.send(heartBeat); break; + case "hub": + PHCTool.setPosition(jsonObject.getInteger("position"), jsonObject.getString("type")); + break; case "runStep": if (jsonObject.getInteger("pf") == PlatformType.ANDROID) { runAndroidStep(jsonObject); diff --git a/src/test/java/org/cloud/sonic/agent/android/MockStepDebugTest.java b/src/test/java/org/cloud/sonic/agent/android/MockStepDebugTest.java old mode 100644 new mode 100755 diff --git a/src/test/java/org/cloud/sonic/agent/tools/PHCToolTest.java b/src/test/java/org/cloud/sonic/agent/tools/PHCToolTest.java new file mode 100755 index 00000000..9ddd1bdd --- /dev/null +++ b/src/test/java/org/cloud/sonic/agent/tools/PHCToolTest.java @@ -0,0 +1,17 @@ +package org.cloud.sonic.agent.tools; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class PHCToolTest { + + @Test + public void test() { + PHCTool.setPosition(2, "up"); + } + +}