Skip to content

Commit

Permalink
feat: ready hub
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouYixun committed Dec 11, 2022
1 parent 6dfcd87 commit a0c1903
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<dependency>
<groupId>io.github.soniccloudorg</groupId>
<artifactId>sonic-driver-core</artifactId>
<version>1.1.16</version>
<version>1.1.17</version>
</dependency>
<!-- 压缩图片 -->
<dependency>
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/cloud/sonic/agent/tools/PHCTool.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Empty file.
17 changes: 17 additions & 0 deletions src/test/java/org/cloud/sonic/agent/tools/PHCToolTest.java
Original file line number Diff line number Diff line change
@@ -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");
}

}

0 comments on commit a0c1903

Please sign in to comment.