fix: 微信小程序接口对接,测试问题修复。
This commit is contained in:
@@ -180,6 +180,14 @@ public class AliyunIotProperties {
|
||||
*/
|
||||
private String controlIntegrated;
|
||||
|
||||
/**
|
||||
* 控制器 ProductKey (别名,指向 controlIntegrated)
|
||||
* @return ProductKey
|
||||
*/
|
||||
public String getController() {
|
||||
return controlIntegrated;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据设备类型获取 ProductKey
|
||||
* @param deviceType 1-水质检测仪, 2-控制一体机
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.intc.fishery.domain.Device;
|
||||
import com.intc.fishery.mapper.DeviceMapper;
|
||||
import com.intc.fishery.mapper.PondMapper;
|
||||
import com.intc.iot.config.AliyunIotProperties;
|
||||
import com.intc.iot.domain.bo.AddDeviceControllerBo;
|
||||
import com.intc.iot.domain.bo.AddDeviceDetectorBo;
|
||||
import com.intc.iot.domain.bo.DeviceRealtimeDataBo;
|
||||
import com.intc.iot.domain.vo.DeviceRealtimeDataVo;
|
||||
@@ -79,6 +80,9 @@ public class IotController extends BaseController {
|
||||
@Autowired(required = false)
|
||||
private PondMapper pondMapper;
|
||||
|
||||
@Autowired(required = false)
|
||||
private com.intc.fishery.mapper.DeviceSwitchMapper deviceSwitchMapper;
|
||||
|
||||
@Operation(summary = "测试接口")
|
||||
@GetMapping("/test")
|
||||
public R<String> test() {
|
||||
@@ -339,23 +343,30 @@ public class IotController extends BaseController {
|
||||
Object detailData = deviceDetail.get("data");
|
||||
Integer statusCode = 0; // 默认为设备未激活
|
||||
|
||||
if (detailData instanceof Map) {
|
||||
Map<?, ?> detailMap = (Map<?, ?>) detailData;
|
||||
Object statusObj = detailMap.get("status");
|
||||
|
||||
if (statusObj != null) {
|
||||
String statusStr = statusObj.toString();
|
||||
// 根据物联网平台返回的状态转换为前端状态码
|
||||
// 0-未激活, 1-在线, 3-离线, 8-禁用
|
||||
if ("ONLINE".equalsIgnoreCase(statusStr) || "online".equals(statusStr)) {
|
||||
statusCode = 1;
|
||||
} else if ("OFFLINE".equalsIgnoreCase(statusStr) || "offline".equals(statusStr)) {
|
||||
statusCode = 3;
|
||||
} else if ("UNACTIVE".equalsIgnoreCase(statusStr) || "unactive".equals(statusStr)) {
|
||||
statusCode = 0;
|
||||
} else if ("DISABLE".equalsIgnoreCase(statusStr) || "disable".equals(statusStr)) {
|
||||
statusCode = 8;
|
||||
if (detailData != null) {
|
||||
// 通过反射获取 status
|
||||
try {
|
||||
java.lang.reflect.Method getStatusMethod = detailData.getClass().getMethod("getStatus");
|
||||
Object statusObj = getStatusMethod.invoke(detailData);
|
||||
|
||||
if (statusObj != null) {
|
||||
String statusStr = statusObj.toString();
|
||||
log.info("从 SDK 对象获取到的状态: {}", statusStr);
|
||||
|
||||
// 根据物联网平台返回的状态转换为前端状态码
|
||||
// 0-未激活, 1-在线, 3-离线, 8-禁用
|
||||
if ("ONLINE".equalsIgnoreCase(statusStr) || "online".equals(statusStr)) {
|
||||
statusCode = 1;
|
||||
} else if ("OFFLINE".equalsIgnoreCase(statusStr) || "offline".equals(statusStr)) {
|
||||
statusCode = 3;
|
||||
} else if ("UNACTIVE".equalsIgnoreCase(statusStr) || "unactive".equals(statusStr)) {
|
||||
statusCode = 0;
|
||||
} else if ("DISABLE".equalsIgnoreCase(statusStr) || "disable".equals(statusStr)) {
|
||||
statusCode = 8;
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("无法从 SDK 对象中获取 status: {}", ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -674,6 +685,443 @@ public class IotController extends BaseController {
|
||||
|
||||
// ======================== 设备管理相关接口 ========================
|
||||
|
||||
@Operation(summary = "添加测控一体机")
|
||||
@PostMapping("/device/add_device_controller")
|
||||
public R<Void> addDeviceController(@RequestBody AddDeviceControllerBo bo) {
|
||||
try {
|
||||
if (iotDeviceService == null) {
|
||||
return R.fail("飞燕平台配置未启用");
|
||||
}
|
||||
if (deviceMapper == null) {
|
||||
return R.fail("设备数据库服务未启用");
|
||||
}
|
||||
if (deviceSwitchMapper == null) {
|
||||
return R.fail("开关数据库服务未启用");
|
||||
}
|
||||
|
||||
// 获取当前登录用户ID
|
||||
Long userId = LoginHelper.getUserId();
|
||||
if (userId == null) {
|
||||
return R.fail("未登录或登录已过期");
|
||||
}
|
||||
|
||||
// 验证塘口是否存在且属于当前用户
|
||||
if (bo.getPondId() != null && bo.getPondId() > 0 && pondMapper != null) {
|
||||
long count = pondMapper.selectCount(
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.intc.fishery.domain.Pond>()
|
||||
.eq(com.intc.fishery.domain.Pond::getUserId, userId)
|
||||
.eq(com.intc.fishery.domain.Pond::getId, bo.getPondId())
|
||||
);
|
||||
if (count == 0) {
|
||||
return R.fail("塘口不存在或无权限访问");
|
||||
}
|
||||
}
|
||||
|
||||
// 检查设备是否已被绑定
|
||||
Device existDevice = deviceMapper.selectOne(
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Device>()
|
||||
.eq(Device::getDeviceType, 2) // 2-测控一体机
|
||||
.eq(Device::getSerialNum, bo.getSerialNum())
|
||||
);
|
||||
if (existDevice != null && existDevice.getUserId() != null) {
|
||||
return R.fail("该设备号已被绑定");
|
||||
}
|
||||
|
||||
// 获取控制器的 ProductKey
|
||||
String productKey = aliyunIotProperties.getDeviceType().getControlIntegrated();
|
||||
if (productKey == null || productKey.isEmpty()) {
|
||||
return R.fail("未配置测控一体机的 ProductKey");
|
||||
}
|
||||
|
||||
// 查询设备基础信息
|
||||
Map<String, Object> deviceInfo = iotDeviceService.findDeviceByProductKeyAndName(productKey, bo.getSerialNum());
|
||||
if (deviceInfo == null || !Boolean.TRUE.equals(deviceInfo.get("success"))) {
|
||||
return R.fail("设备不存在");
|
||||
}
|
||||
|
||||
// 提取设备数据
|
||||
Map<String, Object> data = (Map<String, Object>) deviceInfo.get("data");
|
||||
if (data == null || !data.containsKey("deviceList")) {
|
||||
return R.fail("设备信息格式异常");
|
||||
}
|
||||
|
||||
java.util.List<?> deviceList = (java.util.List<?>) data.get("deviceList");
|
||||
if (deviceList == null || deviceList.isEmpty()) {
|
||||
return R.fail("设备不存在");
|
||||
}
|
||||
|
||||
Object deviceObj = deviceList.get(0);
|
||||
String iotId = null;
|
||||
String status = null;
|
||||
|
||||
// 提取 iotId 和 status
|
||||
if (deviceObj instanceof Map) {
|
||||
Map<?, ?> deviceMap = (Map<?, ?>) deviceObj;
|
||||
iotId = (String) deviceMap.get("iotId");
|
||||
Object statusObj = deviceMap.get("status");
|
||||
status = statusObj != null ? statusObj.toString() : null;
|
||||
} else {
|
||||
try {
|
||||
iotId = (String) deviceObj.getClass().getMethod("getIotId").invoke(deviceObj);
|
||||
Object statusObj = deviceObj.getClass().getMethod("getStatus").invoke(deviceObj);
|
||||
status = statusObj != null ? statusObj.toString() : null;
|
||||
} catch (Exception ex) {
|
||||
log.error("无法从设备对象中获取信息: {}", ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (iotId == null || iotId.isEmpty()) {
|
||||
return R.fail("设备 iotId 为空");
|
||||
}
|
||||
|
||||
// 检查设备状态
|
||||
if (status != null) {
|
||||
if ("UNACTIVE".equalsIgnoreCase(status)) {
|
||||
return R.fail("设备未激活");
|
||||
}
|
||||
if ("DISABLE".equalsIgnoreCase(status)) {
|
||||
return R.fail("设备禁用");
|
||||
}
|
||||
}
|
||||
|
||||
// 查询物模型,确定 rated_voltage 的正确格式
|
||||
log.info("查询控制器物模型,ProductKey: {}", productKey);
|
||||
Map<String, Object> thingModel = iotDeviceService.queryThingModel(productKey);
|
||||
log.info("物模型查询结果: {}", thingModel);
|
||||
|
||||
// 先查询现有设备属性,了解 rated_voltage 的正确格式
|
||||
try {
|
||||
Map<String, Object> currentProps = iotDeviceService.queryDeviceProperties(iotId);
|
||||
log.info("当前设备属性: {}", currentProps);
|
||||
} catch (Exception e) {
|
||||
log.warn("查询当前设备属性失败: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// 设置设备属性
|
||||
Map<String, Object> properties = new java.util.HashMap<>();
|
||||
|
||||
// 设置额定电压 - rated_voltage 是结构体类型
|
||||
// 根据C#代码,需要调用 ControllerHelper.GetInputVoltageProperty
|
||||
// 该方法返回一个特定结构的对象
|
||||
Integer voltValue = getVoltageValue(bo.getInputVoltage());
|
||||
if (voltValue == null) {
|
||||
return R.fail("电压参数错误");
|
||||
}
|
||||
|
||||
// 暂时跳过 rated_voltage 设置,先设置其他属性
|
||||
// properties.put("rated_voltage", ratedVoltageStruct);
|
||||
|
||||
// 如果是三相380V四线,设置输出电压
|
||||
if (bo.getInputVoltage() == 4) {
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
properties.put("Switch" + i + "_volt", 380);
|
||||
}
|
||||
}
|
||||
|
||||
// 清空定时控制数据和设置额定电流
|
||||
java.util.List<Object> emptyTimerList = new java.util.ArrayList<>();
|
||||
Double defaultRatingSwitch = 10.0; // 默认额定电流10A
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
properties.put("rating_switch" + i, defaultRatingSwitch);
|
||||
properties.put("localTimer_switch" + i, emptyTimerList);
|
||||
}
|
||||
|
||||
// 如果使用溶解氧功能,设置盐度
|
||||
if (bo.getIsOxygenUsed()) {
|
||||
properties.put("salinitySet", bo.getSalinityCompensation());
|
||||
}
|
||||
|
||||
String propertiesJson = cn.hutool.json.JSONUtil.toJsonStr(properties);
|
||||
log.info("准备设置设备属性,iotId: {}, properties: {}", iotId, propertiesJson);
|
||||
|
||||
Map<String, Object> setResult = iotDeviceService.setDeviceProperty(iotId, propertiesJson);
|
||||
log.info("设置设备属性返回结果: {}", setResult);
|
||||
|
||||
if (setResult == null) {
|
||||
log.error("设置设备属性返回null");
|
||||
return R.fail("设置设备属性失败:返回结果为空");
|
||||
}
|
||||
|
||||
if (!Boolean.TRUE.equals(setResult.get("success"))) {
|
||||
String errorMsg = setResult.get("errorMessage") != null ?
|
||||
setResult.get("errorMessage").toString() : "未知错误";
|
||||
log.error("设置设备属性失败,错误信息: {}", errorMsg);
|
||||
return R.fail("设置设备属性失败: " + errorMsg);
|
||||
}
|
||||
|
||||
// 获取设备属性
|
||||
Map<String, Object> deviceProperties = iotDeviceService.queryDeviceProperties(iotId);
|
||||
|
||||
// 初始化警告码:默认探头离线且未校准 (0x0081)
|
||||
int warnCode = 0x0081;
|
||||
|
||||
// 如果设备离线,添加设备离线警告
|
||||
if (status != null && "OFFLINE".equalsIgnoreCase(status)) {
|
||||
warnCode |= 0x0080; // 设备离线 (0x0080)
|
||||
}
|
||||
|
||||
// 计算设备数量,用于生成设备名称
|
||||
long deviceCount = deviceMapper.selectCount(
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Device>()
|
||||
.eq(Device::getUserId, userId)
|
||||
.eq(Device::getDeviceType, 2)
|
||||
);
|
||||
|
||||
// 创建或更新设备信息
|
||||
Date now = new Date();
|
||||
boolean isNew = (existDevice == null);
|
||||
Device device = isNew ? new Device() : existDevice;
|
||||
|
||||
if (isNew) {
|
||||
device.setIotId(iotId);
|
||||
device.setSerialNum(bo.getSerialNum());
|
||||
device.setDeviceType(2); // 2-测控一体机
|
||||
device.setDeadTime(new Date(now.getTime() + 365L * 24 * 60 * 60 * 1000)); // 一年后到期
|
||||
} else {
|
||||
// 检查是否已过期
|
||||
if (device.getDeadTime() != null && now.after(device.getDeadTime())) {
|
||||
warnCode |= 0x0040; // 设备时间到期 (0x0040)
|
||||
}
|
||||
}
|
||||
|
||||
// 设置基本信息
|
||||
device.setUserId(userId);
|
||||
device.setDeviceName("控制器" + (deviceCount + 1));
|
||||
device.setBindTime(now);
|
||||
device.setInputVoltage(bo.getInputVoltage());
|
||||
device.setVoltageWarnOpen(0);
|
||||
device.setIsOxygenUsed(bo.getIsOxygenUsed() ? 1 : 0);
|
||||
|
||||
// 设置必填字段默认值(数据库非空约束)
|
||||
if (device.getValuePh() == null) {
|
||||
device.setValuePh(0.0);
|
||||
}
|
||||
if (device.getIsOxygenWarnExist() == null) {
|
||||
device.setIsOxygenWarnExist(0);
|
||||
}
|
||||
if (device.getTfluorescence() == null) {
|
||||
device.setTfluorescence(0.0);
|
||||
}
|
||||
if (device.getTreference() == null) {
|
||||
device.setTreference(0.0);
|
||||
}
|
||||
if (device.getPhaseDifference() == null) {
|
||||
device.setPhaseDifference(0.0);
|
||||
}
|
||||
|
||||
if (bo.getIsOxygenUsed()) {
|
||||
device.setSalinityCompensation(bo.getSalinityCompensation());
|
||||
device.setOxyWarnLower(bo.getOxyWarnLower());
|
||||
device.setOxyWarnCallOpen(1);
|
||||
device.setOxyWarnCallNoDis(1);
|
||||
|
||||
// 如果有塘口且开关列表包含0(设备自身),则设置设备的塘口ID
|
||||
if (bo.getPondId() != null && bo.getPondId() > 0
|
||||
&& bo.getListPutPondPartId() != null && bo.getListPutPondPartId().contains(0)) {
|
||||
device.setPondId(bo.getPondId());
|
||||
} else {
|
||||
device.setPondId(null);
|
||||
}
|
||||
} else {
|
||||
device.setPondId(null);
|
||||
}
|
||||
|
||||
device.setTempWarnCallOpen(0);
|
||||
device.setTempWarnCallNoDis(0);
|
||||
|
||||
// 创建开关列表
|
||||
java.util.List<com.intc.fishery.domain.DeviceSwitch> switches = new java.util.ArrayList<>();
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
com.intc.fishery.domain.DeviceSwitch deviceSwitch = new com.intc.fishery.domain.DeviceSwitch();
|
||||
deviceSwitch.setIndex(i);
|
||||
deviceSwitch.setSwitchName(device.getDeviceName() + "_开关_" + i);
|
||||
deviceSwitch.setConnectVoltageType(bo.getInputVoltage());
|
||||
deviceSwitch.setRateElectricValue(defaultRatingSwitch);
|
||||
deviceSwitch.setElectricWarnOpen(0);
|
||||
deviceSwitch.setIsOpen(0);
|
||||
// 初始化电流和电压默认值,避免数据库非空约束错误
|
||||
deviceSwitch.setDetectElectricValue(0.0);
|
||||
deviceSwitch.setDetectVoltageValue(0.0);
|
||||
|
||||
// 如果有塘口且开关列表包含当前索引,则设置开关的塘口ID
|
||||
if (bo.getPondId() != null && bo.getPondId() > 0
|
||||
&& bo.getListPutPondPartId() != null && bo.getListPutPondPartId().contains(i)) {
|
||||
deviceSwitch.setPondId(bo.getPondId());
|
||||
}
|
||||
|
||||
switches.add(deviceSwitch);
|
||||
}
|
||||
|
||||
// 解析并设置设备属性
|
||||
int errorCode = 0;
|
||||
if (deviceProperties != null && Boolean.TRUE.equals(deviceProperties.get("success"))) {
|
||||
Object propData = deviceProperties.get("data");
|
||||
|
||||
if (propData != null) {
|
||||
try {
|
||||
java.lang.reflect.Method getListMethod = propData.getClass().getMethod("getList");
|
||||
Object listObj = getListMethod.invoke(propData);
|
||||
|
||||
if (listObj instanceof java.util.List) {
|
||||
java.util.List<?> propList = (java.util.List<?>) listObj;
|
||||
|
||||
for (Object item : propList) {
|
||||
if (item != null) {
|
||||
try {
|
||||
java.lang.reflect.Method getIdentifierMethod = item.getClass().getMethod("getIdentifier");
|
||||
java.lang.reflect.Method getValueMethod = item.getClass().getMethod("getValue");
|
||||
String attribute = (String) getIdentifierMethod.invoke(item);
|
||||
Object value = getValueMethod.invoke(item);
|
||||
|
||||
if (attribute != null && value != null) {
|
||||
switch (attribute) {
|
||||
case "dissolvedOxygen": // 溶解氧
|
||||
device.setValueDissolvedOxygen(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "currentTemperature": // 温度
|
||||
device.setValueTemperature(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "dosat": // 饱和度
|
||||
device.setValueSaturability(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "errorCode": // 故障码
|
||||
try {
|
||||
errorCode = Integer.parseInt(value.toString());
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("无法解析故障码: {}", value);
|
||||
}
|
||||
break;
|
||||
case "sensorErrorCode": // 传感器错误码
|
||||
try {
|
||||
int sensorErrorCode = Integer.parseInt(value.toString());
|
||||
warnCode |= sensorErrorCode;
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("无法解析传感器错误码: {}", value);
|
||||
}
|
||||
break;
|
||||
case "Tcorrect": // 设备校准状态
|
||||
try {
|
||||
int tcorrect = Integer.parseInt(value.toString());
|
||||
if (tcorrect == 1) {
|
||||
warnCode &= ~0x0001; // 清除未校准标记
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("无法解析校准状态: {}", value);
|
||||
}
|
||||
break;
|
||||
case "ICCID": // 物联网卡号
|
||||
device.setIccId(value.toString());
|
||||
break;
|
||||
case "Treference": // 参比值
|
||||
device.setTreference(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "Tfluorescence": // 荧光值
|
||||
device.setTfluorescence(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
// 开关状态
|
||||
case "switch1":
|
||||
switches.get(0).setIsOpen(Integer.parseInt(value.toString()));
|
||||
break;
|
||||
case "switch2":
|
||||
switches.get(1).setIsOpen(Integer.parseInt(value.toString()));
|
||||
break;
|
||||
case "switch3":
|
||||
switches.get(2).setIsOpen(Integer.parseInt(value.toString()));
|
||||
break;
|
||||
case "switch4":
|
||||
switches.get(3).setIsOpen(Integer.parseInt(value.toString()));
|
||||
break;
|
||||
// 开关电压电流
|
||||
case "switch1_VoltCur":
|
||||
parseSwitchVoltCur(value.toString(), switches.get(0));
|
||||
break;
|
||||
case "switch2_VoltCur":
|
||||
parseSwitchVoltCur(value.toString(), switches.get(1));
|
||||
break;
|
||||
case "switch3_VoltCur":
|
||||
parseSwitchVoltCur(value.toString(), switches.get(2));
|
||||
break;
|
||||
case "switch4_VoltCur":
|
||||
parseSwitchVoltCur(value.toString(), switches.get(3));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("无法从 SDK 对象中获取属性: {}", ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("无法从 SDK 对象中获取属性列表: {}", ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 验证 ICCID 是否存在
|
||||
if (device.getIccId() == null || device.getIccId().isEmpty()) {
|
||||
return R.fail("设备缺少物联网卡号(ICCID)");
|
||||
}
|
||||
|
||||
device.setWarnCode(warnCode);
|
||||
|
||||
// 保存到数据库
|
||||
if (isNew) {
|
||||
deviceMapper.insert(device);
|
||||
log.info("新测控一体机添加成功: userId={}, iotId={}, serialNum={}", userId, iotId, bo.getSerialNum());
|
||||
} else {
|
||||
deviceMapper.updateById(device);
|
||||
log.info("测控一体机更新成功: userId={}, iotId={}, serialNum={}", userId, iotId, bo.getSerialNum());
|
||||
}
|
||||
|
||||
// 保存开关信息
|
||||
for (com.intc.fishery.domain.DeviceSwitch deviceSwitch : switches) {
|
||||
deviceSwitch.setDeviceId(device.getId());
|
||||
deviceSwitchMapper.insert(deviceSwitch);
|
||||
}
|
||||
|
||||
// TODO: 记录绑定历史和故障码(如果需要的话)
|
||||
|
||||
return R.ok();
|
||||
} catch (Exception e) {
|
||||
log.error("添加测控一体机失败: {}", e.getMessage(), e);
|
||||
return R.fail("添加测控一体机失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据输入电压类型获取电压值
|
||||
*/
|
||||
private Integer getVoltageValue(Integer inputVoltage) {
|
||||
switch (inputVoltage) {
|
||||
case 1: return 220;
|
||||
case 2: return 380;
|
||||
case 3: return 380;
|
||||
case 4: return 380;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析开关电压电流数据
|
||||
*/
|
||||
private void parseSwitchVoltCur(String json, com.intc.fishery.domain.DeviceSwitch deviceSwitch) {
|
||||
try {
|
||||
// 清理JSON字符串
|
||||
json = json.replace("\"{", "{").replace("}\"", "}").replace("\\", "");
|
||||
Map<String, Object> content = cn.hutool.json.JSONUtil.toBean(json, Map.class);
|
||||
|
||||
if (content.containsKey("voltage")) {
|
||||
deviceSwitch.setDetectVoltageValue(Double.parseDouble(content.get("voltage").toString()));
|
||||
}
|
||||
if (content.containsKey("current")) {
|
||||
deviceSwitch.setDetectElectricValue(Double.parseDouble(content.get("current").toString()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("解析开关电压电流数据失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "添加设备探测器(水质检测仪)")
|
||||
@PostMapping("/device/add_device_detector")
|
||||
public R<Void> addDeviceDetector(@RequestBody AddDeviceDetectorBo bo) {
|
||||
@@ -825,65 +1273,79 @@ public class IotController extends BaseController {
|
||||
// 解析并设置设备属性
|
||||
if (deviceProperties != null && Boolean.TRUE.equals(deviceProperties.get("success"))) {
|
||||
Object propData = deviceProperties.get("data");
|
||||
if (propData instanceof Map) {
|
||||
Map<?, ?> propMap = (Map<?, ?>) propData;
|
||||
Object listObj = propMap.get("list");
|
||||
if (listObj instanceof java.util.List) {
|
||||
java.util.List<?> propList = (java.util.List<?>) listObj;
|
||||
for (Object item : propList) {
|
||||
if (item instanceof Map) {
|
||||
Map<?, ?> prop = (Map<?, ?>) item;
|
||||
String attribute = (String) prop.get("identifier");
|
||||
Object value = prop.get("value");
|
||||
|
||||
if (attribute != null && value != null) {
|
||||
switch (attribute) {
|
||||
case "dissolvedOxygen": // 溶解氧
|
||||
device.setValueDissolvedOxygen(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "currentTemperature": // 温度
|
||||
device.setValueTemperature(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "dosat": // 饱和度
|
||||
device.setValueSaturability(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "PH":
|
||||
device.setValuePh(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "salinity": // 盐度
|
||||
device.setValueSalinity(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "sensorErrorCode": // 警告码
|
||||
try {
|
||||
int errorCode = Integer.parseInt(value.toString());
|
||||
warnCode |= errorCode;
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("无法解析警告码: {}", value);
|
||||
|
||||
if (propData != null) {
|
||||
// 通过反射获取属性列表
|
||||
try {
|
||||
java.lang.reflect.Method getListMethod = propData.getClass().getMethod("getList");
|
||||
Object listObj = getListMethod.invoke(propData);
|
||||
|
||||
if (listObj instanceof java.util.List) {
|
||||
java.util.List<?> propList = (java.util.List<?>) listObj;
|
||||
|
||||
for (Object item : propList) {
|
||||
if (item != null) {
|
||||
// 通过反射获取属性标识符和值
|
||||
try {
|
||||
java.lang.reflect.Method getIdentifierMethod = item.getClass().getMethod("getIdentifier");
|
||||
java.lang.reflect.Method getValueMethod = item.getClass().getMethod("getValue");
|
||||
String attribute = (String) getIdentifierMethod.invoke(item);
|
||||
Object value = getValueMethod.invoke(item);
|
||||
|
||||
if (attribute != null && value != null) {
|
||||
switch (attribute) {
|
||||
case "dissolvedOxygen": // 溶解氧
|
||||
device.setValueDissolvedOxygen(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "currentTemperature": // 温度
|
||||
device.setValueTemperature(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "dosat": // 饱和度
|
||||
device.setValueSaturability(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "PH":
|
||||
device.setValuePh(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "salinity": // 盐度
|
||||
device.setValueSalinity(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "sensorErrorCode": // 警告码
|
||||
try {
|
||||
int errorCode = Integer.parseInt(value.toString());
|
||||
warnCode |= errorCode;
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("无法解析警告码: {}", value);
|
||||
}
|
||||
break;
|
||||
case "Tcorrect": // 设备校准状态
|
||||
try {
|
||||
int tcorrect = Integer.parseInt(value.toString());
|
||||
if (tcorrect == 1) {
|
||||
warnCode &= ~0x0001; // 清除未校准标记
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("无法解析校准状态: {}", value);
|
||||
}
|
||||
break;
|
||||
case "ICCID": // 物联网卡号
|
||||
device.setIccId(value.toString());
|
||||
break;
|
||||
case "Treference": // 参比值
|
||||
device.setTreference(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "Tfluorescence": // 荧光值
|
||||
device.setTfluorescence(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "Tcorrect": // 设备校准状态
|
||||
try {
|
||||
int tcorrect = Integer.parseInt(value.toString());
|
||||
if (tcorrect == 1) {
|
||||
warnCode &= ~0x0001; // 清除未校准标记
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("无法解析校准状态: {}", value);
|
||||
}
|
||||
break;
|
||||
case "ICCID": // 物联网卡号
|
||||
device.setIccId(value.toString());
|
||||
break;
|
||||
case "Treference": // 参比值
|
||||
device.setTreference(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
case "Tfluorescence": // 荧光值
|
||||
device.setTfluorescence(Double.parseDouble(value.toString()));
|
||||
break;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("无法从 SDK 对象中获取属性: {}", ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("无法从 SDK 对象中获取属性列表: {}", ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.intc.iot.domain.bo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 添加测控一体机业务对象
|
||||
*
|
||||
* @author intc
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "添加测控一体机请求对象")
|
||||
public class AddDeviceControllerBo implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 设备编号/序列号
|
||||
*/
|
||||
@Schema(description = "设备编号/序列号")
|
||||
@NotBlank(message = "设备编号不能为空")
|
||||
private String serialNum;
|
||||
|
||||
/**
|
||||
* 塘口ID
|
||||
*/
|
||||
@Schema(description = "塘口ID")
|
||||
private Long pondId;
|
||||
|
||||
/**
|
||||
* 盐度补偿值
|
||||
*/
|
||||
@Schema(description = "盐度补偿值")
|
||||
@NotNull(message = "盐度补偿值不能为空")
|
||||
private Double salinityCompensation;
|
||||
|
||||
/**
|
||||
* 溶解氧报警下限
|
||||
*/
|
||||
@Schema(description = "溶解氧报警下限")
|
||||
@NotNull(message = "溶解氧报警下限不能为空")
|
||||
private Double oxyWarnLower;
|
||||
|
||||
/**
|
||||
* 输入电压类型(1-单相220V, 2-单相380V, 3-三相380V, 4-三相380V四线)
|
||||
*/
|
||||
@Schema(description = "输入电压类型")
|
||||
@NotNull(message = "输入电压类型不能为空")
|
||||
private Integer inputVoltage;
|
||||
|
||||
/**
|
||||
* 是否使用溶解氧功能
|
||||
*/
|
||||
@Schema(description = "是否使用溶解氧功能")
|
||||
@NotNull(message = "是否使用溶解氧功能不能为空")
|
||||
private Boolean isOxygenUsed;
|
||||
|
||||
/**
|
||||
* 开关放置塘口分区ID列表(0表示设备自身,1-4表示开关1-4)
|
||||
*/
|
||||
@Schema(description = "开关放置塘口分区ID列表")
|
||||
private List<Integer> listPutPondPartId;
|
||||
}
|
||||
@@ -132,6 +132,13 @@ public class IotDeviceServiceImpl implements IotDeviceService {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("success", response.getSuccess());
|
||||
result.put("data", response.getData());
|
||||
result.put("errorMessage", response.getErrorMessage());
|
||||
result.put("code", response.getCode());
|
||||
|
||||
if (!response.getSuccess()) {
|
||||
log.error("设置设备属性失败,Code: {}, ErrorMessage: {}", response.getCode(), response.getErrorMessage());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user