Commit 87ceec57a291e98e3c5e162592bbd98199760a2b

Authored by 杨鸣坤
1 parent 93359b64

国宏假数据同步

1 package com.iot.scheduler.controller; 1 package com.iot.scheduler.controller;
2 2
  3 +import com.iot.scheduler.service.GhDevicePullService;
3 import com.iot.scheduler.service.HnDeviceReportService; 4 import com.iot.scheduler.service.HnDeviceReportService;
4 import com.iot.scheduler.service.ShDevicePullService; 5 import com.iot.scheduler.service.ShDevicePullService;
5 import com.iot.scheduler.service.SlDevicePullService; 6 import com.iot.scheduler.service.SlDevicePullService;
@@ -16,6 +17,8 @@ public class HealthController { @@ -16,6 +17,8 @@ public class HealthController {
16 private ShDevicePullService shDevicePullService; 17 private ShDevicePullService shDevicePullService;
17 @Resource 18 @Resource
18 private SlDevicePullService slDevicePullService; 19 private SlDevicePullService slDevicePullService;
  20 + @Resource
  21 + private GhDevicePullService ghDevicePullService;
19 22
20 @GetMapping("/health") 23 @GetMapping("/health")
21 public String health() { 24 public String health() {
@@ -30,7 +33,8 @@ public class HealthController { @@ -30,7 +33,8 @@ public class HealthController {
30 @GetMapping("/pullSynchronization") 33 @GetMapping("/pullSynchronization")
31 public void pullSynchronization() { 34 public void pullSynchronization() {
32 // shDevicePullService.pullDeviceAndPushToIot(); 35 // shDevicePullService.pullDeviceAndPushToIot();
33 - slDevicePullService.pullDeviceAndPushToIot(); 36 +// slDevicePullService.pullDeviceAndPushToIot();
  37 + ghDevicePullService.pullDeviceAndPushToIot();
34 } 38 }
35 39
36 } 40 }
  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 GhDevicePullService {
  45 +
  46 + @Value("${gh.iot.organizeId:}")
  47 + private String iotOrganizeId;
  48 + @Value("${gh.iot.profileId}")
  49 + private String iotProfileId;
  50 + @Value("${gh.iot.deviceProfileId}")
  51 + private String iotDeviceProfileId;
  52 + @Value("${gh.iot.userName:}")
  53 + private String iotUserName;
  54 + @Value("${gh.iot.password:}")
  55 + private String iotPassword;
  56 + @Value("${gh.iot.tokenUrl}")
  57 + private String iotTokenUrl;
  58 + @Value("${gh.iot.infoUrl}")
  59 + private String iotDeviceInfoUrl;
  60 + @Value("${gh.iot.detailUrl}")
  61 + private String iotDeviceDetailUrl;
  62 +
  63 +
  64 + @Resource
  65 + private RedisTemplate<String, String> redisTemplate;
  66 +
  67 + public void pullDeviceAndPushToIot() {
  68 + JSONArray deviceInfoList = getDeviceInfo();
  69 + List<QxDeviceInfo> qxDeviceInfos = new ArrayList<>();
  70 + List<QxDeviceInfoDetail> qxAddDeviceInfoDetails = new ArrayList<>();
  71 + Map<String, QxDeviceInfoDetail> qxDeviceInfoDetailMap = new HashMap<>();
  72 + for (Object o : deviceInfoList) {
  73 + JSONObject deviceInfoJson = (JSONObject) o;
  74 + QxDeviceInfo qxDeviceInfo = new QxDeviceInfo();
  75 + qxDeviceInfo.setDeviceType("DIRECT_CONNECTION");
  76 + qxDeviceInfo.setTransportType("DEFAULT");
  77 + qxDeviceInfo.setOrganizationId(iotOrganizeId);
  78 + qxDeviceInfo.setDeviceProfileId(iotDeviceProfileId);
  79 + qxDeviceInfo.setProfileId(iotProfileId);
  80 +// //项目状态(1:在线,2:离线,3:报警)
  81 +// Integer projectState = deviceInfoJson.getInteger("projectState");
  82 +// if (projectState != null) {
  83 +// qxDeviceInfo.setDescription(String.valueOf(projectState));
  84 +// }
  85 +
  86 + //项目类型
  87 + qxDeviceInfo.setLabel("生产设备");
  88 + //设备名称
  89 + String deviceName = deviceInfoJson.getString("deviceName");
  90 +
  91 + qxDeviceInfo.setName(deviceName);
  92 + qxDeviceInfo.setBrand(deviceName);
  93 + //序列号
  94 + String dtuSn = deviceInfoJson.getString("dtuSn");
  95 + qxDeviceInfo.setSn(dtuSn);
  96 + DeviceToken deviceToken = new DeviceToken();
  97 + deviceToken.setCredentialsType("ACCESS_TOKEN");
  98 + deviceToken.setCredentialsId(dtuSn);
  99 + deviceToken.setCredentialsValue(dtuSn);
  100 + qxDeviceInfo.setDeviceToken(deviceToken);
  101 + qxDeviceInfos.add(qxDeviceInfo);
  102 + QxDeviceInfoDetail qxDeviceInfoDetail = new QxDeviceInfoDetail();
  103 + qxDeviceInfoDetail.setAlarm(false);
  104 + //灯状态(0:灭灯,1:红,2:黄,3:绿,4:蓝)
  105 + Integer lampState = deviceInfoJson.getInteger("lampState");
  106 + switch (lampState) {
  107 + case 0:
  108 + qxDeviceInfoDetail.setStatus("OFF");
  109 + break;
  110 + case 1:
  111 + qxDeviceInfoDetail.setStatus("ERROR");
  112 + qxDeviceInfoDetail.setAlarm(true);
  113 + break;
  114 + case 2:
  115 + qxDeviceInfoDetail.setStatus("STAND");
  116 + break;
  117 + case 3:
  118 + qxDeviceInfoDetail.setStatus("RUN");
  119 + //先从缓存里面拿token信息
  120 + String totalCapacity = getTotalCapacity("gh_total_capacity_" + dtuSn);
  121 + qxDeviceInfoDetail.setCumulativeOutput(Double.valueOf(totalCapacity));
  122 + break;
  123 + default:
  124 + continue;
  125 + }
  126 +
  127 +
  128 + qxDeviceInfoDetail.setStartTime(new Date());
  129 + qxDeviceInfoDetail.setDtuSn(dtuSn);
  130 + qxDeviceInfoDetailMap.put(dtuSn, qxDeviceInfoDetail);
  131 + qxAddDeviceInfoDetails.add(qxDeviceInfoDetail);
  132 + }
  133 +
  134 + //将数据同步到IOT平台
  135 + Map<String, String> qxParam = new HashMap<>(2);
  136 + qxParam.put("username", iotUserName);
  137 + qxParam.put("password", iotPassword);
  138 +
  139 + HttpPost qxHttpPost = new HttpPost(iotTokenUrl);
  140 + String qxResult = sendPost(qxHttpPost, JSON.toJSONString(qxParam));
  141 + if (StringUtils.isBlank(qxResult)) {
  142 + return;
  143 + }
  144 + Map<String, Object> qxRes = JSON.parseObject(qxResult, new TypeReference<>() {
  145 + });
  146 +
  147 + String qxAccessToken = (String) qxRes.get("token");
  148 + if (StringUtils.isBlank(qxAccessToken)) {
  149 + return;
  150 + }
  151 +
  152 + BasicHeader qxAuthorization = new BasicHeader("X-Authorization", "Bearer " + qxAccessToken);
  153 + if (!CollectionUtils.isEmpty(qxDeviceInfos)) {
  154 + HttpPost qxDeviceInfoPost = new HttpPost(iotDeviceInfoUrl);
  155 + qxDeviceInfoPost.addHeader(qxAuthorization);
  156 + for (QxDeviceInfo qxDeviceInfo : qxDeviceInfos) {
  157 + // todo
  158 + String syncDeviceInfo = sendPost(qxDeviceInfoPost, JSON.toJSONString(qxDeviceInfo));
  159 + //log.info("同步设备信息 syncDeviceInfo:{}", syncDeviceInfo);
  160 + }
  161 + }
  162 +
  163 + if (!CollectionUtils.isEmpty(qxAddDeviceInfoDetails)) {
  164 + for (QxDeviceInfoDetail qxDeviceInfoDetail : qxAddDeviceInfoDetails) {
  165 + String qxDeviceInfoDetailUrlStr = iotDeviceDetailUrl + qxDeviceInfoDetail.getDtuSn() + "/telemetry";
  166 + HttpPost qxDeviceInfoDetailPost = new HttpPost(qxDeviceInfoDetailUrlStr);
  167 + qxDeviceInfoDetailPost.addHeader(qxAuthorization);
  168 + String syncDeviceInfoDetail = sendPost(qxDeviceInfoDetailPost, JSON.toJSONString(qxDeviceInfoDetail));
  169 + }
  170 + }
  171 + }
  172 +
  173 + private JSONArray getDeviceInfo() {
  174 + JSONArray jsonArray = new JSONArray();
  175 + List<JSONObject> jsonObjectList = new ArrayList<>(13);
  176 + for (int index = 1; index < 14; index++) {
  177 + Map<String, Object> map = new HashMap<>(3);
  178 + map.put("deviceName", "无纺布机组" + index);
  179 + map.put("dtuSn", "AGH20260123" + String.format("%03d", index));
  180 + map.put("lampState", 3);
  181 + JSONObject jsonObject = new JSONObject();
  182 + jsonObject.putAll(map);
  183 + jsonObjectList.add(jsonObject);
  184 + }
  185 +
  186 + jsonArray.addAll(jsonObjectList);
  187 + return jsonArray;
  188 + }
  189 +
  190 + private String getTotalCapacity(String key) {
  191 + String totalCapacity = redisTemplate.opsForValue().get(key);
  192 + Double production = 100D;
  193 + Double totalCapacityD = 0D;
  194 + if (StringUtils.isEmpty(totalCapacity)) {
  195 + totalCapacityD = production;
  196 + } else {
  197 + totalCapacityD = Double.parseDouble(totalCapacity) + production;
  198 + }
  199 +
  200 + redisTemplate.opsForValue().set(key, String.valueOf(totalCapacityD));
  201 + return String.valueOf(totalCapacityD);
  202 + }
  203 +
  204 + private String sendPost(HttpPost httpPost, String jsonData) {
  205 + CloseableHttpClient httpClient = HttpClients.createDefault();
  206 + StringEntity entity = new StringEntity(jsonData, ContentType.create("application/json", Consts.UTF_8));
  207 + httpPost.setEntity(entity);
  208 + String result = null;
  209 + try {
  210 + CloseableHttpResponse execute = httpClient.execute(httpPost);
  211 + HttpEntity res = execute.getEntity();
  212 + InputStream is = res.getContent();
  213 + int len;
  214 + byte[] buf = new byte[128];
  215 + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  216 + while ((len = is.read(buf)) != -1) {
  217 + byteArrayOutputStream.write(buf, 0, len);
  218 + }
  219 + result = byteArrayOutputStream.toString();
  220 + } catch (IOException e) {
  221 + e.printStackTrace();
  222 + }
  223 + return result;
  224 + }
  225 +}
@@ -75,6 +75,28 @@ sl: @@ -75,6 +75,28 @@ sl:
75 infoUrl: "https://iot.hzzlyun.com/api/yt/device" 75 infoUrl: "https://iot.hzzlyun.com/api/yt/device"
76 detailUrl: "https://iot.hzzlyun.com/api/v1/" 76 detailUrl: "https://iot.hzzlyun.com/api/v1/"
77 77
  78 +gh:
  79 + iot:
  80 + organizeId: "875a4841-c7f2-4e2c-88a2-ea62d4642132"
  81 + profileId: "f2292f60-f738-11f0-9cb8-e3376d1e7978"
  82 + deviceProfileId: "f2292f60-f738-11f0-9cb8-e3376d1e7978"
  83 + userName: "ghxcl"
  84 + password: "123456"
  85 + tokenUrl: "https://iot.hzzlyun.com/api/auth/login"
  86 + infoUrl: "https://iot.hzzlyun.com/api/yt/device"
  87 + detailUrl: "https://iot.hzzlyun.com/api/v1/"
  88 +
  89 +shzz:
  90 + iot:
  91 + organizeId: "3304ebd5-71ae-448a-91a2-e3b4d6ae258a"
  92 + profileId: "f2292f60-f738-11f0-9cb8-e3376d1e7978"
  93 + deviceProfileId: "f2292f60-f738-11f0-9cb8-e3376d1e7978"
  94 + userName: "shzz"
  95 + password: "123456"
  96 + tokenUrl: "https://iot.hzzlyun.com/api/auth/login"
  97 + infoUrl: "https://iot.hzzlyun.com/api/yt/device"
  98 + detailUrl: "https://iot.hzzlyun.com/api/v1/"
  99 +
78 device: 100 device:
79 token: 101 token:
80 url: "https://iotgc.cniot.vip/auth/token" 102 url: "https://iotgc.cniot.vip/auth/token"