package com.iailab.module.data.feign; import com.alibaba.cloud.commons.lang.StringUtils; import com.iailab.module.data.api.IFeignDataApi; import com.iailab.framework.common.util.date.DateUtils; import com.iailab.framework.common.util.object.BeanUtils; import com.iailab.module.data.common.enums.DataTypeEnum; import com.iailab.module.data.dto.*; import com.iailab.module.data.http.entity.HttpApiEntity; import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO; import com.iailab.module.data.point.collection.PointCollector; import com.iailab.module.data.point.dto.DaPointDTO; import com.iailab.module.data.point.dto.DaPointWriteValueDTO; import com.iailab.module.data.point.service.DaPointService; import com.iailab.module.data.http.service.HttpApiService; import com.iailab.module.data.ind.collection.IndItemCollector; import com.iailab.module.data.ind.dto.IndItemValueDTO; import com.iailab.module.data.influxdb.service.InfluxDBService; import javax.annotation.Resource; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; import java.util.*; /** * @author PanZhibao * @Description * @createTime 2023年06月28日 08:53:00 */ @RestController public class FeignDataImpl implements IFeignDataApi { @Resource private InfluxDBService influxDBService; @Resource DaPointService daPointService; @Resource private HttpApiService httpApiService; @Resource private IndItemCollector indItemCollector; @Resource private PointCollector pointCollector; @Override public List queryPointValues(FeignQueryPointDTO dto) { DaPointDTO daPointDTO = daPointService.getByNo(dto.getPointCode()); InfluxPointValuePOJO pojo = new InfluxPointValuePOJO(); pojo.setPoint(dto.getPointCode()); pojo.setType(daPointDTO.getDataType()); Date startTime = dto.getStartTime(); Date endTime = dto.getEndTime(); List> list = influxDBService.queryPointValues(pojo, startTime, endTime); List dataEntityList = new ArrayList<>(); for (int i = 0; list.size() - i >= 1; i++) { ApiDataDTO dataEntity = new ApiDataDTO(); dataEntity.setDataValue(Double.parseDouble(list.get(i).get("value").toString())); dataEntity.setTimeStamp((Date)(list.get(i).get("time"))); dataEntityList.add(dataEntity); } return dataEntityList; } @Override public List querySimTagValues(FeignQueryPointDTO dto) { InfluxPointValuePOJO pojo = new InfluxPointValuePOJO(); pojo.setPoint(dto.getPointCode()); pojo.setType(DataTypeEnum.FLOAT.getCode()); Date startTime = dto.getStartTime(); Date endTime = dto.getEndTime(); List> list = influxDBService.queryPointValues(pojo, startTime, endTime); List dataEntityList = new ArrayList<>(); for (int i = 0; list.size() - i >= 1; i++) { ApiDataDTO dataEntity = new ApiDataDTO(); dataEntity.setDataValue(Double.parseDouble(list.get(i).get("value").toString())); dataEntity.setTimeStamp((Date)(list.get(i).get("time"))); dataEntityList.add(dataEntity); } return dataEntityList; } @Override public Map getCurrentValue(List pointNos) { return pointCollector.getCurrentValue(pointNos); } @Override public ApiDataPointDTO getPoint(String pointNo) { DaPointDTO daPointDTO = daPointService.getByNo(pointNo); return BeanUtils.toBean(daPointDTO, ApiDataPointDTO.class); } @Override public FeignHttpApiDTO getHttpApi(String code) { HttpApiEntity entity = httpApiService.getByCode(code); return BeanUtils.toBean(entity, FeignHttpApiDTO.class); } @Override public List queryIndItemValues(FeignQueryPointDTO dto) { List result = new ArrayList<>(); List itemNos = new ArrayList<>(); itemNos.add(dto.getPointCode()); Map> data = indItemCollector.getValueList(itemNos); List list = data.get(dto.getPointCode()); if (CollectionUtils.isEmpty(list)) { return result; } list.forEach(item -> { BigDecimal dv = null; ApiDataDTO entity = new ApiDataDTO(); entity.setDataTime(item.getDataTime()); entity.setDataValue(item.getDataValue() == null ? 0 : item.getDataValue().doubleValue()); result.add(entity); }); return result; } @Override public List getIndItemValues(FeignQueryPointDTO dto) { List result = new ArrayList<>(); List itemNos = new ArrayList<>(); itemNos.add(dto.getPointCode()); Map> data = indItemCollector.getValueList(itemNos); List list = data.get(dto.getPointCode()); if (CollectionUtils.isEmpty(list)) { return result; } list.forEach(item -> { BigDecimal dv = null; ApiIndValueDTO entity = new ApiIndValueDTO(); entity.setDataTime(item.getDataTime()); entity.setDataValue(item.getDataValue()); result.add(entity); }); return result; } @Override public List queryIndItemValuesRange(FeignQueryPointDTO dto) { List result = new ArrayList<>(); List itemNos = new ArrayList<>(); itemNos.add(dto.getPointCode()); Map> data = new HashMap<>(); if (StringUtils.isBlank(dto.getStartDate()) && StringUtils.isBlank(dto.getEndDate())) { data = indItemCollector.getValueList(itemNos); } else { if (dto.getStartDate() == null) { dto.setStartDate(DateUtils.format(new Date())); } if (dto.getEndDate() == null) { dto.setEndDate(DateUtils.format(new Date())); } data = indItemCollector.getValueListRange(itemNos, dto.getStartDate(), dto.getEndDate()); } List list = data.get(dto.getPointCode()); if (CollectionUtils.isEmpty(list)) { return result; } list.forEach(item -> { BigDecimal dv = null; ApiIndValueDTO entity = new ApiIndValueDTO(); entity.setDataTime(item.getDataTime()); entity.setDataValue(item.getDataValue()); result.add(entity); }); return result; } @Override public FeignResultDTO setPointValue(FeignWritePointValueDTO writePointValue) { FeignResultDTO result = new FeignResultDTO(); try { DaPointWriteValueDTO wr = new DaPointWriteValueDTO(); wr.setPointNo(writePointValue.getPointCode()); wr.setPointValue(writePointValue.getPointValue()); pointCollector.setValue(wr); result.setCode(0); result.setMsg(""); } catch (Exception ex) { result.setCode(-1); result.setMsg(ex.getMessage()); } return result; } }