|
@@ -212,7 +212,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Boolean deviceBind(DeviceBandingParams request) {
|
|
|
+ public DeviceDTO deviceBind(DeviceBandingParams request) {
|
|
|
// 1.参数范围大小校验
|
|
|
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());
|
|
@@ -242,7 +242,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
throw new BizException(ErrorEnum.DEVICE_IS_BINDING.getErrorCode(), ErrorEnum.DEVICE_IS_BINDING.getErrorMessage());
|
|
|
}
|
|
|
|
|
|
- // 3. 更新设备信息
|
|
|
+ // 3. 更新设备信息到数据库
|
|
|
boolean updateResult = devInfoService.updateDevice(devInfo.getClientId(), request);
|
|
|
if (!updateResult){
|
|
|
log.warn("更新设备信息失败,clientId:{}", devInfo.getClientId());
|
|
@@ -251,7 +251,10 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
//4.通过MQTT把信息参数发送到设备
|
|
|
sendDeviceParamsToMqtt(devInfo.getClientId(), request);
|
|
|
|
|
|
- return updateResult;
|
|
|
+ //5.返回绑定结果和设备主键id
|
|
|
+ DeviceDTO result = new DeviceDTO();
|
|
|
+ BeanUtils.copyProperties(devInfo, result);
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -337,7 +340,12 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
devInfo.setZzEnd(params.getZzEnd());
|
|
|
devInfo.setLength(params.getXxEnd().subtract(params.getXxStart()));
|
|
|
devInfo.setWidth(params.getYyEnd().subtract(params.getYyStart()));
|
|
|
- devInfo.setFallingConfirm(params.getFallingConfirm());
|
|
|
+
|
|
|
+ if (params.getFallingConfirm() != null) {
|
|
|
+ devInfo.setFallingConfirm(params.getFallingConfirm());
|
|
|
+ } else {
|
|
|
+ devInfo.setFallingConfirm((short)53);
|
|
|
+ }
|
|
|
|
|
|
boolean updated = devInfoService.updateById(devInfo);
|
|
|
|
|
@@ -491,30 +499,29 @@ public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
}
|
|
|
|
|
|
//2.校验status_light值,只能传 0 与 1
|
|
|
- if (params.getStatusLight() == null || params.getStatusLight() != 0 && params.getStatusLight() != 1){
|
|
|
+ Integer statusLight = params.getStatusLight();
|
|
|
+ if (statusLight == null || (statusLight != 0 && statusLight != 1)){
|
|
|
throw new BizException(ErrorEnum.STATUS_LIGHT_IS_NOT_EXIST.getErrorCode(), ErrorEnum.STATUS_LIGHT_IS_NOT_EXIST.getErrorMessage());
|
|
|
}
|
|
|
|
|
|
- //3.更新设备指示灯状态
|
|
|
+ //3.更新数据库
|
|
|
boolean updateResult = devInfoService.update(
|
|
|
Wrappers.<DevInfo>lambdaUpdate()
|
|
|
- .set(DevInfo::getStatusLight, params.getStatusLight())
|
|
|
+ .set(DevInfo::getStatusLight, statusLight)
|
|
|
.eq(DevInfo::getDevId, params.getDevId())
|
|
|
);
|
|
|
|
|
|
- //4.通过MQTT把信息参数发送到设备
|
|
|
+ //4.组装MQTT消息并发送
|
|
|
String topic = String.format(TopicConstants.TOPIC_SET_DEVICE_PARAM, devInfo.getClientId());
|
|
|
JSONObject msg = new JSONObject();
|
|
|
- int indicatorLed = 0;
|
|
|
-
|
|
|
- if (params.getStatusLight() == 0){
|
|
|
- indicatorLed = 1;
|
|
|
- }
|
|
|
+ // 设备协议中,statusLight 为 0 时表示前端关闭指示灯,但设备端需发送 indicator_led = 1
|
|
|
+ int indicatorLed = (statusLight == 0) ? 1:0;
|
|
|
msg.put("indicator_led", indicatorLed);
|
|
|
try {
|
|
|
mqttClient.sendMessage(topic, msg.toJSONString());
|
|
|
} catch (Exception e) {
|
|
|
- log.error("发送设备参数到MQTT失败", e);
|
|
|
+ log.error("发送设备参数到MQTT失败, topic: {}, payload: {}", topic, msg.toJSONString(), e);
|
|
|
+ throw new BizException(ErrorEnum.MQTT_SEND_ERROR.getErrorCode(), ErrorEnum.MQTT_SEND_ERROR.getErrorMessage());
|
|
|
}
|
|
|
|
|
|
return updateResult;
|