代码优化

This commit is contained in:
zzc 2024-08-30 23:12:02 +08:00
parent 70f38708f6
commit 17d9b7e041
6 changed files with 42 additions and 10 deletions

View File

@ -13,6 +13,9 @@ import org.springframework.stereotype.Component;
@Component @Component
public class CommonYmlConfig { public class CommonYmlConfig {
public static String URL_IOT_USERNAME = "iot用户名";
public static String URL_IOT_PASSWORD = "iot密码";
public static String URL_IOT_BASEURL = "ip端口"; public static String URL_IOT_BASEURL = "ip端口";
public static String URL_IOT_SCOPE_ATTRIBUTE = "作用域属性"; public static String URL_IOT_SCOPE_ATTRIBUTE = "作用域属性";
public static String URL_IOT_TELEMETRY = "遥测"; public static String URL_IOT_TELEMETRY = "遥测";
@ -20,6 +23,9 @@ public class CommonYmlConfig {
public static String URL_IOT_DEVICE = "设备信息"; public static String URL_IOT_DEVICE = "设备信息";
public static String URL_IOT_DEVICE_BY_NAME = "设备名称查询设备信息"; public static String URL_IOT_DEVICE_BY_NAME = "设备名称查询设备信息";
public String username = "";
public String password = "";
public String baseurl = ""; public String baseurl = "";
public String scopeAttribute = ""; public String scopeAttribute = "";
public String telemetry = ""; public String telemetry = "";
@ -27,6 +33,15 @@ public class CommonYmlConfig {
public String device = ""; public String device = "";
public String deviceByName = ""; public String deviceByName = "";
@Value("${iot.username}")
public String setUsername(String username) {
return URL_IOT_USERNAME = username;
}
@Value("${iot.password}")
public String setPassword(String password) {
return URL_IOT_PASSWORD = password;
}
@Value("${iot.url.scopeAttribute}") @Value("${iot.url.scopeAttribute}")
public String setScopeAttribute(String scopeAttribute) { public String setScopeAttribute(String scopeAttribute) {
return URL_IOT_SCOPE_ATTRIBUTE = scopeAttribute; return URL_IOT_SCOPE_ATTRIBUTE = scopeAttribute;

View File

@ -60,7 +60,7 @@ public class IotService {
map.put("entityType","DEVICE"); map.put("entityType","DEVICE");
map.put("entityId",deviceCode); map.put("entityId",deviceCode);
String send = getSend(CommonYmlConfig.URL_IOT_TELEMETRY, map); String send = getSend(CommonYmlConfig.URL_IOT_TELEMETRY, map);
if (send.startsWith("{\"status\":")) return null; if (send.startsWith("{\"status\":") || "Device with requested id wasn't found!".equals(send)) return null;
JSONObject jsonObject = JSON.parseObject(send); JSONObject jsonObject = JSON.parseObject(send);
//转换 //转换
Map<String, String> resultMap = new HashMap<>(); Map<String, String> resultMap = new HashMap<>();
@ -207,8 +207,8 @@ public class IotService {
String token; String token;
String longinUrl = "/api/auth/login"; String longinUrl = "/api/auth/login";
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("username", "tenant@thingsboard.org"); map.put("username", CommonYmlConfig.URL_IOT_USERNAME);
map.put("password", "jsDU89naXB4X2y!Y"); map.put("password", CommonYmlConfig.URL_IOT_PASSWORD);
String loginPath = baseUrl + longinUrl; String loginPath = baseUrl + longinUrl;
HttpRequest request = HttpUtil.createPost(loginPath); HttpRequest request = HttpUtil.createPost(loginPath);
HttpResponse execute = request.body(JSON.toJSONString(map)).execute(); HttpResponse execute = request.body(JSON.toJSONString(map)).execute();

View File

@ -2,6 +2,7 @@ package org.dromara.web.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils; import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.MessageUtils; import org.dromara.common.core.utils.MessageUtils;
import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.StringUtils;
@ -37,6 +38,7 @@ import java.util.*;
*/ */
@RequiredArgsConstructor @RequiredArgsConstructor
@Service @Service
@Slf4j
public class EqEquipmentServiceImpl implements IEqEquipmentService { public class EqEquipmentServiceImpl implements IEqEquipmentService {
private final EqEquipmentMapper baseMapper; private final EqEquipmentMapper baseMapper;
@ -76,15 +78,24 @@ public class EqEquipmentServiceImpl implements IEqEquipmentService {
for (EqEquipmentVo record : records) { for (EqEquipmentVo record : records) {
String equipmentCode = record.getEquipmentCode(); String equipmentCode = record.getEquipmentCode();
if (record.getLineStatus() == 1){//在线更新数据
DeviceMsgDto telemetryMsg = iotService.httpTelemetryGetSend(equipmentCode); DeviceMsgDto telemetryMsg = iotService.httpTelemetryGetSend(equipmentCode);
if (telemetryMsg == null) { if (telemetryMsg == null ){
record.setLineStatus(0);//未获取到数据统一为离线 record.setLineStatus(0);//未获取到数据统一为离线
log.warn("检测到设备离线状态未更新 设备[{}]离线为更新", equipmentCode);
EqEquipment eqEquipment = new EqEquipment();
eqEquipment.setEquipmentId(record.getEquipmentId());
eqEquipment.setLineStatus(0);
baseMapper.updateById(eqEquipment);
log.warn("检测到设备离线状态未更新 设备[{}]离线已更新", equipmentCode);
continue; continue;
} }
record.setStatus(telemetryMsg.getOpenPercentage()); record.setStatus(telemetryMsg.getOpenPercentage());
record.setFaultStatus(telemetryMsg.getGarageException()); record.setFaultStatus(telemetryMsg.getGarageException());
}else{
// 离线不需要调用接口 也不更新状态 在线离线状态要在事件触发的时候更新 否则性能很差
}
} }
return TableDataInfo.build(result); return TableDataInfo.build(result);
} }

View File

@ -259,6 +259,8 @@ justauth:
iot: iot:
baseurl: http://8.219.157.97:9090 baseurl: http://8.219.157.97:9090
username: tenant@thingsboard.org
password: jsDU89naXB4X2y!Y
url: url:
scopeAttribute: /api/plugins/telemetry/{entityType}/{entityId}/values/attributes/{scope} scopeAttribute: /api/plugins/telemetry/{entityType}/{entityId}/values/attributes/{scope}
telemetry: /api/plugins/telemetry/{entityType}/{entityId}/values/timeseries telemetry: /api/plugins/telemetry/{entityType}/{entityId}/values/timeseries

View File

@ -259,6 +259,8 @@ justauth:
iot: iot:
baseurl: http://118.89.86.111:8080 baseurl: http://118.89.86.111:8080
username: tenant@thingsboard.org
password: jsDU89naXB4X2y!Y
url: url:
scopeAttribute: /api/plugins/telemetry/{entityType}/{entityId}/values/attributes/{scope} scopeAttribute: /api/plugins/telemetry/{entityType}/{entityId}/values/attributes/{scope}
telemetry: /api/plugins/telemetry/{entityType}/{entityId}/values/timeseries telemetry: /api/plugins/telemetry/{entityType}/{entityId}/values/timeseries

View File

@ -261,6 +261,8 @@ justauth:
iot: iot:
baseurl: http://8.219.157.97:8080 baseurl: http://8.219.157.97:8080
username: tenant@thingsboard.org
password: jsDU89naXB4X2y!Y
url: url:
scopeAttribute: /api/plugins/telemetry/{entityType}/{entityId}/values/attributes/{scope} scopeAttribute: /api/plugins/telemetry/{entityType}/{entityId}/values/attributes/{scope}
telemetry: /api/plugins/telemetry/{entityType}/{entityId}/values/timeseries telemetry: /api/plugins/telemetry/{entityType}/{entityId}/values/timeseries