From cc0080374e14a8c0bc4a9ebd30afad95a1daf684 Mon Sep 17 00:00:00 2001 From: pengjie <17373303529@163.com> Date: Sat, 18 May 2024 01:06:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=90=A8=E8=BE=BE=E7=94=B5=E7=AE=B1=E5=AF=B9?= =?UTF-8?q?=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zhgd/xmgl/task/ElectricalTask.java | 92 ++++++++++++++++++- src/main/resources/application.properties | 2 + 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/zhgd/xmgl/task/ElectricalTask.java b/src/main/java/com/zhgd/xmgl/task/ElectricalTask.java index 975f6045e..2ac7ab789 100644 --- a/src/main/java/com/zhgd/xmgl/task/ElectricalTask.java +++ b/src/main/java/com/zhgd/xmgl/task/ElectricalTask.java @@ -1,8 +1,12 @@ package com.zhgd.xmgl.task; import cn.hutool.core.collection.CollUtil; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.baidubce.services.iotshc.model.token.GetTokenRequest; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zhgd.xmgl.modules.electrical.entity.ElectricalData; import com.zhgd.xmgl.modules.electrical.entity.ElectricalDev; @@ -17,6 +21,7 @@ import com.zhgd.xmgl.util.XiwonUtil; import lombok.extern.slf4j.Slf4j; import net.javacrumbs.shedlock.core.SchedulerLock; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -25,10 +30,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.CompletableFuture; /** @@ -58,6 +60,9 @@ public class ElectricalTask { @Resource private XiwonUtil xiwonUtil; + @Value("${sada.host}") + private String sadaHost; + /** * 获取最新电量使用情况 每5分钟触发任务 */ @@ -85,6 +90,85 @@ public class ElectricalTask { } } + /** + * 获取电箱情况 每5分钟触发任务 0 30 * * * ? + */ + @Scheduled(cron = "0 0/1 * * * ?") + @RequestMapping("/xmgl/task/getElectricData") + public void getElectricData() { + log.info("获取设备数据 每1分钟触发任务"); + JSONObject obj = getToken(); + if (obj == null) { + log.error("获取token失败!"); + } + String token = obj.getString("token"); + String hashId = obj.getJSONObject("account").getJSONObject("currentProject").getString("hashId"); + List devList = doGetElectricalData(token, hashId); + devList.forEach(dev -> CompletableFuture.supplyAsync(() -> { + return doAddElectricalData(dev, token); + }).exceptionally(throwable -> { + log.error("err:", throwable); + return null; + }) + ); + } + + private List doGetElectricalData(String token, String hashId) { + HttpRequest post = HttpRequest.get(sadaHost + "/box/project/" + hashId); + post.header("Auth", token); + HttpResponse execute = post.execute(); + boolean ok = execute.isOk(); + if (ok) { + log.info("电箱数据" + execute.body()); + List devList = new ArrayList<>(); + JSONObject result = JSONObject.parseObject(execute.body()); + JSONArray result1 = result.getJSONArray("result"); + for (int i = 0; i < result1.size(); i++) { + devList.add(result1.getJSONObject(i).getString("hashId")); + } + return devList; + } + return null; + } + + private boolean doAddElectricalData(String devId, String token) { + HttpRequest post = HttpRequest.get(sadaHost + "/metric/box/" + devId); + post.header("Auth", token); + HttpResponse execute = post.execute(); + boolean ok = execute.isOk(); + if (ok) { + log.info("电箱实时数据" + execute.body()); + JSONObject result = JSONObject.parseObject(execute.body()).getJSONObject("result"); + JSONObject metric = result.getJSONObject("metric"); + ElectricalData electricalData = new ElectricalData(devId, metric.getString("modified"), metric.getString("voltage_a"), + metric.getString("voltage_b"), + metric.getString("voltage_c"), metric.getString("current_a"), metric.getString("current_b"), metric.getString("current_c"), + result.getJSONArray("leakages").getJSONObject(0).getString("leakage"), + "0", "0", "0", + result.getJSONArray("temperatures").getJSONObject(0).getString("temperature")); + electricalDataService.saveElectricalData(electricalData); + return true; + } + return false; + } + + private JSONObject getToken() { + JSONObject param = new JSONObject(); + param.put("username", "赤峰智慧工地-zcr"); + param.put("password", "IXNOI9mk35"); + HttpRequest post = HttpRequest.post(sadaHost + "/v2/api-token-auth"); + post.body(JSON.toJSONString(param)); + HttpResponse execute = post.execute(); + boolean ok = execute.isOk(); + if (ok) { + log.info(execute.body()); + JSONObject result = JSONObject.parseObject(execute.body()); + return result.getJSONObject("result"); + } + return null; + } + + private boolean doGetElectricRealTimeData(ElectricalDev dev, Project project) { // 请求参数 Map map = new HashMap<>(4); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 389b2ce4d..4afd9a999 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -202,3 +202,5 @@ tenant.tables[7]=wflow_models tenant.tables[8]=wflow_notifys tenant.tables[9]=wflow_sub_groups tenant.tables[10]=wflow_sub_process + +sada.host=http://api.e.v1.i-sada.net