Commit 98e11082 authored by 赵啸非's avatar 赵啸非

日志展示修改

parent c347cd33
...@@ -7,4 +7,5 @@ package com.mortals.xhx.queue; ...@@ -7,4 +7,5 @@ package com.mortals.xhx.queue;
* @date: 2021/11/22 10:56 * @date: 2021/11/22 10:56
*/ */
public interface TbQueueMsgMetadata { public interface TbQueueMsgMetadata {
String getMessageId();
} }
...@@ -15,4 +15,6 @@ import org.apache.kafka.clients.producer.RecordMetadata; ...@@ -15,4 +15,6 @@ import org.apache.kafka.clients.producer.RecordMetadata;
@AllArgsConstructor @AllArgsConstructor
public class KafkaTbQueueMsgMetadata implements TbQueueMsgMetadata { public class KafkaTbQueueMsgMetadata implements TbQueueMsgMetadata {
private RecordMetadata metadata; private RecordMetadata metadata;
private String messageId;
} }
...@@ -69,7 +69,7 @@ public class TbKafkaProducerTemplate<T extends TbQueueMsg> implements TbQueuePro ...@@ -69,7 +69,7 @@ public class TbKafkaProducerTemplate<T extends TbQueueMsg> implements TbQueuePro
producer.send(record, (metadata, exception) -> { producer.send(record, (metadata, exception) -> {
if (exception == null) { if (exception == null) {
if (callback != null) { if (callback != null) {
callback.onSuccess(new KafkaTbQueueMsgMetadata(metadata)); callback.onSuccess(new KafkaTbQueueMsgMetadata(metadata,key));
} }
} else { } else {
if (callback != null) { if (callback != null) {
......
package com.mortals.xhx.queue.rabbitmq;
import com.mortals.xhx.queue.TbQueueMsgMetadata;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.kafka.clients.producer.RecordMetadata;
/**
* 队列元数据
*
* @author: zxfei
* @date: 2021/11/22 14:40
*/
@Data
@AllArgsConstructor
public class RabbitQueueMsgMetadata implements TbQueueMsgMetadata {
private String messageId;
}
...@@ -96,7 +96,7 @@ public class TbRabbitMqProducerTemplate<T extends TbQueueMsg> implements TbQueue ...@@ -96,7 +96,7 @@ public class TbRabbitMqProducerTemplate<T extends TbQueueMsg> implements TbQueue
channel.queueBind(tpi.getTopic(), tpi.getExchangeName(), tpi.getTopic()); channel.queueBind(tpi.getTopic(), tpi.getExchangeName(), tpi.getTopic());
channel.basicPublish(tpi.getExchangeName(), tpi.getTopic(), properties, JSON.toJSONString(new DefaultTbQueueMsg(msg)).getBytes()); channel.basicPublish(tpi.getExchangeName(), tpi.getTopic(), properties, JSON.toJSONString(new DefaultTbQueueMsg(msg)).getBytes());
if (callback != null) { if (callback != null) {
callback.onSuccess(null); callback.onSuccess(new RabbitQueueMsgMetadata(msg.getKey()));
} }
} catch (IOException e) { } catch (IOException e) {
log.error("Failed publish message: [{}].", msg, e); log.error("Failed publish message: [{}].", msg, e);
...@@ -143,7 +143,8 @@ public class TbRabbitMqProducerTemplate<T extends TbQueueMsg> implements TbQueue ...@@ -143,7 +143,8 @@ public class TbRabbitMqProducerTemplate<T extends TbQueueMsg> implements TbQueue
channel = connection.createChannel(); channel = connection.createChannel();
channel.exchangeDeclare(tpi.getExchangeName(), BuiltinExchangeType.DIRECT, true, false, null); channel.exchangeDeclare(tpi.getExchangeName(), BuiltinExchangeType.DIRECT, true, false, null);
} }
callback.onSuccess(null);
// callback.onSuccess(new RabbitQueueMsgMetadata());
} catch (IOException e) { } catch (IOException e) {
log.error("Failed publish message: {}.", e); log.error("Failed publish message: {}.", e);
if (callback != null) { if (callback != null) {
...@@ -168,14 +169,13 @@ public class TbRabbitMqProducerTemplate<T extends TbQueueMsg> implements TbQueue ...@@ -168,14 +169,13 @@ public class TbRabbitMqProducerTemplate<T extends TbQueueMsg> implements TbQueue
private Boolean createTopicIfNotExist(TopicPartitionInfo tpi) { private Boolean createTopicIfNotExist(TopicPartitionInfo tpi) {
if (topics.contains(tpi)) { if (topics.contains(tpi)) {
log.info("contains topc:{}", tpi.getTopic()); log.debug("contains topc:{}", tpi.getTopic());
return true; return true;
} }
topics.add(tpi); topics.add(tpi);
return false; return false;
} }
private boolean innerExists(String exchangeName, Channel outerChannel) { private boolean innerExists(String exchangeName, Channel outerChannel) {
boolean result = true; boolean result = true;
try { try {
......
...@@ -46,12 +46,14 @@ ...@@ -46,12 +46,14 @@
], ],
columns: [ columns: [
{type: "selection", width: 60}, {label: "traceID", prop: "traceID"},
{label: "设备编号", prop: "deviceNum"}, {label: "设备编号", prop: "deviceNum"},
{label: "设备名称", prop: "deviceName"}, {label: "设备名称", prop: "deviceName"},
{label: "业务标识", prop: "messageHead"},
{label: "日志类型", prop: "logType",formatter: this.formatter}, {label: "日志类型", prop: "logType",formatter: this.formatter},
{label: "内容", prop: "content"}, {label: "内容", prop: "content"},
......
...@@ -3,15 +3,21 @@ package com.mortals.xhx.busiz.web; ...@@ -3,15 +3,21 @@ package com.mortals.xhx.busiz.web;
import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.annotation.UnAuth; import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.common.Rest; import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException; import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.IAuthTokenService; import com.mortals.framework.service.IAuthTokenService;
import com.mortals.framework.service.ICacheService; import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.ILogService;
import com.mortals.framework.service.IUser;
import com.mortals.framework.service.impl.FileLogServiceImpl;
import com.mortals.framework.util.DateUtils; import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.framework.annotation.ApiUserAuth; import com.mortals.xhx.base.framework.annotation.ApiUserAuth;
import com.mortals.xhx.base.login.web.LoginForm; import com.mortals.xhx.base.login.web.LoginForm;
import com.mortals.xhx.base.system.user.model.UserEntity; import com.mortals.xhx.base.system.user.model.UserEntity;
...@@ -50,12 +56,15 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -50,12 +56,15 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.mortals.framework.ap.SysConstains.PROP_PLATFORM_MARK;
import static com.mortals.xhx.common.key.Constant.DES_STR; import static com.mortals.xhx.common.key.Constant.DES_STR;
import static com.mortals.xhx.common.key.Constant.ENCRYPT_STR; import static com.mortals.xhx.common.key.Constant.ENCRYPT_STR;
import static com.mortals.xhx.common.key.ErrorCode.*; import static com.mortals.xhx.common.key.ErrorCode.*;
...@@ -106,6 +115,9 @@ public class DeviceApiController { ...@@ -106,6 +115,9 @@ public class DeviceApiController {
@Autowired @Autowired
private ICacheService cacheService; private ICacheService cacheService;
@Autowired
protected ILogService logService = FileLogServiceImpl.getInstance();
/** /**
* 设备注册 * 设备注册
* *
...@@ -208,7 +220,7 @@ public class DeviceApiController { ...@@ -208,7 +220,7 @@ public class DeviceApiController {
//long expireTime = DateUtil.between(DateUtil.beginOfWeek(new Date()), DateUtil.endOfWeek(new Date()), DateUnit.SECOND); //long expireTime = DateUtil.between(DateUtil.beginOfWeek(new Date()), DateUtil.endOfWeek(new Date()), DateUnit.SECOND);
cacheService.setnx(KEY_TOKEN_API_CACHE+token,"",604800); cacheService.setnx(KEY_TOKEN_API_CACHE + token, "", 604800);
rsp.setData(token); rsp.setData(token);
} }
} catch (AppException e) { } catch (AppException e) {
...@@ -479,7 +491,7 @@ public class DeviceApiController { ...@@ -479,7 +491,7 @@ public class DeviceApiController {
header.put(MessageHeader.MESSAGETYPE, Constant.MESSAGETYPE_HEARTBEAT); header.put(MessageHeader.MESSAGETYPE, Constant.MESSAGETYPE_HEARTBEAT);
header.put(MessageHeader.DEVICECODE, deviceEntity.getDeviceCode()); header.put(MessageHeader.DEVICECODE, deviceEntity.getDeviceCode());
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
deviceService.sendDeviceMessage(deviceEntity, info, header, JSON.toJSONString(req), null); deviceService.sendDeviceMessage(deviceEntity, info, header, JSON.toJSONString(req),null, null);
Thread.sleep(50); Thread.sleep(50);
} }
//log.info(String.format("sendMsgResp:%s", JSON.toJSONString(sendDeviceMessageResp))); //log.info(String.format("sendMsgResp:%s", JSON.toJSONString(sendDeviceMessageResp)));
...@@ -504,11 +516,14 @@ public class DeviceApiController { ...@@ -504,11 +516,14 @@ public class DeviceApiController {
*/ */
@PostMapping("receive") @PostMapping("receive")
@ApiUserAuth @ApiUserAuth
public String receive(@RequestBody DeviceReq req) { public String receive(HttpServletRequest request, @RequestBody DeviceReq req) {
log.info("【设备接收】【请求体】--> " + JSONObject.toJSONString(req)); log.info("【设备接收】【请求体】--> " + JSONObject.toJSONString(req));
ApiResp<String> rsp = new ApiResp<>(); ApiResp<String> rsp = new ApiResp<>();
rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel()); rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue()); rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
StringBuilder message = new StringBuilder();
message.append(String.format("【外部请求】类型【%s】 内容:%s", DeviceMethodEnum.getByValue(req.getReceiveMethod()).getDesc(), JSONObject.toJSONString(req)));
recordSysLog(request, message.toString());
try { try {
switch (DeviceMethodEnum.getByValue(req.getReceiveMethod())) { switch (DeviceMethodEnum.getByValue(req.getReceiveMethod())) {
case ADD: case ADD:
...@@ -706,4 +721,18 @@ public class DeviceApiController { ...@@ -706,4 +721,18 @@ public class DeviceApiController {
return deviceEntity; return deviceEntity;
} }
protected void recordSysLog(HttpServletRequest request, String message) {
if (this.logService != null) {
String platformMark = "other-platform";
String requestUrl = request.getRequestURI();
try {
this.logService.doHandlerLog(platformMark, 0L, "other", "other", requestUrl, message, ServletUtil.getClientIP(request), new Date());
} catch (Exception e) {
this.log.debug("记录系统操作日志异常-->" + e.getMessage());
}
}
}
} }
...@@ -10,7 +10,8 @@ import java.util.Map; ...@@ -10,7 +10,8 @@ import java.util.Map;
*/ */
public enum LogTypeEnum { public enum LogTypeEnum {
上报事件(0, "上行"), 上报事件(0, "上行"),
下发服务(1, "下行"); 下发服务(1, "下行"),
消息异常(99, "消息发送异常");
private Integer value; private Integer value;
private String desc; private String desc;
......
package com.mortals.xhx.common.utils; package com.mortals.xhx.common.utils;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.mortals.xhx.base.framework.ws.message.SendToAllRequest; import com.mortals.xhx.base.framework.ws.message.SendToAllRequest;
...@@ -19,7 +20,9 @@ import com.mortals.xhx.module.platform.model.PlatformEntity; ...@@ -19,7 +20,9 @@ import com.mortals.xhx.module.platform.model.PlatformEntity;
import com.mortals.xhx.module.platform.service.PlatformService; import com.mortals.xhx.module.platform.service.PlatformService;
import com.mortals.xhx.module.product.model.ProductEntity; import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.service.ProductService; import com.mortals.xhx.module.product.service.ProductService;
import com.mortals.xhx.queue.TbQueueCallback;
import com.mortals.xhx.queue.TbQueueMsgHeaders; import com.mortals.xhx.queue.TbQueueMsgHeaders;
import com.mortals.xhx.queue.TbQueueMsgMetadata;
import com.mortals.xhx.queue.TopicPartitionInfo; import com.mortals.xhx.queue.TopicPartitionInfo;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -71,24 +74,50 @@ public class DownMsgTask implements Runnable { ...@@ -71,24 +74,50 @@ public class DownMsgTask implements Runnable {
header.put(MessageHeader.MESSAGETYPE, item.getMessageType()); header.put(MessageHeader.MESSAGETYPE, item.getMessageType());
header.put(MessageHeader.DEVICECODE, item.getDeviceCode()); header.put(MessageHeader.DEVICECODE, item.getDeviceCode());
header.put(MessageHeader.TIMESTAMP, item.getTimestamp().toString()); header.put(MessageHeader.TIMESTAMP, item.getTimestamp().toString());
deviceService.sendDeviceMessage(deviceEntity, info, header, item.getData(), null); TbQueueCallback callback = new TbQueueCallback() {
@Override
public void onSuccess(TbQueueMsgMetadata metadata) {
//下行业务消息保存日志
if (!Constant.MESSAGETYPE_HEARTBEAT.equalsIgnoreCase(item.getMessageType())) {
DeviceLogEntity deviceLogEntity = new DeviceLogEntity();
deviceLogEntity.initAttrValue();
deviceLogEntity.setTraceID(metadata.getMessageId());
deviceLogEntity.setDeviceId(deviceEntity.getId());
deviceLogEntity.setDeviceName(deviceEntity.getDeviceName());
deviceLogEntity.setDeviceCode(deviceEntity.getDeviceCode());
deviceLogEntity.setMessageHead(item.getMessageType());
deviceLogEntity.setContent(item.getData());
deviceLogEntity.setLogType(LogTypeEnum.下发服务.getValue());
deviceLogEntity.setCreateUserId(1L);
deviceLogEntity.setCreateTime(new Date());
deviceLogService.save(deviceLogEntity);
}
log.info("消息投递成功,设备通道编码:" + deviceEntity.getDeviceMac());
}
//下行业务消息保存日志 @Override
if (!Constant.MESSAGETYPE_HEARTBEAT.equals(item.getMessageType())) { public void onFailure(Throwable t) {
DeviceLogEntity deviceLogEntity = new DeviceLogEntity(); DeviceLogEntity deviceLogEntity = new DeviceLogEntity();
deviceLogEntity.initAttrValue(); deviceLogEntity.initAttrValue();
deviceLogEntity.setDeviceId(deviceEntity.getId()); String traceID = IdUtil.fastSimpleUUID();
deviceLogEntity.setDeviceName(deviceEntity.getDeviceName()); deviceLogEntity.setTraceID(traceID);
deviceLogEntity.setDeviceCode(deviceEntity.getDeviceCode()); deviceLogEntity.setDeviceId(deviceEntity.getId());
deviceLogEntity.setMessageHead(item.getMessageType()); deviceLogEntity.setDeviceName(deviceEntity.getDeviceName());
deviceLogEntity.setContent(item.getData()); deviceLogEntity.setDeviceCode(deviceEntity.getDeviceCode());
deviceLogEntity.setLogType(LogTypeEnum.下发服务.getValue()); deviceLogEntity.setMessageHead(item.getMessageType());
deviceLogEntity.setCreateUserId(1L); deviceLogEntity.setContent(item.getData());
deviceLogEntity.setCreateTime(new Date()); deviceLogEntity.setLogType(LogTypeEnum.消息异常.getValue());
deviceLogService.save(deviceLogEntity); deviceLogEntity.setCreateUserId(1L);
} deviceLogEntity.setCreateTime(new Date());
}else{ deviceLogService.save(deviceLogEntity);
log.info("未找到设备,deviceCode:{}",item.getDeviceCode()); log.error("traceID:"+traceID,"消息投递失败,设备通道编码:" + deviceEntity.getDeviceMac(), t);
}
};
deviceService.sendDeviceMessage(deviceEntity, info, header, item.getData(),callback, null);
} else {
log.info("未找到设备,deviceCode:{}", item.getDeviceCode());
} }
}); });
} catch (Exception e) { } catch (Exception e) {
......
...@@ -39,8 +39,10 @@ public class SendTask implements Runnable { ...@@ -39,8 +39,10 @@ public class SendTask implements Runnable {
try { try {
Map<String, String> header = new HashMap<>(); Map<String, String> header = new HashMap<>();
header.put(HEADER_CONTENT_TYPE, "application/json"); header.put(HEADER_CONTENT_TYPE, "application/json");
resp = HttpUtil.doPost(sendUrl, header, JSON.toJSONString(deviceReqApiReq)); String reqStr = JSON.toJSONString(deviceReqApiReq);
log.debug("resp:{}", resp); log.debug("sendMessageReq:{}", reqStr);
resp = HttpUtil.doPost(sendUrl, header, reqStr);
log.debug("sendMessageResp:{}", resp);
} catch (Exception e) { } catch (Exception e) {
log.error("异常:", e); log.error("异常:", e);
} }
......
...@@ -170,7 +170,7 @@ public class DemoApiController { ...@@ -170,7 +170,7 @@ public class DemoApiController {
uploadDeviceReq.setDeviceCode(deviceEntity.getDeviceCode()); uploadDeviceReq.setDeviceCode(deviceEntity.getDeviceCode());
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
deviceService.sendDeviceMessage(deviceEntity, info, header, JSON.toJSONString(deviceEntity), null); deviceService.sendDeviceMessage(deviceEntity, info, header, JSON.toJSONString(deviceEntity), null,null);
} }
} }
......
package com.mortals.xhx.daemon.applicationservice; package com.mortals.xhx.daemon.applicationservice;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.ap.GlobalSysInfo; import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.service.ICacheService; import com.mortals.framework.service.ICacheService;
import com.mortals.framework.springcloud.service.IApplicationStartedService; import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.busiz.req.DeviceReq;
import com.mortals.xhx.common.code.*; import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.key.Constant; import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.key.RedisKey; import com.mortals.xhx.common.key.RedisKey;
...@@ -35,7 +34,7 @@ import java.util.concurrent.Executors; ...@@ -35,7 +34,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.mortals.xhx.common.model.MessageHeader.MESSAGETYPE; import static com.mortals.xhx.common.model.MessageHeader.*;
@Component @Component
...@@ -156,18 +155,14 @@ public class DeviceMsgComsumerStartedService implements IApplicationStartedServi ...@@ -156,18 +155,14 @@ public class DeviceMsgComsumerStartedService implements IApplicationStartedServi
for (TbQueueMsg queueMsg : messages) { for (TbQueueMsg queueMsg : messages) {
log.debug("[WRITE MESSAGE] ->message:{}", JSON.toJSONString(queueMsg)); log.debug("[WRITE MESSAGE] ->message:{}", JSON.toJSONString(queueMsg));
//做相应业务,做日志操作 //做相应业务,做日志操作
// TODO: 2022/7/12 设备来源从设备编码中获取,内容不做处理 String deviceCode = queueMsg.getHeaders().get(DEVICECODE);
String deviceCode = queueMsg.getHeaders().get("deviceCode");
String messageType = queueMsg.getHeaders().get(MESSAGETYPE); String messageType = queueMsg.getHeaders().get(MESSAGETYPE);
//DeviceReq deviceReq = JSON.parseObject(new String(queueMsg.getData()), DeviceReq.class); //DeviceReq deviceReq = JSON.parseObject(new String(queueMsg.getData()), DeviceReq.class);
boolean bool = false;
DeviceEntity deviceEntity = deviceService.getExtCache(deviceCode); DeviceEntity deviceEntity = deviceService.getExtCache(deviceCode);
if (!ObjectUtils.isEmpty(deviceEntity)) { if (!ObjectUtils.isEmpty(deviceEntity)) {
cacheService.set(RedisKey.KEY_DEVICE_ONLINE_CACHE + deviceEntity.getDeviceCode(), "", GlobalSysInfo.getParamIntValue(Constant.HEARTBEAT_TIMEOUT, 120)); cacheService.set(RedisKey.KEY_DEVICE_ONLINE_CACHE + deviceEntity.getDeviceCode(), "", GlobalSysInfo.getParamIntValue(Constant.HEARTBEAT_TIMEOUT, 120));
if (deviceEntity.getDeviceStatus() == DeviceStatusEnum.离线.getValue()) {
bool = true;
}
if (deviceEntity.getDeviceStatus() == DeviceStatusEnum.离线.getValue()) { if (deviceEntity.getDeviceStatus() == DeviceStatusEnum.离线.getValue()) {
deviceEntity.setOnlineTime(new Date()); deviceEntity.setOnlineTime(new Date());
deviceEntity.setDeviceStatus(DeviceStatusEnum.在线.getValue()); deviceEntity.setDeviceStatus(DeviceStatusEnum.在线.getValue());
...@@ -177,17 +172,18 @@ public class DeviceMsgComsumerStartedService implements IApplicationStartedServi ...@@ -177,17 +172,18 @@ public class DeviceMsgComsumerStartedService implements IApplicationStartedServi
ProductEntity productEntity = productService.get(deviceEntity.getProductId()); ProductEntity productEntity = productService.get(deviceEntity.getProductId());
if (!ObjectUtils.isEmpty(platformEntity) && !ObjectUtils.isEmpty(productEntity)) { if (!ObjectUtils.isEmpty(platformEntity) && !ObjectUtils.isEmpty(productEntity)) {
//新增设备通知第三方平台 //新增设备通知第三方平台
deviceService.sendThirdParty(deviceEntity, productEntity, platformEntity, DeviceMethodEnum.OFFLINE); deviceService.sendThirdParty(deviceEntity, productEntity, platformEntity, DeviceMethodEnum.ONLINE);
} }
} }
if (!Constant.MESSAGETYPE_HEARTBEAT.equals(messageType)) { if (!Constant.MESSAGETYPE_HEARTBEAT.equalsIgnoreCase(messageType)) {
DeviceLogEntity deviceLogEntity = new DeviceLogEntity(); DeviceLogEntity deviceLogEntity = new DeviceLogEntity();
deviceLogEntity.initAttrValue(); deviceLogEntity.initAttrValue();
deviceLogEntity.setTraceID(IdUtil.fastSimpleUUID());
deviceLogEntity.setDeviceId(deviceEntity.getId()); deviceLogEntity.setDeviceId(deviceEntity.getId());
deviceLogEntity.setDeviceName(deviceEntity.getDeviceName()); deviceLogEntity.setDeviceName(deviceEntity.getDeviceName());
deviceLogEntity.setDeviceCode(deviceEntity.getDeviceCode()); deviceLogEntity.setDeviceCode(deviceEntity.getDeviceCode());
deviceLogEntity.setMessageHead(queueMsg.getHeaders().getData().get(MESSAGETYPE)); deviceLogEntity.setMessageHead(messageType);
deviceLogEntity.setContent(new String(queueMsg.getData())); deviceLogEntity.setContent(queueMsg.getData());
deviceLogEntity.setLogType(LogTypeEnum.上报事件.getValue()); deviceLogEntity.setLogType(LogTypeEnum.上报事件.getValue());
deviceLogEntity.setCreateUserId(1L); deviceLogEntity.setCreateUserId(1L);
deviceLogEntity.setCreateTime(new Date()); deviceLogEntity.setCreateTime(new Date());
...@@ -199,7 +195,7 @@ public class DeviceMsgComsumerStartedService implements IApplicationStartedServi ...@@ -199,7 +195,7 @@ public class DeviceMsgComsumerStartedService implements IApplicationStartedServi
if (platformEntity.getSendMsgType() == SendMsgTypeEnum.http.getValue() && !ObjectUtils.isEmpty(platformEntity.getSendUrl())) { if (platformEntity.getSendMsgType() == SendMsgTypeEnum.http.getValue() && !ObjectUtils.isEmpty(platformEntity.getSendUrl())) {
//http方式 //http方式
//通过线程池进行发送消息 //通过线程池进行发送消息
SendTask sendTask = new SendTask(platformEntity.getSendUrl(), new String(queueMsg.getData())); SendTask sendTask = new SendTask(platformEntity.getSendUrl(),queueMsg.getData());
sendTaskThreadPool.execute(sendTask); sendTaskThreadPool.execute(sendTask);
} }
} }
......
...@@ -7,6 +7,7 @@ import com.mortals.xhx.module.device.dao.DeviceDao; ...@@ -7,6 +7,7 @@ import com.mortals.xhx.module.device.dao.DeviceDao;
import com.mortals.xhx.module.device.model.DeviceEntity; import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.platform.model.PlatformEntity; import com.mortals.xhx.module.platform.model.PlatformEntity;
import com.mortals.xhx.module.product.model.ProductEntity; import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.queue.TbQueueCallback;
import com.mortals.xhx.queue.TbQueueMsgHeaders; import com.mortals.xhx.queue.TbQueueMsgHeaders;
import com.mortals.xhx.queue.TopicPartitionInfo; import com.mortals.xhx.queue.TopicPartitionInfo;
...@@ -23,7 +24,7 @@ import java.util.List; ...@@ -23,7 +24,7 @@ import java.util.List;
public interface DeviceService extends ICRUDCacheService<DeviceEntity,Long>{ public interface DeviceService extends ICRUDCacheService<DeviceEntity,Long>{
ApiResp<String> sendDeviceMessage(DeviceEntity deviceEntity, TopicPartitionInfo info, TbQueueMsgHeaders header,String message , Context context); ApiResp<String> sendDeviceMessage(DeviceEntity deviceEntity, TopicPartitionInfo info, TbQueueMsgHeaders header, String message, TbQueueCallback callback , Context context);
ApiResp<String> sendDeviceMessage(List<Long> deviceIds, TopicPartitionInfo info,TbQueueMsgHeaders header, String message , Context context); ApiResp<String> sendDeviceMessage(List<Long> deviceIds, TopicPartitionInfo info,TbQueueMsgHeaders header, String message , Context context);
......
package com.mortals.xhx.module.device.service.impl; package com.mortals.xhx.module.device.service.impl;
import cn.hutool.core.codec.Base64;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl; import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.device.dao.DeviceLogDao; import com.mortals.xhx.module.device.dao.DeviceLogDao;
import com.mortals.xhx.module.device.model.DeviceLogEntity; import com.mortals.xhx.module.device.model.DeviceLogEntity;
import com.mortals.xhx.module.device.service.DeviceLogService; import com.mortals.xhx.module.device.service.DeviceLogService;
import java.util.List;
/** /**
* DeviceLogService * DeviceLogService
* 设备日志 service实现 * 设备日志 service实现
...@@ -13,5 +20,12 @@ import com.mortals.xhx.module.device.service.DeviceLogService; ...@@ -13,5 +20,12 @@ import com.mortals.xhx.module.device.service.DeviceLogService;
*/ */
@Service("deviceLogService") @Service("deviceLogService")
public class DeviceLogServiceImpl extends AbstractCRUDServiceImpl<DeviceLogDao, DeviceLogEntity, Long> implements DeviceLogService { public class DeviceLogServiceImpl extends AbstractCRUDServiceImpl<DeviceLogDao, DeviceLogEntity, Long> implements DeviceLogService {
@Override
protected void findAfter(DeviceLogEntity params, PageInfo pageInfo, Context context, List<DeviceLogEntity> list) throws AppException {
list.stream().forEach(item->{
item.setContent(Base64.decodeStr(item.getContent()));
});
}
} }
\ No newline at end of file
...@@ -130,21 +130,22 @@ public class DeviceServiceImpl extends AbstractCRUDCacheServiceImpl<DeviceDao, D ...@@ -130,21 +130,22 @@ public class DeviceServiceImpl extends AbstractCRUDCacheServiceImpl<DeviceDao, D
} }
@Override @Override
public ApiResp<String> sendDeviceMessage(DeviceEntity deviceEntity, TopicPartitionInfo info, TbQueueMsgHeaders header, String message, Context context) { public ApiResp<String> sendDeviceMessage(DeviceEntity deviceEntity, TopicPartitionInfo info, TbQueueMsgHeaders header, String message,TbQueueCallback callback, Context context) {
ApiResp<String> resp = new ApiResp<>(); ApiResp<String> resp = new ApiResp<>();
resp.setCode(ApiRespCodeEnum.SUCCESS.getValue()); resp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
resp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel()); resp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
TbQueueCallback callback = new TbQueueCallback() { // TbQueueCallback callback = new TbQueueCallback() {
@Override // @Override
public void onSuccess(TbQueueMsgMetadata metadata) { // public void onSuccess(TbQueueMsgMetadata metadata) {
log.info("消息投递成功,设备通道编码:" + deviceEntity.getDeviceMac()); //
} // log.info("消息投递成功,设备通道编码:" + deviceEntity.getDeviceMac());
// }
@Override //
public void onFailure(Throwable t) { // @Override
log.error("消息投递成功,设备通道编码:" + deviceEntity.getDeviceMac(), t); // public void onFailure(Throwable t) {
} // log.error("消息投递成功,设备通道编码:" + deviceEntity.getDeviceMac(), t);
}; // }
// };
messageService.send(info, header, message, callback); messageService.send(info, header, message, callback);
return resp; return resp;
} }
...@@ -156,7 +157,7 @@ public class DeviceServiceImpl extends AbstractCRUDCacheServiceImpl<DeviceDao, D ...@@ -156,7 +157,7 @@ public class DeviceServiceImpl extends AbstractCRUDCacheServiceImpl<DeviceDao, D
deviceQuery.setIdList(deviceIds); deviceQuery.setIdList(deviceIds);
List<DeviceEntity> deviceEntityList = this.find(deviceQuery); List<DeviceEntity> deviceEntityList = this.find(deviceQuery);
for (DeviceEntity deviceEntity : deviceEntityList) { for (DeviceEntity deviceEntity : deviceEntityList) {
resp = sendDeviceMessage(deviceEntity, info, header, message, context); resp = sendDeviceMessage(deviceEntity, info, header, message,null, context);
} }
return resp; return resp;
} }
......
...@@ -102,7 +102,7 @@ public class DeviceController extends BaseCRUDJsonBodyMappingController<DeviceSe ...@@ -102,7 +102,7 @@ public class DeviceController extends BaseCRUDJsonBodyMappingController<DeviceSe
/** /**
* 下发信息 * 下发信息
*/ */
@PostMapping(value = "downMsg") /* @PostMapping(value = "downMsg")
public String downMsg(@RequestParam(value = "deviceId") Long deviceId, @RequestParam(value = "content") String content) { public String downMsg(@RequestParam(value = "deviceId") Long deviceId, @RequestParam(value = "content") String content) {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
Map<String, Object> model = new HashMap<>(); Map<String, Object> model = new HashMap<>();
...@@ -128,6 +128,8 @@ public class DeviceController extends BaseCRUDJsonBodyMappingController<DeviceSe ...@@ -128,6 +128,8 @@ public class DeviceController extends BaseCRUDJsonBodyMappingController<DeviceSe
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("content", content); obj.put("content", content);
ApiResp<String> sendDeviceMessageResp = this.service.sendDeviceMessage(deviceEntity, info, header, JSON.toJSONString(obj), null); ApiResp<String> sendDeviceMessageResp = this.service.sendDeviceMessage(deviceEntity, info, header, JSON.toJSONString(obj), null);
log.info(String.format("sendMsgResp:%s", JSON.toJSONString(sendDeviceMessageResp))); log.info(String.format("sendMsgResp:%s", JSON.toJSONString(sendDeviceMessageResp)));
if (ApiRespCodeEnum.SUCCESS.getValue() != sendDeviceMessageResp.getCode()) { if (ApiRespCodeEnum.SUCCESS.getValue() != sendDeviceMessageResp.getCode()) {
...@@ -150,7 +152,7 @@ public class DeviceController extends BaseCRUDJsonBodyMappingController<DeviceSe ...@@ -150,7 +152,7 @@ public class DeviceController extends BaseCRUDJsonBodyMappingController<DeviceSe
jsonObject.put(KEY_RESULT_MSG, super.convertException(e)); jsonObject.put(KEY_RESULT_MSG, super.convertException(e));
} }
return jsonObject.toJSONString(); return jsonObject.toJSONString();
} }*/
/** /**
......
package com.mortals.xhx.module.device.web; package com.mortals.xhx.module.device.web;
import cn.hutool.core.codec.Base64;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context; import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController; import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService; import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.device.model.DeviceLogEntity; import com.mortals.xhx.module.device.model.DeviceLogEntity;
...@@ -9,30 +12,45 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -9,30 +12,45 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* * 设备日志
* 设备日志 *
* * @author zxfei
* @author zxfei * @date 2022-06-27
* @date 2022-06-27 */
*/
@RestController @RestController
@RequestMapping("device/log") @RequestMapping("device/log")
public class DeviceLogController extends BaseCRUDJsonBodyMappingController<DeviceLogService,DeviceLogEntity,Long> { public class DeviceLogController extends BaseCRUDJsonBodyMappingController<DeviceLogService, DeviceLogEntity, Long> {
@Autowired @Autowired
private ParamService paramService; private ParamService paramService;
public DeviceLogController(){ public DeviceLogController() {
super.setModuleDesc( "设备日志"); super.setModuleDesc("设备日志");
} }
@Override @Override
protected void init(Map<String, Object> model, Context context) { protected void init(Map<String, Object> model, Context context) {
this.addDict(model, "logType", paramService.getParamBySecondOrganize("DeviceLog","logType")); this.addDict(model, "logType", paramService.getParamBySecondOrganize("DeviceLog", "logType"));
super.init(model, context); super.init(model, context);
} }
@Override
protected void doListBefore(DeviceLogEntity query, Map<String, Object> model, Context context) throws AppException {
List<OrderCol> orderColList = new ArrayList<>();
orderColList.add(new OrderCol("createTime", OrderCol.DESCENDING));
query.setOrderColList(orderColList);
super.doListBefore(query, model, context);
}
@Override
protected int viewAfter(Long id, Map<String, Object> model, DeviceLogEntity entity, Context context) throws AppException {
entity.setContent(Base64.decodeStr(entity.getContent()));
return super.viewAfter(id, model, entity, context);
}
} }
\ No newline at end of file
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