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 tMap) { Map monitorCountMap = new HashMap<>(); HttpApiEntity httpApi = httpApiService.getByCode(tMap.get("code")); String token = httpTokenService.queryToken(tMap.get("client_id")); Map 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 tMap) { Map monitorCountMap = new HashMap<>(); HttpApiEntity httpApi = httpApiService.getByCode(tMap.get("code")); String token = httpTokenService.queryToken(tMap.get("client_id")); Map 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 tMap) { HttpApiEntity httpApi = httpApiService.getByCode(tMap.get("code")); tMap.put("url", httpApi.getUrl()); feignModelApi.insertDeviceList(tMap); } public void getTemperatureValue(Map tMap, Date date) { Map monitorCountMap = new HashMap<>(); HttpApiEntity httpApi = httpApiService.getByCode(tMap.get("code")); String token = httpTokenService.queryToken(tMap.get("client_id")); List deviceIdList = feignModelApi.getDeviceIdList(); if (!CollectionUtils.isEmpty(deviceIdList)) { for (String deviceId : deviceIdList) { List tagNoList = httpTagService.getByTagType(deviceId); if (CollectionUtils.isEmpty(tagNoList)) { continue; } StringBuffer sb = new StringBuffer(); sb.append(httpApi.getUrl()); sb.append("/"); sb.append(deviceId); Map 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()); } } } } } } } } } }