Merge branch 'develop' of https://gitee.com/L-Shilin/go-fly-platform-services into feature_dzc
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
# 下发设备指令接口
|
||||
|
||||
## 功能描述
|
||||
|
||||
新增一个下发设备指令的接口,用于通过设备ID直接下发指令到设备,无需依赖订单号。该接口会自动生成随机订单号,并根据设备ID和二维码类型获取对应的航线信息。
|
||||
|
||||
## 接口信息
|
||||
|
||||
### 基本信息
|
||||
- **接口路径**: `POST /command/{deviceId}`
|
||||
- **接口名称**: 下发设备指令
|
||||
- **所属模块**: 设备管理
|
||||
|
||||
### 请求参数
|
||||
|
||||
| 参数名 | 类型 | 必填 | 默认值 | 说明 |
|
||||
|--------|------|------|--------|------|
|
||||
| deviceId | String | 是 | - | 设备ID |
|
||||
| qrcodeType | Integer | 否 | 2 | 二维码类型(1:游客下单,2:定时巡查,3:其他) |
|
||||
|
||||
### 响应格式
|
||||
|
||||
```json
|
||||
{
|
||||
"resp_code": 0,
|
||||
"resp_msg": "设置成功",
|
||||
"data": {
|
||||
// 响应数据
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 功能特点
|
||||
|
||||
### 1. 自动生成订单号
|
||||
- 生成格式:`CMD_{时间戳}_{随机数}`
|
||||
- 示例:`CMD_1703123456789_123`
|
||||
|
||||
### 2. 智能获取航线信息
|
||||
- 根据设备ID和二维码类型查询对应的二维码配置
|
||||
- 自动获取航线文件路径和航线ID
|
||||
- 获取航线文件签名信息
|
||||
|
||||
### 3. 指令参数设置
|
||||
- 使用默认的 `camera_photo_take` 指令
|
||||
- 自动设置订单号、航线信息、视频流地址
|
||||
- **新增 `takeOffOneKey` 字段,值为 `true`**
|
||||
|
||||
### 4. 错误处理
|
||||
- 二维码配置不存在时返回错误信息
|
||||
- 设备离线时自动创建退款订单
|
||||
- 完整的异常处理和日志记录
|
||||
|
||||
## 实现流程
|
||||
|
||||
### 1. 参数验证
|
||||
```java
|
||||
// 生成随机订单号
|
||||
String orderNo = generateRandomOrderNo();
|
||||
|
||||
// 根据设备ID和二维码类型获取二维码信息
|
||||
Map<String, Object> qrcodeResult = orderCenterService.getQrcodeByDeviceId(deviceId, qrcodeType);
|
||||
```
|
||||
|
||||
### 2. 构建指令消息
|
||||
```java
|
||||
// 构建控制消息
|
||||
JsonObject controlMessage = buildCommandMessage(orderNo, deviceId, qrcodeResult);
|
||||
|
||||
// 设置takeOffOneKey字段
|
||||
data.addProperty("takeOffOneKey", true);
|
||||
```
|
||||
|
||||
### 3. 发送设备请求
|
||||
```java
|
||||
// 发送设备控制请求
|
||||
String url = baseUrl + "/device/instance/" + deviceId + "/property";
|
||||
Map<String, String> result = requestUtils.getPostRequestResultMap(url, controlMessage.toString());
|
||||
```
|
||||
|
||||
### 4. 处理响应
|
||||
```java
|
||||
// 处理响应结果
|
||||
return handleDeviceResponse(result, orderNo);
|
||||
```
|
||||
|
||||
## 数据库支持
|
||||
|
||||
### 新增查询方法
|
||||
|
||||
在 `QrCodeMapper.xml` 中添加了根据设备ID和二维码类型查询的方法:
|
||||
|
||||
```xml
|
||||
<select id="getByDeviceIdAndType" resultMap="QrCodeResultMap">
|
||||
SELECT * FROM qrcodes
|
||||
WHERE device_sn = #{deviceId}
|
||||
AND qrcode_type = #{qrcodeType}
|
||||
AND is_del = 0
|
||||
ORDER BY create_time DESC
|
||||
LIMIT 1
|
||||
</select>
|
||||
```
|
||||
|
||||
### 服务层支持
|
||||
|
||||
- `IQrCodeService.getByDeviceIdAndType(String deviceId, Integer qrcodeType)`
|
||||
- `QrCodeServiceImpl.getByDeviceIdAndType(String deviceId, Integer qrcodeType)`
|
||||
|
||||
## 使用场景
|
||||
|
||||
### 1. 定时巡查(默认)
|
||||
- 二维码类型:2
|
||||
- 用于系统定时任务触发的设备巡查
|
||||
- 自动生成订单号,无需用户交互
|
||||
|
||||
### 2. 游客下单
|
||||
- 二维码类型:1
|
||||
- 用于普通游客扫码下单场景
|
||||
- 需要支付和用户交互
|
||||
|
||||
### 3. 其他用途
|
||||
- 二维码类型:3
|
||||
- 特殊业务场景
|
||||
- 可根据具体需求扩展
|
||||
|
||||
## 调用示例
|
||||
|
||||
### cURL 示例
|
||||
|
||||
```bash
|
||||
# 使用默认二维码类型(定时巡查)
|
||||
curl -X POST "http://localhost:8080/command/D-Dock2-123456" \
|
||||
-H "Content-Type: application/json"
|
||||
|
||||
# 指定二维码类型
|
||||
curl -X POST "http://localhost:8080/command/D-Dock2-123456?qrcodeType=2" \
|
||||
-H "Content-Type: application/json"
|
||||
```
|
||||
|
||||
### 响应示例
|
||||
|
||||
```json
|
||||
{
|
||||
"resp_code": 0,
|
||||
"resp_msg": "设置成功",
|
||||
"data": {
|
||||
"orderNo": "CMD_1703123456789_123",
|
||||
"deviceId": "D-Dock2-123456",
|
||||
"status": "processing"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **二维码配置**:确保设备ID对应的二维码配置存在且正确
|
||||
2. **航线文件**:确保航线文件已上传且签名正确
|
||||
3. **设备状态**:设备必须在线才能正常执行指令
|
||||
4. **权限控制**:接口可能需要相应的权限验证
|
||||
5. **日志记录**:所有操作都会记录详细日志,便于问题排查
|
||||
|
||||
## 相关文件
|
||||
|
||||
- 控制器:`zlt-business/device-center/src/main/java/com/central/device/controller/ApiDeviceController.java`
|
||||
- 服务接口:`zlt-business/device-center/src/main/java/com/central/device/feign/OrderCenterService.java`
|
||||
- 二维码服务:`zlt-business/order-center/src/main/java/com/central/order/service/IQrCodeService.java`
|
||||
- 数据访问:`zlt-business/order-center/src/main/resources/mapper/QrCodeMapper.xml`
|
||||
+91
@@ -214,4 +214,95 @@ public class ApiDeviceController {
|
||||
public Result<Map> getWayline(@RequestParam String no){
|
||||
return Result.succeed(orderCenterService.getWaylineByOrderNo(no));
|
||||
}
|
||||
|
||||
/**
|
||||
* 下发设备指令接口
|
||||
* 通过设备ID和二维码类型获取航线信息,生成随机订单号,下发指令
|
||||
*/
|
||||
@Operation(summary = "下发设备指令")
|
||||
@PostMapping("/command/{deviceId}")
|
||||
public Result sendDeviceCommand(@PathVariable("deviceId") String deviceId, @RequestParam(defaultValue = "2") Integer qrcodeType) {
|
||||
log.info("开始下发设备指令: deviceId={}, qrcodeType={}", deviceId, qrcodeType);
|
||||
try {
|
||||
// 1. 生成随机订单号
|
||||
String orderNo = generateRandomOrderNo();
|
||||
|
||||
// 2. 根据设备ID和二维码类型获取二维码信息
|
||||
Map<String, Object> qrcodeResult = orderCenterService.getQrcodeByDeviceId(deviceId, qrcodeType);
|
||||
if (qrcodeResult == null || qrcodeResult.isEmpty()) {
|
||||
return Result.failed("未找到对应的二维码配置");
|
||||
}
|
||||
|
||||
// 3. 构建控制消息(使用默认的control参数)
|
||||
JsonObject controlMessage = buildCommandMessage(orderNo, deviceId, qrcodeResult);
|
||||
if (controlMessage == null) {
|
||||
return Result.failed("暂未开放控制权限");
|
||||
}
|
||||
|
||||
// 4. 发送设备控制请求
|
||||
String url = baseUrl + "/device/instance/" + deviceId + "/property";
|
||||
Map<String, String> result = requestUtils.getPostRequestResultMap(url, controlMessage.toString());
|
||||
|
||||
// 5. 处理响应结果
|
||||
return handleDeviceResponse(result, orderNo);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("下发设备指令异常: deviceId={}, error={}", deviceId, e.getMessage(), e);
|
||||
return Result.failed("系统异常");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机订单号
|
||||
*/
|
||||
private String generateRandomOrderNo() {
|
||||
// 生成13位随机数字
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 13; i++) {
|
||||
sb.append((int)(Math.random() * 10));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建指令消息
|
||||
*/
|
||||
private JsonObject buildCommandMessage(String orderNo, String deviceId, Map<String, Object> qrcodeResult) {
|
||||
// 使用默认的control参数,这里可以根据需要修改
|
||||
String control = "flighttask_prepare"; // 使用飞行任务准备指令
|
||||
|
||||
if (!ControlEnum.contains(ControlEnum.valueOf(control.toUpperCase()))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Gson gson = new Gson();
|
||||
JsonObject jsonObject = gson.fromJson(controlMessageConstant.CONTROL_MESSAGE_MAP.get(control), JsonObject.class);
|
||||
JsonObject data = jsonObject.getAsJsonObject("data");
|
||||
|
||||
// 设置订单信息
|
||||
data.addProperty("order_no", orderNo);
|
||||
|
||||
// 设置航线信息
|
||||
String waylineFilePath = MapUtil.getStrValue(qrcodeResult, "waylineFilePath");
|
||||
String waylineId = MapUtil.getStrValue(qrcodeResult, "waylineId");
|
||||
log.info("waylineFilePath:{}, waylineId:{}", waylineFilePath, waylineId);
|
||||
data.addProperty("wayline_link_url", waylineFilePath);
|
||||
|
||||
LineFile lineFile = lineFileService.findLineFileByWaylineId(waylineId);
|
||||
Assert.notNull(lineFile,"航线文件签名不正确");
|
||||
data.addProperty("file_md5", lineFile.getSign());
|
||||
|
||||
// 新增takeOffOneKey字段
|
||||
data.addProperty("takeOffOneKey", true);
|
||||
|
||||
// 设置设备视频流地址
|
||||
try {
|
||||
data.add("stream_urls", gson.fromJson(liveStreamService.generateStreamUrls(deviceId).toJSONString(), JsonElement.class));
|
||||
} catch (Exception e) {
|
||||
log.error("生成设备推流/拉流地址异常: deviceId={}, error={}", deviceId, e.getMessage(), e);
|
||||
data.addProperty("stream_urls", "{}");
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
}
|
||||
|
||||
+10
-4
@@ -4,10 +4,7 @@ import com.central.common.constant.ServiceNameConstants;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.device.feign.fallback.OrderCenterServiceFallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -31,6 +28,15 @@ public interface OrderCenterService {
|
||||
@GetMapping("/qrcode/wayline/{orderNo}")
|
||||
Map<String,Object> getWaylineByOrderNo(@PathVariable("orderNo") String orderNo);
|
||||
|
||||
/**
|
||||
* 根据设备ID和二维码类型获取二维码信息
|
||||
* @param deviceId 设备ID
|
||||
* @param qrcodeType 二维码类型(1:游客下单,2:定时巡查,3:其他)
|
||||
* @return 二维码信息
|
||||
*/
|
||||
@GetMapping("/qrcode/device/{deviceId}")
|
||||
Map<String,Object> getQrcodeByDeviceId(@PathVariable("deviceId") String deviceId, @RequestParam("qrcodeType") Integer qrcodeType);
|
||||
|
||||
/**
|
||||
* 设备离线时自动创建退款订单
|
||||
* @param orderNo 订单号
|
||||
|
||||
+5
@@ -39,6 +39,11 @@ public class OrderCenterServiceFallbackFactory implements FallbackFactory<OrderC
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getQrcodeByDeviceId(String deviceId, Integer qrcodeType) {
|
||||
return Map.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result createRefundForOfflineDevice(String orderNo, Map<String, Object> refundBody) {
|
||||
log.error("调用订单中心退款接口失败: orderNo={}, cause={}", orderNo, cause.getMessage());
|
||||
|
||||
+1
@@ -84,6 +84,7 @@ public class ControlMessageConstant {
|
||||
CONTROL_MESSAGE_MAP.put("flighttask_prepare", FLIGHTTASK_PREPARE);
|
||||
CONTROL_MESSAGE_MAP.put("return_home", RETURN_HOME);
|
||||
CONTROL_MESSAGE_MAP.put("flighttask_recovery", FLIGHTTASK_RECOVERY);
|
||||
CONTROL_MESSAGE_MAP.put("takeoff_to_point", TAKEOFF_TO_POINT);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user