This commit is contained in:
磷叶 2025-04-29 16:28:48 +08:00
parent 6f3bb42742
commit 578342ec14
7 changed files with 44 additions and 44 deletions

View File

@ -1,6 +1,7 @@
package com.ruoyi.common.utils; package com.ruoyi.common.utils;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
/** /**
* @author wjh * @author wjh
@ -160,7 +161,7 @@ public class MathUtils {
if (a == null || b == null) { if (a == null || b == null) {
return false; return false;
} }
return equals(a.setScale(precision, BigDecimal.ROUND_HALF_UP), b.setScale(precision, BigDecimal.ROUND_HALF_UP)); return equals(a.setScale(precision, RoundingMode.HALF_UP), b.setScale(precision, RoundingMode.HALF_UP));
} }
// 获取最大值 // 获取最大值

View File

@ -55,4 +55,8 @@ public class DeviceQuery extends DeviceVO {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime lastTimeEnd; private LocalDateTime lastTimeEnd;
@ApiModelProperty("最后上报时间(起始)")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime lastTimeStart;
} }

View File

@ -127,6 +127,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="query.lastUserPhone != null and query.lastUserPhone != ''">and bul.user_name like concat('%', #{query.lastUserPhone}, '%')</if> <if test="query.lastUserPhone != null and query.lastUserPhone != ''">and bul.user_name like concat('%', #{query.lastUserPhone}, '%')</if>
<if test="query.mchName != null and query.mchName != ''">and mch.nick_name like concat('%', #{query.mchName}, '%')</if> <if test="query.mchName != null and query.mchName != ''">and mch.nick_name like concat('%', #{query.mchName}, '%')</if>
<if test="query.modelName != null and query.modelName != ''">and bm.name like concat('%', #{query.modelName}, '%')</if> <if test="query.modelName != null and query.modelName != ''">and bm.name like concat('%', #{query.modelName}, '%')</if>
<if test="query.lastTimeEnd != null">and bd.last_location_time &lt;= #{query.lastTimeEnd}</if>
<if test="query.lastTimeStart != null">and bd.last_location_time &gt;= #{query.lastTimeStart}</if>
<if test="query.keyword != null and query.keyword != ''"> <if test="query.keyword != null and query.keyword != ''">
and ( and (
bd.sn like concat('%', #{query.keyword}, '%') bd.sn like concat('%', #{query.keyword}, '%')

View File

@ -274,28 +274,18 @@ public class DeviceIotServiceImpl implements DeviceIotService {
DeviceUtil.setIotInfo(device, iot); DeviceUtil.setIotInfo(device, iot);
} }
// 异步更新数据库 // 异步发送命令强制设备上报数据
// scheduledExecutorService.schedule(() -> { scheduledExecutorService.schedule(() -> {
// for (DeviceVO device : deviceList) { for (DeviceVO device : deviceList) {
// try { try {
// // 加锁控制更新频率 if (onlineType != null) {
// boolean lock = redisLock.lock(RedisLockKey.DEVICE_IOT_REFRESH, device.getMac(), 10); iotService.getOnlineStatus(device, onlineType);
// if (!lock) { }
// continue; } catch (Exception e) {
// } log.error("强制设备{}上报数据失败", device.getSn(), e);
}
// // 获取在线状态 }
// if (StringUtils.hasText(onlineType)) { }, 0, TimeUnit.SECONDS);
// String onlineStatus = iotService.getOnlineStatus(device, onlineType);
// this.setOnlineStatus(device, onlineStatus);
// }
// // 更新物联网信息
// this.updateIot(device);
// } catch (Exception e) {
// log.error("更新设备{}物联网信息失败", device.getSn(), e);
// }
// }
// }, 0, TimeUnit.SECONDS);
} }
private void setOnlineStatus(DeviceVO device, String onlineStatus) { private void setOnlineStatus(DeviceVO device, String onlineStatus) {

View File

@ -1,12 +1,12 @@
package com.ruoyi.iot.enums; package com.ruoyi.iot.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Getter;
/** /**
* @author wjh * @author wjh
* 2024/7/11 * 2024/7/11

View File

@ -1,6 +1,7 @@
package com.ruoyi.iot.service.impl; package com.ruoyi.iot.service.impl;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration; import java.time.Duration;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
@ -83,21 +84,21 @@ public class IotReceiveServiceImpl implements IotReceiveService {
if (device == null) { if (device == null) {
return; return;
} }
BigDecimal oldLon = device.getLongitude();
BigDecimal oldLat = device.getLatitude();
// 转为设备信息 // 转为设备信息
log.info("收到sys数据{}", msg.getValue()); log.info("收到sys数据{}", msg.getValue());
IotDeviceSysInfo sys = IotUtil.toSysInfo(msg.getValue()); IotDeviceSysInfo sys = IotUtil.toSysInfo(msg.getValue());
LocalDateTime at = DateUtils.toLocalDateTime(msg.getAt()); LocalDateTime at = DateUtils.toLocalDateTime(msg.getAt());
DeviceUtil.setIotSysInfo(device, sys, at);
// 处理设备定位BUG若出现BUG则重启设备 // 处理设备定位BUG若出现BUG则重启设备
int handle = this.handleDeviceLocationBug(device, sys, at); int handle = this.handleDeviceLocationBug(device, at, oldLon, oldLat);
if (handle == 1) { if (handle == 1) {
return; return;
} }
// 设置设备信息
DeviceUtil.setIotSysInfo(device, sys, at);
// 更新设备信息 // 更新设备信息
device.setOnlineStatus(DeviceOnlineStatus.ONLINE.getStatus()); device.setOnlineStatus(DeviceOnlineStatus.ONLINE.getStatus());
device.setLastOnlineTime(at); device.setLastOnlineTime(at);
@ -134,7 +135,7 @@ public class IotReceiveServiceImpl implements IotReceiveService {
* 处理设备定位BUG * 处理设备定位BUG
* @param device * @param device
*/ */
private int handleDeviceLocationBug(DeviceVO device, IotDeviceSysInfo sys, LocalDateTime at) { private int handleDeviceLocationBug(DeviceVO device, LocalDateTime at, BigDecimal oldLon, BigDecimal oldLat) {
if (device == null || StringUtils.isBlank(device.getMac()) || device.getLastLocationTime() == null || at == null) { if (device == null || StringUtils.isBlank(device.getMac()) || device.getLastLocationTime() == null || at == null) {
return 0; return 0;
} }
@ -144,22 +145,22 @@ public class IotReceiveServiceImpl implements IotReceiveService {
return 0; return 0;
} }
// 0,0定位不处理 // 0,0定位不处理
if (MathUtils.equals(sys.getLon(), BigDecimal.ZERO) && MathUtils.equals(sys.getLat(), BigDecimal.ZERO)) { if (MathUtils.equals(device.getLongitude(), BigDecimal.ZERO) && MathUtils.equals(device.getLatitude(), BigDecimal.ZERO)) {
log.info("设备{}定位异常:{},{},不处理", device.getMac(), sys.getLon(), sys.getLat()); log.info("设备{}定位异常:{},{},不处理", device.getMac(), device.getLongitude(), device.getLatitude());
return 0; return 0;
} }
boolean isOpen = DeviceQuality.OPEN.getCode().equals(sys.getQ()); // 电门是否开启 boolean isOpen = DeviceQuality.OPEN.getCode().equals(device.getQuality()); // 电门是否开启
// 需要在设备未启动的时候做否则可能有安全问题 // 需要在设备未启动的时候做否则可能有安全问题
if (!isOpen) { if (!isOpen) {
// 若当前数据点的上报时间和设备内上次上报的时间不一样但是定位一样则重启设备 // 若当前数据点的上报时间和设备内上次上报的时间不一样但是定位一样则重启设备
log.info("当前定位:{},{},上次定位:{},{}", log.info("当前定位:{},{},上次定位:{},{}",
sys.getLon().setScale(8, BigDecimal.ROUND_HALF_UP), device.getLongitude().setScale(8, RoundingMode.HALF_UP),
sys.getLat().setScale(8, BigDecimal.ROUND_HALF_UP), device.getLatitude().setScale(8, RoundingMode.HALF_UP),
device.getLongitude().setScale(8, BigDecimal.ROUND_HALF_UP), oldLon.setScale(8, RoundingMode.HALF_UP),
device.getLatitude().setScale(8, BigDecimal.ROUND_HALF_UP) oldLat.setScale(8, RoundingMode.HALF_UP)
); );
if (at.isAfter(device.getLastLocationTime()) && MathUtils.equalsFixed(device.getLongitude(), sys.getLon(), 8) && MathUtils.equalsFixed(device.getLatitude(), sys.getLat(), 8)) { if (at.isAfter(device.getLastLocationTime()) && MathUtils.equalsFixed(device.getLongitude(), oldLon, 8) && MathUtils.equalsFixed(device.getLatitude(), oldLat, 8)) {
String reason = String.format("设备不同时间的两次定位一样,重启设备。新定位:%s,%s旧定位%s,%s", sys.getLon(), sys.getLat(), device.getLongitude(), device.getLatitude()); String reason = String.format("设备不同时间的两次定位一样,重启设备。新定位:%s,%s旧定位%s,%s", device.getLongitude(), device.getLatitude(), oldLon, oldLat);
deviceIotService.reboot(device, reason, true); deviceIotService.reboot(device, reason, true);
return 1; return 1;
} }

View File

@ -31,6 +31,7 @@ import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.iot.domain.IotDeviceSysInfo; import com.ruoyi.iot.domain.IotDeviceSysInfo;
import com.ruoyi.iot.domain.ObjBody; import com.ruoyi.iot.domain.ObjBody;
import com.ruoyi.iot.domain.response.CommandResponse; import com.ruoyi.iot.domain.response.CommandResponse;
import com.ruoyi.iot.enums.IotHttpStatus;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -214,7 +215,8 @@ public class IotUtil {
if (result == null) { if (result == null) {
return "未知错误"; return "未知错误";
} }
return result.getMsg(); IotHttpStatus status = IotHttpStatus.convertByCode(result.getCode());
return status.getMsg();
} }
public static IotDeviceSysInfo toSysInfo(Object value) { public static IotDeviceSysInfo toSysInfo(Object value) {