Commit 020ea372e56d4c865d5ef2958d44d14d9cacf7e8
1 parent
009374ed
fea: 测试接口添加
Showing
35 changed files
with
1429 additions
and
180 deletions
pom.xml
@@ -6,7 +6,7 @@ | @@ -6,7 +6,7 @@ | ||
6 | 6 | ||
7 | <groupId>com.objecteye</groupId> | 7 | <groupId>com.objecteye</groupId> |
8 | <artifactId>viid-collector</artifactId> | 8 | <artifactId>viid-collector</artifactId> |
9 | - <version>1.0-SNAPSHOT</version> | 9 | + <version>1.0</version> |
10 | 10 | ||
11 | <properties> | 11 | <properties> |
12 | <maven.compiler.source>8</maven.compiler.source> | 12 | <maven.compiler.source>8</maven.compiler.source> |
@@ -49,14 +49,6 @@ | @@ -49,14 +49,6 @@ | ||
49 | <artifactId>spring-boot-starter-web</artifactId> | 49 | <artifactId>spring-boot-starter-web</artifactId> |
50 | </dependency> | 50 | </dependency> |
51 | <dependency> | 51 | <dependency> |
52 | - <groupId>com.alibaba.cloud</groupId> | ||
53 | - <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> | ||
54 | - </dependency> | ||
55 | - <dependency> | ||
56 | - <groupId>com.alibaba.cloud</groupId> | ||
57 | - <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> | ||
58 | - </dependency> | ||
59 | - <dependency> | ||
60 | <groupId>org.projectlombok</groupId> | 52 | <groupId>org.projectlombok</groupId> |
61 | <artifactId>lombok</artifactId> | 53 | <artifactId>lombok</artifactId> |
62 | </dependency> | 54 | </dependency> |
@@ -72,5 +64,41 @@ | @@ -72,5 +64,41 @@ | ||
72 | <groupId>org.apache.commons</groupId> | 64 | <groupId>org.apache.commons</groupId> |
73 | <artifactId>commons-pool2</artifactId> | 65 | <artifactId>commons-pool2</artifactId> |
74 | </dependency> | 66 | </dependency> |
67 | + <dependency> | ||
68 | + <groupId>org.apache.commons</groupId> | ||
69 | + <artifactId>commons-lang3</artifactId> | ||
70 | + </dependency> | ||
71 | + <dependency> | ||
72 | + <groupId>commons-codec</groupId> | ||
73 | + <artifactId>commons-codec</artifactId> | ||
74 | + </dependency> | ||
75 | + <dependency> | ||
76 | + <groupId>org.springframework.boot</groupId> | ||
77 | + <artifactId>spring-boot-starter-validation</artifactId> | ||
78 | + </dependency> | ||
79 | + <dependency> | ||
80 | + <groupId>org.apache.httpcomponents</groupId> | ||
81 | + <artifactId>httpclient</artifactId> | ||
82 | + </dependency> | ||
75 | </dependencies> | 83 | </dependencies> |
84 | + | ||
85 | + <build> | ||
86 | + <plugins> | ||
87 | + <plugin> | ||
88 | + <groupId>org.springframework.boot</groupId> | ||
89 | + <artifactId>spring-boot-maven-plugin</artifactId> | ||
90 | + <configuration> | ||
91 | + <finalName>viid-collector</finalName> | ||
92 | + <mainClass>com.objecteye.VIIDCollectorApplication</mainClass> | ||
93 | + </configuration> | ||
94 | + <executions> | ||
95 | + <execution> | ||
96 | + <goals> | ||
97 | + <goal>repackage</goal> | ||
98 | + </goals> | ||
99 | + </execution> | ||
100 | + </executions> | ||
101 | + </plugin> | ||
102 | + </plugins> | ||
103 | + </build> | ||
76 | </project> | 104 | </project> |
77 | \ No newline at end of file | 105 | \ No newline at end of file |
src/main/java/com/objecteye/config/ApplicationConfigurationProperties.java renamed to src/main/java/com/objecteye/config/Gat1400Properties.java
@@ -2,16 +2,22 @@ package com.objecteye.config; | @@ -2,16 +2,22 @@ package com.objecteye.config; | ||
2 | 2 | ||
3 | import lombok.Data; | 3 | import lombok.Data; |
4 | import org.springframework.boot.context.properties.ConfigurationProperties; | 4 | import org.springframework.boot.context.properties.ConfigurationProperties; |
5 | -import org.springframework.stereotype.Component; | 5 | +import org.springframework.context.annotation.Configuration; |
6 | 6 | ||
7 | /** | 7 | /** |
8 | * @author: liuhaoyu | 8 | * @author: liuhaoyu |
9 | - * @date: 2023/6/28 | 9 | + * @date: 2023/7/5 |
10 | */ | 10 | */ |
11 | @Data | 11 | @Data |
12 | -@Component | ||
13 | -@ConfigurationProperties(prefix = "config.application") | ||
14 | -public class ApplicationConfigurationProperties { | 12 | +@Configuration |
13 | +@ConfigurationProperties(prefix = "gat1400") | ||
14 | +public class Gat1400Properties { | ||
15 | 15 | ||
16 | - private String platformCode; | 16 | + private String serverAddress; |
17 | + | ||
18 | + private Integer port; | ||
19 | + | ||
20 | + private String accessKey; | ||
21 | + | ||
22 | + private String securityKey; | ||
17 | } | 23 | } |
src/main/java/com/objecteye/config/ObjectMapperConfigure.java deleted
1 | -package com.objecteye.config; | ||
2 | - | ||
3 | -import com.fasterxml.jackson.databind.ObjectMapper; | ||
4 | -import lombok.Data; | ||
5 | -import org.springframework.context.annotation.Bean; | ||
6 | - | ||
7 | -/** | ||
8 | - * @author: liuhaoyu | ||
9 | - * @date: 2023/6/28 | ||
10 | - */ | ||
11 | -@Data | ||
12 | -public class ObjectMapperConfigure { | ||
13 | - | ||
14 | - @Bean | ||
15 | - public ObjectMapper objectMapper() { | ||
16 | - return new ObjectMapper(); | ||
17 | - } | ||
18 | -} |
src/main/java/com/objecteye/content/VIIDApi.java
@@ -6,7 +6,8 @@ package com.objecteye.content; | @@ -6,7 +6,8 @@ package com.objecteye.content; | ||
6 | */ | 6 | */ |
7 | public class VIIDApi { | 7 | public class VIIDApi { |
8 | 8 | ||
9 | - public static final String SYSTEM_PREFIX = "/VIID/System"; | 9 | + private static final String VIID = "/VIID"; |
10 | + public static final String SYSTEM_PREFIX = VIID + "/System"; | ||
10 | /** | 11 | /** |
11 | * 注册 | 12 | * 注册 |
12 | */ | 13 | */ |
@@ -19,6 +20,12 @@ public class VIIDApi { | @@ -19,6 +20,12 @@ public class VIIDApi { | ||
19 | * 保活 | 20 | * 保活 |
20 | */ | 21 | */ |
21 | public static final String KEEPALIVE = SYSTEM_PREFIX + "/Keepalive"; | 22 | public static final String KEEPALIVE = SYSTEM_PREFIX + "/Keepalive"; |
23 | + /** | ||
24 | + * 校时 | ||
25 | + */ | ||
26 | + public static final String TIME = SYSTEM_PREFIX + "/Time"; | ||
27 | + | ||
28 | + public static final String SUBSCRIBES = VIID + "/Subscribes"; | ||
22 | 29 | ||
23 | private VIIDApi() { | 30 | private VIIDApi() { |
24 | } | 31 | } |
src/main/java/com/objecteye/content/VIIDContent.java
0 → 100644
src/main/java/com/objecteye/content/ViidCollectorRedisKey.java
@@ -6,7 +6,7 @@ package com.objecteye.content; | @@ -6,7 +6,7 @@ package com.objecteye.content; | ||
6 | */ | 6 | */ |
7 | public class ViidCollectorRedisKey { | 7 | public class ViidCollectorRedisKey { |
8 | /** | 8 | /** |
9 | - * 设备连接信息缓存 | 9 | + * 设备订阅信息 |
10 | */ | 10 | */ |
11 | public static final String DEVICE_CACHE = "vc:dc"; | 11 | public static final String DEVICE_CACHE = "vc:dc"; |
12 | 12 |
src/main/java/com/objecteye/controller/SubscribeController.java
0 → 100644
1 | +package com.objecteye.controller; | ||
2 | + | ||
3 | +import com.objecteye.handle.viid.SubscribeHandle; | ||
4 | +import com.objecteye.pojo.DeviceIdParams; | ||
5 | +import com.objecteye.pojo.Result; | ||
6 | +import com.objecteye.pojo.viid.subscribe.SubscribeNotificationRequestObject; | ||
7 | +import com.objecteye.utils.JSONUtils; | ||
8 | +import lombok.RequiredArgsConstructor; | ||
9 | +import lombok.extern.slf4j.Slf4j; | ||
10 | +import org.springframework.validation.annotation.Validated; | ||
11 | +import org.springframework.web.bind.annotation.PostMapping; | ||
12 | +import org.springframework.web.bind.annotation.RequestBody; | ||
13 | +import org.springframework.web.bind.annotation.RestController; | ||
14 | + | ||
15 | +/** | ||
16 | + * @author: liuhaoyu | ||
17 | + * @date: 2023/7/6 | ||
18 | + */ | ||
19 | +@Slf4j | ||
20 | +@RestController | ||
21 | +@RequiredArgsConstructor | ||
22 | +public class SubscribeController { | ||
23 | + private final SubscribeHandle subscribeHandle; | ||
24 | + | ||
25 | + @PostMapping("subscribe") | ||
26 | + public Result<Boolean> subscribe(@RequestBody @Validated DeviceIdParams params) { | ||
27 | + return Result.success(subscribeHandle.subscribe(params.getDeviceId())); | ||
28 | + } | ||
29 | + | ||
30 | + @PostMapping("receiver") | ||
31 | + public Result receiver(@RequestBody SubscribeNotificationRequestObject subscribeNotificationRequestObject) { | ||
32 | + log.info("notification: \n{}", JSONUtils.toPrettyJsonString(subscribeNotificationRequestObject)); | ||
33 | + return Result.success(); | ||
34 | + } | ||
35 | +} |
src/main/java/com/objecteye/controller/SystemController.java
0 → 100644
1 | +package com.objecteye.controller; | ||
2 | + | ||
3 | +import com.objecteye.handle.viid.CommonHandle; | ||
4 | +import com.objecteye.pojo.DeviceIdParams; | ||
5 | +import com.objecteye.pojo.Result; | ||
6 | +import lombok.RequiredArgsConstructor; | ||
7 | +import org.springframework.validation.annotation.Validated; | ||
8 | +import org.springframework.web.bind.annotation.PostMapping; | ||
9 | +import org.springframework.web.bind.annotation.RequestBody; | ||
10 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
11 | +import org.springframework.web.bind.annotation.RestController; | ||
12 | + | ||
13 | +/** | ||
14 | + * @author: liuhaoyu | ||
15 | + * @date: 2023/7/6 | ||
16 | + */ | ||
17 | +@RestController | ||
18 | +@RequestMapping("system") | ||
19 | +@RequiredArgsConstructor | ||
20 | +public class SystemController { | ||
21 | + | ||
22 | + private final CommonHandle commonHandle; | ||
23 | + | ||
24 | + @PostMapping("register") | ||
25 | + public Result<Boolean> register(@RequestBody @Validated DeviceIdParams params) { | ||
26 | + return Result.success(commonHandle.register(params.getDeviceId())); | ||
27 | + } | ||
28 | + | ||
29 | + @PostMapping("unregister") | ||
30 | + public Result<Boolean> unregister(@RequestBody @Validated DeviceIdParams params) { | ||
31 | + return Result.success(commonHandle.unregister(params.getDeviceId())); | ||
32 | + } | ||
33 | + | ||
34 | + @PostMapping("keepalive") | ||
35 | + public Result<Boolean> keepalive(@RequestBody @Validated DeviceIdParams params) { | ||
36 | + return Result.success(commonHandle.keepalive(params.getDeviceId())); | ||
37 | + } | ||
38 | +} |
src/main/java/com/objecteye/handle/StartUpAndShutdown.java
1 | package com.objecteye.handle; | 1 | package com.objecteye.handle; |
2 | 2 | ||
3 | -import com.alibaba.nacos.common.utils.ExceptionUtil; | ||
4 | -import com.fasterxml.jackson.databind.ObjectMapper; | ||
5 | import com.objecteye.content.ViidCollectorRedisKey; | 3 | import com.objecteye.content.ViidCollectorRedisKey; |
6 | -import com.objecteye.handle.viid.VIIDCommonHandle; | ||
7 | -import com.objecteye.pojo.platform.DeviceRequestParams; | 4 | +import com.objecteye.handle.viid.CommonHandle; |
8 | import lombok.RequiredArgsConstructor; | 5 | import lombok.RequiredArgsConstructor; |
9 | -import org.slf4j.Logger; | ||
10 | -import org.slf4j.LoggerFactory; | ||
11 | import org.springframework.beans.factory.DisposableBean; | 6 | import org.springframework.beans.factory.DisposableBean; |
12 | import org.springframework.boot.ApplicationArguments; | 7 | import org.springframework.boot.ApplicationArguments; |
13 | import org.springframework.boot.ApplicationRunner; | 8 | import org.springframework.boot.ApplicationRunner; |
@@ -26,32 +21,25 @@ import java.util.function.Consumer; | @@ -26,32 +21,25 @@ import java.util.function.Consumer; | ||
26 | @Component | 21 | @Component |
27 | @RequiredArgsConstructor | 22 | @RequiredArgsConstructor |
28 | public class StartUpAndShutdown implements DisposableBean, ApplicationRunner { | 23 | public class StartUpAndShutdown implements DisposableBean, ApplicationRunner { |
29 | - private final VIIDCommonHandle viidCommonHandle; | 24 | + private final CommonHandle commonHandle; |
30 | private final StringRedisTemplate stringRedisTemplate; | 25 | private final StringRedisTemplate stringRedisTemplate; |
31 | - private final ObjectMapper objectMapper; | ||
32 | - | ||
33 | - private final Logger logger = LoggerFactory.getLogger(StartUpAndShutdown.class); | ||
34 | 26 | ||
35 | @Override | 27 | @Override |
36 | public void destroy() throws Exception { | 28 | public void destroy() throws Exception { |
37 | - allCacheHandle(viidCommonHandle::unregister); | 29 | + allCacheHandle(commonHandle::unregister); |
38 | } | 30 | } |
39 | 31 | ||
40 | @Override | 32 | @Override |
41 | public void run(ApplicationArguments args) throws Exception { | 33 | public void run(ApplicationArguments args) throws Exception { |
42 | - allCacheHandle(viidCommonHandle::register); | 34 | + allCacheHandle(commonHandle::register); |
43 | } | 35 | } |
44 | 36 | ||
45 | - private void allCacheHandle(Consumer<DeviceRequestParams> func) { | 37 | + private void allCacheHandle(Consumer<String> func) { |
46 | try (Cursor<Map.Entry<Object, Object>> cursor = stringRedisTemplate.boundHashOps(ViidCollectorRedisKey.DEVICE_CACHE) | 38 | try (Cursor<Map.Entry<Object, Object>> cursor = stringRedisTemplate.boundHashOps(ViidCollectorRedisKey.DEVICE_CACHE) |
47 | .scan(ScanOptions.scanOptions().build())) { | 39 | .scan(ScanOptions.scanOptions().build())) { |
48 | while (cursor.hasNext()) { | 40 | while (cursor.hasNext()) { |
49 | Map.Entry<Object, Object> entry = cursor.next(); | 41 | Map.Entry<Object, Object> entry = cursor.next(); |
50 | - try { | ||
51 | - func.accept(objectMapper.readValue(entry.getValue().toString(), DeviceRequestParams.class)); | ||
52 | - } catch (Exception e) { | ||
53 | - logger.error(ExceptionUtil.getAllExceptionMsg(e)); | ||
54 | - } | 42 | + func.accept(entry.getKey().toString()); |
55 | } | 43 | } |
56 | } | 44 | } |
57 | } | 45 | } |
src/main/java/com/objecteye/handle/viid/CommonHandle.java
0 → 100644
1 | +package com.objecteye.handle.viid; | ||
2 | + | ||
3 | +import com.objecteye.config.Gat1400Properties; | ||
4 | +import com.objecteye.content.VIIDApi; | ||
5 | +import com.objecteye.pojo.viid.common.KeepaliveRequestObject; | ||
6 | +import com.objecteye.pojo.viid.common.RegisterObject; | ||
7 | +import com.objecteye.pojo.viid.common.RegisterRequestObject; | ||
8 | +import com.objecteye.pojo.viid.common.UnRegisterRequestObject; | ||
9 | +import com.objecteye.utils.DigestUtils; | ||
10 | +import com.objecteye.utils.JSONUtils; | ||
11 | +import lombok.RequiredArgsConstructor; | ||
12 | +import lombok.extern.slf4j.Slf4j; | ||
13 | +import org.springframework.http.HttpEntity; | ||
14 | +import org.springframework.http.HttpHeaders; | ||
15 | +import org.springframework.http.HttpMethod; | ||
16 | +import org.springframework.http.ResponseEntity; | ||
17 | +import org.springframework.stereotype.Component; | ||
18 | +import org.springframework.web.client.RestTemplate; | ||
19 | + | ||
20 | +/** | ||
21 | + * @author: liuhaoyu | ||
22 | + * @date: 2023/6/28 | ||
23 | + */ | ||
24 | +@Slf4j | ||
25 | +@Component | ||
26 | +@RequiredArgsConstructor | ||
27 | +public class CommonHandle { | ||
28 | + private final RestTemplate restTemplate; | ||
29 | + private final Gat1400Properties gat1400Properties; | ||
30 | + private static final String HTTP = "http://"; | ||
31 | + | ||
32 | + /** | ||
33 | + * 注册 | ||
34 | + * | ||
35 | + * @param deviceId 注册目标设备 | ||
36 | + * @return 注册结果 | ||
37 | + */ | ||
38 | + public boolean register(String deviceId) { | ||
39 | + // 请求参数设置 | ||
40 | + RegisterRequestObject registerRequestObject = new RegisterRequestObject(); | ||
41 | + RegisterObject registerObject = new RegisterObject(); | ||
42 | + registerObject.setDeviceId(deviceId); | ||
43 | + registerRequestObject.setRegisterObject(registerObject); | ||
44 | + return twoStepCheck(VIIDApi.REGISTER, deviceId, JSONUtils.toJsonString(registerRequestObject)); | ||
45 | + } | ||
46 | + | ||
47 | + /** | ||
48 | + * 保活 | ||
49 | + * | ||
50 | + * @param deviceId 心跳目标设备 | ||
51 | + * @return 发送结果 | ||
52 | + */ | ||
53 | + public boolean keepalive(String deviceId) { | ||
54 | + String keepaliveUrl = getAddress() + VIIDApi.KEEPALIVE; | ||
55 | + // 请求参数设置 | ||
56 | + KeepaliveRequestObject keepaliveRequestObject = new KeepaliveRequestObject(); | ||
57 | + RegisterObject keepaliveObject = new RegisterObject(); | ||
58 | + keepaliveObject.setDeviceId(deviceId); | ||
59 | + keepaliveRequestObject.setKeepaliveObject(keepaliveObject); | ||
60 | + if (log.isDebugEnabled()) { | ||
61 | + log.debug("保活请求 url:{} ,参数:{}", keepaliveUrl, keepaliveRequestObject); | ||
62 | + } | ||
63 | + | ||
64 | + HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtils.toJsonString(keepaliveRequestObject), | ||
65 | + buildRequestHttpHeader(deviceId)); | ||
66 | + // 请求执行 | ||
67 | + ResponseEntity<String> responseEntity = postForEntity(keepaliveUrl, httpEntity); | ||
68 | + return org.apache.http.HttpStatus.SC_OK == responseEntity.getStatusCode().value(); | ||
69 | + } | ||
70 | + | ||
71 | + /** | ||
72 | + * 注销 | ||
73 | + * | ||
74 | + * @param deviceId 注销目标设备 | ||
75 | + * @return 注销结果 | ||
76 | + */ | ||
77 | + public boolean unregister(String deviceId) { | ||
78 | + // 请求参数设置 | ||
79 | + UnRegisterRequestObject unRegisterRequestObject = new UnRegisterRequestObject(); | ||
80 | + RegisterObject unRegisterObject = new RegisterObject(); | ||
81 | + unRegisterObject.setDeviceId(deviceId); | ||
82 | + unRegisterRequestObject.setUnRegisterObject(unRegisterObject); | ||
83 | + return twoStepCheck(VIIDApi.UNREGISTER, deviceId, JSONUtils.toJsonString(unRegisterRequestObject)); | ||
84 | + } | ||
85 | + | ||
86 | + private String getAddress() { | ||
87 | + return HTTP + gat1400Properties.getServerAddress() + ":" + gat1400Properties.getPort(); | ||
88 | + } | ||
89 | + | ||
90 | + private HttpHeaders buildRequestHttpHeader(String deviceId) { | ||
91 | + // 请求头设置 | ||
92 | + HttpHeaders headers = new HttpHeaders(); | ||
93 | + headers.add("Content-Type", "application/json;charset=UTF-8"); | ||
94 | + headers.set("User-Identify", deviceId); | ||
95 | + headers.setConnection("keepalive"); | ||
96 | + return headers; | ||
97 | + } | ||
98 | + | ||
99 | + private ResponseEntity<String> postForEntity(String url, HttpEntity<String> httpEntity) { | ||
100 | + return restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class); | ||
101 | + } | ||
102 | + | ||
103 | + private boolean twoStepCheck(String uri, String deviceId, String requestJson) { | ||
104 | + String url = getAddress() + uri; | ||
105 | + // 请求头设置 | ||
106 | + HttpHeaders headers = buildRequestHttpHeader(deviceId); | ||
107 | + // 第一次请求 | ||
108 | + ResponseEntity<String> responseEntity = postForEntity(url, new HttpEntity<>(requestJson, headers)); | ||
109 | + if (org.apache.http.HttpStatus.SC_UNAUTHORIZED == responseEntity.getStatusCode().value()) { | ||
110 | + headers.set("Authorization", getAuthorization(responseEntity, url)); | ||
111 | + // 第二次请求 | ||
112 | + responseEntity = postForEntity(url, new HttpEntity<>(requestJson, headers)); | ||
113 | + return org.apache.http.HttpStatus.SC_OK == responseEntity.getStatusCode().value(); | ||
114 | + } | ||
115 | + return false; | ||
116 | + } | ||
117 | + | ||
118 | + private String getAuthorization(ResponseEntity<String> responseEntity, String url) { | ||
119 | + HttpHeaders responseEntityHeaders = responseEntity.getHeaders(); | ||
120 | + String authenticate = responseEntityHeaders.get("WWW-Authenticate").get(0); | ||
121 | + String[] children = authenticate.split(","); | ||
122 | + String realm = null, qop = null, nonce = null, opaque = null, method = "POST"; | ||
123 | + for (String item : children) { | ||
124 | + String[] itemEntry = item.split("="); | ||
125 | + if ("Digest realm".equals(itemEntry[0])) { | ||
126 | + realm = itemEntry[1].replaceAll("\"", ""); | ||
127 | + } else if ("qop".equals(itemEntry[0])) { | ||
128 | + qop = itemEntry[1].replaceAll("\"", ""); | ||
129 | + } else if ("nonce".equals(itemEntry[0])) { | ||
130 | + nonce = itemEntry[1].replaceAll("\"", ""); | ||
131 | + } | ||
132 | + } | ||
133 | + String nc = "00000001"; | ||
134 | + String cnonce = DigestUtils.generateSalt2(8); | ||
135 | + String response = DigestUtils.getResponse(gat1400Properties.getAccessKey(), realm, gat1400Properties.getSecurityKey(), | ||
136 | + nonce, nc, cnonce, qop, method, url); | ||
137 | + return DigestUtils.getAuthorization(gat1400Properties.getAccessKey(), realm, nonce, | ||
138 | + url, qop, nc, cnonce, response, opaque); | ||
139 | + } | ||
140 | +} |
src/main/java/com/objecteye/handle/viid/SubscribeHandle.java
0 → 100644
1 | +package com.objecteye.handle.viid; | ||
2 | + | ||
3 | +import com.objecteye.config.Gat1400Properties; | ||
4 | +import com.objecteye.content.VIIDApi; | ||
5 | +import com.objecteye.content.ViidCollectorRedisKey; | ||
6 | +import com.objecteye.pojo.viid.subscribe.Subscribe; | ||
7 | +import com.objecteye.pojo.viid.subscribe.SubscribeRequestObject; | ||
8 | +import com.objecteye.utils.JSONUtils; | ||
9 | +import lombok.RequiredArgsConstructor; | ||
10 | +import lombok.extern.slf4j.Slf4j; | ||
11 | +import org.apache.commons.lang3.StringUtils; | ||
12 | +import org.springframework.beans.factory.annotation.Value; | ||
13 | +import org.springframework.data.redis.core.StringRedisTemplate; | ||
14 | +import org.springframework.http.HttpEntity; | ||
15 | +import org.springframework.http.HttpHeaders; | ||
16 | +import org.springframework.http.HttpMethod; | ||
17 | +import org.springframework.http.ResponseEntity; | ||
18 | +import org.springframework.stereotype.Component; | ||
19 | +import org.springframework.web.client.RestTemplate; | ||
20 | + | ||
21 | +import java.time.LocalDateTime; | ||
22 | +import java.time.format.DateTimeFormatter; | ||
23 | +import java.util.ArrayList; | ||
24 | +import java.util.List; | ||
25 | + | ||
26 | +/** | ||
27 | + * @author: liuhaoyu | ||
28 | + * @date: 2023/7/5 | ||
29 | + */ | ||
30 | +@Slf4j | ||
31 | +@Component | ||
32 | +@RequiredArgsConstructor | ||
33 | +public class SubscribeHandle { | ||
34 | + private final StringRedisTemplate stringRedisTemplate; | ||
35 | + private final Gat1400Properties gat1400Properties; | ||
36 | + private final RestTemplate restTemplate; | ||
37 | + @Value("${receiver}") | ||
38 | + private String receiver; | ||
39 | + | ||
40 | + public boolean subscribe(String deviceId) { | ||
41 | + // 订阅统一标识码 = 公安机关机构代码 + 子类型编码(03-订阅)+ 时间编码(YYYYMMDDhhmmss) + 流水序号(00001) | ||
42 | + String time = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").format(LocalDateTime.now()); | ||
43 | + String toSecond = time.substring(0, 14); | ||
44 | + String toMill = time.substring(14); | ||
45 | + String subscribeId = "123456789012" + "03" + toSecond + StringUtils.leftPad(toMill, 5, "0"); | ||
46 | + String title = "订阅人脸识别、车牌识别消息"; | ||
47 | + // 订阅类别: 12-人脸信息 13-车辆信息 | ||
48 | + String subscribeDetail = "12,13"; | ||
49 | + | ||
50 | + String beginTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()); | ||
51 | + | ||
52 | + String endTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now().plusMinutes(10L)); | ||
53 | + | ||
54 | + Integer operateType = 0; | ||
55 | + | ||
56 | + SubscribeRequestObject subscribeRequestObject = new SubscribeRequestObject(); | ||
57 | + SubscribeRequestObject.SubscribeListObject subscribeListObject = new SubscribeRequestObject.SubscribeListObject(); | ||
58 | + subscribeRequestObject.setSubscribeListObject(subscribeListObject); | ||
59 | + | ||
60 | + List<Subscribe> subscribeList = new ArrayList<>(); | ||
61 | + | ||
62 | + Subscribe subscribe = new Subscribe(); | ||
63 | + subscribe.setSubscribeId(subscribeId); | ||
64 | + subscribe.setTitle(title); | ||
65 | + subscribe.setSubscribeDetail(subscribeDetail); | ||
66 | + subscribe.setResourceUri(deviceId); | ||
67 | + subscribe.setBeginTime(beginTime); | ||
68 | + subscribe.setEndTime(endTime); | ||
69 | + subscribe.setReceiveAddr(receiver); | ||
70 | + subscribe.setOperateType(operateType); | ||
71 | + subscribeList.add(subscribe); | ||
72 | + subscribeListObject.setSubscribeObject(subscribeList); | ||
73 | + | ||
74 | + HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtils.toJsonString(subscribeRequestObject), | ||
75 | + buildHttpHeaders(deviceId)); | ||
76 | + if (log.isDebugEnabled()) { | ||
77 | + log.debug("上传人脸消息体:{}", JSONUtils.toJsonString(subscribeRequestObject)); | ||
78 | + } | ||
79 | + // 请求执行 | ||
80 | + ResponseEntity<String> responseEntity = restTemplate.exchange(getUrl(VIIDApi.SUBSCRIBES), HttpMethod.POST, | ||
81 | + httpEntity, String.class); | ||
82 | + if (org.apache.http.HttpStatus.SC_OK == responseEntity.getStatusCode().value()) { | ||
83 | + stringRedisTemplate.opsForHash().put(ViidCollectorRedisKey.DEVICE_CACHE, deviceId, subscribeId); | ||
84 | + return true; | ||
85 | + } | ||
86 | + return false; | ||
87 | + } | ||
88 | + | ||
89 | + public boolean unsubscribe(String deviceId) { | ||
90 | + Object subscribeIdObj = stringRedisTemplate.opsForHash().get(ViidCollectorRedisKey.DEVICE_CACHE, deviceId); | ||
91 | + if (null == subscribeIdObj) { | ||
92 | + return false; | ||
93 | + } | ||
94 | + SubscribeRequestObject subscribeRequestObject = new SubscribeRequestObject(); | ||
95 | + SubscribeRequestObject.SubscribeListObject subscribeListObject = new SubscribeRequestObject.SubscribeListObject(); | ||
96 | + subscribeRequestObject.setSubscribeListObject(subscribeListObject); | ||
97 | + | ||
98 | + List<Subscribe> subscribeList = new ArrayList<>(); | ||
99 | + | ||
100 | + Subscribe subscribe = new Subscribe(); | ||
101 | + subscribe.setSubscribeId(subscribeIdObj.toString()); | ||
102 | + subscribe.setOperateType(1); | ||
103 | + subscribeList.add(subscribe); | ||
104 | + subscribeListObject.setSubscribeObject(subscribeList); | ||
105 | + | ||
106 | + HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtils.toJsonString(subscribeRequestObject), | ||
107 | + buildHttpHeaders(deviceId)); | ||
108 | + ResponseEntity<String> responseEntity = restTemplate.exchange(getUrl(VIIDApi.SUBSCRIBES), | ||
109 | + HttpMethod.POST, httpEntity, String.class); | ||
110 | + if (org.apache.http.HttpStatus.SC_OK == responseEntity.getStatusCode().value()) { | ||
111 | + stringRedisTemplate.opsForHash().delete(ViidCollectorRedisKey.DEVICE_CACHE, deviceId); | ||
112 | + return true; | ||
113 | + } | ||
114 | + return false; | ||
115 | + } | ||
116 | + | ||
117 | + private String getUrl(String uri) { | ||
118 | + return "http://" + gat1400Properties.getServerAddress() + ":" + gat1400Properties.getPort() + uri; | ||
119 | + } | ||
120 | + | ||
121 | + private HttpHeaders buildHttpHeaders(String deviceId) { | ||
122 | + HttpHeaders headers = new HttpHeaders(); | ||
123 | + headers.add("Content-Type", "application/json;charset=utf-8"); | ||
124 | + headers.set("User-Identify", deviceId); | ||
125 | + return headers; | ||
126 | + } | ||
127 | +} |
src/main/java/com/objecteye/handle/viid/VIIDCommonHandle.java deleted
1 | -package com.objecteye.handle.viid; | ||
2 | - | ||
3 | -import com.fasterxml.jackson.core.JsonProcessingException; | ||
4 | -import com.fasterxml.jackson.databind.ObjectMapper; | ||
5 | -import com.objecteye.config.ApplicationConfigurationProperties; | ||
6 | -import com.objecteye.content.VIIDApi; | ||
7 | -import com.objecteye.content.enums.VIIDConfirmStatusTypeEnum; | ||
8 | -import com.objecteye.pojo.platform.DeviceRequestParams; | ||
9 | -import com.objecteye.pojo.viid.common.CommonDeviceIdRequestParams; | ||
10 | -import com.objecteye.pojo.viid.common.ResponseStatusResponse; | ||
11 | -import lombok.RequiredArgsConstructor; | ||
12 | -import lombok.extern.slf4j.Slf4j; | ||
13 | -import org.springframework.http.HttpEntity; | ||
14 | -import org.springframework.http.HttpHeaders; | ||
15 | -import org.springframework.http.ResponseEntity; | ||
16 | -import org.springframework.stereotype.Component; | ||
17 | -import org.springframework.util.StringUtils; | ||
18 | -import org.springframework.web.client.RestTemplate; | ||
19 | - | ||
20 | -/** | ||
21 | - * @author: liuhaoyu | ||
22 | - * @date: 2023/6/28 | ||
23 | - */ | ||
24 | -@Slf4j | ||
25 | -@Component | ||
26 | -@RequiredArgsConstructor | ||
27 | -public class VIIDCommonHandle { | ||
28 | - private final ObjectMapper objectMapper; | ||
29 | - private final RestTemplate restTemplate; | ||
30 | - private final ApplicationConfigurationProperties applicationConfigurationProperties; | ||
31 | - private static final String CONTENT_TYPE = "application/*+JSON"; | ||
32 | - private static final String HTTP = "http://"; | ||
33 | - | ||
34 | - /** | ||
35 | - * 注册 | ||
36 | - * | ||
37 | - * @param deviceRequestParams 注册目标设备 | ||
38 | - * @return 注册结果 | ||
39 | - */ | ||
40 | - public boolean register(DeviceRequestParams deviceRequestParams) { | ||
41 | - return commonDeviceIdPost(deviceRequestParams, VIIDApi.REGISTER); | ||
42 | - } | ||
43 | - | ||
44 | - /** | ||
45 | - * 保活 | ||
46 | - * | ||
47 | - * @param deviceRequestParams 心跳目标设备 | ||
48 | - * @return 发送结果 | ||
49 | - */ | ||
50 | - public boolean keepalive(DeviceRequestParams deviceRequestParams) { | ||
51 | - return commonDeviceIdPost(deviceRequestParams, VIIDApi.KEEPALIVE); | ||
52 | - } | ||
53 | - | ||
54 | - /** | ||
55 | - * 注销 | ||
56 | - * | ||
57 | - * @param deviceRequestParams 注销目标设备 | ||
58 | - * @return 注销结果 | ||
59 | - */ | ||
60 | - public boolean unregister(DeviceRequestParams deviceRequestParams) { | ||
61 | - return commonDeviceIdPost(deviceRequestParams, VIIDApi.UNREGISTER); | ||
62 | - } | ||
63 | - | ||
64 | - private boolean commonDeviceIdPost(DeviceRequestParams deviceRequestParams, String uri) { | ||
65 | - return checkResponse(restTemplate.postForEntity(getAddress(deviceRequestParams) + uri, | ||
66 | - buildHttpEntity(buildCommonDeviceIdRequestParam(deviceRequestParams)), String.class)); | ||
67 | - } | ||
68 | - | ||
69 | - private CommonDeviceIdRequestParams buildCommonDeviceIdRequestParam(DeviceRequestParams deviceRequestParams) { | ||
70 | - CommonDeviceIdRequestParams commonDeviceIdRequestParams = new CommonDeviceIdRequestParams(); | ||
71 | - commonDeviceIdRequestParams.setDeviceId(applicationConfigurationProperties.getPlatformCode()); | ||
72 | - return commonDeviceIdRequestParams; | ||
73 | - } | ||
74 | - | ||
75 | - private String getAddress(DeviceRequestParams deviceRequestParams) { | ||
76 | - return HTTP + deviceRequestParams.getHost() + ":" + deviceRequestParams.getPort(); | ||
77 | - } | ||
78 | - | ||
79 | - private boolean checkResponse(ResponseEntity<String> responseEntity) { | ||
80 | - String body = responseEntity.getBody(); | ||
81 | - if (StringUtils.hasText(body)) { | ||
82 | - try { | ||
83 | - ResponseStatusResponse responseStatusResponse = objectMapper.readValue(body, ResponseStatusResponse.class); | ||
84 | - if (null == responseStatusResponse || null == responseStatusResponse.getStatusCode() || | ||
85 | - !responseStatusResponse.getStatusCode().equals(VIIDConfirmStatusTypeEnum.OK.ordinal())) { | ||
86 | - if (log.isDebugEnabled()) { | ||
87 | - log.debug("register failed, {}", body); | ||
88 | - } | ||
89 | - return false; | ||
90 | - } | ||
91 | - return true; | ||
92 | - } catch (JsonProcessingException e) { | ||
93 | - if (log.isDebugEnabled()) { | ||
94 | - log.debug("convert failed, {}", body); | ||
95 | - } | ||
96 | - return false; | ||
97 | - } | ||
98 | - } | ||
99 | - return false; | ||
100 | - } | ||
101 | - | ||
102 | - private <T> HttpEntity<T> buildHttpEntity(T t) { | ||
103 | - HttpHeaders httpHeaders = new HttpHeaders(); | ||
104 | - httpHeaders.add(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE); | ||
105 | - | ||
106 | - return new HttpEntity<>(t, httpHeaders); | ||
107 | - } | ||
108 | -} |
src/main/java/com/objecteye/pojo/platform/DeviceRequestParams.java renamed to src/main/java/com/objecteye/pojo/DeviceIdParams.java
1 | -package com.objecteye.pojo.platform; | 1 | +package com.objecteye.pojo; |
2 | 2 | ||
3 | import lombok.Data; | 3 | import lombok.Data; |
4 | 4 | ||
5 | +import javax.validation.constraints.NotEmpty; | ||
5 | import java.io.Serializable; | 6 | import java.io.Serializable; |
6 | 7 | ||
7 | /** | 8 | /** |
8 | * @author: liuhaoyu | 9 | * @author: liuhaoyu |
9 | - * @date: 2023/6/28 | 10 | + * @date: 2023/7/6 |
10 | */ | 11 | */ |
11 | @Data | 12 | @Data |
12 | -public class DeviceRequestParams implements Serializable { | ||
13 | - private static final long serialVersionUID = -7801549061937667804L; | 13 | +public class DeviceIdParams implements Serializable { |
14 | + private static final long serialVersionUID = 2371999237808825162L; | ||
14 | 15 | ||
16 | + @NotEmpty(message = "设备信息为空") | ||
15 | private String deviceId; | 17 | private String deviceId; |
16 | - | ||
17 | - private String host; | ||
18 | - | ||
19 | - private String port; | ||
20 | - | ||
21 | - private String accessKey; | ||
22 | - | ||
23 | - private String securityKey; | ||
24 | - | ||
25 | } | 18 | } |
src/main/java/com/objecteye/pojo/Result.java
0 → 100644
1 | +package com.objecteye.pojo; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +import java.io.Serializable; | ||
6 | + | ||
7 | +/** | ||
8 | + * @author: liuhaoyu | ||
9 | + * @date: 2023/7/6 | ||
10 | + */ | ||
11 | +@Data | ||
12 | +public class Result<T> implements Serializable { | ||
13 | + | ||
14 | + private static final long serialVersionUID = -3498710365399499894L; | ||
15 | + | ||
16 | + private Integer code; | ||
17 | + | ||
18 | + private String message; | ||
19 | + | ||
20 | + private T data; | ||
21 | + | ||
22 | + public static <T> Result<T> success(T t) { | ||
23 | + Result<T> result = new Result<>(); | ||
24 | + result.setCode(200); | ||
25 | + result.setMessage("Success"); | ||
26 | + result.setData(t); | ||
27 | + return result; | ||
28 | + } | ||
29 | + | ||
30 | + public static <T> Result<T> success() { | ||
31 | + return success(null); | ||
32 | + } | ||
33 | + | ||
34 | + public static <T> Result<T> failed(String message) { | ||
35 | + return failed(message, null); | ||
36 | + } | ||
37 | + | ||
38 | + public static <T> Result<T> failed(String message, T t) { | ||
39 | + Result<T> result = new Result<>(); | ||
40 | + result.setCode(201); | ||
41 | + result.setMessage(message); | ||
42 | + result.setData(t); | ||
43 | + return result; | ||
44 | + } | ||
45 | +} |
src/main/java/com/objecteye/pojo/viid/common/KeepaliveRequestObject.java
0 → 100644
1 | +package com.objecteye.pojo.viid.common; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +/** | ||
7 | + * @author: liuhaoyu | ||
8 | + * @date: 2023/7/5 | ||
9 | + */ | ||
10 | +@Data | ||
11 | +public class KeepaliveRequestObject { | ||
12 | + | ||
13 | + @JsonProperty("KeepaliveObject") | ||
14 | + private RegisterObject keepaliveObject; | ||
15 | +} |
src/main/java/com/objecteye/pojo/viid/common/CommonDeviceIdRequestParams.java renamed to src/main/java/com/objecteye/pojo/viid/common/RegisterObject.java
@@ -3,15 +3,12 @@ package com.objecteye.pojo.viid.common; | @@ -3,15 +3,12 @@ package com.objecteye.pojo.viid.common; | ||
3 | import com.fasterxml.jackson.annotation.JsonProperty; | 3 | import com.fasterxml.jackson.annotation.JsonProperty; |
4 | import lombok.Data; | 4 | import lombok.Data; |
5 | 5 | ||
6 | -import java.io.Serializable; | ||
7 | - | ||
8 | /** | 6 | /** |
9 | * @author: liuhaoyu | 7 | * @author: liuhaoyu |
10 | - * @date: 2023/6/28 | 8 | + * @date: 2023/7/5 |
11 | */ | 9 | */ |
12 | @Data | 10 | @Data |
13 | -public class CommonDeviceIdRequestParams implements Serializable { | ||
14 | - private static final long serialVersionUID = 1L; | 11 | +public class RegisterObject { |
15 | 12 | ||
16 | @JsonProperty("DeviceID") | 13 | @JsonProperty("DeviceID") |
17 | private String deviceId; | 14 | private String deviceId; |
src/main/java/com/objecteye/pojo/viid/common/RegisterRequestObject.java
0 → 100644
1 | +package com.objecteye.pojo.viid.common; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +/** | ||
7 | + * @author: liuhaoyu | ||
8 | + * @date: 2023/7/5 | ||
9 | + */ | ||
10 | +@Data | ||
11 | +public class RegisterRequestObject { | ||
12 | + | ||
13 | + @JsonProperty("RegisterObject") | ||
14 | + private RegisterObject registerObject; | ||
15 | +} |
src/main/java/com/objecteye/pojo/viid/common/ResponseStatusResponse.java renamed to src/main/java/com/objecteye/pojo/viid/common/ResponseStatus.java
@@ -10,7 +10,7 @@ import java.io.Serializable; | @@ -10,7 +10,7 @@ import java.io.Serializable; | ||
10 | * @date: 2023/6/28 | 10 | * @date: 2023/6/28 |
11 | */ | 11 | */ |
12 | @Data | 12 | @Data |
13 | -public class ResponseStatusResponse implements Serializable { | 13 | +public class ResponseStatus implements Serializable { |
14 | 14 | ||
15 | private static final long serialVersionUID = 1L; | 15 | private static final long serialVersionUID = 1L; |
16 | 16 |
src/main/java/com/objecteye/pojo/viid/common/ResponseStatusListObject.java
0 → 100644
1 | +package com.objecteye.pojo.viid.common; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +import java.util.List; | ||
7 | + | ||
8 | +/** | ||
9 | + * @author: liuhaoyu | ||
10 | + * @date: 2023/7/5 | ||
11 | + */ | ||
12 | +@Data | ||
13 | +public class ResponseStatusListObject { | ||
14 | + | ||
15 | + @JsonProperty("ResponseStatusList") | ||
16 | + private ResponseStatusList responseStatusList; | ||
17 | + | ||
18 | + @Data | ||
19 | + public static class ResponseStatusList{ | ||
20 | + @JsonProperty("ResponseStatusObject") | ||
21 | + private List<ResponseStatus> responseStatusObject; | ||
22 | + } | ||
23 | +} |
src/main/java/com/objecteye/pojo/viid/common/ResponseStatusObjectWrapper.java
0 → 100644
1 | +package com.objecteye.pojo.viid.common; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +/** | ||
7 | + * @author: liuhaoyu | ||
8 | + * @date: 2023/7/5 | ||
9 | + */ | ||
10 | +@Data | ||
11 | +public class ResponseStatusObjectWrapper { | ||
12 | + | ||
13 | + @JsonProperty("ResponseStatusObject") | ||
14 | + private ResponseStatus responseStatusObject; | ||
15 | +} |
src/main/java/com/objecteye/pojo/viid/common/SystemTimeObject.java
0 → 100644
1 | +package com.objecteye.pojo.viid.common; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +/** | ||
7 | + * @author: liuhaoyu | ||
8 | + * @date: 2023/7/5 | ||
9 | + */ | ||
10 | +@Data | ||
11 | +public class SystemTimeObject { | ||
12 | + | ||
13 | + @JsonProperty("SystemTime") | ||
14 | + private SystemTime systemTime; | ||
15 | + | ||
16 | + @Data | ||
17 | + public static class SystemTime { | ||
18 | + @JsonProperty("VIIDServerID") | ||
19 | + private String serverId; | ||
20 | + @JsonProperty("TimeMode") | ||
21 | + private String timeMode; | ||
22 | + @JsonProperty("LocalTime") | ||
23 | + private String localTime; | ||
24 | + @JsonProperty("TimeZone") | ||
25 | + private String timeZone; | ||
26 | + } | ||
27 | +} |
src/main/java/com/objecteye/pojo/viid/common/UnRegisterRequestObject.java
0 → 100644
1 | +package com.objecteye.pojo.viid.common; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +/** | ||
7 | + * @author: liuhaoyu | ||
8 | + * @date: 2023/7/5 | ||
9 | + */ | ||
10 | +@Data | ||
11 | +public class UnRegisterRequestObject { | ||
12 | + | ||
13 | + @JsonProperty("UnRegisterObject") | ||
14 | + private RegisterObject unRegisterObject; | ||
15 | +} |
src/main/java/com/objecteye/pojo/viid/detail/Face.java
0 → 100644
1 | +package com.objecteye.pojo.viid.detail; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +/** | ||
7 | + * @author: liuhaoyu | ||
8 | + * @date: 2023/7/6 | ||
9 | + */ | ||
10 | +@Data | ||
11 | +public class Face { | ||
12 | + | ||
13 | + @JsonProperty("FaceID") | ||
14 | + private String faceId; | ||
15 | + @JsonProperty("InfoKind") | ||
16 | + private Integer infoKind; | ||
17 | + @JsonProperty("SourceID") | ||
18 | + private String sourceId; | ||
19 | + @JsonProperty("DeviceID") | ||
20 | + private String deviceId; | ||
21 | + @JsonProperty("LeftTopX") | ||
22 | + private Integer leftTopX; | ||
23 | + @JsonProperty("LeftTopY") | ||
24 | + private Integer leftTopY; | ||
25 | + @JsonProperty("RightBtmX") | ||
26 | + private Integer rightBtmX; | ||
27 | + @JsonProperty("RightBtmY") | ||
28 | + private Integer rightBtmY; | ||
29 | + @JsonProperty("IsDriver") | ||
30 | + private Integer isDriver; | ||
31 | + @JsonProperty("IsForeigner") | ||
32 | + private Integer isForeigner; | ||
33 | + @JsonProperty("IsSuspectedTerrorist") | ||
34 | + private Integer isSuspectedTerrorist; | ||
35 | + @JsonProperty("IsCriminalInvolved") | ||
36 | + private Integer isCriminalInvolved; | ||
37 | + @JsonProperty("IsDetainees") | ||
38 | + private Integer isDetainees; | ||
39 | + @JsonProperty("IsVictim") | ||
40 | + private Integer isVictim; | ||
41 | + @JsonProperty("IsSuspiciousPerson") | ||
42 | + private Integer isSuspiciousPerson; | ||
43 | + @JsonProperty("SubImageList") | ||
44 | + private SubImageList subImageList; | ||
45 | + | ||
46 | +} |
src/main/java/com/objecteye/pojo/viid/detail/FaceRequestObject.java
0 → 100644
1 | +package com.objecteye.pojo.viid.detail; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +import java.util.List; | ||
7 | + | ||
8 | +/** | ||
9 | + * @author: liuhaoyu | ||
10 | + * @date: 2023/7/6 | ||
11 | + */ | ||
12 | +@Data | ||
13 | +public class FaceRequestObject { | ||
14 | + | ||
15 | + @JsonProperty("FaceListObject") | ||
16 | + private FaceListObject faceListObject; | ||
17 | + | ||
18 | + @Data | ||
19 | + public static class FaceListObject { | ||
20 | + @JsonProperty("FaceObject") | ||
21 | + private List<Face> faceObject; | ||
22 | + } | ||
23 | + | ||
24 | +} |
src/main/java/com/objecteye/pojo/viid/detail/MotorVehicle.java
0 → 100644
1 | +package com.objecteye.pojo.viid.detail; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +/** | ||
7 | + * @author: liuhaoyu | ||
8 | + * @date: 2023/7/6 | ||
9 | + */ | ||
10 | +@Data | ||
11 | +public class MotorVehicle { | ||
12 | + | ||
13 | + @JsonProperty("MotorVehicleID") | ||
14 | + private String motorVehicleId; | ||
15 | + @JsonProperty("InfoKind") | ||
16 | + private Integer infoKind; | ||
17 | + @JsonProperty("SourceID") | ||
18 | + private String sourceId; | ||
19 | + @JsonProperty("DeviceID") | ||
20 | + private String deviceId; | ||
21 | + @JsonProperty("StorageUrl1") | ||
22 | + private String storageUrl1; | ||
23 | + @JsonProperty("LeftTopX") | ||
24 | + private Integer leftTopX; | ||
25 | + @JsonProperty("LeftTopY") | ||
26 | + private Integer leftTopY; | ||
27 | + @JsonProperty("RightBtmX") | ||
28 | + private Integer rightBtmX; | ||
29 | + @JsonProperty("RightBtmY") | ||
30 | + private Integer rightBtmY; | ||
31 | + @JsonProperty("HasPlate") | ||
32 | + private Boolean hasPlate; | ||
33 | + @JsonProperty("PlateClass") | ||
34 | + private String plateClass; | ||
35 | + @JsonProperty("PlateColor") | ||
36 | + private String plateColor; | ||
37 | + @JsonProperty("PlateNo") | ||
38 | + private String plateNo; | ||
39 | + @JsonProperty("VehicleColor") | ||
40 | + private String vehicleColor; | ||
41 | + @JsonProperty("SubImageList") | ||
42 | + private SubImageList subImageList; | ||
43 | +} |
src/main/java/com/objecteye/pojo/viid/detail/MotorVehicleRequestObject.java
0 → 100644
1 | +package com.objecteye.pojo.viid.detail; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +import java.util.List; | ||
7 | + | ||
8 | +/** | ||
9 | + * @author: liuhaoyu | ||
10 | + * @date: 2023/7/6 | ||
11 | + */ | ||
12 | +@Data | ||
13 | +public class MotorVehicleRequestObject { | ||
14 | + | ||
15 | + @JsonProperty("MotorVehicleListObject") | ||
16 | + private MotorVehicleListObject motorVehicleListObject; | ||
17 | + | ||
18 | + @Data | ||
19 | + public static class MotorVehicleListObject { | ||
20 | + | ||
21 | + @JsonProperty("MotorVehicleObject") | ||
22 | + private List<MotorVehicle> motorVehicleObject; | ||
23 | + } | ||
24 | +} |
src/main/java/com/objecteye/pojo/viid/detail/SubImageInfo.java
0 → 100644
1 | +package com.objecteye.pojo.viid.detail; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +/** | ||
7 | + * @author: liuhaoyu | ||
8 | + * @date: 2023/7/6 | ||
9 | + */ | ||
10 | +@Data | ||
11 | +public class SubImageInfo { | ||
12 | + /** | ||
13 | + * 图片ID【String(41)】 | ||
14 | + */ | ||
15 | + @JsonProperty("ImageID") | ||
16 | + private String imageId; | ||
17 | + /** | ||
18 | + * 视频图像分析处理事件类型 | ||
19 | + */ | ||
20 | + @JsonProperty("EventSort") | ||
21 | + private Integer eventSort; | ||
22 | + /** | ||
23 | + * 设备编码【String(20)】 | ||
24 | + */ | ||
25 | + @JsonProperty("DeviceID") | ||
26 | + private String deviceId; | ||
27 | + /** | ||
28 | + * 存储路径 | ||
29 | + */ | ||
30 | + @JsonProperty("StoragePath") | ||
31 | + private String storagePath; | ||
32 | + /** | ||
33 | + * 图像类型 | ||
34 | + */ | ||
35 | + @JsonProperty("ImageType") | ||
36 | + private String imageType; | ||
37 | + /** | ||
38 | + * 图片格式 | ||
39 | + */ | ||
40 | + @JsonProperty("FileFormat") | ||
41 | + private String fileFormat; | ||
42 | + /** | ||
43 | + * 拍摄时间【格式(yyyyMMddHHmmss)】 | ||
44 | + */ | ||
45 | + @JsonProperty("ShotTime") | ||
46 | + private String shotTime; | ||
47 | + /** | ||
48 | + * 水平像素值 | ||
49 | + */ | ||
50 | + @JsonProperty("Width") | ||
51 | + private Integer width; | ||
52 | + /** | ||
53 | + * 垂直像素值 | ||
54 | + */ | ||
55 | + @JsonProperty("Height") | ||
56 | + private Integer height; | ||
57 | + /** | ||
58 | + * 图片(base64) | ||
59 | + */ | ||
60 | + @JsonProperty("Data") | ||
61 | + private String data; | ||
62 | + /** | ||
63 | + * 图像类型(02-车牌彩色小图,11-人脸图) | ||
64 | + */ | ||
65 | + @JsonProperty("Type") | ||
66 | + private String type; | ||
67 | +} |
src/main/java/com/objecteye/pojo/viid/detail/SubImageList.java
0 → 100644
1 | +package com.objecteye.pojo.viid.detail; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +import java.util.List; | ||
7 | + | ||
8 | +/** | ||
9 | + * @author: liuhaoyu | ||
10 | + * @date: 2023/7/6 | ||
11 | + */ | ||
12 | +@Data | ||
13 | +public class SubImageList { | ||
14 | + | ||
15 | + @JsonProperty("SubImageInfoObject") | ||
16 | + private List<SubImageInfo> subImageInfoObject; | ||
17 | +} |
src/main/java/com/objecteye/pojo/viid/subscribe/Subscribe.java
@@ -95,4 +95,29 @@ public class Subscribe implements Serializable { | @@ -95,4 +95,29 @@ public class Subscribe implements Serializable { | ||
95 | */ | 95 | */ |
96 | @JsonProperty("SubscribeStatus") | 96 | @JsonProperty("SubscribeStatus") |
97 | private Integer subscribeStatus; | 97 | private Integer subscribeStatus; |
98 | + /** | ||
99 | + * 订阅原因 | ||
100 | + */ | ||
101 | + @JsonProperty("Reason") | ||
102 | + private String reason; | ||
103 | + /** | ||
104 | + * 取消订阅机构 | ||
105 | + */ | ||
106 | + @JsonProperty("SubscribeCancelOrg") | ||
107 | + private String subscribeCancelOrg; | ||
108 | + /** | ||
109 | + * 取消订阅人员 | ||
110 | + */ | ||
111 | + @JsonProperty("SubscribeCancelPerson") | ||
112 | + private String subscribeCancelPerson; | ||
113 | + /** | ||
114 | + * 取消时间 | ||
115 | + */ | ||
116 | + @JsonProperty("CancelTime") | ||
117 | + private String cancelTime; | ||
118 | + /** | ||
119 | + * 取消原因 | ||
120 | + */ | ||
121 | + @JsonProperty("CancelReason") | ||
122 | + private String cancelReason; | ||
98 | } | 123 | } |
src/main/java/com/objecteye/pojo/viid/subscribe/SubscribeNotification.java
0 → 100644
1 | +package com.objecteye.pojo.viid.subscribe; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import com.objecteye.pojo.viid.detail.FaceRequestObject; | ||
5 | +import com.objecteye.pojo.viid.detail.MotorVehicleRequestObject; | ||
6 | +import lombok.Data; | ||
7 | + | ||
8 | +/** | ||
9 | + * @author: liuhaoyu | ||
10 | + * @date: 2023/7/6 | ||
11 | + */ | ||
12 | +@Data | ||
13 | +public class SubscribeNotification { | ||
14 | + | ||
15 | + @JsonProperty("NotificationID") | ||
16 | + private String notificationId; | ||
17 | + @JsonProperty("SubscribeID") | ||
18 | + private String subscribeId; | ||
19 | + @JsonProperty("Title") | ||
20 | + private String title; | ||
21 | + @JsonProperty("TriggerTime") | ||
22 | + private String triggerTime; | ||
23 | + @JsonProperty("InfoIDs") | ||
24 | + private String infoIds; | ||
25 | + @JsonProperty("FaceListObject") | ||
26 | + private FaceRequestObject.FaceListObject faceListObject; | ||
27 | + @JsonProperty("MotorVehicleListObject") | ||
28 | + private MotorVehicleRequestObject.MotorVehicleListObject motorVehicleListObject; | ||
29 | +} |
src/main/java/com/objecteye/pojo/viid/subscribe/SubscribeNotificationRequestObject.java
0 → 100644
1 | +package com.objecteye.pojo.viid.subscribe; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +import java.util.List; | ||
7 | + | ||
8 | +/** | ||
9 | + * @author: liuhaoyu | ||
10 | + * @date: 2023/7/6 | ||
11 | + */ | ||
12 | +@Data | ||
13 | +public class SubscribeNotificationRequestObject { | ||
14 | + | ||
15 | + @JsonProperty("SubscribeNotificationListObject") | ||
16 | + private SubscribeNotificationListObject subscribeNotificationListObject; | ||
17 | + | ||
18 | + @Data | ||
19 | + public static class SubscribeNotificationListObject { | ||
20 | + @JsonProperty("SubscribeNotificationObject") | ||
21 | + private List<SubscribeNotification> subscribeNotificationObject; | ||
22 | + } | ||
23 | +} |
src/main/java/com/objecteye/pojo/viid/subscribe/SubscribeRequestObject.java
0 → 100644
1 | +package com.objecteye.pojo.viid.subscribe; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +import java.util.List; | ||
7 | + | ||
8 | +/** | ||
9 | + * @author: liuhaoyu | ||
10 | + * @date: 2023/7/5 | ||
11 | + */ | ||
12 | +@Data | ||
13 | +public class SubscribeRequestObject { | ||
14 | + | ||
15 | + @JsonProperty("SubscribeListObject") | ||
16 | + private SubscribeListObject subscribeListObject; | ||
17 | + | ||
18 | + @Data | ||
19 | + public static class SubscribeListObject { | ||
20 | + @JsonProperty("SubscribeObject") | ||
21 | + private List<Subscribe> subscribeObject; | ||
22 | + } | ||
23 | +} |
src/main/java/com/objecteye/utils/DigestUtils.java
0 → 100644
1 | +package com.objecteye.utils; | ||
2 | + | ||
3 | +import org.apache.commons.codec.binary.Hex; | ||
4 | + | ||
5 | +import java.security.GeneralSecurityException; | ||
6 | +import java.security.MessageDigest; | ||
7 | +import java.util.Random; | ||
8 | + | ||
9 | +public class DigestUtils { | ||
10 | + | ||
11 | + | ||
12 | + /** | ||
13 | + * 加密遵循RFC2671规范 将相关参数加密生成一个MD5字符串,并返回 | ||
14 | + */ | ||
15 | + public static String getResponse( | ||
16 | + String username, | ||
17 | + String realm, | ||
18 | + String password, | ||
19 | + String nonce, | ||
20 | + String nc, | ||
21 | + String cnonce, | ||
22 | + String qop, | ||
23 | + String method, | ||
24 | + String uri | ||
25 | + ) { | ||
26 | + String A1 = username + ":" + realm + ":" + password; | ||
27 | + byte[] md5ByteA1 = md5(A1.getBytes()); | ||
28 | + String HA1 = new String(Hex.encodeHex(md5ByteA1)); | ||
29 | + | ||
30 | + String A2 = method + ":" + uri; | ||
31 | + byte[] md5ByteA2 = md5(A2.getBytes()); | ||
32 | + String HA2 = new String(Hex.encodeHex(md5ByteA2)); | ||
33 | + | ||
34 | + | ||
35 | + String original = HA1 + ":" + (nonce + ":" + nc + ":" + cnonce + ":" + qop) + ":" + HA2; | ||
36 | + byte[] md5ByteOriginal = md5(original.getBytes()); | ||
37 | + return new String(Hex.encodeHex(md5ByteOriginal)); | ||
38 | + } | ||
39 | + | ||
40 | + public static String getAuthorization(String username, String realm, String nonce, String uri, String qop, String nc, String cnonce, String response, String opaque) { | ||
41 | + return "Digest username=\"" + username + "\"" + | ||
42 | + ",realm=\"" + realm + "\"" + | ||
43 | + ",nonce=\"" + nonce + "\"" + | ||
44 | + ",uri=\"" + uri + "\"" + | ||
45 | + ",qop=\"" + qop + "\"" + | ||
46 | + ",nc=\"" + nc + "\"" + | ||
47 | + ",cnonce=\"" + cnonce + "\"" + | ||
48 | + ",response=\"" + response + "\"" + | ||
49 | + ",opaque=\"" + opaque; | ||
50 | + } | ||
51 | + | ||
52 | + | ||
53 | + /** | ||
54 | + * 对输入字符串进行md5散列. | ||
55 | + */ | ||
56 | + public static byte[] md5(byte[] input) { | ||
57 | + return digest(input, "MD5", null, 1); | ||
58 | + } | ||
59 | + | ||
60 | + /** | ||
61 | + * 对字符串进行散列, 支持md5与sha1算法. | ||
62 | + */ | ||
63 | + private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) { | ||
64 | + try { | ||
65 | + MessageDigest digest = MessageDigest.getInstance(algorithm); | ||
66 | + | ||
67 | + if (salt != null) { | ||
68 | + digest.update(salt); | ||
69 | + } | ||
70 | + | ||
71 | + byte[] result = digest.digest(input); | ||
72 | + | ||
73 | + for (int i = 1; i < iterations; i++) { | ||
74 | + digest.reset(); | ||
75 | + result = digest.digest(result); | ||
76 | + } | ||
77 | + return result; | ||
78 | + } catch (GeneralSecurityException e) { | ||
79 | + throw new RuntimeException(e); | ||
80 | + } | ||
81 | + } | ||
82 | + | ||
83 | + public static String generateSalt2(int length) { | ||
84 | + StringBuilder val = new StringBuilder(); | ||
85 | + Random random = new Random(); | ||
86 | + //参数length,表示生成几位随机数 | ||
87 | + for (int i = 0; i < length; i++) { | ||
88 | + //输出字母还是数字 | ||
89 | + if (random.nextInt(2) % 2 == 0) { | ||
90 | + //输出是大写字母还是小写字母 | ||
91 | + val.append((char) (random.nextInt(26) + random.nextInt(2) % 2 == 0 ? 65 : 97)); | ||
92 | + } else { | ||
93 | + val.append(random.nextInt(10)); | ||
94 | + } | ||
95 | + } | ||
96 | + return val.toString().toLowerCase(); | ||
97 | + } | ||
98 | + | ||
99 | + | ||
100 | +} |
src/main/java/com/objecteye/utils/JSONUtils.java
0 → 100644
1 | +/* | ||
2 | + * Licensed to the Apache Software Foundation (ASF) under one or more | ||
3 | + * contributor license agreements. See the NOTICE file distributed with | ||
4 | + * this work for additional information regarding copyright ownership. | ||
5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
6 | + * (the "License"); you may not use this file except in compliance with | ||
7 | + * the License. You may obtain a copy of the License at | ||
8 | + * | ||
9 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
10 | + * | ||
11 | + * Unless required by applicable law or agreed to in writing, software | ||
12 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
14 | + * See the License for the specific language governing permissions and | ||
15 | + * limitations under the License. | ||
16 | + */ | ||
17 | + | ||
18 | +package com.objecteye.utils; | ||
19 | + | ||
20 | +import com.fasterxml.jackson.core.JsonGenerator; | ||
21 | +import com.fasterxml.jackson.core.JsonParser; | ||
22 | +import com.fasterxml.jackson.core.JsonProcessingException; | ||
23 | +import com.fasterxml.jackson.core.type.TypeReference; | ||
24 | +import com.fasterxml.jackson.databind.*; | ||
25 | +import com.fasterxml.jackson.databind.json.JsonMapper; | ||
26 | +import com.fasterxml.jackson.databind.module.SimpleModule; | ||
27 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
28 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
29 | +import com.fasterxml.jackson.databind.node.TextNode; | ||
30 | +import com.fasterxml.jackson.databind.type.CollectionType; | ||
31 | +import lombok.extern.slf4j.Slf4j; | ||
32 | +import org.apache.commons.lang3.StringUtils; | ||
33 | +import org.springframework.lang.Nullable; | ||
34 | + | ||
35 | +import java.io.IOException; | ||
36 | +import java.text.SimpleDateFormat; | ||
37 | +import java.time.LocalDateTime; | ||
38 | +import java.time.format.DateTimeFormatter; | ||
39 | +import java.util.*; | ||
40 | + | ||
41 | +import static com.fasterxml.jackson.databind.DeserializationFeature.*; | ||
42 | +import static com.fasterxml.jackson.databind.MapperFeature.REQUIRE_SETTERS_FOR_GETTERS; | ||
43 | +import static com.objecteye.content.VIIDContent.YYYY_MM_DD_HH_MM_SS; | ||
44 | +import static java.nio.charset.StandardCharsets.UTF_8; | ||
45 | + | ||
46 | +/** | ||
47 | + * json utils | ||
48 | + */ | ||
49 | +@Slf4j | ||
50 | +public class JSONUtils { | ||
51 | + | ||
52 | + static { | ||
53 | + log.info("init timezone: {}", TimeZone.getDefault()); | ||
54 | + } | ||
55 | + | ||
56 | + private static final ObjectMapper objectMapper = JsonMapper.builder() | ||
57 | + .configure(FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
58 | + .configure(ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true) | ||
59 | + .configure(READ_UNKNOWN_ENUM_VALUES_AS_NULL, true) | ||
60 | + .configure(REQUIRE_SETTERS_FOR_GETTERS, true) | ||
61 | + .addModule(new SimpleModule() | ||
62 | + .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()) | ||
63 | + .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer())) | ||
64 | + .defaultTimeZone(TimeZone.getDefault()) | ||
65 | + .defaultDateFormat(new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS)) | ||
66 | + .build(); | ||
67 | + | ||
68 | + private JSONUtils() { | ||
69 | + throw new UnsupportedOperationException("Construct JSONUtils"); | ||
70 | + } | ||
71 | + | ||
72 | + public static synchronized void setTimeZone(TimeZone timeZone) { | ||
73 | + objectMapper.setTimeZone(timeZone); | ||
74 | + } | ||
75 | + | ||
76 | + public static ArrayNode createArrayNode() { | ||
77 | + return objectMapper.createArrayNode(); | ||
78 | + } | ||
79 | + | ||
80 | + public static ObjectNode createObjectNode() { | ||
81 | + return objectMapper.createObjectNode(); | ||
82 | + } | ||
83 | + | ||
84 | + public static JsonNode toJsonNode(Object obj) { | ||
85 | + return objectMapper.valueToTree(obj); | ||
86 | + } | ||
87 | + | ||
88 | + /** | ||
89 | + * json representation of object | ||
90 | + * | ||
91 | + * @param object object | ||
92 | + * @param feature feature | ||
93 | + * @return object to json string | ||
94 | + */ | ||
95 | + public static String toJsonString(Object object, SerializationFeature feature) { | ||
96 | + try { | ||
97 | + ObjectWriter writer = objectMapper.writer(feature); | ||
98 | + return writer.writeValueAsString(object); | ||
99 | + } catch (Exception e) { | ||
100 | + log.error("object to json exception!", e); | ||
101 | + } | ||
102 | + | ||
103 | + return null; | ||
104 | + } | ||
105 | + | ||
106 | + /** | ||
107 | + * This method deserializes the specified Json into an object of the specified class. It is not | ||
108 | + * suitable to use if the specified class is a generic type since it will not have the generic | ||
109 | + * type information because of the Type Erasure feature of Java. Therefore, this method should not | ||
110 | + * be used if the desired type is a generic type. Note that this method works fine if the any of | ||
111 | + * the fields of the specified object are generics, just the object itself should not be a | ||
112 | + * generic type. | ||
113 | + * | ||
114 | + * @param json the string from which the object is to be deserialized | ||
115 | + * @param clazz the class of T | ||
116 | + * @param <T> T | ||
117 | + * @return an object of type T from the string | ||
118 | + * classOfT | ||
119 | + */ | ||
120 | + public static @Nullable <T> T parseObject(String json, Class<T> clazz) { | ||
121 | + if (StringUtils.isBlank(json)) { | ||
122 | + return null; | ||
123 | + } | ||
124 | + | ||
125 | + try { | ||
126 | + return objectMapper.readValue(json, clazz); | ||
127 | + } catch (Exception e) { | ||
128 | + log.error("Parse object exception, jsonStr: {}, class: {}", json, clazz, e); | ||
129 | + } | ||
130 | + return null; | ||
131 | + } | ||
132 | + | ||
133 | + /** | ||
134 | + * deserialize | ||
135 | + * | ||
136 | + * @param src byte array | ||
137 | + * @param clazz class | ||
138 | + * @param <T> deserialize type | ||
139 | + * @return deserialize type | ||
140 | + */ | ||
141 | + public static <T> T parseObject(byte[] src, Class<T> clazz) { | ||
142 | + if (src == null) { | ||
143 | + return null; | ||
144 | + } | ||
145 | + String json = new String(src, UTF_8); | ||
146 | + return parseObject(json, clazz); | ||
147 | + } | ||
148 | + | ||
149 | + /** | ||
150 | + * json to list | ||
151 | + * | ||
152 | + * @param json json string | ||
153 | + * @param clazz class | ||
154 | + * @param <T> T | ||
155 | + * @return list | ||
156 | + */ | ||
157 | + public static <T> List<T> toList(String json, Class<T> clazz) { | ||
158 | + if (StringUtils.isBlank(json)) { | ||
159 | + return Collections.emptyList(); | ||
160 | + } | ||
161 | + | ||
162 | + try { | ||
163 | + CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, clazz); | ||
164 | + return objectMapper.readValue(json, listType); | ||
165 | + } catch (Exception e) { | ||
166 | + log.error("parse list exception!", e); | ||
167 | + } | ||
168 | + | ||
169 | + return Collections.emptyList(); | ||
170 | + } | ||
171 | + | ||
172 | + /** | ||
173 | + * check json object valid | ||
174 | + * | ||
175 | + * @param json json | ||
176 | + * @return true if valid | ||
177 | + */ | ||
178 | + public static boolean checkJsonValid(String json) { | ||
179 | + | ||
180 | + if (StringUtils.isBlank(json)) { | ||
181 | + return false; | ||
182 | + } | ||
183 | + | ||
184 | + try { | ||
185 | + objectMapper.readTree(json); | ||
186 | + return true; | ||
187 | + } catch (IOException e) { | ||
188 | + log.error("check json object valid exception!", e); | ||
189 | + } | ||
190 | + | ||
191 | + return false; | ||
192 | + } | ||
193 | + | ||
194 | + /** | ||
195 | + * Method for finding a JSON Object field with specified name in this | ||
196 | + * node or its child nodes, and returning value it has. | ||
197 | + * If no matching field is found in this node or its descendants, returns null. | ||
198 | + * | ||
199 | + * @param jsonNode json node | ||
200 | + * @param fieldName Name of field to look for | ||
201 | + * @return Value of first matching node found, if any; null if none | ||
202 | + */ | ||
203 | + public static String findValue(JsonNode jsonNode, String fieldName) { | ||
204 | + JsonNode node = jsonNode.findValue(fieldName); | ||
205 | + | ||
206 | + if (node == null) { | ||
207 | + return null; | ||
208 | + } | ||
209 | + | ||
210 | + return node.asText(); | ||
211 | + } | ||
212 | + | ||
213 | + /** | ||
214 | + * json to map | ||
215 | + * {@link #toMap(String, Class, Class)} | ||
216 | + * | ||
217 | + * @param json json | ||
218 | + * @return json to map | ||
219 | + */ | ||
220 | + public static Map<String, String> toMap(String json) { | ||
221 | + return parseObject(json, new TypeReference<Map<String, String>>() { | ||
222 | + }); | ||
223 | + } | ||
224 | + | ||
225 | + /** | ||
226 | + * json to map | ||
227 | + * | ||
228 | + * @param json json | ||
229 | + * @param classK classK | ||
230 | + * @param classV classV | ||
231 | + * @param <K> K | ||
232 | + * @param <V> V | ||
233 | + * @return to map | ||
234 | + */ | ||
235 | + public static <K, V> Map<K, V> toMap(String json, Class<K> classK, Class<V> classV) { | ||
236 | + if (StringUtils.isBlank(json)) { | ||
237 | + return Collections.emptyMap(); | ||
238 | + } | ||
239 | + | ||
240 | + try { | ||
241 | + return objectMapper.readValue(json, new TypeReference<Map<K, V>>() { | ||
242 | + }); | ||
243 | + } catch (Exception e) { | ||
244 | + log.error("json to map exception!", e); | ||
245 | + } | ||
246 | + | ||
247 | + return Collections.emptyMap(); | ||
248 | + } | ||
249 | + | ||
250 | + /** | ||
251 | + * from the key-value generated json to get the str value no matter the real type of value | ||
252 | + * @param json the json str | ||
253 | + * @param nodeName key | ||
254 | + * @return the str value of key | ||
255 | + */ | ||
256 | + public static String getNodeString(String json, String nodeName) { | ||
257 | + try { | ||
258 | + JsonNode rootNode = objectMapper.readTree(json); | ||
259 | + JsonNode jsonNode = rootNode.findValue(nodeName); | ||
260 | + if (Objects.isNull(jsonNode)) { | ||
261 | + return ""; | ||
262 | + } | ||
263 | + return jsonNode.isTextual() ? jsonNode.asText() : jsonNode.toString(); | ||
264 | + } catch (JsonProcessingException e) { | ||
265 | + return ""; | ||
266 | + } | ||
267 | + } | ||
268 | + | ||
269 | + /** | ||
270 | + * json to object | ||
271 | + * | ||
272 | + * @param json json string | ||
273 | + * @param type type reference | ||
274 | + * @param <T> | ||
275 | + * @return return parse object | ||
276 | + */ | ||
277 | + public static <T> T parseObject(String json, TypeReference<T> type) { | ||
278 | + if (StringUtils.isBlank(json)) { | ||
279 | + return null; | ||
280 | + } | ||
281 | + | ||
282 | + try { | ||
283 | + return objectMapper.readValue(json, type); | ||
284 | + } catch (Exception e) { | ||
285 | + log.error("json to map exception!", e); | ||
286 | + } | ||
287 | + | ||
288 | + return null; | ||
289 | + } | ||
290 | + | ||
291 | + /** | ||
292 | + * object to json string | ||
293 | + * | ||
294 | + * @param object object | ||
295 | + * @return json string | ||
296 | + */ | ||
297 | + public static String toJsonString(Object object) { | ||
298 | + try { | ||
299 | + return objectMapper.writeValueAsString(object); | ||
300 | + } catch (Exception e) { | ||
301 | + throw new RuntimeException("Object json deserialization exception.", e); | ||
302 | + } | ||
303 | + } | ||
304 | + | ||
305 | + public static String toPrettyJsonString(Object object) { | ||
306 | + try { | ||
307 | + return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object); | ||
308 | + } catch (Exception e) { | ||
309 | + throw new RuntimeException("Object json deserialization exception.", e); | ||
310 | + } | ||
311 | + } | ||
312 | + | ||
313 | + /** | ||
314 | + * serialize to json byte | ||
315 | + * | ||
316 | + * @param obj object | ||
317 | + * @param <T> object type | ||
318 | + * @return byte array | ||
319 | + */ | ||
320 | + public static <T> byte[] toJsonByteArray(T obj) { | ||
321 | + if (obj == null) { | ||
322 | + return null; | ||
323 | + } | ||
324 | + String json = ""; | ||
325 | + try { | ||
326 | + json = toJsonString(obj); | ||
327 | + } catch (Exception e) { | ||
328 | + log.error("json serialize exception.", e); | ||
329 | + } | ||
330 | + | ||
331 | + return json.getBytes(UTF_8); | ||
332 | + } | ||
333 | + | ||
334 | + public static ObjectNode parseObject(String text) { | ||
335 | + try { | ||
336 | + if (StringUtils.isEmpty(text)) { | ||
337 | + return parseObject(text, ObjectNode.class); | ||
338 | + } else { | ||
339 | + return (ObjectNode) objectMapper.readTree(text); | ||
340 | + } | ||
341 | + } catch (Exception e) { | ||
342 | + throw new RuntimeException("String json deserialization exception.", e); | ||
343 | + } | ||
344 | + } | ||
345 | + | ||
346 | + public static ArrayNode parseArray(String text) { | ||
347 | + try { | ||
348 | + return (ArrayNode) objectMapper.readTree(text); | ||
349 | + } catch (Exception e) { | ||
350 | + throw new RuntimeException("Json deserialization exception.", e); | ||
351 | + } | ||
352 | + } | ||
353 | + | ||
354 | + /** | ||
355 | + * json serializer | ||
356 | + */ | ||
357 | + public static class JsonDataSerializer extends JsonSerializer<String> { | ||
358 | + | ||
359 | + @Override | ||
360 | + public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException { | ||
361 | + gen.writeRawValue(value); | ||
362 | + } | ||
363 | + | ||
364 | + } | ||
365 | + | ||
366 | + /** | ||
367 | + * json data deserializer | ||
368 | + */ | ||
369 | + public static class JsonDataDeserializer extends JsonDeserializer<String> { | ||
370 | + | ||
371 | + @Override | ||
372 | + public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
373 | + JsonNode node = p.getCodec().readTree(p); | ||
374 | + if (node instanceof TextNode) { | ||
375 | + return node.asText(); | ||
376 | + } else { | ||
377 | + return node.toString(); | ||
378 | + } | ||
379 | + } | ||
380 | + | ||
381 | + } | ||
382 | + | ||
383 | + public static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { | ||
384 | + | ||
385 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS); | ||
386 | + | ||
387 | + @Override | ||
388 | + public void serialize(LocalDateTime value, | ||
389 | + JsonGenerator gen, | ||
390 | + SerializerProvider serializers) throws IOException { | ||
391 | + gen.writeString(value.format(formatter)); | ||
392 | + } | ||
393 | + } | ||
394 | + | ||
395 | + public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { | ||
396 | + | ||
397 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS); | ||
398 | + | ||
399 | + @Override | ||
400 | + public LocalDateTime deserialize(JsonParser p, DeserializationContext context) throws IOException { | ||
401 | + return LocalDateTime.parse(p.getValueAsString(), formatter); | ||
402 | + } | ||
403 | + } | ||
404 | +} |
src/main/resources/application.yml
1 | +gat1400: | ||
2 | + server-address: 127.0.0.1 | ||
3 | + port: 9000 | ||
4 | + access-key: admin | ||
5 | + security-key: 123456 | ||
6 | +receiver: http://localhost:8888/receiver | ||
7 | +spring: | ||
8 | + application: | ||
9 | + name: viid-collector | ||
10 | + redis: | ||
11 | + host: 192.168.60.126 | ||
12 | + port: 6379 | ||
13 | + timeout: 10000ms | ||
14 | + lettuce: | ||
15 | + pool: | ||
16 | + max-active: 8 | ||
17 | + max-wait: -1ms | ||
18 | + min-idle: 0 | ||
19 | + max-idle: 8 | ||
20 | +logging: | ||
21 | + level: | ||
22 | + root: error | ||
23 | + com.objecteye: debug | ||
0 | \ No newline at end of file | 24 | \ No newline at end of file |