8 changed files with 626 additions and 10 deletions
@ -0,0 +1,566 @@ |
|||||||
|
package com.mh.quartz.task; |
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSON; |
||||||
|
import com.alibaba.fastjson2.JSONArray; |
||||||
|
import com.alibaba.fastjson2.JSONObject; |
||||||
|
import com.mh.common.constant.Constants; |
||||||
|
import com.mh.common.core.domain.entity.CollectionParamsManage; |
||||||
|
import com.mh.common.core.domain.entity.OrderEntity; |
||||||
|
import com.mh.common.core.redis.RedisCache; |
||||||
|
import com.mh.common.model.request.AdvantechDatas; |
||||||
|
import com.mh.common.model.request.AdvantechReceiver; |
||||||
|
import com.mh.common.utils.DateUtils; |
||||||
|
import com.mh.common.utils.StringUtils; |
||||||
|
import com.mh.common.utils.sign.Md5Utils; |
||||||
|
import com.mh.framework.rabbitmq.producer.SendMsgByTopic; |
||||||
|
import com.mh.system.mapper.device.GatewayManageMapper; |
||||||
|
import com.mh.system.service.device.ICollectionParamsManageService; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
import java.io.BufferedReader; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStreamReader; |
||||||
|
import java.io.PrintWriter; |
||||||
|
import java.math.BigDecimal; |
||||||
|
import java.net.HttpURLConnection; |
||||||
|
import java.net.URL; |
||||||
|
import java.nio.charset.StandardCharsets; |
||||||
|
import java.util.*; |
||||||
|
import java.util.concurrent.TimeUnit; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author LJF |
||||||
|
* @version 1.0 |
||||||
|
* @project EEMCS |
||||||
|
* @description 获取维控网关数据定时任务 |
||||||
|
* @date 2026-07-07 14:57:49 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
@Component("getWeiKongSysDataJob") |
||||||
|
public class GetWeiKongSysDataJob { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private ICollectionParamsManageService collectionParamsManageService; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private GatewayManageMapper gatewayManageMapper; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private SendMsgByTopic sendMsgByTopic; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private RedisCache redisCache; |
||||||
|
|
||||||
|
// ==== 维控网关账号配置(建议后续改为 @Value 从 application.yml 注入) ====
|
||||||
|
private static final String VK_ALIAS = "170b9f801b0c40e1b83d63217726fa18"; |
||||||
|
private static final String VK_PASSWORD_MD5 = "7a627c3ef7bf1cae22474ad516415281"; // URL中password参数(密码的MD5)
|
||||||
|
private static final String VK_CUID = "123456789"; |
||||||
|
private static final String VK_SV = "1.0"; |
||||||
|
private static final String VK_MT = "255"; |
||||||
|
private static final String VK_PID = "1"; |
||||||
|
private static final String VK_SIGN_KEY = "5cee621329f24e5cbdc43daa959ce9a1"; // sign拼接用的key
|
||||||
|
|
||||||
|
// ==== 通用常量 ====
|
||||||
|
private static final int PAGE_SIZE = 50; |
||||||
|
private static final int REDIS_CACHE_SECONDS = 8 * 60 * 60; |
||||||
|
|
||||||
|
|
||||||
|
// 获取维控网关数据
|
||||||
|
public void getWeiKongSysData(String realUrl) { |
||||||
|
log.info("开始获取维控网关数据"); |
||||||
|
try { |
||||||
|
if (StringUtils.isEmpty(realUrl)) { |
||||||
|
realUrl = "http://api.v-iec.com"; |
||||||
|
} else { |
||||||
|
realUrl = "http://" + realUrl; |
||||||
|
} |
||||||
|
|
||||||
|
// // 获取冷源所有点位(预留,后续用于 realdata 数据与点位映射)
|
||||||
|
// HashMap<String, Object> params = new HashMap<>();
|
||||||
|
// params.put("systemType", "0");
|
||||||
|
// List<CollectionParamsManage> collectionParamsManages = collectionParamsManageService.selectListByParams(params);
|
||||||
|
|
||||||
|
List<AdvantechDatas<BigDecimal>> allAdvantechDatas = new ArrayList<>(); |
||||||
|
String token = null; |
||||||
|
|
||||||
|
// 增加redis 8小时过期缓存时间
|
||||||
|
if (redisCache.hasKey(Constants.WEI_KONG_SID)) { |
||||||
|
token = redisCache.getCacheObject(Constants.WEI_KONG_SID); |
||||||
|
} else { |
||||||
|
token = loginToWeiKong(realUrl); |
||||||
|
if (StringUtils.isBlank(token)) { |
||||||
|
log.error("未获取到维控网关token,终止本次采集"); |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// 获取到token值之后,开始获取设备列表
|
||||||
|
String deviceListUrl = realUrl + "/m16/we-data/device/getList"; |
||||||
|
// 其中wcommon值,会发生改变
|
||||||
|
// 计算签名拼接发生改变:cuid=123456789&mt=255&pid=1&sid=84b46fd2565b49e9ab678b6cd211721e&sv=1.0&ts=1783409441936&key=5cee621329f24e5cbdc43daa959ce9a1
|
||||||
|
// wcommon的值变成:{"cuid":"123456789","sv":"1.0","mt":"255","sign":"5021bd40d02659558c9c7e5056fdcf67","pid":"1","ts":1783409441936,"sid":"84b46fd2565b49e9ab678b6cd211721e"}
|
||||||
|
String deviceListWc = buildWcommonHeader(token, |
||||||
|
"cuid", VK_CUID, "mt", VK_MT, "pid", VK_PID, "sid", token, "sv", VK_SV); |
||||||
|
String deviceListResp = sendGetWithHeader(realUrl, deviceListUrl, deviceListWc, allAdvantechDatas); |
||||||
|
log.info("维控网关设备列表响应: {}", deviceListResp); |
||||||
|
|
||||||
|
// 封装成研华网关格式并发送到消息队列
|
||||||
|
if (!allAdvantechDatas.isEmpty()) { |
||||||
|
AdvantechReceiver advantechReceiver = new AdvantechReceiver(); |
||||||
|
advantechReceiver.setTs(DateUtils.dateToString(new Date(), Constants.DATE_FORMAT)); |
||||||
|
advantechReceiver.setD(allAdvantechDatas); |
||||||
|
|
||||||
|
String message = JSONObject.toJSONString(advantechReceiver); |
||||||
|
log.info("发送维控数据到消息队列,数据点数: {}", allAdvantechDatas.size()); |
||||||
|
sendMsgByTopic.sendToDeviceMQ(message); |
||||||
|
log.info("维控数据发送成功"); |
||||||
|
} else { |
||||||
|
log.warn("没有获取到任何维控数据"); |
||||||
|
} |
||||||
|
|
||||||
|
} catch (Exception e) { |
||||||
|
log.error("获取维控数据异常", e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 登录维控网关,获取会话 sid |
||||||
|
* |
||||||
|
* @param realUrl 维控网关基础地址 |
||||||
|
* @return 会话 sid,登录失败返回 null |
||||||
|
*/ |
||||||
|
private String loginToWeiKong(String realUrl) { |
||||||
|
// 登录维控网关
|
||||||
|
String loginUrl = realUrl + "/m16/we-data/sign/login"; |
||||||
|
// 根据这个cli的请求方式生成对应的请求逻辑
|
||||||
|
// postman request POST 'http://api.v-iec.com/m16/we-data/sign/login?alias=170b9f801b0c40e1b83d63217726fa18&password=2348648c42e6913b53bf5b0e3c366f64' \
|
||||||
|
// --header 'Wcommon: {"cuid":"123456789","sv":"1.0","mt":"255","sign":"5fa49940234e55c13fd831ef5b5079dc","pid":"1","ts":1783407005717}'
|
||||||
|
//其中ts是当前时间戳,sign是"alias=test&cuid=123456789&mt=255&password=e10adc3949ba59abbe56e057f20f883e&pid=1&sv=1.0&ts=1756708016315&key=5cee621329f24e5cbdc43daa959ce9a1"的md5 32位小加密
|
||||||
|
// sign 拼接串:注意 password 使用明文密码的MD5,key 放在最后
|
||||||
|
String passwordMd5 = Md5Utils.hash(VK_PASSWORD_MD5); |
||||||
|
String wcommonHeader = buildWcommonHeader("", |
||||||
|
"alias", VK_ALIAS, "cuid", VK_CUID, "mt", VK_MT, |
||||||
|
"password", passwordMd5, "pid", VK_PID, "sv", VK_SV); |
||||||
|
|
||||||
|
// 组装带 query 参数的完整登录URL(password 为密码MD5)
|
||||||
|
String fullLoginUrl = loginUrl + "?alias=" + VK_ALIAS + "&password=" + passwordMd5; |
||||||
|
|
||||||
|
// 发送带 Wcommon 头的 POST 登录请求(HttpUtils 不支持自定义header,使用 HttpURLConnection)
|
||||||
|
String loginResp = sendPostWithHeader(fullLoginUrl, wcommonHeader); |
||||||
|
log.info("维控网关登录响应: {}", loginResp); |
||||||
|
|
||||||
|
String token = null; |
||||||
|
if (StringUtils.isNotBlank(loginResp)) { |
||||||
|
JSONObject loginJson = JSON.parseObject(loginResp); |
||||||
|
if (loginJson.getInteger("code") == 200) { |
||||||
|
token = loginJson.getJSONObject("result").getString("sid"); // 依据接口实际返回字段调整
|
||||||
|
log.info("维控网关登录成功"); |
||||||
|
} else { |
||||||
|
log.error("维控网关登录失败: {}", loginJson.getString("msg")); |
||||||
|
} |
||||||
|
} |
||||||
|
if (StringUtils.isNotBlank(token)) { |
||||||
|
log.info("维控网关登录成功,sid: {}", token); |
||||||
|
redisCache.setCacheObject(Constants.WEI_KONG_SID, token, REDIS_CACHE_SECONDS, TimeUnit.SECONDS); |
||||||
|
} |
||||||
|
return token; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 构建维控网关 Wcommon 请求头 JSON(含自动签名与时间戳) |
||||||
|
* |
||||||
|
* @param sid 会话ID(登录时传空串) |
||||||
|
* @param signPairs 签名参数,按 key1,val1,key2,val2... 顺序排列,ts 和 key 会自动追加 |
||||||
|
* @return Wcommon 头的 JSON 字符串 |
||||||
|
*/ |
||||||
|
private String buildWcommonHeader(String sid, String... signPairs) { |
||||||
|
long ts = System.currentTimeMillis(); |
||||||
|
// 拼接签名源串:key1=val1&key2=val2&...&ts=xxx&key=xxx
|
||||||
|
StringBuilder signSrc = new StringBuilder(); |
||||||
|
for (int i = 0; i < signPairs.length; i += 2) { |
||||||
|
if (i > 0) { |
||||||
|
signSrc.append("&"); |
||||||
|
} |
||||||
|
signSrc.append(signPairs[i]).append("=").append(signPairs[i + 1]); |
||||||
|
} |
||||||
|
signSrc.append("&ts=").append(ts); |
||||||
|
signSrc.append("&key=").append(VK_SIGN_KEY); |
||||||
|
String sign = Md5Utils.hash(signSrc.toString()); |
||||||
|
|
||||||
|
JSONObject wcommon = new JSONObject(); |
||||||
|
wcommon.put("cuid", VK_CUID); |
||||||
|
wcommon.put("sv", VK_SV); |
||||||
|
wcommon.put("mt", VK_MT); |
||||||
|
wcommon.put("sign", sign); |
||||||
|
wcommon.put("pid", VK_PID); |
||||||
|
wcommon.put("ts", ts); |
||||||
|
if (StringUtils.isNotBlank(sid)) { |
||||||
|
wcommon.put("sid", sid); |
||||||
|
} |
||||||
|
return wcommon.toJSONString(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 发送设备列表请求,遍历设备并拉取实时数据 |
||||||
|
* |
||||||
|
* @return API响应原始字符串 |
||||||
|
*/ |
||||||
|
private String sendGetWithHeader(String baseUrl, String url, String wcommonHeader, List<AdvantechDatas<BigDecimal>> allAdvantechDatas) { |
||||||
|
String deviceResp = sendPostWithHeader(url, wcommonHeader); |
||||||
|
log.info("维控网关设备列表响应: {}", deviceResp); |
||||||
|
// 响应值:{
|
||||||
|
//{
|
||||||
|
// "reqId": "1783412496265",
|
||||||
|
// "code": 200,
|
||||||
|
// "msg": "ok",
|
||||||
|
// "result": {
|
||||||
|
// "list": [
|
||||||
|
// {
|
||||||
|
// "groupName": "默认组",
|
||||||
|
// "boxList": [
|
||||||
|
// {
|
||||||
|
// "devType": 0,
|
||||||
|
// "baseAuth": false,
|
||||||
|
// "devModel": "V-BOX H-4G",
|
||||||
|
// "machineCode": "V12000240702037747696d4cb0f",
|
||||||
|
// "devVer": 1,
|
||||||
|
// "groupId": 10209020,
|
||||||
|
// "sort": 0,
|
||||||
|
// "templateId": 0,
|
||||||
|
// "realAuth": 3,
|
||||||
|
// "boxName": "保利",
|
||||||
|
// "state": 1,
|
||||||
|
// "map": ",",
|
||||||
|
// "boxId": 6434626
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
if (StringUtils.isBlank(deviceResp)) { |
||||||
|
log.error("维控网关设备列表响应为空"); |
||||||
|
return Constants.SUCCESS; |
||||||
|
} |
||||||
|
|
||||||
|
JSONObject deviceListJson = JSON.parseObject(deviceResp); |
||||||
|
JSONObject resultObj = deviceListJson.getJSONObject("result"); |
||||||
|
if (resultObj == null) { |
||||||
|
log.error("维控网关设备列表result为空"); |
||||||
|
return Constants.SUCCESS; |
||||||
|
} |
||||||
|
JSONArray list = resultObj.getJSONArray("list"); |
||||||
|
if (list == null || list.isEmpty()) { |
||||||
|
log.warn("维控网关设备列表为空"); |
||||||
|
return Constants.SUCCESS; |
||||||
|
} |
||||||
|
|
||||||
|
// 从登录后的 wcommon 头中提取 sid,供后续 realdata 请求使用
|
||||||
|
JSONObject wcommonJson = JSON.parseObject(wcommonHeader); |
||||||
|
String sid = wcommonJson.getString("sid"); |
||||||
|
|
||||||
|
// 遍历获取boxList组数据并进行log.info
|
||||||
|
for (int i = 0; i < list.size(); i++) { |
||||||
|
JSONObject groupObj = list.getJSONObject(i); |
||||||
|
log.info("设备列表分组信息: {}", groupObj.toJSONString()); |
||||||
|
JSONArray boxList = groupObj.getJSONArray("boxList"); |
||||||
|
if (boxList == null) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
for (int j = 0; j < boxList.size(); j++) { |
||||||
|
JSONObject boxObj = boxList.getJSONObject(j); |
||||||
|
log.info("设备详情信息: {}", boxObj.toJSONString()); |
||||||
|
String boxId = boxObj.getString("boxId"); |
||||||
|
String devType = boxObj.getString("devType"); |
||||||
|
|
||||||
|
// 根据boxId获取对应的分组列表数据
|
||||||
|
try { |
||||||
|
processBoxGroups(baseUrl, boxId, devType, sid, allAdvantechDatas); |
||||||
|
} catch (Exception e) { |
||||||
|
log.error("处理设备 [boxId={}] 分组数据异常", boxId, e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return Constants.SUCCESS; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取设备的分组列表,并拉取每个分组的实时监控点数据 |
||||||
|
*/ |
||||||
|
private void processBoxGroups(String baseUrl, String boxId, String devType, String sid, List<AdvantechDatas<BigDecimal>> allAdvantechDatas) { |
||||||
|
// 根据boxId获取对应的分组列表数据
|
||||||
|
// 其中签名参数:boxId=6434626&cuid=123456789&mt=255&pid=1&sid=84b46fd2565b49e9ab678b6cd211721e&sv=1.0&ts=1783414379064&key=5cee621329f24e5cbdc43daa959ce9a1
|
||||||
|
// 请求头 wcommon = {"cuid":"123456789","sv":"1.0","mt":"255","sign":"e04d082236a317f262fb8bd1267d0966","pid":"1","ts":1783414379064,"sid":"84b46fd2565b49e9ab678b6cd211721e"}
|
||||||
|
String groupListUrl = baseUrl + "/m16/we-data/actGroup?boxId=" + boxId; |
||||||
|
String groupWc = buildWcommonHeader(sid, |
||||||
|
"boxId", boxId, "cuid", VK_CUID, "mt", VK_MT, |
||||||
|
"pid", VK_PID, "sid", sid, "sv", VK_SV); |
||||||
|
|
||||||
|
// 开始发送请求
|
||||||
|
String groupResp = sendPostWithHeader(groupListUrl, groupWc); |
||||||
|
log.info("设备 [boxId={}] 实时监控点分组列表响应: {}", boxId, groupResp); |
||||||
|
|
||||||
|
if (StringUtils.isBlank(groupResp)) { |
||||||
|
return; |
||||||
|
} |
||||||
|
JSONObject groupJson = JSON.parseObject(groupResp); |
||||||
|
JSONObject listObj = groupJson.getJSONObject("result"); |
||||||
|
if (listObj == null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
// 请求后响应:{
|
||||||
|
// "list":
|
||||||
|
// [
|
||||||
|
// {
|
||||||
|
// "groupName":"默认组",
|
||||||
|
// "groupId":338364
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// ]
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
JSONArray groupList = listObj.getJSONArray("list"); |
||||||
|
if (groupList == null || groupList.isEmpty()) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
for (int k = 0; k < groupList.size(); k++) { |
||||||
|
JSONObject groupItem = groupList.getJSONObject(k); |
||||||
|
String groupId = groupItem.getString("groupId"); |
||||||
|
if (StringUtils.isBlank(groupId)) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
fetchRealDataByPage(baseUrl, boxId, devType, groupId, sid, allAdvantechDatas); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 分页拉取设备实时监控点数据 |
||||||
|
*/ |
||||||
|
private void fetchRealDataByPage(String baseUrl, String boxId, String devType, String groupId, String sid, List<AdvantechDatas<BigDecimal>> allAdvantechDatas) { |
||||||
|
String realDataUtl = baseUrl + "/m16/we-data/realdata"; |
||||||
|
// 其中参数是:boxId 盒子ID(不传为自定义的监控点分组)
|
||||||
|
//groupId 组别ID,必传
|
||||||
|
//pageSize 每页数量,不传默认10条
|
||||||
|
//pageIndex 第几页,从1开始
|
||||||
|
//devtype 设备类型(0:box,1:hmi),必传,
|
||||||
|
|
||||||
|
int pageIndex = 1; |
||||||
|
int totalRecord = 0; |
||||||
|
boolean hasMoreData = true; |
||||||
|
|
||||||
|
while (hasMoreData) { |
||||||
|
// 签名字符串是:boxId=6434626&cuid=123456789&devtype=0&groupId=10209020&mt=255&pageIndex=1&pageSize=10&pid=1&sid=84b46fd2565b49e9ab678b6cd211721e&sv=1.0&ts=1783410587018&key=5cee621329f24e5cbdc43daa959ce9a1
|
||||||
|
// wcommon值:{"cuid":"123456789","sv":"1.0","mt":"255","sign":"2f1862fd0144108914b3e0d70c79ee39","pid":"1","ts":1783410587018,"sid":"84b46fd2565b49e9ab678b6cd211721e"}
|
||||||
|
String realDataWc = buildWcommonHeader(sid, |
||||||
|
"boxId", boxId, "cuid", VK_CUID, "devtype", devType, |
||||||
|
"groupId", groupId, "mt", VK_MT, "pageIndex", String.valueOf(pageIndex), |
||||||
|
"pageSize", String.valueOf(PAGE_SIZE), "pid", VK_PID, "sid", sid, "sv", VK_SV); |
||||||
|
|
||||||
|
// 开始发送请求
|
||||||
|
String fullRealDataUrl = realDataUtl + "?boxId=" + boxId |
||||||
|
+ "&groupId=" + groupId |
||||||
|
+ "&devtype=" + devType |
||||||
|
+ "&pageSize=" + PAGE_SIZE |
||||||
|
+ "&pageIndex=" + pageIndex; |
||||||
|
String realDataResp = sendPostWithHeader(fullRealDataUrl, realDataWc); |
||||||
|
log.info("设备 [boxId={}] 实时数据第{}页响应: {}", boxId, pageIndex, realDataResp); |
||||||
|
|
||||||
|
// 请求后数据响应为:{
|
||||||
|
// "code": 200,
|
||||||
|
// "msg": "ok",
|
||||||
|
// "result": {
|
||||||
|
// "list": [
|
||||||
|
// {
|
||||||
|
// "com":1,
|
||||||
|
// "string_point":-1,
|
||||||
|
// "monitorId":1,
|
||||||
|
// "addr_stat_no":-1,
|
||||||
|
// "actAuth":3,
|
||||||
|
// "monitorName":"3v-y3",
|
||||||
|
// "groupId":1,
|
||||||
|
// "state":0,
|
||||||
|
// "float_point":1,
|
||||||
|
// "templateId":0,
|
||||||
|
// "value":"0",
|
||||||
|
// "boxId":1
|
||||||
|
// },
|
||||||
|
// ],
|
||||||
|
//totalRecord:500,
|
||||||
|
//currentPage:2
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
if (StringUtils.isBlank(realDataResp)) { |
||||||
|
log.error("设备 [boxId={}] 实时数据响应为空", boxId); |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
JSONObject realDataJson = JSON.parseObject(realDataResp); |
||||||
|
if (realDataJson.getInteger("code") != 200) { |
||||||
|
log.error("设备 [boxId={}] 实时数据查询失败: {}", boxId, realDataJson.getString("msg")); |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
JSONObject resultObj = realDataJson.getJSONObject("result"); |
||||||
|
if (resultObj == null) { |
||||||
|
log.error("设备 [boxId={}] 实时数据result为空", boxId); |
||||||
|
break; |
||||||
|
} |
||||||
|
JSONArray dataList = resultObj.getJSONArray("list"); |
||||||
|
List<CollectionParamsManage> registers = null; |
||||||
|
if (redisCache.hasKey("DEVICES_REGISTER")) { |
||||||
|
registers = redisCache.getCacheList("DEVICES_REGISTER", CollectionParamsManage.class); |
||||||
|
|
||||||
|
} else { |
||||||
|
registers = collectionParamsManageService.queryCollectionParamsByMtType(Constants.OTHER_TYPE); |
||||||
|
redisCache.setCacheList("DEVICES_REGISTER", registers); |
||||||
|
} |
||||||
|
// 开始遍历数据
|
||||||
|
for (int i = 0; i < dataList.size(); i++) { |
||||||
|
AdvantechDatas<BigDecimal> advantechData = new AdvantechDatas<>(); |
||||||
|
JSONObject dataItem = dataList.getJSONObject(i); |
||||||
|
String tag = dataItem.getString("monitorName"); |
||||||
|
// 根据moitorName获取对应的otherName "DEVICES_REGISTER", Constants.DEVICE
|
||||||
|
long monitorId = dataItem.getLongValue("monitorId"); |
||||||
|
// 遍历所有数据
|
||||||
|
for (CollectionParamsManage register : registers) { |
||||||
|
if (register.getMtNum().equals(String.valueOf(monitorId))) { |
||||||
|
tag = register.getOtherName(); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
BigDecimal value = new BigDecimal(dataItem.getString("value")); |
||||||
|
advantechData.setValue(value); |
||||||
|
advantechData.setTag(tag); |
||||||
|
// 根据state状态值,1为正常,其他为异常
|
||||||
|
if (dataItem.getIntValue("state") == 1) { |
||||||
|
advantechData.setQuality(BigDecimal.valueOf(0)); |
||||||
|
} else { |
||||||
|
advantechData.setQuality(BigDecimal.valueOf(1)); |
||||||
|
} |
||||||
|
allAdvantechDatas.add(advantechData); |
||||||
|
} |
||||||
|
totalRecord = resultObj.getIntValue("totalRecord"); |
||||||
|
int currentPage = resultObj.getIntValue("currentPage"); |
||||||
|
|
||||||
|
if (!dataList.isEmpty()) { |
||||||
|
log.info("设备 [boxId={}] 第{}页获取到{}条数据, 总记录数: {}", |
||||||
|
boxId, pageIndex, dataList.size(), totalRecord); |
||||||
|
} |
||||||
|
|
||||||
|
// 根据响应查看是否是已经全部查询完毕,如果没有就继续请求,如果已经全部查询完毕则结束请求
|
||||||
|
if (currentPage * PAGE_SIZE >= totalRecord) { |
||||||
|
hasMoreData = false; |
||||||
|
log.info("设备 [boxId={}] 数据已全部获取完毕, 总记录数: {}", boxId, totalRecord); |
||||||
|
} else { |
||||||
|
pageIndex++; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 发送带自定义 Wcommon 头的 POST 请求 |
||||||
|
*/ |
||||||
|
private String sendPostWithHeader(String url, String wcommonHeader) { |
||||||
|
StringBuilder result = new StringBuilder(); |
||||||
|
BufferedReader in = null; |
||||||
|
HttpURLConnection conn = null; |
||||||
|
try { |
||||||
|
URL realUrl = new URL(url); |
||||||
|
conn = (HttpURLConnection) realUrl.openConnection(); |
||||||
|
conn.setRequestMethod("POST"); |
||||||
|
conn.setRequestProperty("accept", "*/*"); |
||||||
|
conn.setRequestProperty("connection", "Keep-Alive"); |
||||||
|
conn.setRequestProperty("user-agent", "Mozilla/5.0"); |
||||||
|
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8"); |
||||||
|
conn.setRequestProperty("Wcommon", wcommonHeader); |
||||||
|
conn.setDoOutput(true); |
||||||
|
conn.setDoInput(true); |
||||||
|
try (PrintWriter out = new PrintWriter(conn.getOutputStream())) { |
||||||
|
out.print("{}"); // 登录无 body,传空 JSON
|
||||||
|
out.flush(); |
||||||
|
} |
||||||
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); |
||||||
|
String line; |
||||||
|
while ((line = in.readLine()) != null) { |
||||||
|
result.append(line); |
||||||
|
} |
||||||
|
} catch (Exception e) { |
||||||
|
log.error("维控网关登录请求异常, url={}", url, e); |
||||||
|
} finally { |
||||||
|
if (in != null) { |
||||||
|
try { |
||||||
|
in.close(); |
||||||
|
} catch (IOException ignored) { |
||||||
|
} |
||||||
|
} |
||||||
|
if (conn != null) { |
||||||
|
conn.disconnect(); |
||||||
|
} |
||||||
|
} |
||||||
|
return result.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
public boolean operationDevice(String wkBaseUrl, List<OrderEntity> changeValues) { |
||||||
|
// 地址:/m16/we-data/updrealdata
|
||||||
|
String url = wkBaseUrl + "/m16/we-data/updrealdata"; |
||||||
|
String token = null; |
||||||
|
// 参数:boxId,monitorId,value,devtype(我们是0)
|
||||||
|
for (OrderEntity order : changeValues) { |
||||||
|
// 根据id查询对应的网关,然后查询网关地址
|
||||||
|
CollectionParamsManage collectionParamsManage = collectionParamsManageService.selectCollectionParamsManageById(order.getId()); |
||||||
|
if (collectionParamsManage == null) { |
||||||
|
log.error("设备 [id={}] 未找到对应的网关信息", order.getId()); |
||||||
|
continue; |
||||||
|
} |
||||||
|
String gatewayId = collectionParamsManage.getGatewayId(); |
||||||
|
String gatewayAddr = gatewayManageMapper.selectById(gatewayId).getGwAddr(); |
||||||
|
if (StringUtils.isBlank(gatewayAddr)) { |
||||||
|
log.error("设备 [id={}] 对应的网关地址为空", order.getId()); |
||||||
|
continue; |
||||||
|
} |
||||||
|
String monitorId = collectionParamsManage.getMtNum(); |
||||||
|
BigDecimal value = new BigDecimal(order.getParam()); |
||||||
|
int devType = 0; |
||||||
|
// 增加redis 8小时过期缓存时间
|
||||||
|
if (redisCache.hasKey(Constants.WEI_KONG_SID)) { |
||||||
|
token = redisCache.getCacheObject(Constants.WEI_KONG_SID); |
||||||
|
} else { |
||||||
|
token = loginToWeiKong(wkBaseUrl); |
||||||
|
if (StringUtils.isBlank(token)) { |
||||||
|
log.error("未获取到维控网关token,终止本次采集"); |
||||||
|
continue; |
||||||
|
} |
||||||
|
} |
||||||
|
// boxId=6434626&cuid=123456789&devtype=0&monitorId=12085422&mt=255&pid=1&sid=ec71bf11ad0f43e8926fc1f2c2985787&sv=1.0&ts=1783475921486&value=26&key=5cee621329f24e5cbdc43daa959ce9a1
|
||||||
|
// {"cuid":"123456789","sv":"1.0","mt":"255","sign":"56e11aadbecc2559af7c6fcafcab226e","pid":"1","ts":1783475921486,"sid":"ec71bf11ad0f43e8926fc1f2c2985787"}
|
||||||
|
// 其中wcommon值,会发生改变
|
||||||
|
// 开始发送请求
|
||||||
|
String fullRealDataUrl = url + "?boxId=" + gatewayAddr |
||||||
|
+ "&monitorId=" + monitorId |
||||||
|
+ "&devtype=" + devType |
||||||
|
+ "&value=" + value; |
||||||
|
String deviceListWc = buildWcommonHeader(token, |
||||||
|
"cuid", VK_CUID, "mt", VK_MT, "pid", VK_PID, "sid", token, "sv", VK_SV); |
||||||
|
// 发送请求,并获取响应结果
|
||||||
|
String result = sendPostWithHeader(fullRealDataUrl, deviceListWc); |
||||||
|
JSONObject jsonObject = JSONObject.parseObject(result); |
||||||
|
log.info("维控网关设备设置值操作返回: {}", result); |
||||||
|
if (jsonObject.getInteger("code").equals(200)) { |
||||||
|
continue; |
||||||
|
} else { |
||||||
|
log.error("维控网关设备设置值操作失败: {}", result); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
// 重新查询实时数据
|
||||||
|
try { |
||||||
|
getWeiKongSysData(""); |
||||||
|
} catch (Exception e) { |
||||||
|
log.error("重新查询维控网关实时数据异常", e); |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue