refactor(iot): 重构设备控制相关代码
- 移除 DeviceController 和 DeviceService 中的 topic 参数 - 更新 DeviceServiceImpl 中的 send 方法,使用 MqttConfig 中的 topic - 优化 DeviceStatusVo 的构建方式 - 在数据库中为 iot_device 表添加经纬度默认值 - 更新 MqttConfig 中 topic 的获取方式
This commit is contained in:
parent
c49c05598e
commit
b883b9e343
@ -154,7 +154,7 @@ public class DeviceController {
|
|||||||
* 下发命令
|
* 下发命令
|
||||||
*/
|
*/
|
||||||
@PostMapping("/send")
|
@PostMapping("/send")
|
||||||
public String send(Long deviceId, String topic, Integer qos, @RequestBody JSONObject paras) throws JSONException {
|
public String send(Long deviceId, Integer qos, @RequestBody JSONObject paras) throws JSONException {
|
||||||
return service.send(deviceId, topic, qos, paras);
|
return service.send(deviceId, qos, paras);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,5 +33,5 @@ public interface DeviceService extends IService<Device> {
|
|||||||
|
|
||||||
DeviceStatusVo status();
|
DeviceStatusVo status();
|
||||||
|
|
||||||
String send(Long deviceId, String topic, Integer qos, JSONObject paras) throws JSONException;
|
String send(Long deviceId, Integer qos, JSONObject paras) throws JSONException;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package com.zsc.edu.gateway.modules.iot.device.service.impl;
|
|||||||
import com.alibaba.fastjson2.JSONException;
|
import com.alibaba.fastjson2.JSONException;
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
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.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
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.EventRepository;
|
||||||
import com.zsc.edu.gateway.modules.iot.tsl.repo.PropertyRepository;
|
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.repo.ServeRepository;
|
||||||
|
import com.zsc.edu.gateway.modules.mqtt.config.MqttConfig;
|
||||||
import com.zsc.edu.gateway.modules.mqtt.config.MqttSender;
|
import com.zsc.edu.gateway.modules.mqtt.config.MqttSender;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -56,6 +58,8 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
|
|||||||
private MqttSender mqttSender;
|
private MqttSender mqttSender;
|
||||||
@Resource
|
@Resource
|
||||||
private DeviceRepository deviceRepo;
|
private DeviceRepository deviceRepo;
|
||||||
|
@Resource
|
||||||
|
private MqttConfig mqttConfig;
|
||||||
/**
|
/**
|
||||||
* 新建设备
|
* 新建设备
|
||||||
*/
|
*/
|
||||||
@ -200,32 +204,37 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DeviceStatusVo status() {
|
public DeviceStatusVo status() {
|
||||||
DeviceStatusVo vo = new DeviceStatusVo();
|
return DeviceStatusVo.builder()
|
||||||
vo.deviceCount = baseMapper.selectCount(null);
|
.deviceCount(baseMapper.selectCount(new QueryWrapper<>()))
|
||||||
vo.propertyCount = propertyRepo.selectCount(null);
|
.propertyCount(propertyRepo.selectCount(new QueryWrapper<>()))
|
||||||
vo.serveCount = serveRepo.selectCount(null);
|
.serveCount(serveRepo.selectCount(new QueryWrapper<>()))
|
||||||
vo.eventCount = eventRepo.selectCount(null);
|
.eventCount(eventRepo.selectCount(new QueryWrapper<>()))
|
||||||
vo.onlineCount = baseMapper.selectCount(new LambdaQueryWrapper<Device>().eq(Device::getOnline, true));
|
.onlineCount(baseMapper.selectCount(new LambdaQueryWrapper<Device>().eq(Device::getOnline, true)))
|
||||||
vo.offlineCount = baseMapper.selectCount(new LambdaQueryWrapper<Device>().eq(Device::getOnline, false));
|
.offlineCount(baseMapper.selectCount(new LambdaQueryWrapper<Device>().eq(Device::getOnline, false)))
|
||||||
vo.disabledCount = baseMapper.selectCount(new LambdaQueryWrapper<Device>().eq(Device::getState, Device.Status.UNACTIVATED));
|
.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));
|
.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));
|
.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));
|
.gatewaySubCount(productRepo.selectCount(new LambdaQueryWrapper<Product>().eq(Product::getLink, Product.LinkType.MQTT)))
|
||||||
return vo;
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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));
|
Device device = deviceRepo.selectOne(new LambdaQueryWrapper<Device>().eq(Device::getId, deviceId));
|
||||||
JSONObject payloadJson = new JSONObject();
|
JSONObject payloadJson = new JSONObject();
|
||||||
payloadJson.put("mid", 641);
|
payloadJson.put("mid", 641);
|
||||||
payloadJson.put("serviceId", "runParamsCommand");
|
payloadJson.put("serviceId", "runParamsCommand");
|
||||||
payloadJson.put("deviceId", device.getId());
|
payloadJson.put("deviceId", device.getClientId());
|
||||||
payloadJson.put("cmd", "runParam");
|
payloadJson.put("cmd", "runParam");
|
||||||
payloadJson.put("paras", paras);
|
payloadJson.put("paras", paras);
|
||||||
payloadJson.put("msgType", "cloudReq");
|
payloadJson.put("msgType", "cloudReq");
|
||||||
String payload = payloadJson.toString();
|
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;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package com.zsc.edu.gateway.modules.iot.device.vo;
|
package com.zsc.edu.gateway.modules.iot.device.vo;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhuang
|
* @author zhuang
|
||||||
*/
|
*/
|
||||||
|
@Builder
|
||||||
@Data
|
@Data
|
||||||
public class DeviceStatusVo {
|
public class DeviceStatusVo {
|
||||||
/**
|
/**
|
||||||
|
@ -61,7 +61,6 @@ public class Product extends BaseEntity {
|
|||||||
*/
|
*/
|
||||||
private Boolean enabled = true;
|
private Boolean enabled = true;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 接入方式
|
* 接入方式
|
||||||
*/
|
*/
|
||||||
|
@ -4,6 +4,8 @@ import com.alibaba.fastjson2.JSONObject;
|
|||||||
import com.alibaba.fastjson2.JSONPath;
|
import com.alibaba.fastjson2.JSONPath;
|
||||||
import com.zsc.edu.gateway.modules.iot.record.service.RecordDataService;
|
import com.zsc.edu.gateway.modules.iot.record.service.RecordDataService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@ -49,6 +51,7 @@ public class MqttConfig {
|
|||||||
private String host;
|
private String host;
|
||||||
@Value("${mqtt.port}")
|
@Value("${mqtt.port}")
|
||||||
private String port;
|
private String port;
|
||||||
|
@Getter
|
||||||
@Value("${mqtt.topic}")
|
@Value("${mqtt.topic}")
|
||||||
private String topic;
|
private String topic;
|
||||||
@Value("${mqtt.qos}")
|
@Value("${mqtt.qos}")
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 133 KiB |
@ -930,3 +930,9 @@ VALUES ('1', 'client1', 'attach1', '{
|
|||||||
"key2": 707
|
"key2": 707
|
||||||
}', CURRENT_TIMESTAMP - INTERVAL '10 days', 1, 10);
|
}', 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;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user