package com.fzzy.gateway.hx2023.service; import com.alibaba.fastjson2.JSONObject; import com.fzzy.api.data.GatewayDeviceProtocol; import com.fzzy.api.utils.NumberUtil; import com.fzzy.gateway.api.GatewaySyncGranService; import com.fzzy.gateway.data.BaseResp; import com.fzzy.gateway.data.WeatherWebDto; import com.fzzy.gateway.entity.GatewayDevice; import com.fzzy.gateway.hx2023.ScConstant; import com.fzzy.gateway.hx2023.data.*; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; /** * 网关与粮情分机通讯和解析 */ @Slf4j @Data @Component public class HxGatewaySyncGrainImpl implements GatewaySyncGranService { @Override public String getGrainProtocol() { return GatewayDeviceProtocol.DEVICE_TEST.getCode(); } @Override public KafaGrainData syncGrain(SyncReqData reqData) { return null; } @Override public BaseResp syncGrain2(SyncReqData reqData) { GatewayDevice device = reqData.getDevice(); if (StringUtils.isEmpty(device.getCableCir())) { return getGrainTest1(reqData, device); } BaseResp resp = new BaseResp(); resp.setCode(500); resp.setMsg("没有匹配到规则"); return resp; } private BaseResp getGrainTest1(SyncReqData reqData, GatewayDevice device) { String[] cableRule = device.getCableRule().split("-"); int cableZ = Integer.valueOf(cableRule[0]); int cableY = Integer.valueOf(cableRule[1]); int cableX = Integer.valueOf(cableRule[2]); int sumNum = cableZ * cableY * cableX; WeatherWebDto weather = WeatherWebDto.contextMap.get("default"); double tMIn = 20, tMax = 25; if (null != weather) { double tOut = Double.valueOf(weather.getTem()); tMIn = tOut - 2; tMax = tOut + 3; } //数据封装 GrainData grain = new GrainData(); grain.setMessageId(ScConstant.getMessageId()); grain.setDeviceId(device.getDeviceId()); grain.setTimestamp(System.currentTimeMillis() + ""); ClientHeaders headers = new ClientHeaders(); headers.setDeviceName(device.getDeviceName()); headers.setProductId(device.getProductId()); headers.setOrgId(device.getOrgId()); headers.setMsgId(ScConstant.getMessageId()); grain.setHeaders(headers); GrainOutPut outPut = new GrainOutPut(); outPut.setAvgTemperature(NumberUtil.keepPrecision((tMax + tMIn) / 2, 1) + ""); outPut.setMinTemperature(tMax + ""); outPut.setMaxTemperature(tMIn + ""); List temperature = new ArrayList<>(); //根号 int cableNum = 1, position = 0; double curTemp = tMIn; double randomNumber = tMIn; int x = 0, y = 0, z = 0; for (int i = 0; i < sumNum; i++) { randomNumber = Math.random() * (tMax - tMIn + 1) + tMIn; curTemp = NumberUtil.keepPrecision(randomNumber, 1); position = i; z = i % cableZ + 1; x = i / (cableZ * cableY); y = x * (cableZ * cableY); y = (i - y) / cableZ; //根号 cableNum = (i / cableZ) + 1; temperature.add(new GrainTemp(cableNum + "", z + "", curTemp + "", position + "")); } outPut.setTemperature(temperature); grain.setOutPut(outPut); BaseResp resp = new BaseResp(); resp.setData(JSONObject.toJSONString(grain)); return resp; } }