169 lines
7.6 KiB
Java
169 lines
7.6 KiB
Java
package com.zhgd.xmgl.task;
|
||
|
||
import cn.hutool.core.bean.BeanUtil;
|
||
import cn.hutool.core.collection.CollUtil;
|
||
import com.alibaba.fastjson.JSONArray;
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||
import com.gexin.fastjson.JSON;
|
||
import com.zhgd.xmgl.modules.discharging.entity.DischargingPlatformCurrentData;
|
||
import com.zhgd.xmgl.modules.discharging.entity.DischargingPlatformDev;
|
||
import com.zhgd.xmgl.modules.discharging.mapper.DischargingPlatformCurrentDataMapper;
|
||
import com.zhgd.xmgl.modules.discharging.mapper.DischargingPlatformDevMapper;
|
||
import com.zhgd.xmgl.modules.discharging.service.IDischargingPlatformCurrentDataService;
|
||
import com.zhgd.xmgl.modules.project.entity.Project;
|
||
import com.zhgd.xmgl.modules.project.mapper.ProjectMapper;
|
||
import com.zhgd.xmgl.task.dto.DischargingPlatformCurrentDataDto;
|
||
import com.zhgd.xmgl.util.XiwonUtil;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import net.javacrumbs.shedlock.core.SchedulerLock;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.http.HttpStatus;
|
||
import org.springframework.scheduling.annotation.Scheduled;
|
||
import org.springframework.stereotype.Component;
|
||
import org.springframework.web.bind.annotation.GetMapping;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
|
||
import javax.annotation.Resource;
|
||
import java.util.*;
|
||
import java.util.concurrent.CompletableFuture;
|
||
|
||
@Slf4j
|
||
@Component
|
||
@RestController
|
||
public class DischargingPlatformCurrentDataTask {
|
||
@Autowired
|
||
DischargingPlatformCurrentDataMapper dischargingPlatformCurrentDataMapper;
|
||
@Autowired
|
||
DischargingPlatformDevMapper dischargingPlatformDevMapper;
|
||
@Autowired
|
||
private ProjectMapper projectMapper;
|
||
@Autowired
|
||
private IDischargingPlatformCurrentDataService dischargingPlatformCurrentDataService;
|
||
|
||
@Resource
|
||
private XiwonUtil xiwonUtil;
|
||
|
||
/**
|
||
* 定时获取卸料平台数据
|
||
*/
|
||
@SchedulerLock(name = "getDischargingPlatformCurrentData", lockAtMostFor = 1000 * 60 * 60, lockAtLeastFor = 1000 * 60 * 5)
|
||
@Scheduled(cron = "0 0/5 * * * ?")
|
||
@GetMapping("/xmgl/task/getDischargingPlatformCurrentData")
|
||
public void getDischargingPlatformCurrentData() {
|
||
log.info("获取卸料平台数据开始任务");
|
||
List<Project> projects = projectMapper.selectList(new LambdaQueryWrapper<Project>()
|
||
.isNotNull(Project::getXiwonAppId)
|
||
.isNotNull(Project::getXiwonAppSecret)
|
||
.ne(Project::getXiwonAppId, "")
|
||
.ne(Project::getXiwonAppSecret, "")
|
||
);
|
||
Date now = new Date();
|
||
for (Project project : projects) {
|
||
// 获取设备列表
|
||
List<DischargingPlatformDev> devList = dischargingPlatformDevMapper.selectList(new LambdaQueryWrapper<DischargingPlatformDev>()
|
||
.eq(DischargingPlatformDev::getProjectSn, project.getProjectSn()));
|
||
devList.forEach(dev -> CompletableFuture.runAsync(() -> {
|
||
doGetDischargingPlatformCurrentData(dev, project, now);
|
||
}).exceptionally(throwable -> {
|
||
log.error("err", throwable);
|
||
return null;
|
||
})
|
||
);
|
||
}
|
||
|
||
}
|
||
|
||
private void doGetDischargingPlatformCurrentData(DischargingPlatformDev dev, Project project, Date now) {
|
||
// 设备sn
|
||
String devSn = dev.getDevSn();
|
||
log.info("doGetDischargingPlatformCurrentData设备sn:{}", devSn);
|
||
DischargingPlatformCurrentData newestData = dischargingPlatformCurrentDataMapper.selectOne(
|
||
new LambdaQueryWrapper<DischargingPlatformCurrentData>().eq(DischargingPlatformCurrentData::getDevSn, devSn)
|
||
.orderByDesc(DischargingPlatformCurrentData::getXiewenId).last("limit 1"));
|
||
// 请求参数
|
||
Map<String, Object> map = new HashMap<>(4);
|
||
map.put("pageSize", "10");
|
||
map.put("pageNum", "1");
|
||
if (newestData != null && newestData.getXiewenId() != null) {
|
||
map.put("dataId", newestData.getXiewenId());
|
||
}
|
||
JSONObject realTimeData = xiwonUtil.postForm("/unloading/realTimeData", project.getXiwonAppId(), project.getXiwonAppSecret(), map, JSONObject.class, (() -> {
|
||
map.put("deviceSn", devSn);
|
||
return map;
|
||
}));
|
||
log.info("doGetDischargingPlatformCurrentData携稳接口响应数据(扬尘):devSn:{},rs:{}", devSn, JSON.toJSONString(realTimeData));
|
||
Integer code = realTimeData.getInteger("code");
|
||
// code校验是否成功请求
|
||
if (code == HttpStatus.OK.value()) {
|
||
dev.setRealTime(new Date());
|
||
dischargingPlatformDevMapper.updateById(dev);
|
||
|
||
JSONArray dataArray = realTimeData.getJSONArray("data");
|
||
if (CollUtil.isNotEmpty(dataArray)) {
|
||
// 解析请求到的参数,保存到我们的数据库
|
||
for (Object o : dataArray) {
|
||
DischargingPlatformCurrentDataDto dto = BeanUtil.toBean(o, DischargingPlatformCurrentDataDto.class);
|
||
DischargingPlatformCurrentData data = new DischargingPlatformCurrentData();
|
||
data.setDevSn(devSn);
|
||
data.setProjectSn(dev.getProjectSn());
|
||
data.setReciveTime(dto.getCreateTime());
|
||
data.setLoading(String.valueOf(dto.getRealLoad() / 1000.0));
|
||
data.setDisplacement(null);
|
||
data.setXDipAngle(String.valueOf(dto.getInclinationX() / 10.0));
|
||
data.setYDipAngle(String.valueOf(dto.getInclinationY() / 10.0));
|
||
data.setXiewenId(dto.getId());
|
||
dischargingPlatformCurrentDataMapper.insert(data);
|
||
|
||
transformAlarms(data, dto);
|
||
dischargingPlatformCurrentDataService.addAlarms(data, dev);
|
||
}
|
||
} else {
|
||
log.info("设备sn:{},当前无数据!", devSn);
|
||
}
|
||
} else {
|
||
log.error("设备sn:{},请求失败!当前code:{},msg:{}", devSn, code, realTimeData.getString("msg"));
|
||
}
|
||
|
||
|
||
}
|
||
|
||
private void transformAlarms(DischargingPlatformCurrentData data, DischargingPlatformCurrentDataDto dto) {
|
||
/**
|
||
* weightStatus Integer 重量状态 0-正常 1-预警 2-报警
|
||
* inclinationXStatus Integer 倾角X状态 0-正常 1-预警 2-报警
|
||
* inclinationYStatus Integer 倾角Y状态 0-正常 1-预警 2-报警
|
||
*/
|
||
if (Objects.equals(dto.getWeightStatus(), 1)) {
|
||
data.setIsAlarm(1);
|
||
data.setAlarmLevel(3);
|
||
data.setAlarmType("重量状态次要告警");
|
||
} else if (Objects.equals(dto.getWeightStatus(), 2)) {
|
||
data.setIsAlarm(1);
|
||
data.setAlarmLevel(1);
|
||
data.setAlarmType("重量状态紧急告警");
|
||
}
|
||
|
||
if (Objects.equals(dto.getInclinationXStatus(), 1)) {
|
||
data.setIsAlarm(1);
|
||
data.setAlarmLevel(3);
|
||
data.setAlarmType("倾角X状态次要告警");
|
||
} else if (Objects.equals(dto.getInclinationXStatus(), 2)) {
|
||
data.setIsAlarm(1);
|
||
data.setAlarmLevel(1);
|
||
data.setAlarmType("倾角X状态紧急告警");
|
||
}
|
||
|
||
if (Objects.equals(dto.getInclinationYStatus(), 1)) {
|
||
data.setIsAlarm(1);
|
||
data.setAlarmLevel(3);
|
||
data.setAlarmType("倾角Y状态次要告警");
|
||
} else if (Objects.equals(dto.getInclinationYStatus(), 2)) {
|
||
data.setIsAlarm(1);
|
||
data.setAlarmLevel(1);
|
||
data.setAlarmType("倾角Y状态紧急告警");
|
||
}
|
||
|
||
}
|
||
}
|