萨达电箱对接

This commit is contained in:
pengjie 2024-05-18 01:06:42 +08:00
parent 0c7bd6d0bb
commit cc0080374e
2 changed files with 90 additions and 4 deletions

View File

@ -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<String> devList = doGetElectricalData(token, hashId);
devList.forEach(dev -> CompletableFuture.supplyAsync(() -> {
return doAddElectricalData(dev, token);
}).exceptionally(throwable -> {
log.error("err:", throwable);
return null;
})
);
}
private List<String> 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<String> 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<String, Object> map = new HashMap<>(4);

View File

@ -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