package com.zy.asrs.timer; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.zy.asrs.entity.BasStation; import com.zy.asrs.entity.dto.WcsStationDto; import com.zy.asrs.service.BasStationService; import com.zy.asrs.utils.StationUtils; import com.zy.common.utils.HttpHandler; import com.zy.system.entity.Config; import com.zy.system.service.ConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.concurrent.TimeUnit; @Slf4j @Component public class WcsStationTimer { @Autowired private ConfigService configService; @Autowired private BasStationService basStationService; @Autowired private StationUtils stationUtils; @Scheduled(cron = "0/3 * * * * ? ") public synchronized void execute() { Config wcsStationUrlConfig = configService.selectOne(new EntityWrapper().eq("code", "wcsStationUrl")); if (wcsStationUrlConfig == null) { return; } String wcsStationUrl = wcsStationUrlConfig.getValue(); String response = null; try { HashMap requestParam = new HashMap<>(); List list = new ArrayList<>(); List basStations = basStationService.selectList(new EntityWrapper<>()); for (BasStation basStation : basStations) { list.add(basStation.getStationId()); } requestParam.put("stationIds", list); response = new HttpHandler.Builder() .setUri(wcsStationUrl) .setJson(JSON.toJSONString(requestParam)) .setTimeout(30, TimeUnit.SECONDS) .build() .doPost(); if (response != null) { JSONObject jsonObject = JSON.parseObject(response); JSONArray data = jsonObject.getJSONArray("data"); for (Object o : data) { JSONObject object = (JSONObject) o; Integer stationId = object.getInteger("stationId"); WcsStationDto wcsStationDto = stationUtils.stationMap.get(stationId); if (wcsStationDto == null) { wcsStationDto = new WcsStationDto(); } wcsStationDto.setStationId(stationId); wcsStationDto.setTaskNo(object.getString("superTaskNo")); wcsStationDto.setAuto(object.getBoolean("autoing") ? 1 : 0); wcsStationDto.setLoading(object.getBoolean("loading") ? 1 : 0); wcsStationDto.setBarcode(object.getString("barcode")); wcsStationDto.setErrorMsg(object.getString("errorMsg")); wcsStationDto.setSystemWarning(object.getString("systemWarning")); stationUtils.stationMap.put(stationId, wcsStationDto); } } } catch (Exception e) { e.printStackTrace(); } } }