5 changed files with 199 additions and 139 deletions
@ -1,131 +0,0 @@ |
|||||
package com.hxjt.dataupload.jobhandler.mqtt.job.user.jobhandler; |
|
||||
|
|
||||
import cn.hutool.json.JSONUtil; |
|
||||
|
|
||||
import com.hxjt.dataupload.jobhandler.mqtt.enums.AlarmType; |
|
||||
import com.hxjt.dataupload.jobhandler.mqtt.job.user.domain.AlarmInfoHx; |
|
||||
import com.hxjt.dataupload.jobhandler.mqtt.job.user.domain.AlarmInfoIot; |
|
||||
import com.hxjt.dataupload.mqtt.MqttClient; |
|
||||
import com.xxl.job.core.context.XxlJobHelper; |
|
||||
import com.xxl.job.core.handler.annotation.XxlJob; |
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
import org.springframework.beans.factory.annotation.Value; |
|
||||
import org.springframework.http.ResponseEntity; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
import org.springframework.web.client.RestTemplate; |
|
||||
|
|
||||
import javax.annotation.Resource; |
|
||||
import java.text.SimpleDateFormat; |
|
||||
import java.util.Date; |
|
||||
import java.util.List; |
|
||||
import java.util.Map; |
|
||||
import java.util.TimeZone; |
|
||||
|
|
||||
/* |
|
||||
* @description: 报警信息推送 |
|
||||
* @author: ZhangRY |
|
||||
* @date: 2025/4/9 10:51 |
|
||||
* @param: null |
|
||||
* @return: null |
|
||||
**/ |
|
||||
@Component |
|
||||
@Slf4j |
|
||||
public class AlarmInfo2MqttJob { |
|
||||
|
|
||||
@Resource |
|
||||
MqttClient mqttClient; |
|
||||
|
|
||||
@Value("${spring.mqtt.deviceTopic:}") |
|
||||
private String deviceTopic; |
|
||||
|
|
||||
@Value("${user.token.containerId:}") |
|
||||
private String containerId; |
|
||||
@Resource |
|
||||
RestTemplate restTemplate; |
|
||||
|
|
||||
@Value("${alarm.real.url:}") |
|
||||
private String alarmUrl; |
|
||||
|
|
||||
@Value("${alarm.real.code:}") |
|
||||
private String codeAlarm; |
|
||||
|
|
||||
@XxlJob("alarmInfo2MqttJobHandler") |
|
||||
public void alarmInfo2MqttJobHandler() throws Exception { |
|
||||
try { |
|
||||
XxlJobHelper.log("Alarm info To Mqtt Job Handler Beginning."); |
|
||||
|
|
||||
ResponseEntity<Map> forEntity = restTemplate.getForEntity(alarmUrl, Map.class); |
|
||||
Map body = forEntity.getBody(); |
|
||||
List<Map<String,Object>> data = (List<Map<String,Object>>)body.get("data"); |
|
||||
log.info(">>>>>>>>>>>>>>>>>>data="+data.size()); |
|
||||
XxlJobHelper.log(">>>>>>>>>>>>>>>>>>data="+data.size()); |
|
||||
data.forEach(alarmMap -> { |
|
||||
try { |
|
||||
AlarmInfoIot alarmInfoIot = new AlarmInfoIot(); |
|
||||
AlarmInfoHx alarmInfoHx = new AlarmInfoHx(); |
|
||||
Object id = alarmMap.get("id"); |
|
||||
alarmInfoHx.setUniqueId(id != null ? containerId + id : ""); |
|
||||
alarmInfoHx.setContainerId(containerId); |
|
||||
Object deviceNo = alarmMap.get("deviceNo"); |
|
||||
alarmInfoHx.setDeviceCode(deviceNo != null ? deviceNo.toString() : ""); |
|
||||
Object alarmType = alarmMap.get("warningType"); |
|
||||
String code = alarmType.toString(); |
|
||||
if(codeAlarm.equals(code)||"2".equals(code)||"3".equals(code)||"8".equals(code)||"15".equals(code)||"16".equals(code)) { |
|
||||
log.info(">>>>>>>>>>>>>>>>>>>>>>>>>alarm occur"); |
|
||||
XxlJobHelper.log(">>>>>>>>>>>>>>>>>>>>>>>>>alarm occur"); |
|
||||
if (alarmType != null) { |
|
||||
String contentByCode = AlarmType.getContentByCode(code); |
|
||||
alarmInfoHx.setAlarmType(contentByCode); |
|
||||
alarmInfoHx.setAlarmContent(contentByCode); |
|
||||
} |
|
||||
Object layer = alarmMap.get("factory"); |
|
||||
alarmInfoHx.setLayerId(layer != null ? layer.toString() : ""); |
|
||||
Object area = alarmMap.get("area"); |
|
||||
alarmInfoHx.setSecAreaId(area != null ? area.toString() : ""); |
|
||||
Object areaName = alarmMap.get("areaName"); |
|
||||
alarmInfoHx.setSecAreaName(areaName != null ? areaName.toString() : ""); |
|
||||
Object latitude = alarmMap.get("latitude"); |
|
||||
alarmInfoHx.setX(latitude != null ? new Double(latitude.toString()) : 0); |
|
||||
Object longitude = alarmMap.get("longitude"); |
|
||||
alarmInfoHx.setY(longitude != null ? new Double(longitude.toString()) : 0); |
|
||||
Object dateTime = alarmMap.get("warningtime"); |
|
||||
log.info(">>>>>>>>>>>>>>>>>>>>>>>>>dateTime="+dateTime); |
|
||||
XxlJobHelper.log(">>>>>>>>>>>>>>>>>>>>>>>>>dateTime="+dateTime); |
|
||||
if (dateTime != null) { |
|
||||
try { |
|
||||
Date date = new Date(Long.parseLong(dateTime.toString())); |
|
||||
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
||||
sf.setTimeZone(TimeZone.getTimeZone("UTC")); |
|
||||
String dateStr = sf.format(date); |
|
||||
alarmInfoHx.setDateTime(dateStr); |
|
||||
} catch (NumberFormatException e) { |
|
||||
log.info(">>>>>>>>>>>>>>>>>>>>>>>>>exception occur"+e.getMessage()); |
|
||||
XxlJobHelper.log(">>>>>>>>>>>>>>>>>>>>>>>>>exception occur"+e.getMessage()); |
|
||||
alarmInfoHx.setDateTime(dateTime.toString()); |
|
||||
} |
|
||||
} |
|
||||
Object isHandle = alarmMap.get("isHandle"); |
|
||||
alarmInfoHx.setDealStatus(isHandle != null ? ((boolean) isHandle == false ? "0" : "1") : ""); |
|
||||
alarmInfoIot.setData(alarmInfoHx); |
|
||||
alarmInfoIot.setDataType("alarm"); |
|
||||
String jsonData = JSONUtil.toJsonStr(alarmInfoIot); |
|
||||
log.info(jsonData); |
|
||||
XxlJobHelper.log("mqtt push data : : " + jsonData); |
|
||||
mqttClient.publish(0, false, deviceTopic, jsonData); |
|
||||
} |
|
||||
} catch (Exception ex) { |
|
||||
ex.printStackTrace(); |
|
||||
log.info(">>>>>>>>>>>>>>>>>>>>>>>>>exception occur"+ex.getMessage()); |
|
||||
XxlJobHelper.log(">>>>>>>>>>>>>>>>>>>>>>>>>exception occur"+ex.getMessage()); |
|
||||
} |
|
||||
}); |
|
||||
|
|
||||
} catch (Exception e) { |
|
||||
e.printStackTrace(); |
|
||||
} |
|
||||
log.info(">>>>>>>>>>>>>>>Alarm info mqtt push finished!"); |
|
||||
XxlJobHelper.log(">>>>>>>>>>>>>>>Alarm info mqtt push finished!"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
||||
@ -0,0 +1,98 @@ |
|||||
|
package com.hxjt.dataupload.jobhandler.mqtt.job.user.jobhandler; |
||||
|
|
||||
|
import cn.hutool.json.JSONUtil; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSONObject; |
||||
|
import com.hxjt.dataupload.jobhandler.mqtt.enums.AlarmType; |
||||
|
import com.hxjt.dataupload.jobhandler.mqtt.job.user.domain.AlarmInfoHx; |
||||
|
import com.hxjt.dataupload.jobhandler.mqtt.job.user.domain.AlarmInfoIot; |
||||
|
import com.hxjt.dataupload.mqtt.MqttClient; |
||||
|
import com.xxl.job.core.context.XxlJobHelper; |
||||
|
import com.xxl.job.core.handler.annotation.XxlJob; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.http.ResponseEntity; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.springframework.web.client.RestTemplate; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.text.SimpleDateFormat; |
||||
|
import java.util.*; |
||||
|
|
||||
|
import static com.hxjt.dataupload.utils.ExcelDataExtraction.extractData; |
||||
|
|
||||
|
/* |
||||
|
* @description: 报警信息推送 |
||||
|
* @author: ZhangRY |
||||
|
* @date: 2025/4/9 10:51 |
||||
|
* @param: null |
||||
|
* @return: null |
||||
|
**/ |
||||
|
@Component |
||||
|
@Slf4j |
||||
|
public class SensorDataTopicMqttJobHandler { |
||||
|
|
||||
|
@Resource |
||||
|
MqttClient mqttClient; |
||||
|
|
||||
|
@Value("${spring.mqtt.sensorDataTopic:}") |
||||
|
private String sensorDataTopic; |
||||
|
|
||||
|
|
||||
|
@Resource |
||||
|
RestTemplate restTemplate; |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
@XxlJob("sensorDataTopicMqttJobHandler") |
||||
|
public void execute() throws Exception { |
||||
|
try { |
||||
|
XxlJobHelper.log("sensorDataTopicMqttJobHandler Beginning."); |
||||
|
|
||||
|
String filePath = "F://Users/liwenxuan/Desktop/环保上传园区/数据位号2025.3.4-1.xlsx"; // 请替换为实际的Excel文件路径
|
||||
|
List<String> dataList = extractData(filePath); |
||||
|
dataList.forEach(id -> { |
||||
|
if(id.equals("DK$SO2_ZS_PV")||id.equals("DK$DLGL_GL_GL_DUST_ZS_PV")||id.equals("DK$DLGL_NOX_ZS_PV")||id.equals("DK$DLGL_SFLOW_PV")||id.equals("DK$DLGL_O2_PV")||id.equals("DK$TEMP_PV")||id.equals("DK$DLGL_HUM_PV")||id.equals("DK$DLGL_VELOCITY_PV")||id.equals("DK$DLGL_PRESS_PV")){ |
||||
|
System.out.println(id); |
||||
|
//请求地址
|
||||
|
String url = "http://172.20.5.120:29912/open/app/api/po/send/MkZSTLpZ7vHYcz2clyDMBuUnuqHX9A49?id=DK$1AT_HJ001_FLAG?id={id}"; |
||||
|
|
||||
|
//请求参数
|
||||
|
Map<String, String> uriVariables = new HashMap<>(); |
||||
|
uriVariables.put("id", id); |
||||
|
|
||||
|
|
||||
|
//发起请求,直接返回对象(带参数请求)
|
||||
|
|
||||
|
JSONObject responseBean = restTemplate.getForObject(url, JSONObject.class, uriVariables); |
||||
|
System.out.println(responseBean.toString()); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
//dataList.forEach(id -> {
|
||||
|
try { |
||||
|
|
||||
|
/* |
||||
|
String jsonData = JSONUtil.toJsonStr(alarmInfoIot); |
||||
|
log.info(jsonData); |
||||
|
XxlJobHelper.log("mqtt push data : : " + jsonData); |
||||
|
mqttClient.publish(0, false, deviceTopic, jsonData);*/ |
||||
|
|
||||
|
} catch (Exception ex) { |
||||
|
ex.printStackTrace(); |
||||
|
log.info(">>>>>>>>>>>>>>>>>>>>>>>>>exception occur"+ex.getMessage()); |
||||
|
XxlJobHelper.log(">>>>>>>>>>>>>>>>>>>>>>>>>exception occur"+ex.getMessage()); |
||||
|
} |
||||
|
//});
|
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
log.info(">>>>>>>>>>>>>>>Alarm info mqtt push finished!"); |
||||
|
XxlJobHelper.log(">>>>>>>>>>>>>>>Alarm info mqtt push finished!"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,84 @@ |
|||||
|
package com.hxjt.dataupload.utils; |
||||
|
|
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.apache.poi.ss.usermodel.*; |
||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
||||
|
|
||||
|
import java.io.File; |
||||
|
import java.io.FileInputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public class ExcelDataExtraction { |
||||
|
public static List<String> extractData(String filePath) { |
||||
|
List<String> resultList = new ArrayList<>(); |
||||
|
try (FileInputStream fis = new FileInputStream(new File(filePath))) { |
||||
|
Workbook workbook; |
||||
|
// 判断文件类型,避免OLE2NotOfficeXmlFileException异常
|
||||
|
if (filePath.toLowerCase().endsWith(".xlsx")) { |
||||
|
workbook = new XSSFWorkbook(fis); |
||||
|
} else { |
||||
|
throw new IllegalArgumentException("Unsupported file format. Only.xlsx is supported for now."); |
||||
|
} |
||||
|
|
||||
|
Sheet sheet = workbook.getSheetAt(0); // 假设数据在第一个sheet中
|
||||
|
int headerRowIndex = 1; // 假设表头在第一行
|
||||
|
Row headerRow = sheet.getRow(headerRowIndex); |
||||
|
int targetColumnIndex = -1; |
||||
|
for (int i = 0; i < headerRow.getLastCellNum(); i++) { |
||||
|
Cell cell = headerRow.getCell(i); |
||||
|
if (cell != null && "现数据中台位号".equals(cell.getStringCellValue())) { |
||||
|
targetColumnIndex = i; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (targetColumnIndex != -1) { |
||||
|
for (int rowIndex = headerRowIndex + 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) { |
||||
|
Row row = sheet.getRow(rowIndex); |
||||
|
if (row != null) { |
||||
|
Cell cell = row.getCell(targetColumnIndex); |
||||
|
if (cell != null) { |
||||
|
switch (cell.getCellType()) { |
||||
|
case STRING: |
||||
|
if(!StringUtils.isBlank(cell.getStringCellValue())){ |
||||
|
resultList.add(cell.getStringCellValue()); |
||||
|
} |
||||
|
break; |
||||
|
case NUMERIC: |
||||
|
if (DateUtil.isCellDateFormatted(cell)) { |
||||
|
resultList.add(cell.getDateCellValue().toString()); |
||||
|
} else { |
||||
|
resultList.add(String.valueOf(cell.getNumericCellValue())); |
||||
|
} |
||||
|
break; |
||||
|
case BOOLEAN: |
||||
|
resultList.add(String.valueOf(cell.getBooleanCellValue())); |
||||
|
break; |
||||
|
default: |
||||
|
if(!StringUtils.isBlank(cell.getStringCellValue())){ |
||||
|
resultList.add(cell.toString()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
workbook.close(); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return resultList; |
||||
|
} |
||||
|
|
||||
|
/*public static void main(String[] args) { |
||||
|
String filePath = "F://Users/liwenxuan/Desktop/环保上传园区/数据位号2025.3.4-1.xlsx"; // 请替换为实际的Excel文件路径
|
||||
|
List<String> dataList = extractData(filePath); |
||||
|
for (String data : dataList) { |
||||
|
System.out.println(data); |
||||
|
} |
||||
|
}*/ |
||||
|
} |
||||
Loading…
Reference in new issue