diff --git a/common/src/main/java/cn/skcks/docking/gb28181/common/json/JsonResponse.java b/common/src/main/java/cn/skcks/docking/gb28181/common/json/JsonResponse.java index dce3dc6..0839dea 100644 --- a/common/src/main/java/cn/skcks/docking/gb28181/common/json/JsonResponse.java +++ b/common/src/main/java/cn/skcks/docking/gb28181/common/json/JsonResponse.java @@ -1,10 +1,12 @@ package cn.skcks.docking.gb28181.common.json; import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @NoArgsConstructor +@AllArgsConstructor @Data @SuppressWarnings("unused") @Schema(title = "返回结果") @@ -12,52 +14,50 @@ public class JsonResponse { @Schema(title = "状态码") private int code; - @Schema(title = "响应消息") - private String msg; - @Schema(title = "响应数据") private T data; - public JsonResponse(int code, String msg, T data) { - this.code = code; - this.msg = msg; - this.data = data; - } + @Schema(title = "响应消息") + private String msg; public static JsonResponse success(T data) { return JsonResponse.build(data, ResponseStatus.OK); } public static JsonResponse success(T data, String message) { - return JsonResponse.build(data, ResponseStatus.OK.getCode(), message); + return JsonResponse.build(ResponseStatus.OK.getCode(), data, message); } - public static JsonResponse error(T data) { - return JsonResponse.build(data, ResponseStatus.INTERNAL_SERVER_ERROR); + public static JsonResponse error(String message) { + return JsonResponse.build(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(), null, message); } public static JsonResponse error(T data, String message) { - return JsonResponse.build(data, ResponseStatus.INTERNAL_SERVER_ERROR.getCode(), message); + return JsonResponse.build(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(), data, message); } public static JsonResponse build(ResponseStatus status) { - return new JsonResponse<>(status.getCode(), status.getMessage(),null); + return new JsonResponse<>(status.getCode(), null, status.getMessage()); } public static JsonResponse build(T data, ResponseStatus status) { - return new JsonResponse<>(status.getCode(), status.getMessage(), data); + return new JsonResponse<>(status.getCode(), data, status.getMessage()); } - public static JsonResponse build(ResponseStatus status,String message) { - return new JsonResponse<>(status.getCode(), message, null); + public static JsonResponse build(ResponseStatus status, String message) { + return new JsonResponse<>(status.getCode(), null, message); } - public static JsonResponse build(T data, int status, String msg) { - return new JsonResponse<>(status, msg, data); + public static JsonResponse build(ResponseStatus status, T data, String message) { + return new JsonResponse<>(status.getCode(), data, message); + } + + public static JsonResponse build(int status, T data, String msg) { + return new JsonResponse<>(status, data, msg); } @Override - public String toString(){ + public String toString() { return JsonUtils.toJson(this); } } diff --git a/common/src/main/java/cn/skcks/docking/gb28181/common/json/ResponseStatus.java b/common/src/main/java/cn/skcks/docking/gb28181/common/json/ResponseStatus.java index 8465d00..36ace29 100644 --- a/common/src/main/java/cn/skcks/docking/gb28181/common/json/ResponseStatus.java +++ b/common/src/main/java/cn/skcks/docking/gb28181/common/json/ResponseStatus.java @@ -13,6 +13,7 @@ public enum ResponseStatus { CREATED(201, "Created"), ACCEPTED(202, "Accepted"), NO_CONTENT(204, "No Content"), + PARTIAL_CONTENT(206, "Partial Content"), MOVED_PERMANENTLY(301, "Moved Permanently"), FOUND(302, "Found"), SEE_OTHER(303, "See Other"), diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/MessageHelper.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/MessageHelper.java new file mode 100644 index 0000000..d51023c --- /dev/null +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/MessageHelper.java @@ -0,0 +1,53 @@ +package cn.skcks.docking.gb28181.core.sip.message; + +import lombok.SneakyThrows; + +import javax.sip.SipFactory; +import javax.sip.address.Address; +import javax.sip.address.AddressFactory; +import javax.sip.address.SipURI; +import javax.sip.header.FromHeader; +import javax.sip.header.HeaderFactory; +import javax.sip.header.MaxForwardsHeader; +import javax.sip.header.ToHeader; + +public class MessageHelper { + public static SipFactory getSipFactory() { + return SipFactory.getInstance(); + } + + @SneakyThrows + public static AddressFactory createAddressFactory() { + return getSipFactory().createAddressFactory(); + } + + @SneakyThrows + public static HeaderFactory createHeaderFactory() { + return getSipFactory().createHeaderFactory(); + } + + @SneakyThrows + public static SipURI createSipURI(String id, String address) { + return createAddressFactory().createSipURI(id, address); + } + + @SneakyThrows + public static Address createAddress(SipURI uri) { + return createAddressFactory().createAddress(uri); + } + + @SneakyThrows + public static FromHeader createFromHeader(Address fromAddress, String fromTag) { + return createHeaderFactory().createFromHeader(fromAddress, fromTag); + } + + @SneakyThrows + public static ToHeader createToHeader(Address toAddress, String toTag) { + return createHeaderFactory().createToHeader(toAddress, toTag); + } + + @SneakyThrows + public static MaxForwardsHeader createMaxForwardsHeader(int maxForwards) { + return createHeaderFactory().createMaxForwardsHeader(maxForwards); + } +} diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/request/SipRequestBuilder.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/request/SipRequestBuilder.java index 5a006af..2639753 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/request/SipRequestBuilder.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/request/SipRequestBuilder.java @@ -14,6 +14,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Component; +import cn.skcks.docking.gb28181.core.sip.message.MessageHelper; import javax.sip.InvalidArgumentException; import javax.sip.PeerUnavailableException; @@ -39,22 +40,22 @@ public class SipRequestBuilder implements ApplicationContextAware { @SneakyThrows private static SipURI getSipURI(String id,String address){ - return getSipFactory().createAddressFactory().createSipURI(id, address); + return MessageHelper.createSipURI(id, address); } @SneakyThrows private static Address getAddress(SipURI uri){ - return getSipFactory().createAddressFactory().createAddress(uri); + return MessageHelper.createAddress(uri); } @SneakyThrows private static FromHeader getFromHeader(Address fromAddress,String fromTag){ - return getSipFactory().createHeaderFactory().createFromHeader(fromAddress, fromTag); + return MessageHelper.createFromHeader(fromAddress, fromTag); } @SneakyThrows private static ToHeader getToHeader(Address toAddress,String toTag){ - return getSipFactory().createHeaderFactory().createToHeader(toAddress, toTag); + return MessageHelper.createToHeader(toAddress, toTag); } @SneakyThrows @@ -77,11 +78,11 @@ public class SipRequestBuilder implements ApplicationContextAware { List viaHeaders = getDeviceViaHeaders(device, viaTag); // from SipURI fromSipURI = getSipURI(sipConfig.getId(), sipConfig.getDomain()); - Address fromAddress = getAddress(fromSipURI); + Address fromAddress = MessageHelper.createAddress(fromSipURI); FromHeader fromHeader = getFromHeader(fromAddress, fromTag); // to SipURI toSipURI = getSipURI(device.getDeviceId(), device.getHostAddress()); - Address toAddress = getAddress(toSipURI); + Address toAddress = MessageHelper.createAddress(toSipURI); ToHeader toHeader = getToHeader(toAddress, toTag); // Forwards @@ -108,11 +109,11 @@ public class SipRequestBuilder implements ApplicationContextAware { // from SipURI fromSipURI = getSipURI(sipConfig.getId(), sipConfig.getDomain()); - Address fromAddress = getAddress(fromSipURI); + Address fromAddress = MessageHelper.createAddress(fromSipURI); FromHeader fromHeader = getFromHeader(fromAddress, fromTag); // 必须要有标记,否则无法创建会话,无法回应ack // to SipURI toSipURI = getSipURI(channelId, device.getHostAddress()); - Address toAddress = getAddress(toSipURI); + Address toAddress = MessageHelper.createAddress(toSipURI); ToHeader toHeader = getToHeader(toAddress, null); // Forwards @@ -124,8 +125,8 @@ public class SipRequestBuilder implements ApplicationContextAware { request.addHeader(SipUtil.createUserAgentHeader()); - Address concatAddress = getAddress(getSipURI(sipConfig.getId(), device.getLocalIp() + ":" + sipConfig.getPort())); - // Address concatAddress = getAddress(getSipURI(sipConfig.getId(), device.getHost().getIp()+":"+device.getHost().getPort())); + Address concatAddress = MessageHelper.createAddress(getSipURI(sipConfig.getId(), device.getLocalIp() + ":" + sipConfig.getPort())); + // Address concatAddress = MessageHelper.createAddress(getSipURI(sipConfig.getId(), device.getHost().getIp()+":"+device.getHost().getPort())); request.addHeader(getSipFactory().createHeaderFactory().createContactHeader(concatAddress)); // Subject SubjectHeader subjectHeader = getSipFactory().createHeaderFactory().createSubjectHeader(String.format("%s:%s,%s:%s", channelId, ssrc, sipConfig.getId(), 0)); @@ -146,11 +147,11 @@ public class SipRequestBuilder implements ApplicationContextAware { viaHeaders.add(viaHeader); // from SipURI fromSipURI = getSipURI(sipConfig.getId(), sipConfig.getDomain()); - Address fromAddress = getAddress(fromSipURI); + Address fromAddress = MessageHelper.createAddress(fromSipURI); FromHeader fromHeader = getFromHeader(fromAddress, fromTag); // 必须要有标记,否则无法创建会话,无法回应ack // to SipURI toSipURI = getSipURI(channelId, device.getHostAddress()); - Address toAddress = getAddress(toSipURI); + Address toAddress = MessageHelper.createAddress(toSipURI); ToHeader toHeader = getToHeader(toAddress, null); // Forwards @@ -160,8 +161,8 @@ public class SipRequestBuilder implements ApplicationContextAware { CSeqHeader cSeqHeader = getSipFactory().createHeaderFactory().createCSeqHeader(getCSeq(), Request.INVITE); request = getSipFactory().createMessageFactory().createRequest(requestLine, Request.INVITE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards); - Address concatAddress = getAddress(getSipURI(sipConfig.getId(), device.getLocalIp() + ":" + sipConfig.getPort())); - // Address concatAddress = getAddress(getSipURI(sipConfig.getId(), device.getHost().getIp()+":"+device.getHost().getPort())); + Address concatAddress = MessageHelper.createAddress(getSipURI(sipConfig.getId(), device.getLocalIp() + ":" + sipConfig.getPort())); + // Address concatAddress = MessageHelper.createAddress(getSipURI(sipConfig.getId(), device.getHost().getIp()+":"+device.getHost().getPort())); request.addHeader(getSipFactory().createHeaderFactory().createContactHeader(concatAddress)); request.addHeader(SipUtil.createUserAgentHeader()); @@ -185,11 +186,11 @@ public class SipRequestBuilder implements ApplicationContextAware { viaHeaders.add(viaHeader); // from SipURI fromSipURI = getSipURI(sipConfig.getId(), sipConfig.getDomain()); - Address fromAddress = getAddress(fromSipURI); + Address fromAddress = MessageHelper.createAddress(fromSipURI); FromHeader fromHeader = getFromHeader(fromAddress, transactionInfo.getFromTag()); // to SipURI toSipURI = getSipURI(channelId, device.getHostAddress()); - Address toAddress = getAddress(toSipURI); + Address toAddress = MessageHelper.createAddress(toSipURI); ToHeader toHeader = getToHeader(toAddress, transactionInfo.getToTag()); // Forwards @@ -202,7 +203,7 @@ public class SipRequestBuilder implements ApplicationContextAware { request.addHeader(SipUtil.createUserAgentHeader()); - Address concatAddress = getAddress(getSipURI(sipConfig.getId(), device.getLocalIp() + ":" + sipConfig.getPort())); + Address concatAddress = MessageHelper.createAddress(getSipURI(sipConfig.getId(), device.getLocalIp() + ":" + sipConfig.getPort())); request.addHeader(getSipFactory().createHeaderFactory().createContactHeader(concatAddress)); request.addHeader(SipUtil.createUserAgentHeader()); @@ -222,11 +223,11 @@ public class SipRequestBuilder implements ApplicationContextAware { viaHeaders.add(viaHeader); // from SipURI fromSipURI = getSipURI(sipConfig.getId(), sipConfig.getDomain()); - Address fromAddress = getAddress(fromSipURI); + Address fromAddress = MessageHelper.createAddress(fromSipURI); FromHeader fromHeader = getFromHeader(fromAddress, requestOld == null ? SipUtil.generateFromTag() : requestOld.getFromTag()); // to SipURI toSipURI = getSipURI(device.getDeviceId(), device.getHostAddress()); - Address toAddress = getAddress(toSipURI); + Address toAddress = MessageHelper.createAddress(toSipURI); ToHeader toHeader = getToHeader(toAddress, requestOld == null ? null : requestOld.getToTag()); // Forwards @@ -239,7 +240,7 @@ public class SipRequestBuilder implements ApplicationContextAware { toHeader, viaHeaders, maxForwards); - Address concatAddress = getAddress(getSipURI(sipConfig.getId(), device.getLocalIp() + ":" + sipConfig.getPort())); + Address concatAddress = MessageHelper.createAddress(getSipURI(sipConfig.getId(), device.getLocalIp() + ":" + sipConfig.getPort())); request.addHeader(getSipFactory().createHeaderFactory().createContactHeader(concatAddress)); // Expires @@ -275,11 +276,11 @@ public class SipRequestBuilder implements ApplicationContextAware { viaHeaders.add(viaHeader); // from SipURI fromSipURI = getSipURI(sipConfig.getId(), sipConfig.getDomain()); - Address fromAddress = getAddress(fromSipURI); + Address fromAddress = MessageHelper.createAddress(fromSipURI); FromHeader fromHeader = getFromHeader(fromAddress, transactionInfo.getFromTag()); // to SipURI toSipURI = getSipURI(channelId, device.getHostAddress()); - Address toAddress = getAddress(toSipURI); + Address toAddress = MessageHelper.createAddress(toSipURI); ToHeader toHeader = getToHeader(toAddress, transactionInfo.getToTag()); // Forwards @@ -292,7 +293,7 @@ public class SipRequestBuilder implements ApplicationContextAware { request.addHeader(SipUtil.createUserAgentHeader()); - Address concatAddress = getAddress(getSipURI(sipConfig.getId(), device.getLocalIp() + ":" + sipConfig.getPort())); + Address concatAddress = MessageHelper.createAddress(getSipURI(sipConfig.getId(), device.getLocalIp() + ":" + sipConfig.getPort())); request.addHeader(getSipFactory().createHeaderFactory().createContactHeader(concatAddress)); request.addHeader(SipUtil.createUserAgentHeader()); @@ -323,7 +324,7 @@ public class SipRequestBuilder implements ApplicationContextAware { request.addHeader(SipUtil.createUserAgentHeader()); - Address concatAddress = getAddress(getSipURI(sipConfig.getId(), localIp + ":" + sipConfig.getPort())); + Address concatAddress = MessageHelper.createAddress(getSipURI(sipConfig.getId(), localIp + ":" + sipConfig.getPort())); request.addHeader(getSipFactory().createHeaderFactory().createContactHeader(concatAddress)); request.addHeader(SipUtil.createUserAgentHeader()); diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/record/RecordService.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/record/RecordService.java index 9286b9a..e0761b5 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/record/RecordService.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/record/RecordService.java @@ -2,6 +2,7 @@ package cn.skcks.docking.gb28181.service.record; import cn.hutool.core.date.DateUtil; import cn.skcks.docking.gb28181.common.json.JsonResponse; +import cn.skcks.docking.gb28181.common.json.ResponseStatus; import cn.skcks.docking.gb28181.common.xml.XmlUtils; import cn.skcks.docking.gb28181.core.sip.gb28181.constant.CmdType; import cn.skcks.docking.gb28181.core.sip.message.processor.message.types.recordinfo.query.dto.RecordInfoRequestDTO; @@ -58,8 +59,8 @@ public class RecordService { String sn = String.valueOf((int) (Math.random() * 9 + 1) * 100000); RecordInfoRequestDTO dto = RecordInfoRequestDTO.builder() .deviceId(deviceId) - .startTime(DateUtil.beginOfDay(DateUtil.date())) - .endTime(DateUtil.endOfDay(DateUtil.date())) + .startTime(DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.date(),-1))) + .endTime(DateUtil.endOfDay(DateUtil.offsetDay(DateUtil.date(),-1))) .sn(sn) .build(); Request request = SipRequestBuilder.createMessageRequest(device, @@ -122,7 +123,7 @@ public class RecordService { subscribe.getRecordInfoSubscribe().addSubscribe(key, subscriber); result.onTimeout(()->{ - result.setResult(JsonResponse.success(sortedRecordList(list),"查询超时, 结果可能不完整")); + result.setResult(JsonResponse.build(ResponseStatus.PARTIAL_CONTENT, sortedRecordList(list),"查询超时, 结果可能不完整")); subscribe.getRecordInfoSubscribe().delPublisher(key); }); diff --git a/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/dto/response/ZlmResponseConvertor.java b/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/dto/response/ZlmResponseConvertor.java index c79e1d3..b991eb7 100644 --- a/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/dto/response/ZlmResponseConvertor.java +++ b/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/dto/response/ZlmResponseConvertor.java @@ -10,7 +10,7 @@ public interface ZlmResponseConvertor { ZlmResponseConvertor INSTANCE = Mappers.getMapper(ZlmResponseConvertor.class); default JsonResponse toJsonResponse(ZlmResponse response){ - return JsonResponse.build(response.getData(), response.getCode().getCode(), response.getMsg()); + return JsonResponse.build(response.getCode().getCode(), response.getData(), response.getMsg()); } default ZlmResponse toZlmResponse(JsonResponse response){ return new ZlmResponse<>(ResponseStatus.fromCode(response.getCode()), response.getData(), response.getMsg());