refactor(iot): 重构设备控制相关代码

- 移除 DeviceController 和 DeviceService 中的 topic 参数
- 更新 DeviceServiceImpl 中的 send 方法,使用 MqttConfig 中的 topic
- 优化 DeviceStatusVo 的构建方式
- 在数据库中为 iot_device 表添加经纬度默认值
- 更新 MqttConfig 中 topic 的获取方式
This commit is contained in:
zhuangtianxiang 2025-03-04 18:53:57 +08:00
parent c49c05598e
commit b883b9e343
8 changed files with 38 additions and 19 deletions

View File

@ -154,7 +154,7 @@ public class DeviceController {
* 下发命令
*/
@PostMapping("/send")
public String send(Long deviceId, String topic, Integer qos, @RequestBody JSONObject paras) throws JSONException {
return service.send(deviceId, topic, qos, paras);
public String send(Long deviceId, Integer qos, @RequestBody JSONObject paras) throws JSONException {
return service.send(deviceId, qos, paras);
}
}

View File

@ -33,5 +33,5 @@ public interface DeviceService extends IService<Device> {
DeviceStatusVo status();
String send(Long deviceId, String topic, Integer qos, JSONObject paras) throws JSONException;
String send(Long deviceId, Integer qos, JSONObject paras) throws JSONException;
}

View File

@ -3,6 +3,7 @@ package com.zsc.edu.gateway.modules.iot.device.service.impl;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -24,6 +25,7 @@ import com.zsc.edu.gateway.modules.iot.product.repo.ProductRepository;
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.mqtt.config.MqttConfig;
import com.zsc.edu.gateway.modules.mqtt.config.MqttSender;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
@ -56,6 +58,8 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
private MqttSender mqttSender;
@Resource
private DeviceRepository deviceRepo;
@Resource
private MqttConfig mqttConfig;
/**
* 新建设备
*/
@ -200,32 +204,37 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
@Override
public DeviceStatusVo status() {
DeviceStatusVo vo = new DeviceStatusVo();
vo.deviceCount = baseMapper.selectCount(null);
vo.propertyCount = propertyRepo.selectCount(null);
vo.serveCount = serveRepo.selectCount(null);
vo.eventCount = eventRepo.selectCount(null);
vo.onlineCount = baseMapper.selectCount(new LambdaQueryWrapper<Device>().eq(Device::getOnline, true));
vo.offlineCount = baseMapper.selectCount(new LambdaQueryWrapper<Device>().eq(Device::getOnline, false));
vo.disabledCount = baseMapper.selectCount(new LambdaQueryWrapper<Device>().eq(Device::getState, Device.Status.UNACTIVATED));
vo.directCount = productRepo.selectCount(new LambdaQueryWrapper<Product>().eq(Product::getLink, Product.LinkType.HTTP));
vo.gatewayCount = productRepo.selectCount(new LambdaQueryWrapper<Product>().eq(Product::getLink, Product.LinkType.TCP));
vo.gatewaySubCount = productRepo.selectCount(new LambdaQueryWrapper<Product>().eq(Product::getLink, Product.LinkType.MQTT));
return vo;
return DeviceStatusVo.builder()
.deviceCount(baseMapper.selectCount(new QueryWrapper<>()))
.propertyCount(propertyRepo.selectCount(new QueryWrapper<>()))
.serveCount(serveRepo.selectCount(new QueryWrapper<>()))
.eventCount(eventRepo.selectCount(new QueryWrapper<>()))
.onlineCount(baseMapper.selectCount(new LambdaQueryWrapper<Device>().eq(Device::getOnline, true)))
.offlineCount(baseMapper.selectCount(new LambdaQueryWrapper<Device>().eq(Device::getOnline, false)))
.disabledCount(baseMapper.selectCount(new LambdaQueryWrapper<Device>().eq(Device::getState, Device.Status.UNACTIVATED)))
.directCount(productRepo.selectCount(new LambdaQueryWrapper<Product>().eq(Product::getLink, Product.LinkType.HTTP)))
.gatewayCount(productRepo.selectCount(new LambdaQueryWrapper<Product>().eq(Product::getLink, Product.LinkType.TCP)))
.gatewaySubCount(productRepo.selectCount(new LambdaQueryWrapper<Product>().eq(Product::getLink, Product.LinkType.MQTT)))
.build();
}
@Override
public String send(Long deviceId, String topic, Integer qos, JSONObject paras) throws JSONException {
public String send(Long deviceId, Integer qos, JSONObject paras) throws JSONException {
Device device = deviceRepo.selectOne(new LambdaQueryWrapper<Device>().eq(Device::getId, deviceId));
JSONObject payloadJson = new JSONObject();
payloadJson.put("mid", 641);
payloadJson.put("serviceId", "runParamsCommand");
payloadJson.put("deviceId", device.getId());
payloadJson.put("deviceId", device.getClientId());
payloadJson.put("cmd", "runParam");
payloadJson.put("paras", paras);
payloadJson.put("msgType", "cloudReq");
String payload = payloadJson.toString();
mqttSender.sendMsg(topic, qos, payload);
//TODO 占位符用clientID拼接
try {
mqttSender.sendMsg(mqttConfig.getTopic(), qos, payload);
} catch (Exception e) {
throw new JSONException("发送失败");
}
return payload;
}

View File

@ -1,10 +1,12 @@
package com.zsc.edu.gateway.modules.iot.device.vo;
import lombok.Builder;
import lombok.Data;
/**
* @author zhuang
*/
@Builder
@Data
public class DeviceStatusVo {
/**

View File

@ -61,7 +61,6 @@ public class Product extends BaseEntity {
*/
private Boolean enabled = true;
/**
* 接入方式
*/

View File

@ -4,6 +4,8 @@ import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONPath;
import com.zsc.edu.gateway.modules.iot.record.service.RecordDataService;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Value;
@ -49,6 +51,7 @@ public class MqttConfig {
private String host;
@Value("${mqtt.port}")
private String port;
@Getter
@Value("${mqtt.topic}")
private String topic;
@Value("${mqtt.qos}")

Binary file not shown.

Before

Width:  |  Height:  |  Size: 133 KiB

View File

@ -930,3 +930,9 @@ VALUES ('1', 'client1', 'attach1', '{
"key2": 707
}', CURRENT_TIMESTAMP - INTERVAL '10 days', 1, 10);
ALTER TABLE iot_device
ALTER COLUMN longitude SET DEFAULT 113.39,
ALTER COLUMN latitude SET DEFAULT 22.52;