vince
2023-11-08 96f7af2f3bf9a36dd48e0e6bf4f8a8ca1e31ed7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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<GrainTemp> 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;
    }
}