2024-01-20 10:18:46 +08:00
|
|
|
|
package com.zhgd.xmgl.task;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
2024-01-30 16:24:25 +08:00
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
2024-01-20 10:18:46 +08:00
|
|
|
|
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;
|
2024-01-30 16:24:25 +08:00
|
|
|
|
import com.zhgd.xmgl.modules.discharging.service.IDischargingPlatformCurrentDataService;
|
2024-01-20 10:18:46 +08:00
|
|
|
|
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.Date;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
@Component
|
|
|
|
|
|
@RestController
|
|
|
|
|
|
public class DischargingPlatformCurrentDataTask {
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
DischargingPlatformCurrentDataMapper dischargingPlatformCurrentDataMapper;
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
DischargingPlatformDevMapper dischargingPlatformDevMapper;
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private ProjectMapper projectMapper;
|
2024-01-30 16:24:25 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
|
private IDischargingPlatformCurrentDataService dischargingPlatformCurrentDataService;
|
2024-01-20 10:18:46 +08:00
|
|
|
|
|
|
|
|
|
|
@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);
|
2024-01-30 16:24:25 +08:00
|
|
|
|
DischargingPlatformCurrentData newestData = dischargingPlatformCurrentDataMapper.selectOne(
|
|
|
|
|
|
new LambdaQueryWrapper<DischargingPlatformCurrentData>().eq(DischargingPlatformCurrentData::getDevSn, devSn)
|
|
|
|
|
|
.orderByDesc(DischargingPlatformCurrentData::getXiewenId).last("limit 1"));
|
2024-01-20 10:18:46 +08:00
|
|
|
|
// 请求参数
|
|
|
|
|
|
Map<String, Object> map = new HashMap<>(4);
|
|
|
|
|
|
map.put("pageSize", "10");
|
|
|
|
|
|
map.put("pageNum", "1");
|
2024-01-30 16:24:25 +08:00
|
|
|
|
if (newestData != null && newestData.getXiewenId() != null) {
|
|
|
|
|
|
map.put("dataId", newestData.getXiewenId());
|
|
|
|
|
|
}
|
2024-01-22 09:33:18 +08:00
|
|
|
|
JSONObject realTimeData = xiwonUtil.postForm("/unloading/realTimeData", project.getXiwonAppId(), project.getXiwonAppSecret(), map, JSONObject.class, (() -> {
|
2024-01-20 10:18:46 +08:00
|
|
|
|
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()) {
|
2024-01-30 16:24:25 +08:00
|
|
|
|
dev.setRealTime(new Date());
|
|
|
|
|
|
dischargingPlatformDevMapper.updateById(dev);
|
|
|
|
|
|
|
2024-01-20 10:18:46 +08:00
|
|
|
|
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);
|
2024-01-30 16:24:25 +08:00
|
|
|
|
data.setProjectSn(dev.getProjectSn());
|
2024-01-20 10:18:46 +08:00
|
|
|
|
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));
|
2024-01-30 16:24:25 +08:00
|
|
|
|
data.setXiewenId(dto.getId());
|
2024-01-20 10:18:46 +08:00
|
|
|
|
dischargingPlatformCurrentDataMapper.insert(data);
|
|
|
|
|
|
|
2024-01-30 16:24:25 +08:00
|
|
|
|
dischargingPlatformCurrentDataService.addAlarms(data, dev);
|
2024-01-20 10:18:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
log.info("设备sn:{},当前无数据!", devSn);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
log.error("设备sn:{},请求失败!当前code:{},msg:{}", devSn, code, realTimeData.getString("msg"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|