Commit 8166b0b3 by YG8999

设备网关代码修改

parent e7d48a52
...@@ -16,10 +16,16 @@ import share.common.annotation.Log; ...@@ -16,10 +16,16 @@ import share.common.annotation.Log;
import share.common.core.controller.BaseController; import share.common.core.controller.BaseController;
import share.common.core.domain.AjaxResult; import share.common.core.domain.AjaxResult;
import share.common.enums.BusinessType; import share.common.enums.BusinessType;
import share.common.utils.SecurityUtils;
import share.framework.mqtt.MqttGatewayComponent;
import share.system.domain.Device; import share.system.domain.Device;
import share.system.domain.vo.DeviceParamVo;
import share.system.domain.vo.MqttxVo;
import share.system.service.DeviceLogService;
import share.system.service.DeviceService; import share.system.service.DeviceService;
import share.common.utils.poi.ExcelUtil; import share.common.utils.poi.ExcelUtil;
import share.common.core.page.TableDataInfo; import share.common.core.page.TableDataInfo;
import share.system.service.MqttxService;
/** /**
* 设备信息Controller * 设备信息Controller
...@@ -33,6 +39,12 @@ public class DeviceController extends BaseController ...@@ -33,6 +39,12 @@ public class DeviceController extends BaseController
{ {
@Autowired @Autowired
private DeviceService deviceService; private DeviceService deviceService;
@Autowired
private MqttxService mqttxService;
@Autowired
private DeviceLogService deviceLogService;
@Autowired
private MqttGatewayComponent mqttGatewayComponent;
/** /**
* 查询设备信息列表 * 查询设备信息列表
...@@ -101,4 +113,23 @@ public class DeviceController extends BaseController ...@@ -101,4 +113,23 @@ public class DeviceController extends BaseController
{ {
return toAjax(deviceService.deleteDeviceByIds(ids)); return toAjax(deviceService.deleteDeviceByIds(ids));
} }
/**
* 开门、关门、取电、断电
*/
@PreAuthorize("@ss.hasPermi('system:device:edit')")
@Log(title = "设备信息", businessType = BusinessType.UPDATE)
@PostMapping(value = "/openOrClose")
public AjaxResult openOrClose(@RequestBody DeviceParamVo deviceParam)
{
// 获取mqtt的topic、payload
MqttxVo mqttxVo = mqttxService.openOrCloseDevice(deviceParam.getDevId(),
SecurityUtils.getUsername(), deviceParam.getOpType());
// 发送mqtt消息
mqttGatewayComponent.sendToMqtt(mqttxVo.getTopic(), 0, mqttxVo.getPayload());
// 写日志记录
int result = deviceLogService.addDeviceLog(mqttxVo);
return toAjax(result);
}
} }
...@@ -101,4 +101,6 @@ public class DeviceGatewayController extends BaseController ...@@ -101,4 +101,6 @@ public class DeviceGatewayController extends BaseController
{ {
return toAjax(deviceGatewayService.deleteDeviceGatewayByIds(ids)); return toAjax(deviceGatewayService.deleteDeviceGatewayByIds(ids));
} }
} }
...@@ -58,6 +58,16 @@ public class MqttConstants { ...@@ -58,6 +58,16 @@ public class MqttConstants {
public static final String MQTT_DESCRIBE_DEVICE_ELECTRICITY_INTAKE = "取电"; public static final String MQTT_DESCRIBE_DEVICE_ELECTRICITY_INTAKE = "取电";
/** /**
* mqtt消息描述: 清除网关锁关门
*/
public static final String MQTT_DESCRIBE_DEVICE_CLOSE = "关门";
/**
* mqtt消息描述: 清除网关锁断电
*/
public static final String MQTT_DESCRIBE_DEVICE_ELECTRICITY_CLOSE = "断电";
/**
* mqtt消息描述: 远程下发密码 * mqtt消息描述: 远程下发密码
*/ */
public static final String MQTT_DESCRIBE_DEVICE_PASSWORD = "远程下发密码"; public static final String MQTT_DESCRIBE_DEVICE_PASSWORD = "远程下发密码";
......
...@@ -43,7 +43,7 @@ public enum MqttReportType { ...@@ -43,7 +43,7 @@ public enum MqttReportType {
if (StrUtil.isNotBlank(code)) { if (StrUtil.isNotBlank(code)) {
for (MqttReportType type : MqttReportType.values()) { for (MqttReportType type : MqttReportType.values()) {
if (type.code.compareTo(code)==0) { if (type.code.compareTo(code)==0) {
return type.name; return type.topic;
} }
} }
} }
......
...@@ -23,7 +23,6 @@ public interface MqttGatewayComponent { ...@@ -23,7 +23,6 @@ public interface MqttGatewayComponent {
* @param topic topic * @param topic topic
* @see * @see
*/ */
@Deprecated
void sendToMqtt(String payload, @Header(MqttHeaders.TOPIC) String topic); void sendToMqtt(String payload, @Header(MqttHeaders.TOPIC) String topic);
/** /**
......
...@@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.support.MqttHeaders; import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import share.system.service.MqttxService;
/** /**
* 消息处理器 * 消息处理器
...@@ -18,6 +19,8 @@ import org.springframework.stereotype.Service; ...@@ -18,6 +19,8 @@ import org.springframework.stereotype.Service;
@Service @Service
public class MqttTopicHandler { public class MqttTopicHandler {
@Autowired
private MqttxService mqttxService;
/** /**
* 这一步主要是对消息的有效荷载进行非空校验以及日志打印 * 这一步主要是对消息的有效荷载进行非空校验以及日志打印
...@@ -37,8 +40,8 @@ public class MqttTopicHandler { ...@@ -37,8 +40,8 @@ public class MqttTopicHandler {
private void dispose(String payload, String topic) { private void dispose(String payload, String topic) {
if (!ObjectUtil.isNull(payload)){ if (!ObjectUtil.isNull(payload)){
try { try {
mqttxService.mqttReport(topic, payload);
log.info("来自topic:{}的消息处理完毕!", topic); log.info("来自topic:{}的消息处理完毕!", topic);
log.info("消息内容:{}", payload);
} catch (Exception e) { } catch (Exception e) {
log.error("来自topic:{}的消息处理异常!原因:{}", topic, e); log.error("来自topic:{}的消息处理异常!原因:{}", topic, e);
} }
......
...@@ -68,12 +68,6 @@ public class SnowFlakeUtil { ...@@ -68,12 +68,6 @@ public class SnowFlakeUtil {
return Singleton.get(SnowFlakeUtil.class, new Object[]{1L, 1L}).nextId(); return Singleton.get(SnowFlakeUtil.class, new Object[]{1L, 1L}).nextId();
} }
public static void main(String[] args) {
for(int i = 0; i < 10; ++i) {
System.out.println(getDefaultSnowFlakeId());
System.out.println(getDefaultSnowFlakeId().toString().length());
}
}
} }
package share.system.domain.vo;
import lombok.Data;
import share.common.core.domain.BaseEntity;
/**
* @className: share.system.domain.vo.DeviceParamVo
* @description: 设备参数
* @author: lwj
* @create: 2023-11-07 16:10
*/
@Data
public class DeviceParamVo extends BaseEntity {
/** 设备mac */
private String devMac;
/** 设备id */
private String devId;
/** 操作类型:10:开门,20:取电,30:锁门,40:断电 */
private String opType;
/** 操作用户 */
private String phone;
}
...@@ -3,6 +3,7 @@ package share.system.service; ...@@ -3,6 +3,7 @@ package share.system.service;
import java.util.List; import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import share.system.domain.DeviceLog; import share.system.domain.DeviceLog;
import share.system.domain.vo.MqttxVo;
/** /**
* 设备操作日志Service接口 * 设备操作日志Service接口
...@@ -59,4 +60,13 @@ public interface DeviceLogService extends IService<DeviceLog> ...@@ -59,4 +60,13 @@ public interface DeviceLogService extends IService<DeviceLog>
* @return 结果 * @return 结果
*/ */
public int deleteDeviceLogById(Long id); public int deleteDeviceLogById(Long id);
/**
* 新增设备操作日志
*
* @param mqttxVo 设备操作信息
* @return 结果
*/
int addDeviceLog(MqttxVo mqttxVo);
} }
...@@ -28,10 +28,11 @@ public interface MqttxService { ...@@ -28,10 +28,11 @@ public interface MqttxService {
/** /**
* mqtt 上报接口 * mqtt 上报接口
* @param mqttx 上报数据对象 * @param topic 上报数据主题
* @param payload 上报数据参数
* @return * @return
*/ */
boolean mqttReport(MqttxVo mqttx); boolean mqttReport(String topic, String payload);
/** /**
* 开锁/关锁、取电/断电 * 开锁/关锁、取电/断电
......
...@@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; ...@@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import share.common.utils.DateUtils; import share.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import share.common.utils.SecurityUtils;
import share.system.domain.vo.MqttxVo;
import share.system.mapper.DeviceLogMapper; import share.system.mapper.DeviceLogMapper;
import share.system.domain.DeviceLog; import share.system.domain.DeviceLog;
import share.system.service.DeviceLogService; import share.system.service.DeviceLogService;
...@@ -94,4 +96,19 @@ public class DeviceLogServiceImpl extends ServiceImpl<DeviceLogMapper, DeviceLog ...@@ -94,4 +96,19 @@ public class DeviceLogServiceImpl extends ServiceImpl<DeviceLogMapper, DeviceLog
{ {
return deviceLogMapper.deleteDeviceLogById(id); return deviceLogMapper.deleteDeviceLogById(id);
} }
@Override
public int addDeviceLog(MqttxVo mqttxVo) {
DeviceLog deviceLog = new DeviceLog();
deviceLog.setDevId(mqttxVo.getDevId());
deviceLog.setDevMac(mqttxVo.getDevMac());
deviceLog.setTopic(mqttxVo.getTopic());
deviceLog.setPayload(mqttxVo.getPayload());
deviceLog.setSeq(mqttxVo.getSeq());
deviceLog.setMqttType(mqttxVo.getMqttType());
deviceLog.setMqttDescribe(mqttxVo.getMqttDescribe());
deviceLog.setCreateTime(DateUtils.getNowDate());
deviceLog.setCreateBy(SecurityUtils.getUsername());
return deviceLogMapper.insertDeviceLog(deviceLog);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment