|
@@ -47,7 +47,6 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
-import java.lang.reflect.Field;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
@@ -256,7 +255,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
}
|
|
|
|
|
|
//4.通过MQTT把信息参数发送到设备
|
|
|
- sendDeviceParamsToMqtt(devInfo.getClientId(), request);
|
|
|
+ mqttSubHandle.sendDeviceParamsToMqtt(devInfo.getClientId(), request);
|
|
|
|
|
|
//5.返回绑定结果和设备主键id
|
|
|
DeviceDTO result = new DeviceDTO();
|
|
@@ -320,7 +319,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Boolean updateDevice(UpdateDeviceParams params) {
|
|
|
+ public Boolean webUpdateDevice(WebUpdateDeviceParams params) {
|
|
|
//1.校验设备是否存在
|
|
|
DevInfo devInfo = devInfoService.getOne(
|
|
|
Wrappers.<DevInfo>lambdaQuery()
|
|
@@ -360,98 +359,86 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
}
|
|
|
|
|
|
boolean updated = devInfoService.updateById(devInfo);
|
|
|
-
|
|
|
+ if (!updated){
|
|
|
+ log.warn("更新设备信息失败,clientId:{}", devInfo.getClientId());
|
|
|
+ throw new BizException(ErrorEnum.DEVICE_UPDATE_FAIL.getErrorCode(),ErrorEnum.DEVICE_UPDATE_FAIL.getErrorMessage());
|
|
|
+ }
|
|
|
//4.通过MQTT把信息参数发送到设备
|
|
|
- sendDeviceParamsToMqtt(devInfo.getClientId(), params);
|
|
|
+ mqttSubHandle.sendWebDeviceParamsToMqtt(devInfo.getClientId(), params);
|
|
|
|
|
|
return updated;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public DeviceDTO updateDevice(UpdateDeviceParams params) {
|
|
|
+ //1.校验设备是否存在
|
|
|
+ DevInfo devInfo = devInfoService.getOne(
|
|
|
+ Wrappers.<DevInfo>lambdaQuery()
|
|
|
+ .eq(DevInfo::getClientId, params.getClientId()));
|
|
|
+ if (Objects.isNull(devInfo)){
|
|
|
+ throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
|
|
|
+ }
|
|
|
|
|
|
- // 辅助方法:如果值为null则设置默认值
|
|
|
- private <T> void setIfNull(java.util.function.Supplier<T> getter,
|
|
|
- java.util.function.Supplier<T> defaultValueSupplier,
|
|
|
- java.util.function.Consumer<T> setter) {
|
|
|
- if (getter.get() == null) {
|
|
|
- setter.accept(defaultValueSupplier.get());
|
|
|
+ //2.校验是否为主绑人
|
|
|
+ if (!devInfo.getUserId().equals(params.getUserId())){
|
|
|
+ throw new BizException(ErrorEnum.USER_NO_PERMISSION.getErrorCode(), ErrorEnum.USER_NO_PERMISSION.getErrorMessage());
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 发送设备参数到MQTT的通用方法
|
|
|
- * @param clientId 设备客户端ID
|
|
|
- * @param params 设备参数(DeviceBandingParams或UpdateDeviceParams)
|
|
|
- */
|
|
|
- private void sendDeviceParamsToMqtt(String clientId, Object params) {
|
|
|
- String topic = String.format(TopicConstants.TOPIC_SET_DEVICE_PARAM, clientId);
|
|
|
-
|
|
|
- //构建检测区域
|
|
|
- Map<String, BigDecimal> base = new HashMap<>();
|
|
|
- base.put("x_cm_start", getFieldValue(params, "xxStart"));
|
|
|
- base.put("x_cm_stop", getFieldValue(params, "xxEnd"));
|
|
|
- base.put("y_cm_start", getFieldValue(params, "yyStart"));
|
|
|
- base.put("y_cm_stop", getFieldValue(params, "yyEnd"));
|
|
|
- base.put("z_cm_start", getFieldValue(params, "zzStart"));
|
|
|
- base.put("z_cm_stop", getFieldValue(params, "zzEnd"));
|
|
|
- Map<String, Map<String, BigDecimal>> baseParent = new HashMap<>();
|
|
|
- baseParent.put("base", base);
|
|
|
-
|
|
|
- //构建安装高度
|
|
|
- Map<String, BigDecimal> z_cm = new HashMap<>();
|
|
|
- z_cm.put("z_cm", getFieldValue(params, "height"));
|
|
|
-
|
|
|
- //构建跌倒确认时间
|
|
|
- Map<String, Integer> fallingStateMachineDurations = new HashMap<>();
|
|
|
- // 从参数中获取跌倒确认时间
|
|
|
- BigDecimal fallingConfirm = getFieldValue(params, "fallingConfirm");
|
|
|
- if (fallingConfirm != null && fallingConfirm.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
- int confirmTime = fallingConfirm.intValue();
|
|
|
- fallingStateMachineDurations.put("durationUntilConfirm_sec", confirmTime);
|
|
|
- // minTimeOfTarInFallLoc_sec 是 durationUntilConfirm_sec的90%
|
|
|
- fallingStateMachineDurations.put("minTimeOfTarInFallLoc_sec", (int)(confirmTime * 0.9));
|
|
|
- // 固定默认值
|
|
|
- fallingStateMachineDurations.put("durationUntilCalling_sec", 13);
|
|
|
- fallingStateMachineDurations.put("durationUntilReset_sec", 3);
|
|
|
- } else {
|
|
|
- // 使用默认值
|
|
|
- fallingStateMachineDurations.put("durationUntilConfirm_sec", 53);
|
|
|
- fallingStateMachineDurations.put("minTimeOfTarInFallLoc_sec", 33);
|
|
|
- fallingStateMachineDurations.put("durationUntilCalling_sec", 13);
|
|
|
- fallingStateMachineDurations.put("durationUntilReset_sec", 3);
|
|
|
+
|
|
|
+ //3.更新数据
|
|
|
+ devInfo.setDevName(params.getDevName());
|
|
|
+ devInfo.setHeight(params.getHeight());
|
|
|
+ devInfo.setWifiName(params.getWifiName());
|
|
|
+ devInfo.setWifiPassword(params.getWifiPassword());
|
|
|
+ devInfo.setNorthAngle(params.getNorthAngle());
|
|
|
+ devInfo.setMountPlain(params.getMountPlain());
|
|
|
+ devInfo.setInstallPosition(params.getInstallPosition());
|
|
|
+ devInfo.setXxStart(params.getXxStart());
|
|
|
+ devInfo.setXxEnd(params.getXxEnd());
|
|
|
+ devInfo.setYyStart(params.getYyStart());
|
|
|
+ devInfo.setYyEnd(params.getYyEnd());
|
|
|
+ devInfo.setZzStart(params.getZzStart());
|
|
|
+ devInfo.setZzEnd(params.getZzEnd());
|
|
|
+ devInfo.setLength(params.getXxEnd().subtract(params.getXxStart()));
|
|
|
+ devInfo.setWidth(params.getYyEnd().subtract(params.getYyStart()));
|
|
|
+
|
|
|
+ boolean updated = devInfoService.updateById(devInfo);
|
|
|
+ if (!updated){
|
|
|
+ log.warn("更新设备信息失败,clientId:{}", devInfo.getClientId());
|
|
|
+ throw new BizException(ErrorEnum.DEVICE_UPDATE_FAIL.getErrorCode(),ErrorEnum.DEVICE_UPDATE_FAIL.getErrorMessage());
|
|
|
}
|
|
|
|
|
|
- //发送最终消息
|
|
|
- JSONObject jsonObject = new JSONObject();
|
|
|
- jsonObject.put("ext_region", baseParent);
|
|
|
- jsonObject.put("sensor_location", z_cm);
|
|
|
- jsonObject.put("fallingStateMachineDurations", fallingStateMachineDurations);
|
|
|
+ //4.通过MQTT把信息参数发送到设备
|
|
|
+ mqttSubHandle.sendDeviceParamsToMqtt(devInfo.getClientId(), params);
|
|
|
|
|
|
- try {
|
|
|
- mqttClient.sendMessage(topic, jsonObject.toJSONString());
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("发送设备参数到MQTT失败", e);
|
|
|
- throw new BizException(ErrorEnum.MQTT_SEND_ERROR.getErrorCode(), ErrorEnum.MQTT_SEND_ERROR.getErrorMessage());
|
|
|
+ //5.重新从数据库获取最新的设备信息,确保返回的数据完整
|
|
|
+ DevInfo updatedDevInfo = devInfoService.getOne(
|
|
|
+ Wrappers.<DevInfo> lambdaQuery()
|
|
|
+ .eq(DevInfo::getClientId, params.getClientId()));
|
|
|
+ if (Objects.isNull(updatedDevInfo)){
|
|
|
+ throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
|
|
|
}
|
|
|
+
|
|
|
+ //6.将查询到的完整设备数据复制到 DeviceDTO 返还前端
|
|
|
+ DeviceDTO dto = new DeviceDTO();
|
|
|
+ BeanUtils.copyProperties(updatedDevInfo, dto);
|
|
|
+ return dto;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 通过反射获取对象字段值
|
|
|
- * @param obj 对象
|
|
|
- * @param fieldName 字段名
|
|
|
- * @return 字段值
|
|
|
- */
|
|
|
- private BigDecimal getFieldValue(Object obj, String fieldName) {
|
|
|
- try {
|
|
|
- Field field = obj.getClass().getDeclaredField(fieldName);
|
|
|
- field.setAccessible(true);
|
|
|
- return (BigDecimal) field.get(obj);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("获取字段值失败: {}", fieldName, e);
|
|
|
- return BigDecimal.ZERO;
|
|
|
+
|
|
|
+ // 辅助方法:如果值为null则设置默认值
|
|
|
+ private <T> void setIfNull(java.util.function.Supplier<T> getter,
|
|
|
+ java.util.function.Supplier<T> defaultValueSupplier,
|
|
|
+ java.util.function.Consumer<T> setter) {
|
|
|
+ if (getter.get() == null) {
|
|
|
+ setter.accept(defaultValueSupplier.get());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
public Boolean updateDeviceLocation(DeviceLocationParams params) {
|
|
|
|