Browse Source

完善device service实现

master
白凤吉 6 months ago
parent
commit
7e0a34b019
  1. 15
      src/main/java/com/qyft/gd/device/client/TcpClient.java
  2. 4
      src/main/java/com/qyft/gd/device/common/jsonrpc/JsonRpcRequest.java
  3. 153
      src/main/java/com/qyft/gd/device/service/DeviceService.java

15
src/main/java/com/qyft/gd/device/client/TcpClient.java

@ -19,6 +19,7 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@ -98,6 +99,20 @@ public class TcpClient {
}
public DeviceFeedback sendCommand(String method) {
JsonRpcRequest request = new JsonRpcRequest();
request.setMethod(method);
return this.sendCommand(request);
}
public DeviceFeedback sendCommand(String method, Map<String, Object> params) {
JsonRpcRequest request = new JsonRpcRequest();
request.setMethod(method);
request.setParams(params);
return this.sendCommand(request);
}
public DeviceFeedback sendCommand(JsonRpcRequest request) {
if (request.getId() == null) {
request.setId(UUID.randomUUID().toString());

4
src/main/java/com/qyft/gd/device/common/jsonrpc/JsonRpcRequest.java

@ -2,7 +2,7 @@ package com.qyft.gd.device.common.jsonrpc;
import lombok.Data;
import java.util.List;
import java.util.Map;
/**
* TCP JSON RPC请求
@ -20,5 +20,5 @@ public class JsonRpcRequest {
/**
* 请求参数
*/
private List<String> params;
private Map<String, Object> params;
}

153
src/main/java/com/qyft/gd/device/service/DeviceService.java

@ -2,14 +2,13 @@ package com.qyft.gd.device.service;
import cn.hutool.json.JSONUtil;
import com.qyft.gd.device.client.TcpClient;
import com.qyft.gd.device.common.jsonrpc.JsonRpcRequest;
import com.qyft.gd.device.model.bo.DeviceFeedback;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
@ -24,6 +23,13 @@ public class DeviceService {
* @param distance 移动距离
*/
public boolean moveRailArmRail(double distance) {
Map<String, Object> params = new HashMap<>();
params.put("distance", distance);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmRail", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP moveRailArmRail 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -35,6 +41,15 @@ public class DeviceService {
* @param distance 移动距离
*/
public boolean moveRailArmJoint(double joint1, double joint2, double distance) {
Map<String, Object> params = new HashMap<>();
params.put("joint1", joint1);
params.put("joint2", joint2);
params.put("distance", distance);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmJoint", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP moveRailArmJoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -46,6 +61,15 @@ public class DeviceService {
* @param z 坐标z
*/
public boolean moveRailArmToPoint(int x, int y, int z) {
Map<String, Object> params = new HashMap<>();
params.put("x", x);
params.put("y", y);
params.put("z", z);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmToPoint", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP moveRailArmToPoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -55,6 +79,13 @@ public class DeviceService {
* @param speed 速度值
*/
public boolean setRailArmSpeed(int speed) {
Map<String, Object> params = new HashMap<>();
params.put("speed", speed);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("setRailArmSpeed", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP setRailArmSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -62,16 +93,9 @@ public class DeviceService {
* 开门
*/
public boolean openDoor() {
JsonRpcRequest request = new JsonRpcRequest();
request.setMethod("openDoor");
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
request.setParams(list);
DeviceFeedback deviceFeedback = tcpClient.sendCommand(request);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("openDoor");
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.info("TCP openDoor指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
log.error("TCP openDoor 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
@ -81,6 +105,35 @@ public class DeviceService {
* 关门
*/
public boolean closeDoor() {
DeviceFeedback deviceFeedback = tcpClient.sendCommand("closeDoor");
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP closeDoor 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
/**
* 张开夹爪
*/
public boolean openClaw() {
DeviceFeedback deviceFeedback = tcpClient.sendCommand("openClaw");
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP openClaw 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
/**
* 收合夹爪
*/
public boolean closeClaw() {
DeviceFeedback deviceFeedback = tcpClient.sendCommand("closeClaw");
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP closeClaw 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -91,6 +144,14 @@ public class DeviceService {
* @param joint2 关节2角度
*/
public boolean moveLiquidArmJoint(double joint1, double joint2) {
Map<String, Object> params = new HashMap<>();
params.put("joint1", joint1);
params.put("joint2", joint2);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveLiquidArmJoint", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP moveLiquidArmJoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -102,6 +163,15 @@ public class DeviceService {
* @param z 坐标z
*/
public boolean moveLiquidArmToPoint(int x, int y, int z) {
Map<String, Object> params = new HashMap<>();
params.put("x", x);
params.put("y", y);
params.put("z", z);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveLiquidArmToPoint", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP moveLiquidArmToPoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -111,6 +181,13 @@ public class DeviceService {
* @param speed 速度值
*/
public boolean setLiquidArmSpeed(int speed) {
Map<String, Object> params = new HashMap<>();
params.put("speed", speed);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("setLiquidArmSpeed", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP setLiquidArmSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -121,6 +198,14 @@ public class DeviceService {
* @param volume 液体体积
*/
public boolean addLiquid(int pumpId, int volume) {
Map<String, Object> params = new HashMap<>();
params.put("pumpId", pumpId);
params.put("volume", volume);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("addLiquid", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP addLiquid 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -132,6 +217,14 @@ public class DeviceService {
* @param flowRate 流量值
*/
public boolean setFlowRate(int pumpId, int flowRate) {
Map<String, Object> params = new HashMap<>();
params.put("pumpId", pumpId);
params.put("flowRate", flowRate);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("setFlowRate", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP setFlowRate 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -139,6 +232,11 @@ public class DeviceService {
* 开始摇匀
*/
public boolean startShaking() {
DeviceFeedback deviceFeedback = tcpClient.sendCommand("startShaking");
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP startShaking 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -146,6 +244,11 @@ public class DeviceService {
* 停止摇匀
*/
public boolean stopShaking() {
DeviceFeedback deviceFeedback = tcpClient.sendCommand("stopShaking");
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP stopShaking 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -155,6 +258,13 @@ public class DeviceService {
* @param speed 速度值
*/
public boolean setShakingSpeed(int speed) {
Map<String, Object> params = new HashMap<>();
params.put("speed", speed);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("setShakingSpeed", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP setShakingSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -164,6 +274,13 @@ public class DeviceService {
* @param distance 距离
*/
public boolean moveTrayToHeight(double distance) {
Map<String, Object> params = new HashMap<>();
params.put("distance", distance);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveTrayToHeight", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP moveTrayToHeight 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -173,6 +290,13 @@ public class DeviceService {
* @param speed 速度值
*/
public boolean setTraySpeed(int speed) {
Map<String, Object> params = new HashMap<>();
params.put("speed", speed);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("setTraySpeed", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP setTraySpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
@ -180,6 +304,11 @@ public class DeviceService {
* 拍照
*/
public boolean takePhoto() {
DeviceFeedback deviceFeedback = tcpClient.sendCommand("takePhoto");
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP takePhoto 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
}
}
Loading…
Cancel
Save