From 8f43ce26799c7c1b490507061fcc3c7d1b9e84e2 Mon Sep 17 00:00:00 2001 From: whycxzp <glperry@163.com> Date: 星期四, 10 四月 2025 15:30:37 +0800 Subject: [PATCH] 电池温度告警更新 --- src/main/java/com/whyc/service/BattStationTempAlarmService.java | 66 +++++++++++++ src/main/java/com/whyc/service/BattMonsLocationService.java | 6 + src/main/java/com/whyc/pojo/db_alarm/BattStationTempAlarm.java | 9 + src/main/java/com/whyc/util/PointUtil.java | 181 +++++++++++++---------------------- 4 files changed, 147 insertions(+), 115 deletions(-) diff --git a/src/main/java/com/whyc/pojo/db_alarm/BattStationTempAlarm.java b/src/main/java/com/whyc/pojo/db_alarm/BattStationTempAlarm.java index 5114624..b521b84 100644 --- a/src/main/java/com/whyc/pojo/db_alarm/BattStationTempAlarm.java +++ b/src/main/java/com/whyc/pojo/db_alarm/BattStationTempAlarm.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import com.whyc.pojo.db_batt.BattMonsLocation; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -55,12 +56,16 @@ @TableField(exist = false) private String powerName; - @TableField(exist = false) + /*@TableField(exist = false) @ApiModelProperty("鏈�楂樻俯搴︾偣鍦ㄧ鏉$殑鏂逛綅") private String position; @TableField(exist = false) - private double pointPercentage; + private double pointPercentage;*/ + + @TableField(exist = false) + @ApiModelProperty("鐢垫睜鍗曚綋瀹氫綅淇℃伅") + private BattMonsLocation battMonsLocation; } diff --git a/src/main/java/com/whyc/service/BattMonsLocationService.java b/src/main/java/com/whyc/service/BattMonsLocationService.java index d3cd540..244acc9 100644 --- a/src/main/java/com/whyc/service/BattMonsLocationService.java +++ b/src/main/java/com/whyc/service/BattMonsLocationService.java @@ -37,4 +37,10 @@ mapper.updateById(battMonsLocation); return new Response().setII(1,"淇敼瀹屾垚"); } + + public List<BattMonsLocation> getList() { + QueryWrapper<BattMonsLocation> query = Wrappers.query(); + query.orderByAsc("id"); + return mapper.selectList(query); + } } diff --git a/src/main/java/com/whyc/service/BattStationTempAlarmService.java b/src/main/java/com/whyc/service/BattStationTempAlarmService.java index a9d4e23..cafb8e0 100644 --- a/src/main/java/com/whyc/service/BattStationTempAlarmService.java +++ b/src/main/java/com/whyc/service/BattStationTempAlarmService.java @@ -8,18 +8,25 @@ import com.whyc.dto.Response; import com.whyc.mapper.BattStationTempAlarmMapper; import com.whyc.pojo.db_alarm.BattStationTempAlarm; +import com.whyc.pojo.db_batt.BattMonsLocation; import com.whyc.util.HikTempUtil; import com.whyc.util.PointUtil; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; +import java.util.stream.Collectors; @Service public class BattStationTempAlarmService { @Resource private BattStationTempAlarmMapper mapper; + + @Autowired + private BattMonsLocationService battMonsLocationService; + public Response<PageInfo<BattStationTempAlarm>> getPage(int pageNum, int pageSize, Integer level) { PageHelper.startPage(pageNum,pageSize); @@ -55,10 +62,69 @@ alarm.setPointPercentage(pointPercentage); });*/ + /**鍒ゆ柇 鏈�楂樻俯搴︾偣 鍦� 鍥涗釜鐐瑰舰鎴愮殑妗� 鍐呴儴*/ + List<BattMonsLocation> batteryLocations = battMonsLocationService.getList(); + list.forEach(alarm->{ + String maxTempPointStr = alarm.getMaxTempPoint(); + if(maxTempPointStr.contains(",")){ // 涓囦竴鏈夊涓偣,鍙栫涓�涓偣 + maxTempPointStr = maxTempPointStr.split(",")[0]; + } + String[] split = maxTempPointStr.split("\\*"); + Point tempPoint = new Point(Integer.valueOf(split[0]),Integer.valueOf(split[1])); + List<BattMonsLocation> battGroupLocations = batteryLocations.stream().filter(location -> location.getBattGroupId().intValue() == alarm.getBattGroupId()).collect(Collectors.toList()); + for (int i = 0; i < battGroupLocations.size(); i++) { + BattMonsLocation battMonsLocation = batteryLocations.get(i); + String battMonsPoints = battMonsLocation.getBattMonsPoints(); + //battMonsPoints涓哄洓杈瑰舰鐨勫洓涓潗鏍囩偣,绀轰緥: x1*y1,x2*y2,x3*y3,x4*y4 + + // 瑙f瀽鍥涜竟褰㈢殑鍥涗釜椤剁偣鍧愭爣 + String[] points = battMonsPoints.split(","); + int[] pointsX = new int[4]; + int[] pointsY = new int[4]; + for (int j = 0; j < points.length; j++) { + String[] xy = points[j].split("\\*"); + pointsX[j] = Integer.parseInt(xy[0]); + pointsY[j] = Integer.parseInt(xy[1]); + } + + // 鍒ゆ柇鐐规槸鍚﹀湪鍥涜竟褰㈠唴 + boolean isInside = PointUtil.isPointInQuadrilateral(tempPoint.getX(), tempPoint.getY(),pointsX, pointsY); + //System.out.println("鐐规槸鍚﹀湪鍥涜竟褰㈠唴: " + isInside); + if(isInside){ + alarm.setBattMonsLocation(battMonsLocation); + break; + } + } + + }); PageInfo<BattStationTempAlarm> pageInfo = new PageInfo<>(list); return new Response<PageInfo<BattStationTempAlarm>>().set(1,pageInfo); } + public static void main(String[] args) { + String battMonsPoints = "0*0,0*4,4*4,4*0"; + String tempPointStr = "1*4"; + + // 瑙f瀽鍥涜竟褰㈢殑鍥涗釜椤剁偣鍧愭爣 + String[] points = battMonsPoints.split(","); + int[] pointsX = new int[4]; + int[] pointsY = new int[4]; + for (int i = 0; i < points.length; i++) { + String[] xy = points[i].split("\\*"); + pointsX[i] = Integer.parseInt(xy[0]); + pointsY[i] = Integer.parseInt(xy[1]); + } + + // 瑙f瀽寰呭垽鏂殑鐐瑰潗鏍� + String[] tempPointArray = tempPointStr.split("\\*"); + int tx = Integer.parseInt(tempPointArray[0]); + int ty = Integer.parseInt(tempPointArray[1]); + + // 鍒ゆ柇鐐规槸鍚﹀湪鍥涜竟褰㈠唴 + boolean isInside = PointUtil.isPointInQuadrilateral(tx, ty,pointsX, pointsY); + System.out.println("鐐规槸鍚﹀湪鍥涜竟褰㈠唴: " + isInside); + } + public Response<PageInfo<BattStationTempAlarm>> getHisPage(int pageNum, int pageSize, Integer level) { PageHelper.startPage(pageNum,pageSize); List<BattStationTempAlarm> list = mapper.getHisList(level); diff --git a/src/main/java/com/whyc/util/PointUtil.java b/src/main/java/com/whyc/util/PointUtil.java index 0289312..3cd42ee 100644 --- a/src/main/java/com/whyc/util/PointUtil.java +++ b/src/main/java/com/whyc/util/PointUtil.java @@ -4,133 +4,88 @@ public class PointUtil { public static void main(String[] args) { - /*double a1 = 1, b1 = 2; - double a2 = 3, b2 = 4; - double x0 = 9, y0 = 9; - Point p1 = new Point(1, 2); - Point p2 = new Point(3, 4); - Point p0 = new Point(9, 9); + int[] x = {0, 8, 8, 0}; // 鍥涜竟褰㈤《鐐箈鍧愭爣 + int[] y = {0, 0, 8, 8}; // 鍥涜竟褰㈤《鐐箉鍧愭爣 + int px = 9, py = 9; // 寰呮娴嬬偣鍧愭爣 - Point p = findIntersection2(p1, p2, p0); - System.out.println(p); - - double[] p3 = findIntersection(a1, b1, a2, b2, x0, y0); - System.out.println("Intersection Point P3: (" + p3[0] + ", " + p3[1] + ")"); - - String position = determinePosition(a1, b1, a2, b2, x0, y0); - System.out.println("Point P is on the " + position + " of the line P1P2.");*/ - double x0 = 5, y0 = 5; - double x1 = 0, y1 = 0; - double x2 = 2, y2 = 2; - - double proportion = calculateProportion(x0, y0, x1, y1, x2, y2); - if (proportion >= 0) { - System.out.println("鍨傝冻鍒癿stPointStart鐨勮窛绂诲崰鏁存潯绾挎闀垮害鐨勬瘮渚嬫槸: " + proportion); + if (isPointInQuadrilateral(px, py, x, y)) { + System.out.println("鐐瑰湪鍥涜竟褰㈠唴鎴栬竟涓�"); } else { - System.out.println("鍨傝冻涓嶅湪绾挎涓�"); - } - } - - public static double[] findIntersection(double a1, double b1, double a2, double b2, double x0, double y0) { - // 妫�鏌� p1p2 鏄惁涓哄瀭鐩寸嚎 - if (a2 == a1) { - return new double[]{a1, y0}; - } - - // 妫�鏌� p1p2 鏄惁涓烘按骞崇嚎 - if (b2 == b1) { - return new double[]{x0, b1}; - } - double m = (b2 - b1) / (a2 - a1); - double mPerpendicular = (a1 - a2) / (b2 - b1); - - double x = ((b1 - m * a1) - (y0 - mPerpendicular * x0)) / (mPerpendicular - m); - double y = m * x + (b1 - m * a1); - - return new double[]{x, y}; - } - - public static Point findIntersection2(Point p1, Point p2, Point p0) { - // 妫�鏌� p1p2 鏄惁涓哄瀭鐩寸嚎 - int a1 = p1.getX(); - int b1 = p1.getY(); - - int a2 = p2.getX(); - int b2 = p2.getY(); - - int x0 = p0.getX(); - int y0 = p0.getY(); - - if (a2 == a1) { - return new Point(a1, y0); - } - - // 妫�鏌� p1p2 鏄惁涓烘按骞崇嚎 - if (b2 == b1) { - return new Point(x0, b1); - } - double m = (b2 - b1) / (a2 - a1); - double mPerpendicular = (a1 - a2) / (b2 - b1); - - double x = ((b1 - m * a1) - (y0 - mPerpendicular * x0)) / (mPerpendicular - m); - double y = m * x + (b1 - m * a1); - - return new Point((int) x, (int) y); - } - public static String determinePosition(double a1, double b1, double a2, double b2, double x0, double y0) { - double cross = (a2 - a1) * (y0 - b1) - (x0 - a1) * (b2 - b1); - if (cross > 0) { - return "left"; - } else if (cross < 0) { - return "right"; - } else { - return "line"; + System.out.println("鐐瑰湪鍥涜竟褰㈠"); } } /** - * 璁$畻鐐瑰埌绾挎鐨勮窛绂诲崰绾挎闀垮害鐨勬瘮渚� - * @param x0 鍨傝冻x鐐� - * @param y0 鍨傝冻y鐐� - * @param x1 璧风偣x鐐� - * @param y1 璧风偣y鐐� - * @param x2 缁堢偣x鐐� - * @param y2 缁堢偣y鐐� + * 鍒ゆ柇鐐规槸鍚﹀湪鍥涜竟褰㈠唴鎴栬竟涓� + * @param px 寰呮祴鐐圭殑x + * @param py 寰呮祴鐐圭殑y + * @param x 鍥涗釜鐐圭殑x鏁扮粍 鍥涗釜鐐瑰繀椤绘槸椤烘椂閽堥『搴�,浠庡乏涓婅寮�濮嬮『鏃堕拡 + * @param y 鍥涗釜鐐圭殑y鏁扮粍 * @return */ - public static double calculateProportion(double x0, double y0, double x1, double y1, double x2, double y2) { - // 鍚戦噺 AB - double ABx = x2 - x1; - double ABy = y2 - y1; + public static boolean isPointInQuadrilateral(int px, int py, int[] x, int[] y) { + int n = 4; // 鍥涜竟褰㈢殑杈规暟 - // 鍚戦噺 AP - double APx = x0 - x1; - double APy = y0 - y1; - - // 鍚戦噺绉� AB 脳 AP - double crossProduct = ABx * APy - APx * ABy; - - if (crossProduct == 0) { - // 鐐瑰湪鐩寸嚎涓婏紝鍒ゆ柇鏄惁鍦ㄧ嚎娈典笂 - double dotProduct = APx * ABx + APy * ABy; - double squaredLengthAB = ABx * ABx + ABy * ABy; - double t = dotProduct / squaredLengthAB; - - if (t >= 0 && t <= 1) { - return t; // 鍨傝冻鍒癿stPointStart鐨勮窛绂诲崰鏁存潯绾挎闀垮害鐨勬瘮渚� - } else if (t < 0) { - //鐐瑰湪mstPointStart鐨勫欢闀跨嚎涓� - return 0; - } else { - //鐐瑰湪mstPointEnd鐨勫欢闀跨嚎涓� - return 1; + // 妫�鏌ョ偣鏄惁鍦ㄩ《鐐逛笂 + for (int i = 0; i < n; i++) { + if (px == x[i] && py == y[i]) { + return true; } - } else { - return -1; // 鐐逛笉鍦ㄧ洿绾夸笂 } + + // 妫�鏌ョ偣鏄惁鍦ㄨ竟涓� + for (int i = 0; i < n; i++) { + int j = (i + 1) % n; + if (isPointOnSegment(px, py, x[i], y[i], x[j], y[j])) { + return true; + } + } + + // 浣跨敤灏勭嚎娉曞垽鏂偣鏄惁鍦ㄥ嚫鍥涜竟褰㈠唴 + int intersections = 0; + + for (int i = 0; i < n; i++) { + int j = (i + 1) % n; + if (rayIntersectsSegment(px, py, x[i], y[i], x[j], y[j])) { + intersections++; + } + } + + return intersections % 2 != 0; } + private static boolean isPointOnSegment(int px, int py, int x1, int y1, int x2, int y2) { + // 妫�鏌ョ偣鏄惁鍦▁1, y1 鍜� x2, y2 褰㈡垚鐨勭嚎娈典笂 + if (px < Math.min(x1, x2) || px > Math.max(x1, x2) || py < Math.min(y1, y2) || py > Math.max(y1, y2)) { + return false; + } + // 鍚戦噺鍙夌Н + int crossProduct = (px - x1) * (y2 - y1) - (py - y1) * (x2 - x1); + // 鍚戦噺鐐圭Н + int dotProduct1 = (px - x1) * (x2 - x1) + (py - y1) * (y2 - y1); + int dotProduct2 = (px - x2) * (x1 - x2) + (py - y2) * (y1 - y2); + + return crossProduct == 0 && dotProduct1 >= 0 && dotProduct2 >= 0; + } + + private static boolean rayIntersectsSegment(int px, int py, int x1, int y1, int x2, int y2) { + // 纭繚 y1 <= y2 + if (y1 > y2) { + return rayIntersectsSegment(px, py, x2, y2, x1, y1); + } + + // 灏勭嚎涓嶄笌绾挎骞宠涓旂偣鍦▂鑼冨洿鍐� + if (py > y1 && py <= y2) { + // 璁$畻浜ょ偣x鍧愭爣 + double xIntersection = (double) (px - x1) * (y2 - y1) / (double) (py - y1) + x1; + + // 灏勭嚎浠庡乏鍒板彸锛屼笖浜ょ偣鍦ㄧ偣鐨勫彸渚� + return xIntersection >= px; + } + + return false; + } } -- Gitblit v1.9.1