数据接收功能修改

This commit is contained in:
zhangjunwen 2024-10-09 18:01:26 +08:00
parent 00193a7252
commit cacf21776c
4 changed files with 95 additions and 89 deletions

View File

@ -244,7 +244,7 @@ public class MyMqttCallback implements MqttCallback {
fields.put(v,(Double.parseDouble(msg.get(k).toString()) - 400) * 1.25);
break;
case "temp":
fields.put(v,Double.parseDouble(msg.get(k).toString()) * 0.1);
fields.put(v,Double.parseDouble(msg.get(k).toString()) / 10.0);
break;
default:
fields.put(v,msg.get(k));
@ -258,7 +258,7 @@ public class MyMqttCallback implements MqttCallback {
int alarmNum = alarmRulesApi.alarmRulesFilter(fields, rules);
logger.info("本次产生报警{}条",alarmNum);
}
influxDBService.insert(measurement,tags, fields);
influxDBService.insert(measurement, tags, fields);
logger.info("数据已插入{}表中!",measurement);
}

View File

@ -23,16 +23,16 @@ import java.util.stream.Collectors;
@Service
@Validated
@Slf4j
public class DataQueryService implements IDataQueryService{
public class DataQueryService implements IDataQueryService {
@Autowired
private InfluxDBService influxDBService;
@Override
public Map<String, Object> selectDataListByPages(String equipId,String tableName,String startTime,String endTime, Integer pageSize, Integer pageNum){
public Map<String, Object> selectDataListByPages(String equipId, String tableName, String startTime, String endTime, Integer pageSize, Integer pageNum) {
Map<String, Object> resMap = new HashMap<>();
//TODO 分页查找条数根据设备id表名时间范围页码每页条数
List<Map<String, Object>> dataList = queryDataByTime(tableName, null, startTime, endTime, pageNum, pageSize);
List<Map<String, Object>> dataList = queryDataByTime(tableName, equipId, startTime, endTime, pageNum, pageSize);
dataList.forEach(map -> {
try {
String time = map.get("time").toString();
@ -41,10 +41,14 @@ public class DataQueryService implements IDataQueryService{
throw new RuntimeException(e);
}
});
//TODO 时间需要将查询到的UTC时间转为系统时间北京时间
//TODO 时间需要将查询到的UTC时间转为系统时间北京时间
// DateUtils.tranUTC2LocalDateTime("2024-08-28T08:00:00Z");
//TODO 查询数据总条数
long total = countDataList(tableName, "x_push_temp", null, startTime, endTime);
long total = 0L;
if (dataList.size() != 0) {
List<String> keys = new ArrayList<>(dataList.get(0).keySet());
total = countDataList(tableName, keys.get(0), null, startTime, endTime);
}
resMap.put("list", dataList);
resMap.put("total", total);
return resMap;
@ -70,13 +74,13 @@ public class DataQueryService implements IDataQueryService{
// }
List<Map<String, Object>> list = selectDataByColumnNameandDateSegmentation(equipId, tableName, columnName, startTime, endTime, intervalHours);
Map<String, Object> resMap = new HashMap<>();
if (list.size() == 0){
if (list.size() == 0) {
return null;
}
List<Object> columnList = new ArrayList<>();
List<String> timelist = new ArrayList<>();
if(intervalHours > 6){
List<String> timelist = new ArrayList<>();
if (intervalHours > 6) {
columnName = "mean";
}
for (Map<String, Object> data : list) {
@ -93,49 +97,49 @@ public class DataQueryService implements IDataQueryService{
/**
* 根据设备id获取排屑电流数据
*/
public Map<String, Object> getCurrentData(String equipId, String startTime, String endTime){
public Map<String, Object> getCurrentData(String equipId, String startTime, String endTime) {
String tableName = "gateway_current_data";
String columnName = "chip_removal_1,chip_removal_2";
return getAllData2ChartData(equipId,tableName,columnName, startTime, endTime);
return getAllData2ChartData(equipId, tableName, columnName, startTime, endTime);
}
/**
* 根据设备id获取液压数据
*/
public Map<String, Object> getPressData(String equipId, String startTime, String endTime){
public Map<String, Object> getPressData(String equipId, String startTime, String endTime) {
String tableName = "gateway_hy_data";
String columnName = "hy_1,hy_2,hy_3";
return getAllData2ChartData(equipId,tableName,columnName, startTime, endTime);
return getAllData2ChartData(equipId, tableName, columnName, startTime, endTime);
}
/**
* 根据设备id获取温度数据
*/
public Map<String, Object> getTempData(String equipId, String startTime, String endTime){
public Map<String, Object> getTempData(String equipId, String startTime, String endTime) {
String tableName = "gateway_temp_data";
String columnName = "cr1_temp,cr2_temp,x0_temp,xp_temp,xn_temp,y0_temp,yp_temp,yn_temp,z0_temp,zp_temp,zn_temp";
return getAllData2ChartData(equipId,tableName,columnName, startTime, endTime);
return getAllData2ChartData(equipId, tableName, columnName, startTime, endTime);
}
/**
* 根据设备id获取振动数据
*/
public Map<String, Object> getVibrData(String equipId, String startTime, String endTime){
public Map<String, Object> getVibrData(String equipId, String startTime, String endTime) {
String tableName = "gateway_aclr_data";
String columnName = "x_aclr_rms,x_speed_rms,x_dis_rms,y_aclr_rms,y_speed_rms,y_dis_rms,z_aclr_rms,z_speed_rms,z_dis_rms," +
"x_aclr_peak,x_speed_peak,x_dis_peak,y_aclr_peak,y_speed_peak,y_dis_peak,z_aclr_peak,z_speed_peak,z_dis_peak";
return getAllData2ChartData(equipId,tableName,columnName, startTime, endTime);
return getAllData2ChartData(equipId, tableName, columnName, startTime, endTime);
}
/**
* 根据设备id获取工作数据
*/
public Map<String, Object> getWorkData(String equipId, String startTime, String endTime){
public Map<String, Object> getWorkData(String equipId, String startTime, String endTime) {
String tableName = "gateway_fanuc_data";
String columnName = "power_time,process_num,total_process,work_time";
Map<String, Object> resultMap = getAllData2ChartData(equipId,tableName,columnName, startTime, endTime);
if(resultMap == null){
Map<String, Object> resultMap = getAllData2ChartData(equipId, tableName, columnName, startTime, endTime);
if (resultMap == null) {
return null;
}
List<Double> workTimeList = (List<Double>) resultMap.get("work_time");
@ -145,42 +149,42 @@ public class DataQueryService implements IDataQueryService{
return resultMap;
}
public Map<String, Object> getAllData2ChartData(String equipId,String tableName,String columns, String startTime, String endTime){
public Map<String, Object> getAllData2ChartData(String equipId, String tableName, String columns, String startTime, String endTime) {
LocalDateTime stime = DateUtils.tranUTCtoLocalDateTime(startTime);
LocalDateTime etime = DateUtils.tranUTCtoLocalDateTime(endTime);
long intervalHours = ChronoUnit.HOURS.between(stime, etime);
List<Map<String, Object>> list = selectDataByColumnNameandDateSegmentation(equipId, tableName, columns, startTime, endTime, intervalHours);
Map<String, Object> resMap = new HashMap<>();
if (list.size() == 0){
if (list.size() == 0) {
return null;
}
List<Object> columnList = new ArrayList<>();
List<String> timelist = new ArrayList<>();
List<String> timelist = new ArrayList<>();
String[] columnName = columns.split(",");
if(intervalHours > 6){
for(int i=0;i < columnName.length;i++){
if (intervalHours > 6) {
for (int i = 0; i < columnName.length; i++) {
columnName[i] = "mean_" + columnName[i];
}
}
for (String cn : columnName) {
if(cn.trim().length() == 0){
if (cn.trim().length() == 0) {
continue;
}
String realCn = cn;
if(cn.startsWith("mean")){
if (cn.startsWith("mean")) {
realCn = cn.replaceFirst("mean_", "");
}
resMap.put(realCn, list.stream().map(map->{
Double data = (Double)map.get(cn);
if("gateway_temp_data".equals(tableName)){//温度处理
return data/10.0;
}
if("work_time".equals(columnName) || "power_time".equals(columnName)){//时间转小时
return data/60.0;
}
return data;
resMap.put(realCn, list.stream().map(map -> {
Double data = (Double) map.get(cn);
if ("gateway_temp_data".equals(tableName)) {//温度处理
return data / 10.0;
}
if ("work_time".equals(columnName) || "power_time".equals(columnName)) {//时间转小时
return data / 60.0;
}
return data;
}).collect(Collectors.toList()));
resMap.put("time", list.stream().map(map -> DateUtils.utcToCst(map.get("time").toString())).collect(Collectors.toList()));
}
@ -189,19 +193,18 @@ public class DataQueryService implements IDataQueryService{
}
/**
*按照策略分段查询数据列表
* 按照策略分段查询数据列表
*/
private List<Map<String, Object>> selectDataByColumnNameandDateSegmentation(String equipId, String tableName, String columnName, String startTime, String endTime,long intervalHours)
{
private List<Map<String, Object>> selectDataByColumnNameandDateSegmentation(String equipId, String tableName, String columnName, String startTime, String endTime, long intervalHours) {
List<Map<String, Object>> list = new ArrayList<>();
if(intervalHours <= 6){//6小时内全查
list = selectDataByColumnNameandDate(null,tableName, columnName, startTime, endTime);
}else if(intervalHours <= 7*24 ){//7天内每30s一个
list = selectLongTimeDataByColumnNameandDate(null,tableName, columnName, startTime, endTime, "30s");
}else if(intervalHours <= 30*24 ){//30天内每2min一个
list = selectLongTimeDataByColumnNameandDate(null,tableName, columnName, startTime, endTime, "2m");
}else if (intervalHours <= 90*24){//90天以上每6min一个
list = selectLongTimeDataByColumnNameandDate(null,tableName, columnName, startTime, endTime, "6m");
if (intervalHours <= 6) {//6小时内全查
list = selectDataByColumnNameandDate(null, tableName, columnName, startTime, endTime);
} else if (intervalHours <= 7 * 24) {//7天内每30s一个
list = selectLongTimeDataByColumnNameandDate(null, tableName, columnName, startTime, endTime, "30s");
} else if (intervalHours <= 30 * 24) {//30天内每2min一个
list = selectLongTimeDataByColumnNameandDate(null, tableName, columnName, startTime, endTime, "2m");
} else if (intervalHours <= 90 * 24) {//90天以上每6min一个
list = selectLongTimeDataByColumnNameandDate(null, tableName, columnName, startTime, endTime, "6m");
}
return list;
}
@ -209,26 +212,26 @@ public class DataQueryService implements IDataQueryService{
private List<Map<String, Object>> queryDataByTime(String tableName, String equipId, String beginTime, String endTime, Integer pageNum, Integer pageSize) {
StringBuilder sql = new StringBuilder("select * from ")
.append("\"").append(tableName).append("\"");
if(equipId != null){
if (equipId != null) {
sql.append(" where equip_id = '").append(equipId).append("'");
}
if(beginTime != null){
if(!sql.toString().contains("where")){
if (beginTime != null) {
if (!sql.toString().contains("where")) {
sql.append(" where");
}else{
} else {
sql.append(" and");
}
sql.append(" time >= '").append(beginTime).append("'");
}
if(endTime != null){
if(!sql.toString().contains("where")){
if (endTime != null) {
if (!sql.toString().contains("where")) {
sql.append(" where");
}else{
} else {
sql.append(" and");
}
sql.append(" time <= '").append(endTime).append("'");
}
if(pageNum != null || pageSize != null) {
if (pageNum != null || pageSize != null) {
sql.append(" order by time desc ")
.append(" limit ").append(pageSize)
.append(" offset ").append((pageNum - 1) * pageSize);
@ -238,24 +241,24 @@ public class DataQueryService implements IDataQueryService{
return influxDBService.queryResultProcess(influxDBService.query(sql.toString()));
}
private long countDataList(String tableName,String field, String equipId, String beginTime, String endTime){
private long countDataList(String tableName, String field, String equipId, String beginTime, String endTime) {
StringBuilder sql = new StringBuilder("select count(").append(field).append(") from ")
.append("\"").append(tableName).append("\"");
if(equipId != null){
if (equipId != null) {
sql.append(" where equip_id = '").append(equipId).append("'");
}
if(beginTime != null){
if(!sql.toString().contains("where")){
if (beginTime != null) {
if (!sql.toString().contains("where")) {
sql.append(" where");
}else{
} else {
sql.append(" and");
}
sql.append(" time >= '").append(beginTime).append("'");
}
if(endTime != null){
if(!sql.toString().contains("where")){
if (endTime != null) {
if (!sql.toString().contains("where")) {
sql.append(" where");
}else{
} else {
sql.append(" and");
}
sql.append(" time <= '").append(endTime).append("'");
@ -266,30 +269,31 @@ public class DataQueryService implements IDataQueryService{
/**
* 根据字段名称和起止时间查询数据
* @param tableName 表名
*
* @param tableName 表名
* @param columnName 字段名称
* @param beginTime 起始时间
* @param endTime 结束时间
* @param beginTime 起始时间
* @param endTime 结束时间
* @return 数据
*/
private List<Map<String, Object>> selectDataByColumnNameandDate(String equipId, String tableName, String columnName, String beginTime, String endTime){
private List<Map<String, Object>> selectDataByColumnNameandDate(String equipId, String tableName, String columnName, String beginTime, String endTime) {
StringBuilder sql = new StringBuilder("select time,").append(columnName).append(" from ")
.append("\"").append(tableName).append("\"");//TODO 修改时间
if(equipId != null){
if (equipId != null) {
sql.append(" where equip_id = '").append(equipId).append("'");
}
if(beginTime != null){
if(!sql.toString().contains("where")){
if (beginTime != null) {
if (!sql.toString().contains("where")) {
sql.append(" where");
}else{
} else {
sql.append(" and");
}
sql.append(" time >= '").append(beginTime).append("'");
}
if(endTime != null){
if(!sql.toString().contains("where")){
if (endTime != null) {
if (!sql.toString().contains("where")) {
sql.append(" where");
}else{
} else {
sql.append(" and");
}
sql.append(" time <= '").append(endTime).append("'");
@ -300,27 +304,27 @@ public class DataQueryService implements IDataQueryService{
/**
* 查询长时间数据每分钟取一条展示最长3个月
*/
private List<Map<String, Object>> selectLongTimeDataByColumnNameandDate(String equipId,String tableName, String columnName, String beginTime, String endTime, String interval){
if(columnName.split(",").length > 1){
private List<Map<String, Object>> selectLongTimeDataByColumnNameandDate(String equipId, String tableName, String columnName, String beginTime, String endTime, String interval) {
if (columnName.split(",").length > 1) {
columnName = "*";
}
StringBuilder sql = new StringBuilder("select time, mean(").append(columnName).append(") from ")
.append("\"").append(tableName).append("\"");
if(equipId != null){
if (equipId != null) {
sql.append(" where equip_id = '").append(equipId).append("'");
}
if(beginTime != null){
if(!sql.toString().contains("where")){
if (beginTime != null) {
if (!sql.toString().contains("where")) {
sql.append(" where");
}else{
} else {
sql.append(" and");
}
sql.append(" time >= '").append(beginTime).append("'");
}
if(endTime != null){
if(!sql.toString().contains("where")){
if (endTime != null) {
if (!sql.toString().contains("where")) {
sql.append(" where");
}else{
} else {
sql.append(" and");
}
sql.append(" time <= '").append(endTime).append("'");

View File

@ -104,6 +104,8 @@ public class NoticeController {
Assert.notNull(notice, "公告不能为空");
// 直接插入站内信表
notice.setId(null);
notice.setCreateTime(null);
notice.setUpdateTime(null);
NotifyMessageDO notifyMessage = BeanUtils.toBean(notice, NotifyMessageDO.class);
notifyMessage.setTemplateNickname(SecurityFrameworkUtils.getLoginUserNickname());
notifyMessage.setTemplateType(notice.getType());

View File

@ -55,17 +55,17 @@ public interface NotifyMessageMapper extends BaseMapperX<NotifyMessageDO> {
default List<NotifyMessageDO> selectUnreadListByUserIdAndUserType(Long userId, Integer userType, Integer size) {
return selectList(new QueryWrapperX<NotifyMessageDO>() // 由于要使用 limitN 语句所以只能用 QueryWrapperX
.eq("user_id", userId)
.eq("user_type", userType)
// .eq("user_id", userId)
// .eq("user_type", userType)
.eq("read_status", false)
.orderByDesc("id").limitN(size));
}
default Long selectUnreadCountByUserIdAndUserType(Long userId, Integer userType) {
return selectCount(new LambdaQueryWrapperX<NotifyMessageDO>()
.eq(NotifyMessageDO::getReadStatus, false)
.eq(NotifyMessageDO::getUserId, userId)
.eq(NotifyMessageDO::getUserType, userType));
.eq(NotifyMessageDO::getReadStatus, false));
// .eq(NotifyMessageDO::getUserId, userId)
// .eq(NotifyMessageDO::getUserType, userType));
}
}