2023-12-02 11:10:52 +08:00
|
|
|
|
package com.zhgd.xmgl.task;
|
|
|
|
|
|
|
2023-12-12 18:06:05 +08:00
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
2023-12-04 16:34:08 +08:00
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
2023-12-11 18:11:56 +08:00
|
|
|
|
import com.gexin.fastjson.JSON;
|
2023-12-12 18:06:05 +08:00
|
|
|
|
import com.gexin.fastjson.JSONArray;
|
|
|
|
|
|
import com.gexin.fastjson.JSONObject;
|
2023-12-04 16:34:08 +08:00
|
|
|
|
import com.glodon.cloudt.rest.client.RestServiceClient;
|
|
|
|
|
|
import com.glodon.cloudt.rest.client.data.HmacRestAuthInfo;
|
|
|
|
|
|
import com.glodon.cloudt.rest.client.data.RestResponseInfo;
|
|
|
|
|
|
import com.glodon.cloudt.rest.client.exception.AuthenticateException;
|
|
|
|
|
|
import com.glodon.cloudt.rest.client.exception.InvalidUriException;
|
|
|
|
|
|
import com.glodon.cloudt.rest.client.exception.NoAuthenticateException;
|
|
|
|
|
|
import com.glodon.cloudt.rest.client.impl.HmacRestServiceClient;
|
2023-12-12 18:06:05 +08:00
|
|
|
|
import com.google.common.base.Objects;
|
2023-12-04 16:34:08 +08:00
|
|
|
|
import com.zhgd.xmgl.modules.project.entity.Project;
|
|
|
|
|
|
import com.zhgd.xmgl.modules.project.mapper.ProjectMapper;
|
2023-12-12 18:06:05 +08:00
|
|
|
|
import com.zhgd.xmgl.modules.stablewater.entity.StableWaterMixStationData;
|
|
|
|
|
|
import com.zhgd.xmgl.modules.stablewater.entity.StableWaterMixStationRawMaterialData;
|
|
|
|
|
|
import com.zhgd.xmgl.modules.stablewater.entity.StableWaterMixStationSetData;
|
|
|
|
|
|
import com.zhgd.xmgl.modules.stablewater.mapper.StableWaterMixStationDataMapper;
|
2023-12-02 17:16:10 +08:00
|
|
|
|
import com.zhgd.xmgl.modules.stablewater.service.IStableWaterMixStationDataService;
|
2023-12-02 11:10:52 +08:00
|
|
|
|
import com.zhgd.xmgl.util.ThirdPartRequestUtil;
|
2023-12-11 18:11:56 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2023-12-02 11:10:52 +08:00
|
|
|
|
import net.javacrumbs.shedlock.core.SchedulerLock;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
|
import org.springframework.stereotype.Component;
|
2023-12-04 16:34:08 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
2024-03-25 15:13:09 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
2023-12-04 16:34:08 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
2023-12-02 11:10:52 +08:00
|
|
|
|
|
2023-12-12 18:06:05 +08:00
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
|
import java.util.ArrayList;
|
2023-12-02 17:16:10 +08:00
|
|
|
|
import java.util.List;
|
2023-12-02 11:10:52 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 水稳拌合站任务
|
|
|
|
|
|
*/
|
2023-12-11 18:11:56 +08:00
|
|
|
|
@Slf4j
|
2023-12-02 11:10:52 +08:00
|
|
|
|
@Component
|
2023-12-04 16:34:08 +08:00
|
|
|
|
@RestController
|
2023-12-02 11:10:52 +08:00
|
|
|
|
public class StableWaterMixStationTask {
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
ThirdPartRequestUtil thirdPartRequestUtil;
|
2023-12-02 17:16:10 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
|
IStableWaterMixStationDataService stableWaterMixStationDataService;
|
2023-12-04 16:34:08 +08:00
|
|
|
|
@Autowired
|
2023-12-12 18:06:05 +08:00
|
|
|
|
StableWaterMixStationDataMapper stableWaterMixStationDataMapper;
|
|
|
|
|
|
@Autowired
|
2023-12-04 16:34:08 +08:00
|
|
|
|
ProjectMapper projectMapper;
|
2023-12-02 17:16:10 +08:00
|
|
|
|
|
2023-12-02 11:10:52 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2023-12-12 18:06:05 +08:00
|
|
|
|
* 每5分钟拉取广联达的搅拌站数据,https://xmgl.glodon.com/wl/docs/third_help/part2/openapi/pull/mixTotal.html
|
2023-12-02 11:10:52 +08:00
|
|
|
|
*/
|
2023-12-12 18:06:05 +08:00
|
|
|
|
@Scheduled(cron = "0 0/5 * * * ?")
|
2023-12-02 11:10:52 +08:00
|
|
|
|
@SchedulerLock(name = "executeStableWaterMixStationTask", lockAtMostFor = 1000 * 60 * 2, lockAtLeastFor = 1000 * 60 * 1)
|
2024-03-25 15:13:09 +08:00
|
|
|
|
@RequestMapping("xmgl/task/executeStableWaterMixStationTask")
|
2024-01-08 11:18:39 +08:00
|
|
|
|
public void executeStableWaterMixStationTask() {
|
2023-12-02 11:10:52 +08:00
|
|
|
|
log.info("定时拉取广联达的搅拌站数据任务开始");
|
2023-12-06 14:04:04 +08:00
|
|
|
|
List<Project> projects = projectMapper.selectList(new LambdaQueryWrapper<Project>()
|
2023-12-12 18:06:05 +08:00
|
|
|
|
.isNotNull(Project::getGldLicPath)
|
|
|
|
|
|
.ne(Project::getGldLicPath, "")
|
2023-12-06 14:04:04 +08:00
|
|
|
|
);
|
2023-12-12 18:06:05 +08:00
|
|
|
|
for (Project project : projects) {
|
|
|
|
|
|
StableWaterMixStationData data = stableWaterMixStationDataMapper.selectOne(new LambdaQueryWrapper<StableWaterMixStationData>()
|
|
|
|
|
|
.eq(StableWaterMixStationData::getProjectSn, project.getProjectSn())
|
|
|
|
|
|
.orderByDesc(StableWaterMixStationData::getTimestamp)
|
|
|
|
|
|
.last("limit 1")
|
|
|
|
|
|
);
|
|
|
|
|
|
String timestamp;
|
|
|
|
|
|
if (data != null) {
|
|
|
|
|
|
timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(data.getTimestamp());
|
|
|
|
|
|
} else {
|
|
|
|
|
|
timestamp = "20230101141314082";
|
|
|
|
|
|
}
|
|
|
|
|
|
sendHttp(project, timestamp);
|
|
|
|
|
|
}
|
2023-12-04 16:34:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-12 18:06:05 +08:00
|
|
|
|
private void sendHttp(Project project, String timestamp) {
|
2023-12-04 16:34:08 +08:00
|
|
|
|
try {
|
2023-12-12 18:06:05 +08:00
|
|
|
|
log.info("定时拉取广联达的搅拌站数据任务开始:项目名称:{} 项目sn:{} timestamp:{}", project.getProjectName(), project.getProjectSn(), timestamp);
|
2023-12-04 16:34:08 +08:00
|
|
|
|
/**----------------准备 -------------------*/
|
|
|
|
|
|
//第1步:下载授权文件
|
|
|
|
|
|
//第2步:获取SDK
|
|
|
|
|
|
/** ------------ 授权认证 --------------*/
|
|
|
|
|
|
|
|
|
|
|
|
//第3步:创建客户端实例
|
|
|
|
|
|
RestServiceClient serviceClient = HmacRestServiceClient.getInstance();
|
|
|
|
|
|
//第4步:加载/验证授权文件
|
|
|
|
|
|
//4.1构建认证信息
|
|
|
|
|
|
HmacRestAuthInfo restAuthInfo = new HmacRestAuthInfo();
|
|
|
|
|
|
//4.2设置授权文件路径
|
2023-12-12 18:06:05 +08:00
|
|
|
|
restAuthInfo.setLicPath(project.getGldLicPath());
|
2023-12-04 16:34:08 +08:00
|
|
|
|
//4.3权限认证
|
|
|
|
|
|
serviceClient.authenticate(restAuthInfo);
|
|
|
|
|
|
|
|
|
|
|
|
/** ------------ 拼接请求地址 --------------*/
|
|
|
|
|
|
//第5步:获取授权文件关联的GYS系统地址hostAddress
|
|
|
|
|
|
String hostAddress = serviceClient.getRestRootAddress();
|
|
|
|
|
|
//第6步:设置请求接口的URI地址apiURI(以获取集成项目列表信息为例)
|
2023-12-12 18:06:05 +08:00
|
|
|
|
String apiURI = String.format("/api/mix/v1.0/mix/product/getMachineProductList?beginTimestamp=%s", timestamp);
|
2023-12-04 16:34:08 +08:00
|
|
|
|
//第7步:拼装完整的请求网址
|
|
|
|
|
|
String fullURL = hostAddress + apiURI;
|
|
|
|
|
|
//第8步:请求数据准备(本例中为从GYS系统获取数据,无需进行请求数据的准备)
|
|
|
|
|
|
|
2023-12-11 18:11:56 +08:00
|
|
|
|
log.info("url:{}", fullURL);
|
2023-12-04 16:34:08 +08:00
|
|
|
|
//第9步:发送请求,用来推送/获取数据
|
|
|
|
|
|
RestResponseInfo restResponseInfo = serviceClient.get(fullURL);
|
2023-12-11 18:11:56 +08:00
|
|
|
|
log.info("rs:{}", JSON.toJSONString(restResponseInfo));
|
|
|
|
|
|
|
2023-12-04 16:34:08 +08:00
|
|
|
|
|
|
|
|
|
|
/** ------------ 处理请求返回结果 --------------*/
|
|
|
|
|
|
if (restResponseInfo.isSuccess()) {
|
|
|
|
|
|
//请求成功:
|
|
|
|
|
|
//第10步:处理请求结果(第三方系统自行处理)
|
2023-12-12 18:06:05 +08:00
|
|
|
|
String stringContent = restResponseInfo.getStringContent();
|
|
|
|
|
|
JSONObject scjo = JSON.parseObject(stringContent);
|
|
|
|
|
|
if (scjo.getBoolean("success")) {
|
|
|
|
|
|
saveData(scjo.getJSONObject("data"), project);
|
|
|
|
|
|
JSONObject dataJo = scjo.getJSONObject("data");
|
|
|
|
|
|
//继续发送http查询未保存的数据
|
|
|
|
|
|
if (!Objects.equal(dataJo.getInteger("PageSize"), 0)) {
|
|
|
|
|
|
sendHttp(project, dataJo.getString("MaxTimestamp"));
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
log.error("失败:未知原因1");
|
|
|
|
|
|
}
|
2023-12-04 16:34:08 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
//请求失败:
|
|
|
|
|
|
//第11步:根据返回结果排查失败原因(数据原因第三方系统自行处理)
|
2023-12-12 18:06:05 +08:00
|
|
|
|
log.error("失败:未知原因2");
|
2023-12-04 16:34:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (AuthenticateException e) {
|
2024-04-14 21:05:01 +08:00
|
|
|
|
log.error("error:", e);
|
2023-12-04 16:34:08 +08:00
|
|
|
|
} catch (InvalidUriException e) {
|
2024-04-14 21:05:01 +08:00
|
|
|
|
log.error("error:", e);
|
2023-12-04 16:34:08 +08:00
|
|
|
|
} catch (NoAuthenticateException e) {
|
2024-04-14 21:05:01 +08:00
|
|
|
|
log.error("error:", e);
|
2023-12-04 16:34:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-12 18:06:05 +08:00
|
|
|
|
private void saveData(JSONObject dataJo, Project project) {
|
|
|
|
|
|
//保存拌合站设备,一个项目级别的授权文件对应我们的一个项目,他们的一个项目有多个拌合站(属性是projectId)(和我们设备对应),一个拌合站有多个机组(我们不体现)
|
|
|
|
|
|
//保存拌合站数据
|
|
|
|
|
|
JSONArray bills = dataJo.getJSONArray("Bills");
|
|
|
|
|
|
for (Object bill : bills) {
|
|
|
|
|
|
StableWaterMixStationData data = new StableWaterMixStationData();
|
|
|
|
|
|
List<StableWaterMixStationSetData> setDataList = new ArrayList<>();
|
|
|
|
|
|
data.setSetDataList(setDataList);
|
|
|
|
|
|
BeanUtil.copyProperties(bill, data, true);
|
|
|
|
|
|
data.setDevSn(((JSONObject) bill).getString("projectId"));
|
|
|
|
|
|
JSONArray pcList = ((JSONObject) bill).getJSONArray("PCList");
|
|
|
|
|
|
for (Object pc : pcList) {
|
|
|
|
|
|
StableWaterMixStationSetData setData = new StableWaterMixStationSetData();
|
|
|
|
|
|
BeanUtil.copyProperties(pc, setData, true);
|
|
|
|
|
|
setDataList.add(setData);
|
|
|
|
|
|
List<StableWaterMixStationRawMaterialData> rawMaterialDataList = new ArrayList<>();
|
|
|
|
|
|
setData.setRawMaterialDataList(rawMaterialDataList);
|
|
|
|
|
|
JSONArray ycList = ((JSONObject) pc).getJSONArray("YCList");
|
|
|
|
|
|
for (Object yc : ycList) {
|
|
|
|
|
|
StableWaterMixStationRawMaterialData rawMaterialData = new StableWaterMixStationRawMaterialData();
|
|
|
|
|
|
BeanUtil.copyProperties(yc, rawMaterialData, true);
|
|
|
|
|
|
rawMaterialDataList.add(rawMaterialData);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
stableWaterMixStationDataService.add(data);
|
|
|
|
|
|
} catch (Exception e) {
|
2024-04-14 21:05:01 +08:00
|
|
|
|
log.error("error:", e);
|
2023-12-12 18:06:05 +08:00
|
|
|
|
log.info("新增出错:{},编号:{}", e.getMessage(), data.getDevSn());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-12-02 11:10:52 +08:00
|
|
|
|
}
|
2023-12-04 16:34:08 +08:00
|
|
|
|
|
2023-12-02 11:10:52 +08:00
|
|
|
|
}
|