package com.zhgd.xmgl.task; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpRequest; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.gexin.fastjson.JSON; import com.gexin.fastjson.JSONArray; import com.gexin.fastjson.JSONObject; import com.zhgd.xmgl.modules.bigdevice.entity.GantryCrane; import com.zhgd.xmgl.modules.bigdevice.entity.GantryCraneCurrentData; import com.zhgd.xmgl.modules.bigdevice.mapper.GantryCraneCurrentDataMapper; import com.zhgd.xmgl.modules.bigdevice.mapper.GantryCraneMapper; import com.zhgd.xmgl.task.dto.GantryCraneCurrentDataDto; import com.zhgd.xmgl.util.NumberUtils; import io.swagger.annotations.Api; import lombok.extern.slf4j.Slf4j; import net.javacrumbs.shedlock.core.SchedulerLock; import org.springframework.beans.factory.annotation.Autowired; 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 java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Objects; /** * 龙门吊定时任务 */ @Slf4j @Component @RestController @Api(tags = "定时任务") public class GantryCraneTask { @Autowired GantryCraneCurrentDataMapper gantryCraneCurrentDataMapper; @Autowired GantryCraneMapper gantryCraneMapper; /** * 定时获取实时数据 */ @SchedulerLock(name = "getGantryCraneTaskData", lockAtMostFor = 1000 * 60 * 60, lockAtLeastFor = 1000 * 60 * 5) @Scheduled(cron = "0 0/5 * * * ?") @GetMapping("/xmgl/task/getGantryCraneTaskData") public void getGantryCraneTaskData() { log.info("getGantryCraneTaskData任务执行"); List gantryCranes = gantryCraneMapper.selectList(new LambdaQueryWrapper() .ne(GantryCrane::getRzdToken, "") .isNotNull(GantryCrane::getRzdToken) ); for (GantryCrane gantryCrane : gantryCranes) { String url = "http://www.aqjkpt.com:26001"; log.info("url:{}", url); String rs = HttpRequest.post(url) .header("Authorization", gantryCrane.getRzdToken()) .execute().body(); log.info("rs:{}", rs); //3秒一次 try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } try { JSONArray rsJa = JSON.parseArray(rs); JSONObject rsJaJo = rsJa.getJSONObject(0); if (rsJaJo.containsKey("current")) { GantryCraneCurrentDataDto dto = rsJaJo.getObject("current", GantryCraneCurrentDataDto.class); insert(dto, gantryCrane); } else { log.error("龙门吊定时任务定时获取实时数据出错"); } } catch (Exception e) { e.printStackTrace(); } } } /** * 插入数据 * * @param dto * @param gantryCrane */ private void insert(GantryCraneCurrentDataDto dto, GantryCrane gantryCrane) { GantryCraneCurrentData entity = new GantryCraneCurrentData(); entity.setDevSn(gantryCrane.getDevSn()); entity.setProjectSn(gantryCrane.getProjectSn()); entity.setReciveTime(new Date()); entity.setRunTime(null); entity.setWindSpeed(String.valueOf(dto.getWind1())); entity.setWindSpeedState(getWindSpeedState(dto.getAL1bit5())); entity.setAngleXAxis(null); entity.setAngleYAxis(null); entity.setLargeCraneRange(String.valueOf(dto.getDistance7() * 0.01)); entity.setLargeCraneSpeed(String.valueOf(dto.getSpeed1() * 0.01)); entity.setSmallCraneRange(null); entity.setSmallCraneSpeed(String.valueOf(dto.getSpeed1() * 0.01)); entity.setSecondSmallCraneRange(null); entity.setSecondSmallCraneSpeed(String.valueOf(dto.getSpeed2() * 0.01)); entity.setSmallCraneMainHookLoad(String.valueOf(dto.getNeiweight1() * 0.01)); entity.setSmallCraneMainHookHeight(String.valueOf(dto.getDistance5() * 0.01)); entity.setSmallCraneAuxiliaryHookLoad(String.valueOf(dto.getDistance6() * 0.01)); entity.setSmallCraneAuxiliaryHookHeight(null); entity.setSecondSmallCraneMainHookLoad(String.valueOf(dto.getNeiweight2() * 0.01)); entity.setSecondSmallCraneMainHookHeight(String.valueOf(dto.getDistance6() * 0.01)); entity.setSecondSmallCraneAuxiliaryHookLoad(null); entity.setSecondSmallCraneAuxiliaryHookHeight(null); entity.setMainHookCurrent(null); entity.setMainHookVoltage(null); entity.setSecondMainHookCurrent(null); entity.setSecondMainHookVoltage(null); entity.setSmallCraneRangeState(null); entity.setSecondSmallCraneRangeState(null); entity.setLargeCraneDistanceState(null); entity.setMainHookWeightState(null); entity.setSecondMainHookWeightState(null); entity.setAuxiliaryHookWeightState(null); entity.setSecondAuxiliaryHookWeightState(null); entity.setMainHookHeightState(null); entity.setSecondMainHookHeightState(null); entity.setAuxiliaryHookHeightState(null); entity.setSecondAuxiliaryHookHeightState(null); entity.setAngleXState(null); entity.setAngleYState(null); entity.setGatedState(null); entity.setAntiWindAndNonslipState(null); entity.setCableReelStatus(null); entity.setLargeCarSafeStatus(null); entity.setLargeCarRunStatus(null); entity.setSmallCarRunStatus(null); entity.setSecondSmallCarRunStatus(null); entity.setMasterHookRunStatus(getMasterHookRunStatus(dto)); entity.setSecondMasterHookRunStatus(getSecondMasterHookRunStatus(dto)); entity.setSubHookRunStatus(null); entity.setSecondSubHookRunStatus(null); entity.setDriverName(null); entity.setDriverIdCard(null); entity.setTotalDay(getTotalDay(dto.getCumulativeWorkingTime())); entity.setTotalTimes(getTotalTimes(dto.getCumulativeWorkingTime())); entity.setPowerOnDay(String.valueOf(dto.getWorkTime())); entity.setPowerOnTimes(String.valueOf(dto.getWorkTime())); gantryCraneCurrentDataMapper.insert(entity); } private String getTotalTimes(Double d) { if (d == null) { return null; } //HH:MM:SS int second = (int) (d * 0.1 * 3600); int h = second / 3600; int min = (second % 3600) / 60; int s = (second % 3600) % 60; return (h == 0 ? "00" : NumberUtils.fillZero(h, 2)) + ":" + (min == 0 ? "00" : NumberUtils.fillZero(min, 2)) + ":" + (s == 0 ? "00" : NumberUtils.fillZero(s, 2)); } private String getTotalDay(Double d) { if (d == null) { return null; } return String.valueOf(NumberUtil.round(d * 0.1 / 24, 2)); } private Integer getSecondMasterHookRunStatus(GantryCraneCurrentDataDto dto) { //小车2主钩运行状态 0:停止 1:上升 2:下降 3:故障 if (Objects.equals(dto.getOP1bit6(), 0) || Objects.equals(dto.getOP1bit7(), 0)) { return 0; } else if (Objects.equals(dto.getOP1bit6(), 1)) { return 1; } else if (Objects.equals(dto.getOP1bit7(), 1)) { return 2; } return null; } private Integer getMasterHookRunStatus(GantryCraneCurrentDataDto dto) { //主钩运行状态 0:停止 1:上升 2:下降 3:故障 if (Objects.equals(dto.getOP1bit0(), 0) || Objects.equals(dto.getOP1bit1(), 0)) { return 0; } else if (Objects.equals(dto.getOP1bit0(), 1)) { return 1; } else if (Objects.equals(dto.getOP1bit1(), 1)) { return 2; } return null; } private Integer getWindSpeedState(String al1bit5) { if (StrUtil.isBlank(al1bit5)) { return null; } switch (al1bit5) { case "0": return 0; case "1": return 2; } return null; } }