houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.iailab.module.data.http.collector;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iailab.module.data.common.utils.HttpsRequest;
import com.iailab.api.IFeignModelApi;
import com.iailab.module.data.http.entity.HttpApiEntity;
import com.iailab.module.data.http.service.HttpTokenService;
import com.iailab.module.data.http.service.HttpApiService;
import com.iailab.module.data.http.service.HttpTagService;
import com.iailab.module.data.influxdb.service.InfluxDBService;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 山大设备数据采集
 *
 * @author lirm
 * @Description
 * @createTime 2024年05月21日
 */
@Slf4j
@Component
public class HttpCollectorForSD {
 
    @Resource
    private HttpApiService httpApiService;
 
    @Resource
    private HttpTagService httpTagService;
 
    @Resource
    HttpTokenService httpTokenService;
    @Resource
    HttpsRequest httpsRequest;
 
    @Resource
    private IFeignModelApi feignModelApi;
 
    @Resource
    private InfluxDBService influxDBService;
 
    public void getRunStateValue(Map<String, String> tMap) {
        Map<String, Integer> monitorCountMap = new HashMap<>();
        HttpApiEntity httpApi = httpApiService.getByCode(tMap.get("code"));
        String token = httpTokenService.queryToken(tMap.get("client_id"));
        Map<String, String> queryParams = new HashMap<>();
        queryParams.put("cu_ids", tMap.get("cu_ids"));
        queryParams.put("t", tMap.get("t"));
        String responseStr = httpsRequest.doGetSDData(httpApi.getUrl(), queryParams, "utf-8", token, tMap);
        JSONObject responseObj = JSON.parseObject(responseStr);
        if ("0".equals(responseObj.get("code").toString())) {
            JSONObject dataObject = (JSONObject) responseObj.get("data");
 
            JSONArray hasSensorArray = dataObject.getJSONArray("has_sensor");
            if (!CollectionUtils.isEmpty(hasSensorArray)) {
                for (int i = 0; i < hasSensorArray.size(); i++) {
                    JSONObject item = hasSensorArray.getJSONObject(i);
                    String value = item.get("de_has_sensor").toString();
                    if ("yes".equals(value)) {
                        monitorCountMap.put("total_count", Integer.parseInt(item.get("count").toString()));//在线监控设备数量
                    }
                }
            }
            JSONArray stateArray = dataObject.getJSONArray("state");
            if (!CollectionUtils.isEmpty(stateArray)) {
                monitorCountMap.put("offline_count", 0);
                monitorCountMap.put("halt_count", 0);
                monitorCountMap.put("run_count", 0);
                for (int i = 0; i < stateArray.size(); i++) {
                    JSONObject item = stateArray.getJSONObject(i);
                    String value = item.get("device_state").toString();
                    if ("0".equals(value)) {
                        monitorCountMap.put("offline_count", Integer.parseInt(item.get("count").toString()));//离线设备数量
                    } else if ("1".equals(value)) {
                        monitorCountMap.put("halt_count", Integer.parseInt(item.get("count").toString()));//停机设备数量
                    } else if ("2".equals(value)) {
                        monitorCountMap.put("run_count", Integer.parseInt(item.get("count").toString()));//运行设备数量
                    }
                }
            }
            if (!monitorCountMap.isEmpty()) {
                feignModelApi.insertRunState(monitorCountMap);
            }
        }
    }
 
    public void getHealthStateValue(Map<String, String> tMap) {
        Map<String, Object> monitorCountMap = new HashMap<>();
        HttpApiEntity httpApi = httpApiService.getByCode(tMap.get("code"));
        String token = httpTokenService.queryToken(tMap.get("client_id"));
        Map<String, String> queryParams = new HashMap<>();
        queryParams.put("cu_ids", tMap.get("cu_ids"));
        queryParams.put("t", tMap.get("t"));
        String responseStr = httpsRequest.doGetSDData(httpApi.getUrl(), queryParams, "utf-8", token, tMap);
        JSONObject responseObj = JSON.parseObject(responseStr);
        if ("0".equals(responseObj.get("code").toString())) {
            JSONArray hasSensorArray = responseObj.getJSONArray("data");
            if (!CollectionUtils.isEmpty(hasSensorArray)) {
                for (int i = 0; i < hasSensorArray.size(); i++) {
                    JSONObject item = hasSensorArray.getJSONObject(i);
                    String value = item.get("fault_level").toString();
                    if ("0".equals(value)) {
                        monitorCountMap.put("normal", item.get("count"));//正常
                    } else if ("1".equals(value)) {
                        monitorCountMap.put("common", item.get("count"));//一般
                    } else if ("2".equals(value)) {
                        monitorCountMap.put("heavy", item.get("count"));//较重
                    } else if ("3".equals(value)) {
                        monitorCountMap.put("serious", item.get("count"));//严重
                    } else if ("4".equals(value)) {
                        monitorCountMap.put("espSerious", item.get("count"));//特别严重
                    }
                }
            }
            if (!monitorCountMap.isEmpty()) {
                feignModelApi.insertHealthState(monitorCountMap);
            }
        }
    }
 
    public void getDeviceList(Map<String, String> tMap) {
        HttpApiEntity httpApi = httpApiService.getByCode(tMap.get("code"));
        tMap.put("url", httpApi.getUrl());
 
        feignModelApi.insertDeviceList(tMap);
    }
 
    public void getTemperatureValue(Map<String, String> tMap, Date date) {
        Map<String, Object> monitorCountMap = new HashMap<>();
        HttpApiEntity httpApi = httpApiService.getByCode(tMap.get("code"));
        String token = httpTokenService.queryToken(tMap.get("client_id"));
        List<String> deviceIdList = feignModelApi.getDeviceIdList();
        if (!CollectionUtils.isEmpty(deviceIdList)) {
            for (String deviceId : deviceIdList) {
                List<String> tagNoList = httpTagService.getByTagType(deviceId);
                if (CollectionUtils.isEmpty(tagNoList)) {
                    continue;
                }
                StringBuffer sb = new StringBuffer();
                sb.append(httpApi.getUrl());
                sb.append("/");
                sb.append(deviceId);
                Map<String, String> queryParams = new HashMap<>();
                String responseStr = httpsRequest.doGetSDData(sb.toString(), queryParams, "utf-8", token, tMap);
                JSONObject responseObj = JSON.parseObject(responseStr);
                if ("200".equals(responseObj.get("status").toString())) {
                    JSONObject dataObject = (JSONObject) responseObj.get("data");
                    JSONArray jsonArray = dataObject.getJSONArray("realtime-params");
                    if (!CollectionUtils.isEmpty(jsonArray)) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject item = jsonArray.getJSONObject(i);
                            String order = item.get("cp_order").toString();
                            String name = item.get("param_name").toString();
                            if ("1".equals(order) && "温度".equals(name)) {
                                for (String tagCode : tagNoList) {
                                    String value = item.get("last_value").toString();
                                    // Date date = DateUtils.parse(item.get("last_time").toString(),"yyyy-MM-dd HH:mm:ss");
                                    if (tagCode.contains("Temperature")) {
                                        influxDBService.syncWriteFloatValue(tagCode, value, date.getTime());
                                    }
                                }
                            }
                            if ("1".equals(order) && "1倍频振幅".equals(name)) {
                                for (String tagCode : tagNoList) {
                                    String value = item.get("last_value").toString();
                                    // Date date = DateUtils.parse(item.get("last_time").toString(),"yyyy-MM-dd HH:mm:ss");
                                    if (tagCode.contains("Flutter")) {
                                        influxDBService.syncWriteFloatValue(tagCode, value, date.getTime());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}