Commit 93359b6463d57566a6a28864c4859b2314d3cffb

Authored by 杨鸣坤
1 parent 33ce0055

双龙纸业拉取数据

... ... @@ -2,6 +2,7 @@ package com.iot.scheduler.controller;
2 2
3 3 import com.iot.scheduler.service.HnDeviceReportService;
4 4 import com.iot.scheduler.service.ShDevicePullService;
  5 +import com.iot.scheduler.service.SlDevicePullService;
5 6 import jakarta.annotation.Resource;
6 7 import org.springframework.web.bind.annotation.GetMapping;
7 8 import org.springframework.web.bind.annotation.RestController;
... ... @@ -13,6 +14,8 @@ public class HealthController {
13 14 private HnDeviceReportService hnDeviceReportService;
14 15 @Resource
15 16 private ShDevicePullService shDevicePullService;
  17 + @Resource
  18 + private SlDevicePullService slDevicePullService;
16 19
17 20 @GetMapping("/health")
18 21 public String health() {
... ... @@ -26,7 +29,8 @@ public class HealthController {
26 29
27 30 @GetMapping("/pullSynchronization")
28 31 public void pullSynchronization() {
29   - shDevicePullService.pullDeviceAndPushToIot();
  32 +// shDevicePullService.pullDeviceAndPushToIot();
  33 + slDevicePullService.pullDeviceAndPushToIot();
30 34 }
31 35
32 36 }
... ...
... ... @@ -33,7 +33,6 @@ import java.io.ByteArrayOutputStream;
33 33 import java.io.IOException;
34 34 import java.io.InputStream;
35 35 import java.text.SimpleDateFormat;
36   -import java.time.LocalTime;
37 36 import java.util.*;
38 37 import java.util.concurrent.TimeUnit;
39 38
... ... @@ -111,9 +110,8 @@ public class ShDevicePullService {
111 110 String dtuSn = deviceInfoJson.getString("dtuSn");
112 111 qxDeviceInfo.setSn(dtuSn);
113 112 //高速/中速
114   - List<String> highSpeedDeviceList = Arrays.asList("AGX251111720", "AGY251111266", "AGM251111722", "AGF251111329",
115   - "AGM251111301", "AGM251111746", "AGW251111837", "AGW251111403", "AGW251111450");
116   - if (highSpeedDeviceList.contains(dtuSn)) {
  113 + boolean highSpeed = deviceName.contains("高速");
  114 + if (highSpeed) {
117 115 qxDeviceInfo.setDescription("高速机");
118 116 } else {
119 117 qxDeviceInfo.setDescription("中速机");
... ... @@ -164,7 +162,7 @@ public class ShDevicePullService {
164 162 case 3:
165 163 qxDeviceInfoDetail.setStatus("RUN");
166 164 //先从缓存里面拿token信息
167   - String totalCapacity = getTotalCapacity("total_capacity_" + dtuSn, highSpeedDeviceList.contains(dtuSn));
  165 + String totalCapacity = getTotalCapacity("total_capacity_" + dtuSn, highSpeed);
168 166 qxDeviceInfoDetail.setCumulativeOutput(Double.valueOf(totalCapacity));
169 167 break;
170 168 default:
... ... @@ -294,7 +292,7 @@ public class ShDevicePullService {
294 292 }
295 293
296 294 // 解析设备信息
297   - Map<String, Object> deviceInfos = JSON.parseObject(deviceResult, new TypeReference<Map<String, Object>>() {
  295 + Map<String, Object> deviceInfos = JSON.parseObject(deviceResult, new TypeReference<>() {
298 296 });
299 297 Integer deviceInfoCode = (Integer) deviceInfos.get("code");
300 298
... ... @@ -315,7 +313,7 @@ public class ShDevicePullService {
315 313 }
316 314
317 315 // 重新解析设备信息
318   - deviceInfos = JSON.parseObject(deviceResult, new TypeReference<Map<String, Object>>() {
  316 + deviceInfos = JSON.parseObject(deviceResult, new TypeReference<>() {
319 317 });
320 318 deviceInfoCode = (Integer) deviceInfos.get("code");
321 319
... ...
  1 +package com.iot.scheduler.service;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.alibaba.fastjson.JSONArray;
  5 +import com.alibaba.fastjson.JSONObject;
  6 +import com.alibaba.fastjson.TypeReference;
  7 +import com.iot.scheduler.model.DeviceToken;
  8 +import com.iot.scheduler.model.QxDeviceInfo;
  9 +import com.iot.scheduler.model.QxDeviceInfoDetail;
  10 +import jakarta.annotation.Resource;
  11 +import lombok.extern.slf4j.Slf4j;
  12 +import org.apache.commons.lang3.StringUtils;
  13 +import org.apache.http.Consts;
  14 +import org.apache.http.HttpEntity;
  15 +import org.apache.http.client.methods.CloseableHttpResponse;
  16 +import org.apache.http.client.methods.HttpGet;
  17 +import org.apache.http.client.methods.HttpPost;
  18 +import org.apache.http.entity.ContentType;
  19 +import org.apache.http.entity.StringEntity;
  20 +import org.apache.http.impl.client.CloseableHttpClient;
  21 +import org.apache.http.impl.client.HttpClients;
  22 +import org.apache.http.message.BasicHeader;
  23 +import org.apache.http.util.EntityUtils;
  24 +import org.springframework.beans.factory.annotation.Value;
  25 +import org.springframework.data.redis.core.RedisTemplate;
  26 +import org.springframework.stereotype.Service;
  27 +import org.springframework.util.CollectionUtils;
  28 +import org.springframework.util.LinkedMultiValueMap;
  29 +import org.springframework.util.MultiValueMap;
  30 +import org.springframework.web.util.UriComponentsBuilder;
  31 +
  32 +import java.io.ByteArrayOutputStream;
  33 +import java.io.IOException;
  34 +import java.io.InputStream;
  35 +import java.text.SimpleDateFormat;
  36 +import java.util.*;
  37 +import java.util.concurrent.TimeUnit;
  38 +
  39 +/**
  40 + * 双龙应用数据同步
  41 + */
  42 +@Slf4j
  43 +@Service
  44 +public class SlDevicePullService {
  45 +
  46 + @Value("${sl.iot.organizeId:}")
  47 + private String iotOrganizeId;
  48 + @Value("${sl.iot.profileId}")
  49 + private String iotProfileId;
  50 + @Value("${sl.iot.deviceProfileId}")
  51 + private String iotDeviceProfileId;
  52 + @Value("${sl.iot.userName:}")
  53 + private String iotUserName;
  54 + @Value("${sl.iot.password:}")
  55 + private String iotPassword;
  56 + @Value("${sl.iot.tokenUrl}")
  57 + private String iotTokenUrl;
  58 + @Value("${sl.iot.infoUrl}")
  59 + private String iotDeviceInfoUrl;
  60 + @Value("${sl.iot.detailUrl}")
  61 + private String iotDeviceDetailUrl;
  62 +
  63 + @Value("${device.token.url}")
  64 + private String deviceTokenUrl;
  65 + @Value("${device.token.userName}")
  66 + private String deviceUserName;
  67 + @Value("${device.token.password}")
  68 + private String devicePassword;
  69 + @Value("${device.info.url}")
  70 + private String deviceInfoUrl;
  71 + @Value("${device.detail.url}")
  72 + private String deviceDetailUrl;
  73 +
  74 + @Resource
  75 + private RedisTemplate<String, String> redisTemplate;
  76 +
  77 + final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  78 +
  79 + public void pullDeviceAndPushToIot() {
  80 + String deviceResult = getDeviceInfo();
  81 + Map<String, Object> deviceInfos = JSON.parseObject(deviceResult, new TypeReference<>() {
  82 + });
  83 +
  84 + JSONArray deviceInfoList = (JSONArray) deviceInfos.get("data");
  85 + List<QxDeviceInfo> qxDeviceInfos = new ArrayList<>();
  86 + List<QxDeviceInfoDetail> qxAddDeviceInfoDetails = new ArrayList<>();
  87 + Map<String, QxDeviceInfoDetail> qxDeviceInfoDetailMap = new HashMap<>();
  88 + for (Object o : deviceInfoList) {
  89 + JSONObject deviceInfoJson = (JSONObject) o;
  90 + QxDeviceInfo qxDeviceInfo = new QxDeviceInfo();
  91 + qxDeviceInfo.setDeviceType("DIRECT_CONNECTION");
  92 + qxDeviceInfo.setTransportType("DEFAULT");
  93 + qxDeviceInfo.setOrganizationId(iotOrganizeId);
  94 + qxDeviceInfo.setDeviceProfileId(iotDeviceProfileId);
  95 + qxDeviceInfo.setProfileId(iotProfileId);
  96 +// //项目状态(1:在线,2:离线,3:报警)
  97 +// Integer projectState = deviceInfoJson.getInteger("projectState");
  98 +// if (projectState != null) {
  99 +// qxDeviceInfo.setDescription(String.valueOf(projectState));
  100 +// }
  101 +
  102 + //项目类型
  103 + qxDeviceInfo.setLabel("生产设备");
  104 + //设备名称
  105 + String deviceName = deviceInfoJson.getString("deviceName");
  106 +
  107 + qxDeviceInfo.setName(deviceName);
  108 + qxDeviceInfo.setBrand(deviceName);
  109 + //序列号
  110 + String dtuSn = deviceInfoJson.getString("dtuSn");
  111 + qxDeviceInfo.setSn(dtuSn);
  112 + //高速/中速
  113 + boolean highSpeed = deviceName.contains("高速");
  114 + if (highSpeed) {
  115 + qxDeviceInfo.setDescription("高速机");
  116 + } else {
  117 + qxDeviceInfo.setDescription("中速机");
  118 + }
  119 +
  120 + DeviceToken deviceToken = new DeviceToken();
  121 + deviceToken.setCredentialsType("ACCESS_TOKEN");
  122 + deviceToken.setCredentialsId(dtuSn);
  123 + deviceToken.setCredentialsValue(dtuSn);
  124 + qxDeviceInfo.setDeviceToken(deviceToken);
  125 + qxDeviceInfos.add(qxDeviceInfo);
  126 + //有序列号直接获取灯信息
  127 + if (StringUtils.isNotBlank(dtuSn)) {
  128 + String deviceInfoDetails = getDeviceInfoDetail(dtuSn);
  129 + if (StringUtils.isBlank(deviceInfoDetails)) {
  130 + continue;
  131 + }
  132 +
  133 + Map<String, Object> deviceInfoDetailMap = JSON.parseObject(deviceInfoDetails, new TypeReference<>() {
  134 + });
  135 +
  136 + JSONArray deviceInfoDetailList = (JSONArray) deviceInfoDetailMap.get("data");
  137 + if (CollectionUtils.isEmpty(deviceInfoDetailList)) {
  138 + continue;
  139 + }
  140 +
  141 + JSONObject deviceInfoDetailJson = (JSONObject) deviceInfoDetailList.get(0);
  142 + //灯详情数据
  143 + //序列号
  144 + String dtuSnDetail = deviceInfoDetailJson.getString("dtuSn");
  145 + //开始时间
  146 + String startTime = deviceInfoDetailJson.getString("startTime");
  147 + QxDeviceInfoDetail qxDeviceInfoDetail = new QxDeviceInfoDetail();
  148 + qxDeviceInfoDetail.setAlarm(false);
  149 + //灯状态(0:灭灯,1:红,2:黄,3:绿,4:蓝)
  150 + Integer lampState = deviceInfoDetailJson.getInteger("lampState");
  151 + switch (lampState) {
  152 + case 0:
  153 + qxDeviceInfoDetail.setStatus("OFF");
  154 + break;
  155 + case 1:
  156 + qxDeviceInfoDetail.setStatus("ERROR");
  157 + qxDeviceInfoDetail.setAlarm(true);
  158 + break;
  159 + case 2:
  160 + qxDeviceInfoDetail.setStatus("STAND");
  161 + break;
  162 + case 3:
  163 + qxDeviceInfoDetail.setStatus("RUN");
  164 + //先从缓存里面拿token信息
  165 + String totalCapacity = getTotalCapacity("total_capacity_" + dtuSn, highSpeed);
  166 + qxDeviceInfoDetail.setCumulativeOutput(Double.valueOf(totalCapacity));
  167 + break;
  168 + default:
  169 + continue;
  170 + }
  171 +
  172 + if (StringUtils.isNotBlank(startTime)) {
  173 + try {
  174 + qxDeviceInfoDetail.setStartTime(dateFormat.parse(startTime));
  175 + } catch (Exception e) {
  176 + log.error("时间格式出错");
  177 + qxDeviceInfoDetail.setStartTime(new Date());
  178 + }
  179 + } else {
  180 + qxDeviceInfoDetail.setStartTime(new Date());
  181 + }
  182 +
  183 + qxDeviceInfoDetail.setDtuSn(dtuSn);
  184 + qxDeviceInfoDetailMap.put(dtuSn, qxDeviceInfoDetail);
  185 + qxAddDeviceInfoDetails.add(qxDeviceInfoDetail);
  186 + }
  187 + }
  188 +
  189 + //将数据同步到IOT平台
  190 + Map<String, String> qxParam = new HashMap<>(2);
  191 + qxParam.put("username", iotUserName);
  192 + qxParam.put("password", iotPassword);
  193 +
  194 + HttpPost qxHttpPost = new HttpPost(iotTokenUrl);
  195 + String qxResult = sendPost(qxHttpPost, JSON.toJSONString(qxParam));
  196 + if (StringUtils.isBlank(qxResult)) {
  197 + return;
  198 + }
  199 + Map<String, Object> qxRes = JSON.parseObject(qxResult, new TypeReference<Map<String, Object>>() {
  200 + });
  201 +
  202 + String qxAccessToken = (String) qxRes.get("token");
  203 + if (StringUtils.isBlank(qxAccessToken)) {
  204 + return;
  205 + }
  206 +
  207 + BasicHeader qxAuthorization = new BasicHeader("X-Authorization", "Bearer " + qxAccessToken);
  208 + if (!CollectionUtils.isEmpty(qxDeviceInfos)) {
  209 + HttpPost qxDeviceInfoPost = new HttpPost(iotDeviceInfoUrl);
  210 + qxDeviceInfoPost.addHeader(qxAuthorization);
  211 + for (QxDeviceInfo qxDeviceInfo : qxDeviceInfos) {
  212 + // todo
  213 + String syncDeviceInfo = sendPost(qxDeviceInfoPost, JSON.toJSONString(qxDeviceInfo));
  214 + //log.info("同步设备信息 syncDeviceInfo:{}", syncDeviceInfo);
  215 + }
  216 + }
  217 +
  218 + if (!CollectionUtils.isEmpty(qxAddDeviceInfoDetails)) {
  219 + for (QxDeviceInfoDetail qxDeviceInfoDetail : qxAddDeviceInfoDetails) {
  220 + String qxDeviceInfoDetailUrlStr = iotDeviceDetailUrl + qxDeviceInfoDetail.getDtuSn() + "/telemetry";
  221 + HttpPost qxDeviceInfoDetailPost = new HttpPost(qxDeviceInfoDetailUrlStr);
  222 + qxDeviceInfoDetailPost.addHeader(qxAuthorization);
  223 + String syncDeviceInfoDetail = sendPost(qxDeviceInfoDetailPost, JSON.toJSONString(qxDeviceInfoDetail));
  224 + }
  225 + }
  226 + }
  227 +
  228 + private String getTotalCapacity(String key, boolean highSpeedDevice) {
  229 + String totalCapacity = redisTemplate.opsForValue().get(key);
  230 + Double production = highSpeedDevice ? 120D : 100D;
  231 + Double totalCapacityD = 0D;
  232 + if (StringUtils.isEmpty(totalCapacity)) {
  233 + totalCapacityD = production;
  234 + } else {
  235 + totalCapacityD = Double.parseDouble(totalCapacity) + production;
  236 + }
  237 +
  238 + redisTemplate.opsForValue().set(key, String.valueOf(totalCapacityD));
  239 + return String.valueOf(totalCapacityD);
  240 + }
  241 +
  242 + public String getDeviceInfoDetail(String dtuSn) {
  243 + String accessToken = getAccessToken();
  244 + Map<String, String> dtuSnOb = new HashMap<>(1);
  245 + dtuSnOb.put("dtuSn", dtuSn);
  246 + Map<String, String> headerMap = new HashMap<>(1);
  247 + headerMap.put("Authorization", "Bearer " + accessToken);
  248 + String deviceInfoDetail = sendRequestGet(deviceDetailUrl, dtuSnOb, headerMap);
  249 + if (StringUtils.isBlank(deviceInfoDetail)) {
  250 + return null;
  251 + }
  252 +
  253 + Map<String, Object> deviceInfoDetailMap = JSON.parseObject(deviceInfoDetail,
  254 + new TypeReference<>() {
  255 + });
  256 + Integer deviceInfoDetailCode = (Integer) deviceInfoDetailMap.get("code");
  257 + if (deviceInfoDetailCode != 200) {
  258 + return null;
  259 + }
  260 +
  261 + JSONArray deviceInfoDetailList = (JSONArray) deviceInfoDetailMap.get("data");
  262 + if (CollectionUtils.isEmpty(deviceInfoDetailList)) {
  263 + return null;
  264 + }
  265 +
  266 + JSONObject deviceInfoDetailJson = (JSONObject) deviceInfoDetailList.get(0);
  267 + //灯详情数据
  268 + //灯状态(0:灭灯,1:红,2:黄,3:绿,4:蓝)
  269 + Integer lampState = deviceInfoDetailJson.getInteger("lampState");
  270 + if (lampState == null) {
  271 + return null;
  272 + }
  273 +
  274 + return deviceInfoDetail;
  275 + }
  276 +
  277 + public String getDeviceInfo() {
  278 + String accessToken = getAccessToken();
  279 + // 初始化headerMap并设置Authorization
  280 + Map<String, String> headerMap = new HashMap<>(1);
  281 + headerMap.put("Authorization", "Bearer " + accessToken);
  282 +
  283 + Map<String, String> paramsMap = new HashMap<>();
  284 + paramsMap.put("groupName", "双龙纸业");
  285 +
  286 + // 第一次请求设备信息
  287 + String deviceResult = sendRequestGet(deviceInfoUrl, paramsMap, headerMap);
  288 +
  289 + // 检查设备信息是否为空
  290 + if (StringUtils.isBlank(deviceResult)) {
  291 + return null;
  292 + }
  293 +
  294 + // 解析设备信息
  295 + Map<String, Object> deviceInfos = JSON.parseObject(deviceResult, new TypeReference<>() {
  296 + });
  297 + Integer deviceInfoCode = (Integer) deviceInfos.get("code");
  298 +
  299 + // 如果code不为200,可能是accessToken失效,重新获取token并重试
  300 + if (deviceInfoCode != 200) {
  301 + accessToken = getAccessToken();
  302 + if (StringUtils.isEmpty(accessToken)) {
  303 + return null;
  304 + }
  305 +
  306 + // 更新headerMap中的Authorization
  307 + headerMap.put("Authorization", "Bearer " + accessToken);
  308 +
  309 + // 第二次请求设备信息
  310 + deviceResult = sendRequestGet(deviceInfoUrl, paramsMap, headerMap);
  311 + if (StringUtils.isBlank(deviceResult)) {
  312 + return null;
  313 + }
  314 +
  315 + // 重新解析设备信息
  316 + deviceInfos = JSON.parseObject(deviceResult, new TypeReference<>() {
  317 + });
  318 + deviceInfoCode = (Integer) deviceInfos.get("code");
  319 +
  320 + // 如果第二次请求仍然失败,返回错误信息
  321 + if (deviceInfoCode != 200) {
  322 + return null;
  323 + }
  324 + }
  325 +
  326 + // 返回成功的设备信息
  327 + return deviceResult;
  328 + }
  329 +
  330 + private String getAccessToken() {
  331 + String accessToken = "";
  332 + String redisKey = "device_token";
  333 + if (StringUtils.isNotBlank(redisTemplate.opsForValue().get(redisKey)) && redisTemplate.getExpire(redisKey) > 0) {
  334 + return redisTemplate.opsForValue().get(redisKey);
  335 + }
  336 +
  337 + Map<String, String> param = new HashMap<>(2);
  338 + param.put("username", deviceUserName);
  339 + param.put("password", devicePassword);
  340 + HttpPost httpPost = new HttpPost(deviceTokenUrl);
  341 + String result = sendPost(httpPost, JSON.toJSONString(param));
  342 + if (StringUtils.isBlank(result)) {
  343 + return accessToken;
  344 + }
  345 +
  346 + Map<String, Object> res = JSON.parseObject(result, new TypeReference<>() {
  347 + });
  348 +
  349 + Integer code = (Integer) res.get("code");
  350 + if (code == 200) {
  351 + JSONObject data = (JSONObject) res.get("data");
  352 + accessToken = (String) data.get("token");
  353 + redisTemplate.opsForValue().set(redisKey, accessToken, 3600, TimeUnit.SECONDS); // 一小时过期
  354 + }
  355 +
  356 + return accessToken;
  357 + }
  358 +
  359 + public static String sendRequestGet(String url, Map<String, String> params, Map<String, String> header) {
  360 + //实例化httpclient
  361 + CloseableHttpClient httpclient = HttpClients.createDefault();
  362 + url = builderUrl(url, params);
  363 + //请求结果
  364 + String content = "";
  365 + //实例化get方法
  366 + HttpGet httpget = new HttpGet(url);
  367 + if (!CollectionUtils.isEmpty(header)) {
  368 + for (Map.Entry<String, String> entry : header.entrySet()) {
  369 + httpget.setHeader(entry.getKey(), entry.getValue());
  370 + }
  371 + }
  372 +
  373 + try (CloseableHttpResponse response = httpclient.execute(httpget)) {
  374 +
  375 + //执行get方法
  376 + if (response.getStatusLine().getStatusCode() == 200) {
  377 + content = EntityUtils.toString(response.getEntity(), "UTF-8");
  378 + }
  379 + } catch (IOException e) {
  380 + log.error("sendRequest---GET Error!", e);
  381 + }
  382 + return content;
  383 + }
  384 +
  385 + private static String builderUrl(String url, Map<String, String> params) {
  386 + UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url);
  387 + if (!CollectionUtils.isEmpty(params)) {
  388 + MultiValueMap<String, String> paramsValue = new LinkedMultiValueMap<>();
  389 + for (Map.Entry<String, String> entry : params.entrySet()) {
  390 + paramsValue.add(entry.getKey(), entry.getValue());
  391 + }
  392 +
  393 + uriBuilder = uriBuilder.queryParams(paramsValue);
  394 + }
  395 +
  396 + return uriBuilder.toUriString();
  397 + }
  398 +
  399 + private String sendPost(HttpPost httpPost, String jsonData) {
  400 + CloseableHttpClient httpClient = HttpClients.createDefault();
  401 + StringEntity entity = new StringEntity(jsonData, ContentType.create("application/json", Consts.UTF_8));
  402 + httpPost.setEntity(entity);
  403 + String result = null;
  404 + try {
  405 + CloseableHttpResponse execute = httpClient.execute(httpPost);
  406 + HttpEntity res = execute.getEntity();
  407 + InputStream is = res.getContent();
  408 + int len;
  409 + byte[] buf = new byte[128];
  410 + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  411 + while ((len = is.read(buf)) != -1) {
  412 + byteArrayOutputStream.write(buf, 0, len);
  413 + }
  414 + result = byteArrayOutputStream.toString();
  415 + } catch (IOException e) {
  416 + e.printStackTrace();
  417 + }
  418 + return result;
  419 + }
  420 +}
... ...
... ... @@ -64,6 +64,17 @@ sh:
64 64 infoUrl: "https://iot.hzzlyun.com/api/yt/device"
65 65 detailUrl: "https://iot.hzzlyun.com/api/v1/"
66 66
  67 +sl:
  68 + iot:
  69 + organizeId: "752a7621-b59b-477c-b15e-e06d412e02d5"
  70 + profileId: "f2292f60-f738-11f0-9cb8-e3376d1e7978"
  71 + deviceProfileId: "f2292f60-f738-11f0-9cb8-e3376d1e7978"
  72 + userName: "slzy"
  73 + password: "123456"
  74 + tokenUrl: "https://iot.hzzlyun.com/api/auth/login"
  75 + infoUrl: "https://iot.hzzlyun.com/api/yt/device"
  76 + detailUrl: "https://iot.hzzlyun.com/api/v1/"
  77 +
67 78 device:
68 79 token:
69 80 url: "https://iotgc.cniot.vip/auth/token"
... ...