fix: 盐度补偿设定,太阳能网控缓存逻辑修改完善。
This commit is contained in:
@@ -63,6 +63,7 @@ import java.util.Date;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -2034,55 +2035,104 @@ public class IotController extends BaseController {
|
||||
return R.fail("设备已到期");
|
||||
}
|
||||
|
||||
// 查询设备状态
|
||||
boolean isSolarDevice = Integer.valueOf(3).equals(device.getDeviceType());
|
||||
|
||||
if (isSolarDevice) {
|
||||
if (!cachePendingSolarSalinity(device, request.getSalinityCompensation())) {
|
||||
return R.fail("盐度缓存失败,请稍后重试");
|
||||
}
|
||||
updateDeviceSalinityCompensation(device.getId(), request.getSalinityCompensation());
|
||||
log.info("太阳能网控盐度已缓存: userId={}, deviceId={}, deviceName={}, serialNum={}, salinityCompensation={}‰",
|
||||
userId, device.getId(), device.getDeviceName(), device.getSerialNum(), request.getSalinityCompensation());
|
||||
return R.ok("设置成功");
|
||||
}
|
||||
|
||||
if (iotDeviceService == null) {
|
||||
return R.fail("飞燕平台配置未启用");
|
||||
}
|
||||
|
||||
Map<String, Object> deviceDetail = iotDeviceService.queryDeviceInfo(device.getIotId());
|
||||
if (deviceDetail != null && deviceDetail.get("data") != null) {
|
||||
try {
|
||||
Object detailData = deviceDetail.get("data");
|
||||
java.lang.reflect.Method getStatusMethod = detailData.getClass().getMethod("getStatus");
|
||||
Object statusObj = getStatusMethod.invoke(detailData);
|
||||
if (statusObj != null) {
|
||||
String statusStr = statusObj.toString();
|
||||
if ("OFFLINE".equalsIgnoreCase(statusStr) || "offline".equals(statusStr)) {
|
||||
return R.fail("设备离线或断电");
|
||||
try {
|
||||
Map<String, Object> deviceDetail = iotDeviceService.queryDeviceInfo(device.getIotId());
|
||||
if (deviceDetail != null && deviceDetail.get("data") != null) {
|
||||
try {
|
||||
Object detailData = deviceDetail.get("data");
|
||||
java.lang.reflect.Method getStatusMethod = detailData.getClass().getMethod("getStatus");
|
||||
Object statusObj = getStatusMethod.invoke(detailData);
|
||||
if (statusObj != null) {
|
||||
String statusStr = statusObj.toString();
|
||||
if (!"ONLINE".equalsIgnoreCase(statusStr) && !"online".equalsIgnoreCase(statusStr)) {
|
||||
return R.fail("设备离线或断电");
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.warn("获取设备状态失败: {}", ex.getMessage());
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.warn("获取设备状态失败: {}", ex.getMessage());
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.warn("查询设备状态失败: {}", ex.getMessage());
|
||||
return R.fail("设备状态查询失败,请稍后重试");
|
||||
}
|
||||
|
||||
// 设置盐度补偿属性
|
||||
Map<String, Object> properties = new java.util.HashMap<>();
|
||||
properties.put("salinitySet", request.getSalinityCompensation());
|
||||
String propertiesJson = cn.hutool.json.JSONUtil.toJsonStr(properties);
|
||||
|
||||
Map<String, Object> setResult = iotDeviceService.setDeviceProperty(device.getIotId(), propertiesJson);
|
||||
if (setResult == null || !Boolean.TRUE.equals(setResult.get("success"))) {
|
||||
if (!pushSalinitySetting(device.getIotId(), request.getSalinityCompensation())) {
|
||||
return R.fail("设置盐度补偿失败,请重试");
|
||||
}
|
||||
|
||||
// 更新设备盐度补偿值
|
||||
deviceMapper.update(null,
|
||||
new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<Device>()
|
||||
.eq(Device::getId, request.getId())
|
||||
.set(Device::getSalinityCompensation, request.getSalinityCompensation())
|
||||
);
|
||||
updateDeviceSalinityCompensation(device.getId(), request.getSalinityCompensation());
|
||||
clearPendingSolarSalinity(device.getSerialNum());
|
||||
|
||||
log.info("设置盐度补偿成功: userId={}, deviceId={}, deviceName={}, serialNum={}, salinityCompensation={}‰",
|
||||
userId, device.getId(), device.getDeviceName(), device.getSerialNum(), request.getSalinityCompensation());
|
||||
|
||||
return R.ok();
|
||||
return R.ok("设置成功");
|
||||
} catch (Exception e) {
|
||||
log.error("设置盐度补償失败: {}", e.getMessage(), e);
|
||||
return R.fail("设置盐度补償失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean pushSalinitySetting(String iotId, Double salinityCompensation) throws Exception {
|
||||
Map<String, Object> properties = new java.util.HashMap<>();
|
||||
properties.put("salinitySet", salinityCompensation);
|
||||
String propertiesJson = cn.hutool.json.JSONUtil.toJsonStr(properties);
|
||||
Map<String, Object> setResult = iotDeviceService.setDeviceProperty(iotId, propertiesJson);
|
||||
return setResult != null && Boolean.TRUE.equals(setResult.get("success"));
|
||||
}
|
||||
|
||||
private boolean cachePendingSolarSalinity(Device device, Double salinityCompensation) {
|
||||
try {
|
||||
String key = DeviceDataHandler.PENDING_SOLAR_SALINITY_KEY_PREFIX + device.getSerialNum();
|
||||
Map<String, Object> cacheValue = new java.util.HashMap<>();
|
||||
cacheValue.put("deviceId", device.getId());
|
||||
cacheValue.put("iotId", device.getIotId());
|
||||
cacheValue.put("serialNum", device.getSerialNum());
|
||||
cacheValue.put("salinityCompensation", salinityCompensation);
|
||||
cacheValue.put("updatedAt", System.currentTimeMillis());
|
||||
com.intc.common.redis.utils.RedisUtils.setCacheObject(key, cn.hutool.json.JSONUtil.toJsonStr(cacheValue), Duration.ofDays(7));
|
||||
log.info("太阳能网控盐度已缓存,等待上线补发: deviceId={}, serialNum={}, salinityCompensation={}",
|
||||
device.getId(), device.getSerialNum(), salinityCompensation);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("缓存太阳能网控盐度失败: deviceId={}, serialNum={}, err={}",
|
||||
device.getId(), device.getSerialNum(), e.getMessage(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateDeviceSalinityCompensation(Long deviceId, Double salinityCompensation) {
|
||||
deviceMapper.update(null,
|
||||
new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<Device>()
|
||||
.eq(Device::getId, deviceId)
|
||||
.set(Device::getSalinityCompensation, salinityCompensation)
|
||||
);
|
||||
}
|
||||
|
||||
private void clearPendingSolarSalinity(String serialNum) {
|
||||
if (serialNum == null || serialNum.isBlank()) {
|
||||
return;
|
||||
}
|
||||
com.intc.common.redis.utils.RedisUtils.deleteObject(DeviceDataHandler.PENDING_SOLAR_SALINITY_KEY_PREFIX + serialNum);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改设备输入电压类型
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user