|
|
|
@ -0,0 +1,147 @@ |
|
|
|
package com.inspect.api.service; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.inspect.api.domain.ApiResult; |
|
|
|
import com.inspect.api.domain.AuthTokenData; |
|
|
|
import com.inspect.base.core.utils.DateUtils; |
|
|
|
import com.inspect.base.core.utils.StringUtils; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.net.HttpURLConnection; |
|
|
|
import java.net.URL; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
/** |
|
|
|
* 动环删除数据推送服务(通知中恒) |
|
|
|
* <p> |
|
|
|
* 删除任务状态数据后,POST jb-nessp-external/api/mqtt/sendTopic 发送 MQTT 消息: |
|
|
|
* 请求头携带 joinbright-token(复用 {@link ExtAuthService}), |
|
|
|
* body 为 {@code {"message":"业务消息JSON字符串","topic":"topic/env/delete"}}。 |
|
|
|
* <p> |
|
|
|
* 业务消息字段见《动环删除数据推送信息字段说明》: |
|
|
|
* <pre> |
|
|
|
* {"idType":"TASK","stationCode":"CZ000000004","ids":["123456789"],"deleteTime":"2026-08-19 10:00:00"} |
|
|
|
* </pre> |
|
|
|
* 推送失败只记日志,不影响删除结果。 |
|
|
|
*/ |
|
|
|
@Slf4j |
|
|
|
@Service |
|
|
|
public class TaskStatusDeletePushService { |
|
|
|
|
|
|
|
/** token 请求头名称(外部平台固定) */ |
|
|
|
private static final String TOKEN_HEADER = "joinbright-token"; |
|
|
|
|
|
|
|
/** MQTT 消息发送接口地址 */ |
|
|
|
@Value("${inspect.taskStatus-delete-push.send-topic-url:http://192.168.129.2:32799/jb-gateway/jb-nessp-external/api/mqtt/sendTopic}") |
|
|
|
private String sendTopicUrl; |
|
|
|
|
|
|
|
/** 推送主题 */ |
|
|
|
@Value("${inspect.taskStatus-delete-push.topic:topic/env/delete}") |
|
|
|
private String topic; |
|
|
|
|
|
|
|
/** http 连接/读取超时(秒) */ |
|
|
|
@Value("${inspect.taskStatus-delete-push.http-timeout-seconds:30}") |
|
|
|
private int httpTimeoutSeconds; |
|
|
|
|
|
|
|
private final ExtAuthService extAuthService; |
|
|
|
|
|
|
|
public TaskStatusDeletePushService(ExtAuthService extAuthService) { |
|
|
|
this.extAuthService = extAuthService; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 推送删除消息,ids 取 patrol_task_status.task_patrolled_id |
|
|
|
* |
|
|
|
* @return 是否推送成功 |
|
|
|
*/ |
|
|
|
public boolean pushTaskDelete(String stationCode, List<String> taskPatrolledIds) { |
|
|
|
if (StringUtils.isEmpty(stationCode) || taskPatrolledIds == null || taskPatrolledIds.isEmpty()) { |
|
|
|
log.error("[TASK_STATUS_DELETE] 推送参数为空, stationCode: {}, ids: {}", stationCode, taskPatrolledIds); |
|
|
|
return false; |
|
|
|
} |
|
|
|
// 业务消息体 |
|
|
|
JSONObject message = new JSONObject(true); |
|
|
|
message.put("idType", "TASK"); |
|
|
|
message.put("stationCode", stationCode); |
|
|
|
message.put("ids", new JSONArray(new ArrayList<>(taskPatrolledIds))); |
|
|
|
message.put("deleteTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date())); |
|
|
|
|
|
|
|
// 接口报文 |
|
|
|
JSONObject body = new JSONObject(true); |
|
|
|
body.put("message", message.toJSONString()); |
|
|
|
body.put("topic", topic); |
|
|
|
|
|
|
|
String requestJson = body.toJSONString(); |
|
|
|
HttpURLConnection conn = null; |
|
|
|
try { |
|
|
|
conn = (HttpURLConnection) new URL(sendTopicUrl).openConnection(); |
|
|
|
conn.setRequestMethod("POST"); |
|
|
|
conn.setDoOutput(true); |
|
|
|
conn.setConnectTimeout(httpTimeoutSeconds * 1000); |
|
|
|
conn.setReadTimeout(httpTimeoutSeconds * 1000); |
|
|
|
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); |
|
|
|
conn.setRequestProperty(TOKEN_HEADER, getToken()); |
|
|
|
|
|
|
|
try (OutputStream out = conn.getOutputStream()) { |
|
|
|
out.write(requestJson.getBytes(StandardCharsets.UTF_8)); |
|
|
|
out.flush(); |
|
|
|
} |
|
|
|
|
|
|
|
int responseCode = conn.getResponseCode(); |
|
|
|
String response = readResponse(conn, responseCode); |
|
|
|
log.info("[TASK_STATUS_DELETE] 推送响应: code={}, body={}, request={}", responseCode, response, requestJson); |
|
|
|
if (responseCode == 200) { |
|
|
|
JSONObject resp = JSONObject.parseObject(response); |
|
|
|
if (resp != null && Boolean.TRUE.equals(resp.getBoolean("success"))) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
log.error("[TASK_STATUS_DELETE] 上传业务失败: {}", response); |
|
|
|
} else { |
|
|
|
log.error("[TASK_STATUS_DELETE] 上传HTTP失败: {}, body: {}", responseCode, response); |
|
|
|
} |
|
|
|
return false; |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("[TASK_STATUS_DELETE] 推送删除消息异常, url: {}", sendTopicUrl, e); |
|
|
|
return false; |
|
|
|
} finally { |
|
|
|
if (conn != null) { |
|
|
|
conn.disconnect(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** 获取 Joinbright-Token */ |
|
|
|
private String getToken() { |
|
|
|
ApiResult<AuthTokenData> result = extAuthService.getAuthToken(); |
|
|
|
if (result == null || !result.isSuccess() || result.getData() == null) { |
|
|
|
log.error("[TASK_STATUS_DELETE] 获取 Joinbright-Token 失败: {}", result); |
|
|
|
return ""; |
|
|
|
} |
|
|
|
return result.getData().getAccess_token(); |
|
|
|
} |
|
|
|
|
|
|
|
private String readResponse(HttpURLConnection conn, int code) throws Exception { |
|
|
|
InputStream in = code >= 400 ? conn.getErrorStream() : conn.getInputStream(); |
|
|
|
if (in == null) { |
|
|
|
return ""; |
|
|
|
} |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) { |
|
|
|
String line; |
|
|
|
while ((line = reader.readLine()) != null) { |
|
|
|
sb.append(line); |
|
|
|
} |
|
|
|
} |
|
|
|
return sb.toString(); |
|
|
|
} |
|
|
|
} |