refactor(iot): 重构设备控制和参数处理逻辑
- 更新 DeviceController 中的 send 方法,增加参数接收 - 修改 DeviceService接口和实现类,调整 send 方法签名和逻辑 - 移除 ServeController 中的 detail 方法,增加 getServeParam 方法 - 更新 ServeService 接口和实现类,添加 getServeParam 方法 - 统一使用 fastjson2替代其他 JSON 处理库
This commit is contained in:
parent
1481dbd95e
commit
c49c05598e
@ -1,5 +1,7 @@
|
||||
package com.zsc.edu.gateway.modules.iot.device.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSONException;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
@ -17,8 +19,6 @@ import com.zsc.edu.gateway.modules.iot.record.entity.RecordData;
|
||||
import com.zsc.edu.gateway.modules.iot.record.entity.RecordDataStatusVo;
|
||||
import com.zsc.edu.gateway.modules.iot.record.service.RecordDataService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.boot.configurationprocessor.json.JSONException;
|
||||
import org.springframework.boot.configurationprocessor.json.JSONObject;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
@ -154,7 +154,7 @@ public class DeviceController {
|
||||
* 下发命令
|
||||
*/
|
||||
@PostMapping("/send")
|
||||
public String send(Long deviceId, String topic, Integer qos) throws JSONException {
|
||||
return service.send(deviceId, topic, qos);
|
||||
public String send(Long deviceId, String topic, Integer qos, @RequestBody JSONObject paras) throws JSONException {
|
||||
return service.send(deviceId, topic, qos, paras);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.zsc.edu.gateway.modules.iot.device.service;
|
||||
|
||||
import com.alibaba.fastjson2.JSONException;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
@ -10,8 +12,6 @@ import com.zsc.edu.gateway.modules.iot.device.entity.Device;
|
||||
import com.zsc.edu.gateway.modules.iot.device.query.DeviceQuery;
|
||||
import com.zsc.edu.gateway.modules.iot.device.vo.DeviceStatusVo;
|
||||
import com.zsc.edu.gateway.modules.iot.device.vo.DeviceVo;
|
||||
import org.springframework.boot.configurationprocessor.json.JSONException;
|
||||
import org.springframework.boot.configurationprocessor.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -33,5 +33,5 @@ public interface DeviceService extends IService<Device> {
|
||||
|
||||
DeviceStatusVo status();
|
||||
|
||||
String send(Long deviceId, String topic, Integer qos) throws JSONException;
|
||||
String send(Long deviceId, String topic, Integer qos, JSONObject paras) throws JSONException;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.zsc.edu.gateway.modules.iot.device.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONException;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
@ -20,19 +21,12 @@ import com.zsc.edu.gateway.modules.iot.device.vo.DeviceStatusVo;
|
||||
import com.zsc.edu.gateway.modules.iot.device.vo.DeviceVo;
|
||||
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
|
||||
import com.zsc.edu.gateway.modules.iot.product.repo.ProductRepository;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.entity.Serve;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.mapper.ParamMapper;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.repo.EventRepository;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.repo.PropertyRepository;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.repo.ServeRepository;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.service.ServeService;
|
||||
import com.zsc.edu.gateway.modules.mqtt.config.MqttSender;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.configurationprocessor.json.JSONException;
|
||||
import org.springframework.boot.configurationprocessor.json.JSONObject;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@ -61,11 +55,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
|
||||
@Resource
|
||||
private MqttSender mqttSender;
|
||||
@Resource
|
||||
private ServeService serveService;
|
||||
@Resource
|
||||
private DeviceRepository deviceRepo;
|
||||
@Resource
|
||||
private ParamMapper paramMapper;
|
||||
/**
|
||||
* 新建设备
|
||||
*/
|
||||
@ -225,40 +215,18 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
|
||||
}
|
||||
|
||||
@Override
|
||||
public String send(Long deviceId, String topic, Integer qos) throws JSONException {
|
||||
public String send(Long deviceId, String topic, Integer qos, JSONObject paras) throws JSONException {
|
||||
Device device = deviceRepo.selectOne(new LambdaQueryWrapper<Device>().eq(Device::getId, deviceId));
|
||||
Serve serve1 = serveRepo.selectOne(new LambdaQueryWrapper<Serve>().eq(Serve::getProductId, device.getProductId()));
|
||||
Serve serve = serveService.detail(serve1.getId());
|
||||
List<Param> params = serve.getInputs();
|
||||
params.addAll(serve.getOutputs());
|
||||
JSONObject parasJson = new JSONObject();
|
||||
for (Param param : params) {
|
||||
parasJson.put(param.getIdentifier(), param.getUint());
|
||||
log.info("Param identifier: {}, uint: {}", param.getIdentifier(), param.getUint());
|
||||
}
|
||||
JSONObject payloadJson = new JSONObject();
|
||||
payloadJson.put("mid", 641);
|
||||
payloadJson.put("serviceId", serve.getId());
|
||||
payloadJson.put("serviceId", "runParamsCommand");
|
||||
payloadJson.put("deviceId", device.getId());
|
||||
payloadJson.put("cmd", "runParam");
|
||||
payloadJson.put("paras", parasJson);
|
||||
payloadJson.put("paras", paras);
|
||||
payloadJson.put("msgType", "cloudReq");
|
||||
String payload = payloadJson.toString();
|
||||
boolean sendSuccess = true;
|
||||
try {
|
||||
mqttSender.sendMsg(topic, qos, payload);
|
||||
} catch (Exception e) {
|
||||
log.error("发送mqtt消息失败:topic={}, qos={}, payload={}", topic, qos, payload, e);
|
||||
sendSuccess = false;
|
||||
return payload;
|
||||
}
|
||||
JSONObject responseJson = new JSONObject();
|
||||
responseJson.put("mid", 641);
|
||||
responseJson.put("msgType", "deviceRsp");
|
||||
responseJson.put("errcode", sendSuccess ? 0 : 1);
|
||||
mqttSender.sendMsg(topic, qos, responseJson.toString());
|
||||
log.info("发送mqtt消息成功:topic={}, qos={}, payload={}", topic, qos, payload);
|
||||
return responseJson.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.zsc.edu.gateway.modules.iot.record.service;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.zsc.edu.gateway.modules.iot.record.entity.DataWarningVo;
|
||||
import com.zsc.edu.gateway.modules.iot.record.entity.RecordData;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.zsc.edu.gateway.modules.iot.record.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.zsc.edu.gateway.modules.iot.record.entity.DataWarningVo;
|
||||
@ -55,4 +55,5 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
|
||||
dataWarningVo.setThirdWarningCount(thirdWarningCount);
|
||||
return dataWarningVo;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.zsc.edu.gateway.modules.iot.tsl.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.dto.ServeDto;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.entity.Serve;
|
||||
@ -83,4 +84,10 @@ public class ServeController {
|
||||
public Serve detail(@PathVariable("id") Long id) {
|
||||
return serveService.detail(id);
|
||||
}
|
||||
|
||||
@GetMapping("/params/{id}")
|
||||
@PreAuthorize("hasAnyAuthority('iot:serve:query')")
|
||||
public JSONObject getServeParam(@PathVariable("id") Long id) {
|
||||
return serveService.getServeParam(id);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.zsc.edu.gateway.modules.iot.tsl.service;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
@ -18,4 +19,6 @@ public interface ServeService extends IService<Serve> {
|
||||
Serve detail(Long id);
|
||||
|
||||
Boolean delete(Long id);
|
||||
|
||||
JSONObject getServeParam(Long id);
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.zsc.edu.gateway.modules.iot.tsl.service.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.zsc.edu.gateway.exception.ConstraintException;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto;
|
||||
@ -89,4 +91,22 @@ public class ServeServiceImpl extends ServiceImpl<ServeRepository, Serve> implem
|
||||
removeById(id);
|
||||
return paramService.delete(id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取服务的输入和输出参数并转换为JSON格式
|
||||
*
|
||||
* @param id 服务ID
|
||||
* @return 包含输入和输出参数的JSON对象
|
||||
*/
|
||||
@Override
|
||||
public JSONObject getServeParam(Long id) {
|
||||
Serve serve = detail(id);
|
||||
List<Param> inputs = serve.getInputs();
|
||||
List<Param> outputs = serve.getOutputs();
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("inputs", inputs);
|
||||
result.put("outputs", outputs);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,12 @@
|
||||
package com.zsc.edu.gateway.modules.mqtt.config;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.JSONPath;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.zsc.edu.gateway.modules.iot.device.vo.DeviceVo;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.alibaba.fastjson2.JSONPath;
|
||||
import com.zsc.edu.gateway.modules.iot.record.service.RecordDataService;
|
||||
import com.zsc.edu.gateway.modules.iot.record.service.impl.RecordDataServiceImpl;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.integration.annotation.ServiceActivator;
|
||||
|
Loading…
Reference in New Issue
Block a user