|
|
@@ -0,0 +1,580 @@
|
|
|
+package com.zksy.cockpit.display;
|
|
|
+
|
|
|
+import com.zksy.cockpit.foundation.CockpitCoordinateValidation;
|
|
|
+import com.zksy.cockpit.foundation.CockpitErrorCategory;
|
|
|
+import com.zksy.cockpit.foundation.CockpitErrorPayload;
|
|
|
+import com.zksy.cockpit.foundation.CockpitInvalidRequestException;
|
|
|
+import com.zksy.cockpit.foundation.CockpitReadException;
|
|
|
+import com.zksy.cockpit.foundation.CockpitSnapshotExpiredException;
|
|
|
+import com.zksy.cockpit.infrastructure.CockpitInfrastructureOperatingInformationAdapter;
|
|
|
+import com.zksy.cockpit.infrastructure.CockpitInfrastructureOperatingInformationSnapshot;
|
|
|
+import com.zksy.cockpit.map.CockpitMapSnapshotResponse;
|
|
|
+import com.zksy.cockpit.map.CockpitMapSnapshotService;
|
|
|
+import com.zksy.cockpit.map.CockpitMonitoringPointSummary;
|
|
|
+import com.zksy.cockpit.map.CockpitUnassociatedEquipmentSummary;
|
|
|
+import com.zksy.cockpit.point.CockpitMonitoringPointStatus;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.Clock;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.OffsetDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 服务端显示聚合:按 bbox + zoomBand 把点位/设备落到稳定地理桶,成员用游标分页。
|
|
|
+ *
|
|
|
+ * <p>地理桶的 groupKey 只由类别、缩放档、格坐标决定;同一快照下可复用、可验证,不依赖前端投影矩阵。
|
|
|
+ *
|
|
|
+ * <p>设施运营快照只有设施身份/状态,没有坐标;不能拿设备或点位坐标补齐。
|
|
|
+ * 因此设施类返回 UNAVAILABLE + reason + 来源版本,调用方保留既有设施图层,而不是假装完整。
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class CockpitMapDisplayService {
|
|
|
+ public static final String KIND_EQUIPMENT = "UNASSOCIATED_EQUIPMENT";
|
|
|
+ public static final String KIND_POINT = "MONITORING_POINT";
|
|
|
+ public static final String KIND_FACILITY = "FACILITY_POINT";
|
|
|
+
|
|
|
+ private static final int SNAPSHOT_PAGE_SIZE = 1000;
|
|
|
+ private static final int MAX_SNAPSHOT_PAGES = 1000;
|
|
|
+ private static final int DEFAULT_MEMBER_PAGE_SIZE = 20;
|
|
|
+ private static final int MAX_MEMBER_PAGE_SIZE = 100;
|
|
|
+ private static final int DEFAULT_CURSOR_TTL_SECONDS = 600;
|
|
|
+ private static final double MAX_BBOX_SPAN_DEGREES = 2.0;
|
|
|
+ private static final double METERS_PER_DEGREE = 111320.0;
|
|
|
+ private static final double[] CELL_METERS = new double[] {
|
|
|
+ 50000, 20000, 8000, 3000, 1000, 300, 100, 30, 10
|
|
|
+ };
|
|
|
+ private static final Map<String, Integer> DEVICE_PRIORITY = new HashMap<>();
|
|
|
+ private static final Map<String, String> DEVICE_LABEL = new HashMap<>();
|
|
|
+ static {
|
|
|
+ DEVICE_PRIORITY.put("ALARM", 1);
|
|
|
+ DEVICE_PRIORITY.put("WORK_ORDER", 2);
|
|
|
+ DEVICE_PRIORITY.put("WARNING", 3);
|
|
|
+ DEVICE_PRIORITY.put("NORMAL", 4);
|
|
|
+ DEVICE_PRIORITY.put("OFFLINE", 5);
|
|
|
+ DEVICE_PRIORITY.put("UNKNOWN", 6);
|
|
|
+ DEVICE_LABEL.put("ALARM", "报警");
|
|
|
+ DEVICE_LABEL.put("WORK_ORDER", "工单处置");
|
|
|
+ DEVICE_LABEL.put("WARNING", "预警");
|
|
|
+ DEVICE_LABEL.put("NORMAL", "正常");
|
|
|
+ DEVICE_LABEL.put("OFFLINE", "离线");
|
|
|
+ DEVICE_LABEL.put("UNKNOWN", "状态未发布");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private final CockpitMapSnapshotService mapSnapshotService;
|
|
|
+ private final CockpitInfrastructureOperatingInformationAdapter facilityAdapter;
|
|
|
+ private final Clock clock;
|
|
|
+ private final String cursorSecret;
|
|
|
+ private final int cursorTtlSeconds;
|
|
|
+
|
|
|
+ public CockpitMapDisplayService(
|
|
|
+ CockpitMapSnapshotService mapSnapshotService,
|
|
|
+ CockpitInfrastructureOperatingInformationAdapter facilityAdapter,
|
|
|
+ Clock clock,
|
|
|
+ @Value("${cockpit.display.cursor-secret:cockpit-display-cursor-v1}") String cursorSecret,
|
|
|
+ @Value("${cockpit.display.cursor-ttl-seconds:600}") int cursorTtlSeconds) {
|
|
|
+ this.mapSnapshotService = mapSnapshotService;
|
|
|
+ this.facilityAdapter = facilityAdapter;
|
|
|
+ this.clock = clock;
|
|
|
+ this.cursorSecret = cursorSecret == null ? "" : cursorSecret;
|
|
|
+ this.cursorTtlSeconds = cursorTtlSeconds < 1 ? DEFAULT_CURSOR_TTL_SECONDS : cursorTtlSeconds;
|
|
|
+ }
|
|
|
+
|
|
|
+ public CockpitMapDisplayResponse buckets(String requestId, String bboxText, Integer zoomBand,
|
|
|
+ String snapshotAt, String industry, String district, String deviceType, String pointStatus) {
|
|
|
+ BBox bbox = parseBbox(bboxText);
|
|
|
+ int band = validateZoomBand(zoomBand);
|
|
|
+ Rows rows = loadRows(requestId, snapshotAt, industry, district, deviceType, pointStatus);
|
|
|
+ String filterHash = CockpitMemberCursor.filterHash(industry, district, deviceType, pointStatus);
|
|
|
+ Instant now = clock.instant();
|
|
|
+
|
|
|
+ List<CockpitMapDisplayResponse.ClassBuckets> classes = new ArrayList<>();
|
|
|
+ CockpitMapDisplayResponse.ClassBuckets equipment = buildClass(
|
|
|
+ KIND_EQUIPMENT, rows.equipment, rows.complete, bbox, band, rows.snapshotAt, filterHash,
|
|
|
+ industry, district, deviceType, pointStatus, now);
|
|
|
+ CockpitMapDisplayResponse.ClassBuckets points = buildClass(
|
|
|
+ KIND_POINT, rows.points, rows.complete, bbox, band, rows.snapshotAt, filterHash,
|
|
|
+ industry, district, deviceType, pointStatus, now);
|
|
|
+ CockpitMapDisplayResponse.ClassBuckets facilities = new CockpitMapDisplayResponse.ClassBuckets(
|
|
|
+ KIND_FACILITY, facilityCoverage(), Collections.emptyList());
|
|
|
+ classes.add(equipment);
|
|
|
+ classes.add(points);
|
|
|
+ classes.add(facilities);
|
|
|
+
|
|
|
+ // 顶层 partial 只表示设备/监测点桶是否因快照页未取全;设施无坐标是**类别级**回退,
|
|
|
+ // 由 facilities.coverage 明确表达,不能让右上加载态永远停在 partial。
|
|
|
+ boolean partial = !"COMPLETE".equals(equipment.getCoverage().getState())
|
|
|
+ || !"COMPLETE".equals(points.getCoverage().getState());
|
|
|
+ CockpitMapSnapshotResponse.MapFilter filter = rows.filter;
|
|
|
+ CockpitMapDisplayResponse.Filters filters = new CockpitMapDisplayResponse.Filters(
|
|
|
+ filter == null ? null : filter.getIndustry(),
|
|
|
+ filter == null ? null : filter.getDistrict(),
|
|
|
+ filter == null ? null : filter.getDistrictName(),
|
|
|
+ filter == null ? null : filter.getDistrictFilter(),
|
|
|
+ filter == null ? null : filter.getDeviceType(),
|
|
|
+ filter == null ? null : filter.getPointStatus());
|
|
|
+ return new CockpitMapDisplayResponse(
|
|
|
+ requestId, rows.snapshotAt, rows.snapshotAt, band, bbox.echo(),
|
|
|
+ (int) cellMeters(band), partial, filters, classes);
|
|
|
+ }
|
|
|
+
|
|
|
+ public CockpitMapDisplayResponse.Members members(String requestId, String cursorToken,
|
|
|
+ String search, Integer pageSize) {
|
|
|
+ int safePageSize = pageSize == null ? DEFAULT_MEMBER_PAGE_SIZE : pageSize;
|
|
|
+ if (safePageSize < 1 || safePageSize > MAX_MEMBER_PAGE_SIZE) {
|
|
|
+ throw new CockpitInvalidRequestException("成员分页大小必须在 1 到 100 之间");
|
|
|
+ }
|
|
|
+ CockpitMemberCursor cursor = CockpitMemberCursor.decode(
|
|
|
+ cursorToken, cursorSecret, clock.instant());
|
|
|
+ if (!KIND_EQUIPMENT.equals(cursor.getKind()) && !KIND_POINT.equals(cursor.getKind())) {
|
|
|
+ throw new CockpitInvalidRequestException("成员游标类别不支持,请重新加载聚合桶");
|
|
|
+ }
|
|
|
+ String requestedSearch = normalizeSearch(search);
|
|
|
+ String effectiveSearch;
|
|
|
+ int offset;
|
|
|
+ if (!requestedSearch.isEmpty()) {
|
|
|
+ if (!cursor.getSearch().isEmpty() && !cursor.getSearch().equals(requestedSearch)) {
|
|
|
+ throw new CockpitInvalidRequestException("成员搜索条件与游标不一致,请重新加载聚合桶");
|
|
|
+ }
|
|
|
+ effectiveSearch = requestedSearch;
|
|
|
+ offset = cursor.getSearch().isEmpty() ? 0 : cursor.getOffset();
|
|
|
+ } else {
|
|
|
+ effectiveSearch = cursor.getSearch();
|
|
|
+ offset = cursor.getOffset();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CockpitMemberCursor.filterHash(cursor.getIndustry(), cursor.getDistrict(),
|
|
|
+ cursor.getDeviceType(), cursor.getPointStatus()).equals(cursor.getFilterHash())) {
|
|
|
+ throw new CockpitInvalidRequestException("成员游标筛选指纹不匹配,请重新加载聚合桶");
|
|
|
+ }
|
|
|
+ Rows rows = loadRows(requestId, cursor.getSnapshotAt(), cursor.getIndustry(), cursor.getDistrict(),
|
|
|
+ cursor.getDeviceType(), cursor.getPointStatus());
|
|
|
+ List<Row> classRows = KIND_EQUIPMENT.equals(cursor.getKind()) ? rows.equipment : rows.points;
|
|
|
+ List<Row> bucketRows = classRows.stream()
|
|
|
+ .filter(Row::mapped)
|
|
|
+ .filter(row -> groupKeyForBand(cursor.getGroupKey(), row))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (!effectiveSearch.isEmpty()) {
|
|
|
+ bucketRows = bucketRows.stream()
|
|
|
+ .filter(row -> matchesSearch(row, effectiveSearch))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ sortRows(bucketRows);
|
|
|
+ int total = bucketRows.size();
|
|
|
+ int from = Math.min(offset, total);
|
|
|
+ int to = Math.min(from + safePageSize, total);
|
|
|
+ List<CockpitMapDisplayResponse.Member> items = bucketRows.subList(from, to).stream()
|
|
|
+ .map(Row::member).collect(Collectors.toList());
|
|
|
+ String nextCursor = to < total
|
|
|
+ ? CockpitMemberCursor.encode(rows.snapshotAt, cursor.getKind(), cursor.getGroupKey(),
|
|
|
+ cursor.getFilterHash(), cursor.getIndustry(), cursor.getDistrict(),
|
|
|
+ cursor.getDeviceType(), cursor.getPointStatus(), effectiveSearch, to,
|
|
|
+ clock.instant(), cursorTtlSeconds, cursorSecret)
|
|
|
+ : null;
|
|
|
+ return new CockpitMapDisplayResponse.Members(requestId, rows.snapshotAt, cursor.getKind(),
|
|
|
+ cursor.getGroupKey(), effectiveSearch, from, safePageSize, total, items, nextCursor);
|
|
|
+ }
|
|
|
+
|
|
|
+ public CockpitErrorPayload invalidRequest(String requestId, String message) {
|
|
|
+ return new CockpitErrorPayload(CockpitErrorCategory.INVALID_REQUEST, message, requestId, businessTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ public CockpitErrorPayload cursorExpired(String requestId, String message) {
|
|
|
+ return new CockpitErrorPayload(CockpitErrorCategory.MEMBER_CURSOR_EXPIRED, message, requestId, businessTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ public CockpitErrorPayload snapshotExpired(String requestId) {
|
|
|
+ return new CockpitErrorPayload(CockpitErrorCategory.SNAPSHOT_EXPIRED,
|
|
|
+ "地图快照已失效,请重新加载", requestId, businessTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ public CockpitErrorPayload serverError(String requestId) {
|
|
|
+ return new CockpitErrorPayload(CockpitErrorCategory.SERVER_ERROR,
|
|
|
+ "显示聚合数据暂不可用", requestId, businessTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ private CockpitMapDisplayResponse.ClassBuckets buildClass(String kind, List<Row> source, boolean complete,
|
|
|
+ BBox bbox, int band, String snapshotAt, String filterHash,
|
|
|
+ String filterIndustry, String filterDistrict, String filterDeviceType, String filterPointStatus,
|
|
|
+ Instant now) {
|
|
|
+ List<Row> mapped = source.stream().filter(Row::mapped).collect(Collectors.toList());
|
|
|
+ Map<String, List<Row>> cells = new LinkedHashMap<>();
|
|
|
+ for (Row row : mapped) {
|
|
|
+ String key = groupKey(kind, band, row);
|
|
|
+ cells.computeIfAbsent(key, ignored -> new ArrayList<>()).add(row);
|
|
|
+ }
|
|
|
+ List<CockpitMapDisplayResponse.Bucket> buckets = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, List<Row>> entry : cells.entrySet()) {
|
|
|
+ CockpitMapDisplayResponse.Bounds cell = bounds(entry.getKey());
|
|
|
+ // 桶是完整地理格:只要格与视窗相交就返回整格成员,成员页才不会比桶计数多出界外点。
|
|
|
+ if (!bbox.intersects(cell)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ List<Row> members = entry.getValue();
|
|
|
+ sortRows(members);
|
|
|
+ String cursor = CockpitMemberCursor.encode(snapshotAt, kind, entry.getKey(), filterHash,
|
|
|
+ filterIndustry, filterDistrict, filterDeviceType, filterPointStatus,
|
|
|
+ "", 0, now, cursorTtlSeconds, cursorSecret);
|
|
|
+ buckets.add(new CockpitMapDisplayResponse.Bucket(entry.getKey(), kind, members.size(),
|
|
|
+ statusSummary(members, kind), cell, anchor(members), cursor));
|
|
|
+ }
|
|
|
+ buckets.sort(Comparator.comparing(CockpitMapDisplayResponse.Bucket::getGroupKey));
|
|
|
+ return new CockpitMapDisplayResponse.ClassBuckets(kind,
|
|
|
+ new CockpitMapDisplayResponse.Coverage(complete ? "COMPLETE" : "PARTIAL", null,
|
|
|
+ snapshotAt, source.size(), mapped.size()),
|
|
|
+ buckets);
|
|
|
+ }
|
|
|
+
|
|
|
+ private CockpitMapDisplayResponse.Coverage facilityCoverage() {
|
|
|
+ try {
|
|
|
+ CockpitInfrastructureOperatingInformationSnapshot snapshot = facilityAdapter.load();
|
|
|
+ if (snapshot == null) {
|
|
|
+ return new CockpitMapDisplayResponse.Coverage(
|
|
|
+ "UNAVAILABLE", "FACILITY_SOURCE_UNAVAILABLE", null, 0, 0);
|
|
|
+ }
|
|
|
+ Set<String> ids = new HashSet<>();
|
|
|
+ for (CockpitInfrastructureOperatingInformationSnapshot.SourceMetric metric : snapshot.getMetrics()) {
|
|
|
+ if (metric.getFacilityId() != null && !metric.getFacilityId().trim().isEmpty()) {
|
|
|
+ ids.add(metric.getFacilityId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new CockpitMapDisplayResponse.Coverage("UNAVAILABLE",
|
|
|
+ "FACILITY_COORDINATE_SOURCE_UNAVAILABLE", snapshot.getUpdatedAt(), ids.size(), 0);
|
|
|
+ } catch (RuntimeException exception) {
|
|
|
+ return new CockpitMapDisplayResponse.Coverage(
|
|
|
+ "UNAVAILABLE", "FACILITY_SOURCE_UNAVAILABLE", null, 0, 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Rows loadRows(String requestId, String snapshotAt, String industry, String district,
|
|
|
+ String deviceType, String pointStatus) {
|
|
|
+ List<CockpitMonitoringPointSummary> points = new ArrayList<>();
|
|
|
+ List<CockpitUnassociatedEquipmentSummary> equipment = new ArrayList<>();
|
|
|
+ String token = snapshotAt;
|
|
|
+ String actualSnapshotAt = null;
|
|
|
+ CockpitMapSnapshotResponse.MapFilter filter = null;
|
|
|
+ boolean complete = false;
|
|
|
+ for (int page = 1; page <= MAX_SNAPSHOT_PAGES; page += 1) {
|
|
|
+ CockpitMapSnapshotResponse response;
|
|
|
+ try {
|
|
|
+ response = mapSnapshotService.load(
|
|
|
+ requestId, page, SNAPSHOT_PAGE_SIZE, token,
|
|
|
+ industry, district, deviceType, pointStatus);
|
|
|
+ } catch (CockpitSnapshotExpiredException exception) {
|
|
|
+ // 保持旧 Controller 的错误载荷口径,不把底层英文/内部原因透给调用方。
|
|
|
+ throw new CockpitSnapshotExpiredException("地图快照已失效,请重新加载");
|
|
|
+ } catch (CockpitReadException exception) {
|
|
|
+ throw new CockpitReadException("显示聚合数据暂不可用");
|
|
|
+ }
|
|
|
+ actualSnapshotAt = response.getSnapshotAt();
|
|
|
+ filter = response.getMapFilter();
|
|
|
+ points.addAll(response.getMonitoringPoints());
|
|
|
+ equipment.addAll(response.getUnassociatedEquipment());
|
|
|
+ if (response.isComplete()) {
|
|
|
+ complete = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ token = response.getSnapshotAt();
|
|
|
+ }
|
|
|
+ if (actualSnapshotAt == null) {
|
|
|
+ throw new CockpitSnapshotExpiredException("地图快照已失效,请重新加载");
|
|
|
+ }
|
|
|
+ if (!complete) {
|
|
|
+ // 不静默截断:返回半份桶会让成员数和业务总数同时失真。
|
|
|
+ throw new CockpitReadException("显示聚合数据暂不可用");
|
|
|
+ }
|
|
|
+ return new Rows(actualSnapshotAt, true, points, equipment, filter);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CockpitMapDisplayResponse.StatusSummary statusSummary(List<Row> members, String kind) {
|
|
|
+ Map<String, Integer> counts = new LinkedHashMap<>();
|
|
|
+ for (Row row : members) {
|
|
|
+ counts.put(row.status, counts.getOrDefault(row.status, 0) + 1);
|
|
|
+ }
|
|
|
+ List<CockpitMapDisplayResponse.StatusCount> rows = counts.entrySet().stream()
|
|
|
+ .map(entry -> new CockpitMapDisplayResponse.StatusCount(entry.getKey(),
|
|
|
+ label(entry.getKey()), entry.getValue()))
|
|
|
+ .sorted(Comparator.comparingInt(item -> priority(kind, item.getStatus())))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (rows.isEmpty()) {
|
|
|
+ return new CockpitMapDisplayResponse.StatusSummary("UNKNOWN", "状态未发布", 0, 0, 0, rows);
|
|
|
+ }
|
|
|
+ CockpitMapDisplayResponse.StatusCount primary = rows.get(0);
|
|
|
+ int abnormal = 0;
|
|
|
+ for (CockpitMapDisplayResponse.StatusCount item : rows) {
|
|
|
+ if (priority(kind, item.getStatus()) <= 3) {
|
|
|
+ abnormal += item.getCount();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ int other = abnormal - (priority(kind, primary.getStatus()) <= 3 ? primary.getCount() : 0);
|
|
|
+ return new CockpitMapDisplayResponse.StatusSummary(primary.getStatus(), primary.getLabel(),
|
|
|
+ primary.getCount(), abnormal, Math.max(0, other), rows);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void sortRows(List<Row> rows) {
|
|
|
+ rows.sort(Comparator.comparingInt((Row row) -> row.priority)
|
|
|
+ .thenComparing(row -> safe(row.name))
|
|
|
+ .thenComparing(row -> safe(row.code)));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CockpitMapDisplayResponse.Anchor anchor(List<Row> rows) {
|
|
|
+ double longitude = 0;
|
|
|
+ double latitude = 0;
|
|
|
+ for (Row row : rows) {
|
|
|
+ longitude += row.longitude;
|
|
|
+ latitude += row.latitude;
|
|
|
+ }
|
|
|
+ return new CockpitMapDisplayResponse.Anchor(longitude / rows.size(), latitude / rows.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String groupKey(String kind, int band, Row row) {
|
|
|
+ double step = cellMeters(band) / METERS_PER_DEGREE;
|
|
|
+ int x = (int) Math.floor((row.longitude + 180.0) / step);
|
|
|
+ int y = (int) Math.floor((row.latitude + 90.0) / step);
|
|
|
+ return kind + "|" + band + "|" + x + "|" + y;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean groupKeyForBand(String groupKey, Row row) {
|
|
|
+ String[] parts = groupKey.split("\\|", -1);
|
|
|
+ if (parts.length != 4) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ int band = Integer.parseInt(parts[1]);
|
|
|
+ if (band < 0 || band >= CELL_METERS.length) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return groupKey(parts[0], band, row).equals(groupKey);
|
|
|
+ } catch (NumberFormatException exception) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CockpitMapDisplayResponse.Bounds bounds(String groupKey) {
|
|
|
+ String[] parts = groupKey.split("\\|", -1);
|
|
|
+ int band = Integer.parseInt(parts[1]);
|
|
|
+ int x = Integer.parseInt(parts[2]);
|
|
|
+ int y = Integer.parseInt(parts[3]);
|
|
|
+ double step = cellMeters(band) / METERS_PER_DEGREE;
|
|
|
+ double west = -180.0 + x * step;
|
|
|
+ double south = -90.0 + y * step;
|
|
|
+ return new CockpitMapDisplayResponse.Bounds(west, south, west + step, south + step);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean matchesSearch(Row row, String search) {
|
|
|
+ String needle = normalizeSearch(search);
|
|
|
+ return safe(row.name).toLowerCase().contains(needle)
|
|
|
+ || safe(row.code).toLowerCase().contains(needle);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String normalizeSearch(String value) {
|
|
|
+ return value == null ? "" : value.trim().toLowerCase();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int validateZoomBand(Integer zoomBand) {
|
|
|
+ if (zoomBand == null || zoomBand < 0 || zoomBand >= CELL_METERS.length) {
|
|
|
+ throw new CockpitInvalidRequestException("zoomBand 必须在 0 到 8 之间");
|
|
|
+ }
|
|
|
+ return zoomBand;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static double cellMeters(int band) {
|
|
|
+ return CELL_METERS[band];
|
|
|
+ }
|
|
|
+
|
|
|
+ private static BBox parseBbox(String value) {
|
|
|
+ if (value == null || value.trim().isEmpty()) {
|
|
|
+ throw new CockpitInvalidRequestException("bbox 不能为空,格式为 west,south,east,north");
|
|
|
+ }
|
|
|
+ String[] parts = value.split(",", -1);
|
|
|
+ if (parts.length != 4) {
|
|
|
+ throw new CockpitInvalidRequestException("bbox 格式不合法,应为 west,south,east,north");
|
|
|
+ }
|
|
|
+ BigDecimal west;
|
|
|
+ BigDecimal south;
|
|
|
+ BigDecimal east;
|
|
|
+ BigDecimal north;
|
|
|
+ try {
|
|
|
+ west = new BigDecimal(parts[0].trim());
|
|
|
+ south = new BigDecimal(parts[1].trim());
|
|
|
+ east = new BigDecimal(parts[2].trim());
|
|
|
+ north = new BigDecimal(parts[3].trim());
|
|
|
+ } catch (NumberFormatException exception) {
|
|
|
+ throw new CockpitInvalidRequestException("bbox 坐标必须是数字");
|
|
|
+ }
|
|
|
+ if (west.doubleValue() < -180 || east.doubleValue() > 180
|
|
|
+ || south.doubleValue() < -90 || north.doubleValue() > 90
|
|
|
+ || west.compareTo(east) >= 0 || south.compareTo(north) >= 0) {
|
|
|
+ throw new CockpitInvalidRequestException("bbox 坐标越界或顺序不合法");
|
|
|
+ }
|
|
|
+ if (east.subtract(west).doubleValue() > MAX_BBOX_SPAN_DEGREES
|
|
|
+ || north.subtract(south).doubleValue() > MAX_BBOX_SPAN_DEGREES) {
|
|
|
+ throw new CockpitInvalidRequestException("bbox 范围不能超过 2 度");
|
|
|
+ }
|
|
|
+ return new BBox(west.doubleValue(), south.doubleValue(), east.doubleValue(), north.doubleValue(),
|
|
|
+ west.stripTrailingZeros().toPlainString() + "," + south.stripTrailingZeros().toPlainString()
|
|
|
+ + "," + east.stripTrailingZeros().toPlainString() + "," + north.stripTrailingZeros().toPlainString());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String label(String status) {
|
|
|
+ String label = DEVICE_LABEL.get(status);
|
|
|
+ if (label != null) {
|
|
|
+ return label;
|
|
|
+ }
|
|
|
+ for (CockpitMonitoringPointStatus item : CockpitMonitoringPointStatus.values()) {
|
|
|
+ if (item.name().equals(status)) {
|
|
|
+ return item.getLabel();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "状态未发布";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int priority(String kind, String status) {
|
|
|
+ if (KIND_POINT.equals(kind)) {
|
|
|
+ for (CockpitMonitoringPointStatus item : CockpitMonitoringPointStatus.values()) {
|
|
|
+ if (item.name().equals(status)) {
|
|
|
+ return item.getPriority();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 999;
|
|
|
+ }
|
|
|
+ return DEVICE_PRIORITY.getOrDefault(status, 999);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String businessTime() {
|
|
|
+ return OffsetDateTime.now(clock).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String safe(String value) {
|
|
|
+ return value == null ? "" : value;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class Row {
|
|
|
+ private final String kind;
|
|
|
+ private final String code;
|
|
|
+ private final String name;
|
|
|
+ private final String status;
|
|
|
+ private final String statusLabel;
|
|
|
+ private final int priority;
|
|
|
+ private final double longitude;
|
|
|
+ private final double latitude;
|
|
|
+ private final String industry;
|
|
|
+ private final String typeCode;
|
|
|
+ private final String typeName;
|
|
|
+ private final Integer district;
|
|
|
+ private final String districtName;
|
|
|
+ private final boolean mapped;
|
|
|
+
|
|
|
+ private Row(String kind, String code, String name, String status, String statusLabel, int priority,
|
|
|
+ double longitude, double latitude, String industry, String typeCode, String typeName,
|
|
|
+ Integer district, String districtName, boolean mapped) {
|
|
|
+ this.kind = kind;
|
|
|
+ this.code = code;
|
|
|
+ this.name = name;
|
|
|
+ this.status = status;
|
|
|
+ this.statusLabel = statusLabel;
|
|
|
+ this.priority = priority;
|
|
|
+ this.longitude = longitude;
|
|
|
+ this.latitude = latitude;
|
|
|
+ this.industry = industry;
|
|
|
+ this.typeCode = typeCode;
|
|
|
+ this.typeName = typeName;
|
|
|
+ this.district = district;
|
|
|
+ this.districtName = districtName;
|
|
|
+ this.mapped = mapped;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean mapped() { return mapped; }
|
|
|
+
|
|
|
+ private CockpitMapDisplayResponse.Member member() {
|
|
|
+ return new CockpitMapDisplayResponse.Member(kind, code, name, status, statusLabel, priority,
|
|
|
+ longitude, latitude, industry, typeCode, typeName, district, districtName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class Rows {
|
|
|
+ private final String snapshotAt;
|
|
|
+ private final boolean complete;
|
|
|
+ private final List<Row> points;
|
|
|
+ private final List<Row> equipment;
|
|
|
+ private final CockpitMapSnapshotResponse.MapFilter filter;
|
|
|
+
|
|
|
+ private Rows(String snapshotAt, boolean complete, List<CockpitMonitoringPointSummary> points,
|
|
|
+ List<CockpitUnassociatedEquipmentSummary> equipment,
|
|
|
+ CockpitMapSnapshotResponse.MapFilter filter) {
|
|
|
+ this.snapshotAt = snapshotAt;
|
|
|
+ this.complete = complete;
|
|
|
+ this.points = points.stream().map(CockpitMapDisplayService::pointRow).collect(Collectors.toList());
|
|
|
+ this.equipment = equipment.stream().map(CockpitMapDisplayService::equipmentRow).collect(Collectors.toList());
|
|
|
+ this.filter = filter;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Row pointRow(CockpitMonitoringPointSummary point) {
|
|
|
+ CockpitCoordinateValidation validation = point.getCoordinateValidation();
|
|
|
+ boolean mapped = validation != null && validation.isRenderable()
|
|
|
+ && point.getLongitude() != null && point.getLatitude() != null;
|
|
|
+ CockpitMonitoringPointStatus status = point.getPointStatus();
|
|
|
+ return new Row(KIND_POINT, point.getPointCode(), point.getPointName(),
|
|
|
+ status == null ? "UNKNOWN" : status.name(),
|
|
|
+ status == null ? "状态未发布" : status.getLabel(),
|
|
|
+ status == null ? 999 : status.getPriority(),
|
|
|
+ number(point.getLongitude()), number(point.getLatitude()),
|
|
|
+ point.getIndustry(), point.getTypeCode(), point.getTypeName(),
|
|
|
+ point.getDistrict(), point.getDistrictName(), mapped);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Row equipmentRow(CockpitUnassociatedEquipmentSummary equipment) {
|
|
|
+ CockpitCoordinateValidation validation = equipment.getCoordinateValidation();
|
|
|
+ boolean mapped = validation != null && validation.isRenderable()
|
|
|
+ && equipment.getLongitude() != null && equipment.getLatitude() != null;
|
|
|
+ String status = equipment.getDeviceStatus() == null ? "UNKNOWN" : equipment.getDeviceStatus();
|
|
|
+ return new Row(KIND_EQUIPMENT, equipment.getDeviceCode(), equipment.getDeviceName(),
|
|
|
+ status, label(status), priority(KIND_EQUIPMENT, status),
|
|
|
+ number(equipment.getLongitude()), number(equipment.getLatitude()),
|
|
|
+ equipment.getIndustry(), equipment.getTypeCode(), equipment.getTypeName(),
|
|
|
+ equipment.getDistrict(), equipment.getDistrictName(), mapped);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static double number(BigDecimal value) {
|
|
|
+ return value == null ? 0 : value.doubleValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class BBox {
|
|
|
+ private final double west;
|
|
|
+ private final double south;
|
|
|
+ private final double east;
|
|
|
+ private final double north;
|
|
|
+ private final String echo;
|
|
|
+
|
|
|
+ private BBox(double west, double south, double east, double north, String echo) {
|
|
|
+ this.west = west;
|
|
|
+ this.south = south;
|
|
|
+ this.east = east;
|
|
|
+ this.north = north;
|
|
|
+ this.echo = echo;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean intersects(CockpitMapDisplayResponse.Bounds bounds) {
|
|
|
+ return bounds.getEast() >= west && bounds.getWest() <= east
|
|
|
+ && bounds.getNorth() >= south && bounds.getSouth() <= north;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String echo() { return echo; }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|