yeg
6 天以前 e38aca8134570513435c003c25dc05f1ae51c9c9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.battery.business.web.device;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.exception.ServiceException;
import com.battery.business.service.DeviceAdapterErrorCodes;
import com.battery.business.service.DeviceIngestErrorCodes;
import com.battery.business.service.IDeviceIngestService;
import com.battery.business.service.IDeviceIngestService.IngestResult;
 
/**
 * 推式来源的接收端点(ADR-0065):甲方机器上的只读取数程序把厂商原始行推到这里。
 *
 * <p>端点匿名放行但自己校验来源凭据(令牌随请求头来):取数程序不带登录态,也不能带。
 * 拒绝一律用非 2xx 状态码——程序只在收到 2xx 后才推进自己的读取位置,
 * 所以"响应 200 但体内是错误码"是不能接受的:那会让程序把没落库的数据当成已送达。
 */
@Anonymous
@RestController
@RequestMapping("/device-ingest")
public class DeviceIngestController
{
    /** 来源凭据的请求头名;每个推式来源实例各有一个令牌。 */
    public static final String TOKEN_HEADER = "X-Device-Ingest-Token";
 
    private final IDeviceIngestService ingest;
 
    @Autowired
    public DeviceIngestController(IDeviceIngestService ingest)
    {
        this.ingest = ingest;
    }
 
    @PostMapping("/{adapterCode}")
    public ResponseEntity<?> receive(@PathVariable String adapterCode,
            @RequestHeader(value = TOKEN_HEADER, required = false) String token,
            @RequestBody(required = false) String body)
    {
        try
        {
            IngestResult result = ingest.ingest(adapterCode, token, body);
            return ResponseEntity.ok(result);
        }
        catch (ServiceException failure)
        {
            return ResponseEntity.status(httpStatus(failure.getCode()))
                    .body(new IngestFailure(failure.getCode(), failure.getMessage()));
        }
    }
 
    /** 业务错误码到 HTTP 状态的映射:只影响取数程序"是否推进水位"这一个判断。 */
    private static HttpStatus httpStatus(Integer code)
    {
        if (code == null) return HttpStatus.INTERNAL_SERVER_ERROR;
        return switch (code)
        {
            case DeviceAdapterErrorCodes.NOT_FOUND -> HttpStatus.NOT_FOUND;
            case DeviceIngestErrorCodes.TOKEN_INVALID -> HttpStatus.UNAUTHORIZED;
            case DeviceAdapterErrorCodes.UNSUPPORTED_TYPE, DeviceIngestErrorCodes.SOURCE_DISABLED -> HttpStatus.FORBIDDEN;
            case DeviceIngestErrorCodes.PAYLOAD_INVALID -> HttpStatus.BAD_REQUEST;
            default -> HttpStatus.INTERNAL_SERVER_ERROR;
        };
    }
 
    /** 拒绝应答:取数程序据此记录原因,并保持自己的读取位置不动。 */
    public record IngestFailure(Integer code, String message) { }
}