|
@@ -19,6 +19,7 @@ import com.hfln.portal.common.dto.data.share.ShareDto;
|
|
|
import com.hfln.portal.common.request.device.DeviceBandingParams;
|
|
|
import com.hfln.portal.common.request.device.DeviceListParams;
|
|
|
import com.hfln.portal.common.request.device.DeviceLocationParams;
|
|
|
+import com.hfln.portal.common.request.device.DeviceTransferParams;
|
|
|
import com.hfln.portal.common.request.event.EventListParams;
|
|
|
import com.hfln.portal.common.request.share.ShareConfirmParam;
|
|
|
import com.hfln.portal.common.request.share.ShareParam;
|
|
@@ -203,20 +204,22 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
|
|
|
|
|
|
@Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public Boolean deviceBind(DeviceBandingParams request) {
|
|
|
// 1.参数范围大小校验
|
|
|
- if (request.getXxEnd().compareTo(request.getXxStart()) <= 0) {
|
|
|
- throw new BizException(ErrorEnum.DEVICE_PARAM_ERROR.getErrorCode(), ErrorEnum.DEVICE_PARAM_ERROR.getErrorMessage());
|
|
|
- }
|
|
|
- if (request.getYyEnd().compareTo(request.getYyStart()) <= 0) {
|
|
|
+ if (request.getXxEnd().compareTo(request.getXxStart()) <= 0 || request.getYyEnd().compareTo(request.getYyStart()) <= 0) {
|
|
|
throw new BizException(ErrorEnum.DEVICE_PARAM_ERROR.getErrorCode(), ErrorEnum.DEVICE_PARAM_ERROR.getErrorMessage());
|
|
|
}
|
|
|
- if (!Arrays.asList(
|
|
|
+
|
|
|
+ List<BigDecimal> validAngles = Arrays.asList(
|
|
|
BigDecimal.ZERO,
|
|
|
- new BigDecimal("90"),
|
|
|
- new BigDecimal("180"),
|
|
|
- new BigDecimal("270")
|
|
|
- ).contains(request.getNorthAngle())) {
|
|
|
+ BigDecimal.valueOf(90),
|
|
|
+ BigDecimal.valueOf(180),
|
|
|
+ BigDecimal.valueOf(270)
|
|
|
+ );
|
|
|
+ boolean valid = validAngles.stream()
|
|
|
+ .anyMatch(angle -> angle.compareTo(request.getNorthAngle()) == 0);
|
|
|
+ if (!valid) {
|
|
|
throw new BizException(ErrorEnum.DEVICE_PARAM_ERROR.getErrorCode(), ErrorEnum.DEVICE_PARAM_ERROR.getErrorMessage());
|
|
|
}
|
|
|
|
|
@@ -234,26 +237,45 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
|
|
|
// 3. 更新设备信息
|
|
|
boolean updateResult = devInfoService.updateDevice(devInfo.getClientId(), request);
|
|
|
+ if (!updateResult){
|
|
|
+ log.warn("更新设备信息失败,clientId:{}", devInfo.getClientId());
|
|
|
+ }
|
|
|
|
|
|
//4.通过MQTT把信息参数发送到设备
|
|
|
- String topic = TopicConstants.TOPIC_SET_DEVICE_PARAM;
|
|
|
- JSONObject msg = new JSONObject();
|
|
|
- msg.put("dev_id", devInfo.getDevId());
|
|
|
- msg.put("mounting_plain", request.getMountPlain());
|
|
|
- msg.put("height", request.getHeight());
|
|
|
-
|
|
|
- JSONObject area = new JSONObject();
|
|
|
- area.put("start_x", request.getXxStart());
|
|
|
- area.put("stop_x", request.getXxEnd());
|
|
|
- area.put("start_y", request.getYyStart());
|
|
|
- area.put("stop_y", request.getYyEnd());
|
|
|
- area.put("start_z", request.getZzStart());
|
|
|
- area.put("stop_z", request.getZzEnd());
|
|
|
- msg.put("area", area);
|
|
|
+ String topic = String.format(TopicConstants.TOPIC_SET_DEVICE_PARAM, devInfo.getClientId());
|
|
|
+
|
|
|
+ //构建检测区域
|
|
|
+ Map<String, BigDecimal> base = new HashMap<>();
|
|
|
+ base.put("x_cm_start", request.getXxStart());
|
|
|
+ base.put("x_cm_stop", request.getXxEnd());
|
|
|
+ base.put("y_cm_start", request.getYyStart());
|
|
|
+ base.put("y_cm_stop", request.getYyEnd());
|
|
|
+ base.put("z_cm_start", request.getZzStart());
|
|
|
+ base.put("z_cm_stop", request.getZzEnd());
|
|
|
+ Map<String, Map<String, BigDecimal>> baseParent = new HashMap<>();
|
|
|
+ baseParent.put("base", base);
|
|
|
+
|
|
|
+ //构建安装高度
|
|
|
+ Map<String, BigDecimal> z_cm = new HashMap<>();
|
|
|
+ z_cm.put("z_cm", request.getHeight());
|
|
|
+
|
|
|
+ //构建呼吸灯
|
|
|
+ int indicatorLed = 0;
|
|
|
+ if (request.getStatusLight() == 0){
|
|
|
+ indicatorLed = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ //发送最终消息
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("ext_region", baseParent);
|
|
|
+ jsonObject.put("sensor_location", z_cm);
|
|
|
+ jsonObject.put("indicator_led", indicatorLed);
|
|
|
+
|
|
|
try {
|
|
|
- mqttClient.sendMessage(topic, msg.toJSONString());
|
|
|
+ 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());
|
|
|
}
|
|
|
|
|
|
return updateResult;
|
|
@@ -416,6 +438,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @Transactional
|
|
|
public Boolean statusLight(Long devId, Integer statusLight) {
|
|
|
//1.校验设备是否存在
|
|
|
DevInfo devInfo = devInfoService.getById(devId);
|
|
@@ -436,10 +459,14 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
);
|
|
|
|
|
|
//4.通过MQTT把信息参数发送到设备
|
|
|
- String topic = TopicConstants.TOPIC_SET_DEVICE_PARAM;
|
|
|
+ String topic = String.format(TopicConstants.TOPIC_SET_DEVICE_PARAM, devInfo.getClientId());
|
|
|
JSONObject msg = new JSONObject();
|
|
|
- msg.put("dev_id", devId);
|
|
|
- msg.put("status_light", statusLight);
|
|
|
+ int indicatorLed = 0;
|
|
|
+
|
|
|
+ if (statusLight == 0){
|
|
|
+ indicatorLed = 1;
|
|
|
+ }
|
|
|
+ msg.put("indicator_led", indicatorLed);
|
|
|
try {
|
|
|
mqttClient.sendMessage(topic, msg.toJSONString());
|
|
|
} catch (Exception e) {
|
|
@@ -809,33 +836,32 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Boolean deviceTransfer(String phone, String devId){
|
|
|
- //1.检查手机号是非为空
|
|
|
- if (StringUtils.isEmpty(phone)){
|
|
|
- throw new BizException(ErrorEnum.PHONE_IS_NULL.getErrorCode(),ErrorEnum.PHONE_IS_NULL.getErrorMessage());
|
|
|
- }
|
|
|
+ public Boolean deviceTransfer(DeviceTransferParams param){
|
|
|
|
|
|
- //2.根据手机号查询user_info看是否注册
|
|
|
- UserInfo userInfo = userService.queryByPhone(phone);
|
|
|
- if (userInfo == null){
|
|
|
- throw new BizException(ErrorEnum.USER_IS_NOT_EXIST.getErrorCode(), ErrorEnum.USER_IS_NOT_EXIST.getErrorMessage());
|
|
|
- }
|
|
|
-
|
|
|
- //3.检查设备是否存在
|
|
|
+ //1. 查询设备信息并校验是否存在
|
|
|
DevInfo devInfo = devInfoService.getOne(
|
|
|
Wrappers.<DevInfo>lambdaQuery()
|
|
|
- .eq(DevInfo::getDevId, devId)
|
|
|
+ .eq(DevInfo::getDevId, param.getDevId())
|
|
|
.eq(DevInfo::getIsDeleted, BasePO.DeleteFlag.NOT_DELETED)
|
|
|
);
|
|
|
if (devInfo == null) {
|
|
|
throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
|
|
|
}
|
|
|
|
|
|
- //4.更新设备拥有者
|
|
|
- devInfo.setUserId(userInfo.getUserId());
|
|
|
- boolean updateResult = devInfoService.updateById(devInfo);
|
|
|
+ //2.校验用户权限
|
|
|
+ if(!Objects.equals(param.getUserId(), devInfo.getUserId())){
|
|
|
+ throw new BizException(ErrorEnum.USER_NO_PERMISSION.getErrorCode(), ErrorEnum.USER_NO_PERMISSION.getErrorMessage());
|
|
|
+ }
|
|
|
|
|
|
- return updateResult;
|
|
|
+ //3.根据手机号查询转移对象是否注册
|
|
|
+ UserInfo userInfo = userService.queryByPhone(param.getPhone());
|
|
|
+ if (userInfo == null){
|
|
|
+ throw new BizException(ErrorEnum.USER_IS_NOT_EXIST.getErrorCode(), ErrorEnum.USER_IS_NOT_EXIST.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ //4.转移设备归属
|
|
|
+ devInfo.setUserId(userInfo.getUserId());
|
|
|
+ return devInfoService.updateById(devInfo);
|
|
|
}
|
|
|
|
|
|
|