12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package com.zy.omp.service;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.zy.omp.common.Constant;
- import com.zy.omp.pojo.io.pc.MqttLogsIO;
- import com.zy.omp.mapper.MqttLogMapper;
- import com.zy.omp.model.MqttLog;
- import com.zy.omp.pojo.dto.MqttMsgDto;
- import org.springframework.stereotype.Service;
- import java.util.HashSet;
- /**
- * MQTT 收发日志 Service
- *
- * @author chenyi
- * Create on 2020/4/10
- */
- @Service
- public class MqttLogService extends ServiceImpl<MqttLogMapper, MqttLog> {
- //所有的注册主题
- private static final HashSet<String> registerSet = new HashSet<>();
- static {
- registerSet.add(Constant.TOPIC_REGISTER_CLIENT);
- registerSet.add(Constant.TOPIC_REGISTER_SERVER);
- }
- /**
- * 保存接收的消息日志
- *
- * @param topic 主题
- * @param payload 消息内容
- */
- public void saveReceiveLog(String topic, String payload) {
- saveLog(topic, payload, 0);
- }
- /**
- * 保存发送的消息日志
- *
- * @param topic 主题
- * @param payload 消息内容
- */
- public void saveSendLog(String topic, String payload) {
- saveLog(topic, payload, 1);
- }
- /**
- * 保存 日志
- *
- * @param topic 主题
- * @param payload 消息内容
- * @param tag 0收 1发
- */
- private void saveLog(String topic, String payload, Integer tag) {
- MqttLog entity = new MqttLog();
- entity.setTopic(topic);
- //mqtt 日志类型
- entity.setTag(tag);
- //获取消息解析对象
- MqttMsgDto mqttMsgDto = MqttMsgDto.parse(payload);
- //注册报文与普通报文不同,注册报文取设备随机码
- String[] topicSplit = topic.split("/");
- String deviceId = topicSplit.length > 1 ? topicSplit[1] : null;
- entity.setDeviceId(registerSet.contains(topic) ? mqttMsgDto.getString("regnum") : deviceId);
- //mqtt 指令
- entity.setInstruction(mqttMsgDto.getM());
- entity.setContent(payload);
- baseMapper.insert(entity);
- }
- /**
- * 分页查询日志
- *
- * @param io 查询条件
- */
- public IPage<MqttLog> getListPage(MqttLogsIO io) {
- Page<MqttLog> page = new Page<>(io.getCurrent(), io.getSize());
- return baseMapper.getListPage(page, io);
- }
- }
|