Commit 480e512cbe0c0d47f316f61878cfd469c244113e

Authored by 杨鸣坤
1 parent 87ceec57

同步数据接口添加至定时任务

1 1 package com.iot.scheduler.controller;
2 2
3   -import com.iot.scheduler.service.GhDevicePullService;
4   -import com.iot.scheduler.service.HnDeviceReportService;
5   -import com.iot.scheduler.service.ShDevicePullService;
6   -import com.iot.scheduler.service.SlDevicePullService;
  3 +import com.iot.scheduler.service.*;
7 4 import jakarta.annotation.Resource;
8 5 import org.springframework.web.bind.annotation.GetMapping;
9 6 import org.springframework.web.bind.annotation.RestController;
... ... @@ -19,6 +16,8 @@ public class HealthController {
19 16 private SlDevicePullService slDevicePullService;
20 17 @Resource
21 18 private GhDevicePullService ghDevicePullService;
  19 + @Resource
  20 + private ShzzDevicePullService shzzDevicePullService;
22 21
23 22 @GetMapping("/health")
24 23 public String health() {
... ... @@ -32,9 +31,10 @@ public class HealthController {
32 31
33 32 @GetMapping("/pullSynchronization")
34 33 public void pullSynchronization() {
35   -// shDevicePullService.pullDeviceAndPushToIot();
36   -// slDevicePullService.pullDeviceAndPushToIot();
  34 + shDevicePullService.pullDeviceAndPushToIot();
  35 + slDevicePullService.pullDeviceAndPushToIot();
37 36 ghDevicePullService.pullDeviceAndPushToIot();
  37 + shzzDevicePullService.pullDeviceAndPushToIot();
38 38 }
39 39
40 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.HttpPost;
  17 +import org.apache.http.entity.ContentType;
  18 +import org.apache.http.entity.StringEntity;
  19 +import org.apache.http.impl.client.CloseableHttpClient;
  20 +import org.apache.http.impl.client.HttpClients;
  21 +import org.apache.http.message.BasicHeader;
  22 +import org.springframework.beans.factory.annotation.Value;
  23 +import org.springframework.data.redis.core.RedisTemplate;
  24 +import org.springframework.stereotype.Service;
  25 +import org.springframework.util.CollectionUtils;
  26 +
  27 +import java.io.ByteArrayOutputStream;
  28 +import java.io.IOException;
  29 +import java.io.InputStream;
  30 +import java.time.LocalTime;
  31 +import java.util.*;
  32 +
  33 +/**
  34 + * 双龙应用数据同步
  35 + */
  36 +@Slf4j
  37 +@Service
  38 +public class ShzzDevicePullService {
  39 +
  40 + @Value("${shzz.iot.organizeId:}")
  41 + private String iotOrganizeId;
  42 + @Value("${shzz.iot.profileId}")
  43 + private String iotProfileId;
  44 + @Value("${shzz.iot.deviceProfileId}")
  45 + private String iotDeviceProfileId;
  46 + @Value("${shzz.iot.userName:}")
  47 + private String iotUserName;
  48 + @Value("${shzz.iot.password:}")
  49 + private String iotPassword;
  50 + @Value("${shzz.iot.tokenUrl}")
  51 + private String iotTokenUrl;
  52 + @Value("${shzz.iot.infoUrl}")
  53 + private String iotDeviceInfoUrl;
  54 + @Value("${shzz.iot.detailUrl}")
  55 + private String iotDeviceDetailUrl;
  56 +
  57 +
  58 + @Resource
  59 + private RedisTemplate<String, String> redisTemplate;
  60 +
  61 + public void pullDeviceAndPushToIot() {
  62 + LocalTime currentTime = LocalTime.now();
  63 + LocalTime startTime = LocalTime.of(8, 0, 0); // 08:00:00
  64 + LocalTime endTime = LocalTime.of(18, 0, 0); // 18:00:00
  65 +
  66 + boolean isWithinRange =
  67 + !currentTime.isBefore(startTime) &&
  68 + currentTime.isBefore(endTime);
  69 +
  70 + if (!isWithinRange) {
  71 + return;
  72 + }
  73 +
  74 + JSONArray deviceInfoList = getDeviceInfo();
  75 + List<QxDeviceInfo> qxDeviceInfos = new ArrayList<>();
  76 + List<QxDeviceInfoDetail> qxAddDeviceInfoDetails = new ArrayList<>();
  77 + Map<String, QxDeviceInfoDetail> qxDeviceInfoDetailMap = new HashMap<>();
  78 + for (Object o : deviceInfoList) {
  79 + JSONObject deviceInfoJson = (JSONObject) o;
  80 + QxDeviceInfo qxDeviceInfo = new QxDeviceInfo();
  81 + qxDeviceInfo.setDeviceType("DIRECT_CONNECTION");
  82 + qxDeviceInfo.setTransportType("DEFAULT");
  83 + qxDeviceInfo.setOrganizationId(iotOrganizeId);
  84 + qxDeviceInfo.setDeviceProfileId(iotDeviceProfileId);
  85 + qxDeviceInfo.setProfileId(iotProfileId);
  86 +// //项目状态(1:在线,2:离线,3:报警)
  87 +// Integer projectState = deviceInfoJson.getInteger("projectState");
  88 +// if (projectState != null) {
  89 +// qxDeviceInfo.setDescription(String.valueOf(projectState));
  90 +// }
  91 +
  92 + //项目类型
  93 + qxDeviceInfo.setLabel("生产设备");
  94 + //设备名称
  95 + String deviceName = deviceInfoJson.getString("deviceName");
  96 +
  97 + qxDeviceInfo.setName(deviceName);
  98 + qxDeviceInfo.setBrand(deviceName);
  99 + //序列号
  100 + String dtuSn = deviceInfoJson.getString("dtuSn");
  101 + qxDeviceInfo.setSn(dtuSn);
  102 + DeviceToken deviceToken = new DeviceToken();
  103 + deviceToken.setCredentialsType("ACCESS_TOKEN");
  104 + deviceToken.setCredentialsId(dtuSn);
  105 + deviceToken.setCredentialsValue(dtuSn);
  106 + qxDeviceInfo.setDeviceToken(deviceToken);
  107 + qxDeviceInfos.add(qxDeviceInfo);
  108 + QxDeviceInfoDetail qxDeviceInfoDetail = new QxDeviceInfoDetail();
  109 + qxDeviceInfoDetail.setAlarm(false);
  110 + //灯状态(0:灭灯,1:红,2:黄,3:绿,4:蓝)
  111 + Integer lampState = deviceInfoJson.getInteger("lampState");
  112 + switch (lampState) {
  113 + case 0:
  114 + qxDeviceInfoDetail.setStatus("OFF");
  115 + break;
  116 + case 1:
  117 + qxDeviceInfoDetail.setStatus("ERROR");
  118 + qxDeviceInfoDetail.setAlarm(true);
  119 + break;
  120 + case 2:
  121 + qxDeviceInfoDetail.setStatus("STAND");
  122 + break;
  123 + case 3:
  124 + qxDeviceInfoDetail.setStatus("RUN");
  125 + //先从缓存里面拿token信息
  126 + String totalCapacity = getTotalCapacity("gh_total_capacity_" + dtuSn, deviceInfoJson.getDouble("production"));
  127 + qxDeviceInfoDetail.setCumulativeOutput(Double.valueOf(totalCapacity));
  128 + break;
  129 + default:
  130 + continue;
  131 + }
  132 +
  133 +
  134 + qxDeviceInfoDetail.setStartTime(new Date());
  135 + qxDeviceInfoDetail.setDtuSn(dtuSn);
  136 + qxDeviceInfoDetailMap.put(dtuSn, qxDeviceInfoDetail);
  137 + qxAddDeviceInfoDetails.add(qxDeviceInfoDetail);
  138 + }
  139 +
  140 + //将数据同步到IOT平台
  141 + Map<String, String> qxParam = new HashMap<>(2);
  142 + qxParam.put("username", iotUserName);
  143 + qxParam.put("password", iotPassword);
  144 +
  145 + HttpPost qxHttpPost = new HttpPost(iotTokenUrl);
  146 + String qxResult = sendPost(qxHttpPost, JSON.toJSONString(qxParam));
  147 + if (StringUtils.isBlank(qxResult)) {
  148 + return;
  149 + }
  150 + Map<String, Object> qxRes = JSON.parseObject(qxResult, new TypeReference<>() {
  151 + });
  152 +
  153 + String qxAccessToken = (String) qxRes.get("token");
  154 + if (StringUtils.isBlank(qxAccessToken)) {
  155 + return;
  156 + }
  157 +
  158 + BasicHeader qxAuthorization = new BasicHeader("X-Authorization", "Bearer " + qxAccessToken);
  159 + if (!CollectionUtils.isEmpty(qxDeviceInfos)) {
  160 + HttpPost qxDeviceInfoPost = new HttpPost(iotDeviceInfoUrl);
  161 + qxDeviceInfoPost.addHeader(qxAuthorization);
  162 + for (QxDeviceInfo qxDeviceInfo : qxDeviceInfos) {
  163 + // todo
  164 + String syncDeviceInfo = sendPost(qxDeviceInfoPost, JSON.toJSONString(qxDeviceInfo));
  165 + //log.info("同步设备信息 syncDeviceInfo:{}", syncDeviceInfo);
  166 + }
  167 + }
  168 +
  169 + if (!CollectionUtils.isEmpty(qxAddDeviceInfoDetails)) {
  170 + for (QxDeviceInfoDetail qxDeviceInfoDetail : qxAddDeviceInfoDetails) {
  171 + String qxDeviceInfoDetailUrlStr = iotDeviceDetailUrl + qxDeviceInfoDetail.getDtuSn() + "/telemetry";
  172 + HttpPost qxDeviceInfoDetailPost = new HttpPost(qxDeviceInfoDetailUrlStr);
  173 + qxDeviceInfoDetailPost.addHeader(qxAuthorization);
  174 + String syncDeviceInfoDetail = sendPost(qxDeviceInfoDetailPost, JSON.toJSONString(qxDeviceInfoDetail));
  175 + }
  176 + }
  177 + }
  178 +
  179 + private JSONArray getDeviceInfo() {
  180 + List<String> deviceNameList = Arrays.asList(
  181 + "新发现KSJ-160智能纸杯机7oz-1", "新发现KSJ-160智能纸杯机7oz-2", "新发现KSJ-160智能纸杯机6.5oz", "新发现KSJ-160智能纸杯机8oz-1",
  182 + "新发现KSJ-160智能纸杯机8oz-2", "新发现PJJ-III型纸容器品检机6.5oz/7oz", "新发现PJJ-III型纸容器品检机7.5oz/8oz",
  183 + "新发现PJJ-I-02型纸容器品检机7.5oz/8oz", "新发现PJJ-I-02型纸容器品检机7.5oz/12oz", "新发现KSJ-160智能快速纸杯机7.5oz-1",
  184 + "新发现KSJ-160智能快速纸杯机7.5oz-2", "新发现KSJ-160智能快速纸杯机12oz", "智能中速纸杯机JBZ-22D-1",
  185 + "智能中速纸杯机JBZ-22D-2", "智能中速纸杯机JBZ-22D-3", "智能中速纸杯机JBZ-22D-4", "智能中速纸杯机JBZ-22D-5", "智能中速纸杯机JBZ-22D-6",
  186 + "智能中速纸杯机JBZ-22D-7", "智能中速纸杯机JBZ-22D-8", "智能中速纸杯机JBZ-22D-9", "智能中速纸杯机JBZ-22S-1", "智能中速纸杯机JBZ-22S-2",
  187 + "高速智能纸杯机DEBAO-100S", "纸机杯XC-L12齿轮340-B型-1", "纸机杯XC-L12齿轮340-B型-2", "中速外贴机SM100-KT490(12oz)",
  188 + "中速外贴机SM100-KT491(4oz)", "中速外贴机SM100-KY373", "中速外贴机SM100-KT489(16oz)", "外贴机8oz", "外贴机12oz", "模切机1",
  189 + "模切机2", "模切机3", "模切机4", "模切机5");
  190 + JSONArray jsonArray = new JSONArray();
  191 + List<JSONObject> jsonObjectList = new ArrayList<>(38);
  192 + for (int index = 0; index < deviceNameList.size(); index++) {
  193 + Map<String, Object> map = new HashMap<>(3);
  194 + map.put("deviceName", deviceNameList.get(index));
  195 + map.put("dtuSn", "SGH20250123" + String.format("%03d", index));
  196 + map.put("lampState", 3);
  197 + if (index < 11) {
  198 + map.put("production", 130D);
  199 + } else if (index == 11) {
  200 + map.put("production", 120D);
  201 + } else if (index < 23) {
  202 + map.put("production", 80D);
  203 + } else if (index == 23) {
  204 + map.put("production", 110D);
  205 + } else if (index < 26) {
  206 + map.put("production", 50D);
  207 + } else if (index < 30) {
  208 + map.put("production", 120D);
  209 + } else {
  210 + map.put("production", 80D);
  211 + }
  212 + JSONObject jsonObject = new JSONObject();
  213 + jsonObject.putAll(map);
  214 + jsonObjectList.add(jsonObject);
  215 + }
  216 +
  217 + jsonArray.addAll(jsonObjectList);
  218 + return jsonArray;
  219 + }
  220 +
  221 + private String getTotalCapacity(String key, Double production) {
  222 + String totalCapacity = redisTemplate.opsForValue().get(key);
  223 + Double totalCapacityD = 0D;
  224 + if (StringUtils.isEmpty(totalCapacity)) {
  225 + totalCapacityD = production;
  226 + } else {
  227 + totalCapacityD = Double.parseDouble(totalCapacity) + production;
  228 + }
  229 +
  230 + redisTemplate.opsForValue().set(key, String.valueOf(totalCapacityD));
  231 + return String.valueOf(totalCapacityD);
  232 + }
  233 +
  234 + private String sendPost(HttpPost httpPost, String jsonData) {
  235 + CloseableHttpClient httpClient = HttpClients.createDefault();
  236 + StringEntity entity = new StringEntity(jsonData, ContentType.create("application/json", Consts.UTF_8));
  237 + httpPost.setEntity(entity);
  238 + String result = null;
  239 + try {
  240 + CloseableHttpResponse execute = httpClient.execute(httpPost);
  241 + HttpEntity res = execute.getEntity();
  242 + InputStream is = res.getContent();
  243 + int len;
  244 + byte[] buf = new byte[128];
  245 + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  246 + while ((len = is.read(buf)) != -1) {
  247 + byteArrayOutputStream.write(buf, 0, len);
  248 + }
  249 + result = byteArrayOutputStream.toString();
  250 + } catch (IOException e) {
  251 + e.printStackTrace();
  252 + }
  253 + return result;
  254 + }
  255 +}
... ...
1 1 package com.iot.scheduler.zone;
2 2
3   -import com.iot.scheduler.service.HnDeviceReportService;
  3 +import com.iot.scheduler.service.*;
4 4 import com.iot.scheduler.task.AbstractZoneScheduler;
5 5 import jakarta.annotation.Resource;
6 6 import lombok.extern.slf4j.Slf4j;
... ... @@ -13,6 +13,14 @@ public class ChizhouZoneScheduler extends AbstractZoneScheduler {
13 13
14 14 @Resource
15 15 private HnDeviceReportService hnDeviceReportService;
  16 + @Resource
  17 + private ShDevicePullService shDevicePullService;
  18 + @Resource
  19 + private SlDevicePullService slDevicePullService;
  20 + @Resource
  21 + private GhDevicePullService ghDevicePullService;
  22 + @Resource
  23 + private ShzzDevicePullService shzzDevicePullService;
16 24
17 25 @Override
18 26 protected String getZoneName() {
... ... @@ -26,6 +34,10 @@ public class ChizhouZoneScheduler extends AbstractZoneScheduler {
26 34 try {
27 35 // TODO: Implement actual logic
28 36 log.info("[{}] Simulating pulling devices...", getZoneName());
  37 + shDevicePullService.pullDeviceAndPushToIot();
  38 + slDevicePullService.pullDeviceAndPushToIot();
  39 + ghDevicePullService.pullDeviceAndPushToIot();
  40 + shzzDevicePullService.pullDeviceAndPushToIot();
29 41 Thread.sleep(1000);
30 42 } catch (Exception e) {
31 43 logError(taskName, e);
... ... @@ -39,8 +51,8 @@ public class ChizhouZoneScheduler extends AbstractZoneScheduler {
39 51 String taskName = "Push Devices (IoT -> 3rd Party)";
40 52 logStart(taskName);
41 53 try {
42   - // TODO: Implement actual logic
43 54 log.info("[{}] Simulating pushing devices...", getZoneName());
  55 + hnDeviceReportService.deviceReport();
44 56 Thread.sleep(1000);
45 57 } catch (Exception e) {
46 58 logError(taskName, e);
... ...
... ... @@ -28,8 +28,8 @@ scheduler:
28 28 pull: "0 0/5 * * * ?" # Every 5 minutes
29 29 push: "0 0/10 * * * ?" # Every 10 minutes
30 30 chizhou:
31   - pull: "0 0/5 * * * ?"
32   - push: "0 0/10 * * * ?"
  31 + pull: "0 0/1 * * * ?"
  32 + push: "0 0/1 * * * ?"
33 33 panji:
34 34 pull: "0 0/5 * * * ?"
35 35 push: "0 0/10 * * * ?"
... ... @@ -51,7 +51,12 @@ hn:
51 51 LEFT JOIN ts_kv_latest tkl2 ON de.id = tkl2.entity_id
52 52 AND tkl2.key = 175
53 53 WHERE
54   - de.organization_id = 'f82530a0-93e4-4aeb-9339-f5b6d1127840';"
  54 + de.organization_id IN(
  55 + 'f82530a0-93e4-4aeb-9339-f5b6d1127840',
  56 + '752a7621-b59b-477c-b15e-e06d412e02d5',
  57 + '3304ebd5-71ae-448a-91a2-e3b4d6ae258a',
  58 + '875a4841-c7f2-4e2c-88a2-ea62d4642132'
  59 + );"
55 60
56 61 sh:
57 62 iot:
... ...