20260531:平台对接第2版实现(bug修复)
This commit is contained in:
parent
db44cda413
commit
e7762bbaf1
62
bin/send_modbus_and_register.py
Normal file
62
bin/send_modbus_and_register.py
Normal file
@ -0,0 +1,62 @@
|
||||
import socket
|
||||
import time
|
||||
from binascii import unhexlify
|
||||
|
||||
# 目标服务器地址和端口
|
||||
TARGET_HOST = 'localhost' # 请根据实际情况修改
|
||||
TARGET_PORT = 8888 # 请根据实际情况修改
|
||||
|
||||
# 你的注册指令数据
|
||||
REGISTER_DATA_HEX = "7e80D2DA725711FB7e"
|
||||
|
||||
# 你的Modbus指令数据 01034040fb6f6141bd5555bfc82e00bd7d2000c2b0e1063e90c9983c9df308be91110041d9e3753fedf7df44789610c1d60000bf555555c477900041e1058342e1b9874f50
|
||||
MODBUS_DATA_HEX = "01034040fb6f6141bd5555bfc82e00bd7d2000c2b0e1063e90c9983c9df308be91110041d9e3753fedf7df44789610c1d60000bf555555c477900041e1058342e1b9874f50"
|
||||
|
||||
def main():
|
||||
# 将十六进制字符串转换为字节
|
||||
register_bytes = unhexlify(REGISTER_DATA_HEX)
|
||||
modbus_bytes = unhexlify(MODBUS_DATA_HEX)
|
||||
|
||||
try:
|
||||
# 创建TCP套接字
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||
# 设置连接超时
|
||||
sock.settimeout(5)
|
||||
print(f"正在连接到 {TARGET_HOST}:{TARGET_PORT}...")
|
||||
sock.connect((TARGET_HOST, TARGET_PORT))
|
||||
print("已连接。")
|
||||
|
||||
# 1. 发送注册包
|
||||
print("发送注册包...")
|
||||
sock.sendall(register_bytes)
|
||||
print("注册包已发送。")
|
||||
|
||||
# 2. 等待0.5秒
|
||||
print("等待 0.5 秒...")
|
||||
time.sleep(0.5)
|
||||
|
||||
# 3. 发送Modbus数据包
|
||||
print("发送Modbus数据包...")
|
||||
sock.sendall(modbus_bytes)
|
||||
print("Modbus数据包已发送。")
|
||||
|
||||
# (可选)接收最终响应
|
||||
try:
|
||||
final_response = sock.recv(1024)
|
||||
if final_response:
|
||||
print(f"收到最终响应: {final_response.hex()}")
|
||||
else:
|
||||
print("收到空的最终响应。")
|
||||
except socket.timeout:
|
||||
print("等待最终响应超时。")
|
||||
# 忽略其他接收错误,直接结束
|
||||
|
||||
except ConnectionRefusedError:
|
||||
print(f"错误: 连接被拒绝,请确认服务器在 {TARGET_HOST}:{TARGET_PORT} 上运行。")
|
||||
except ValueError as e:
|
||||
print(f"错误: 无效的十六进制字符串. {e}")
|
||||
except Exception as e:
|
||||
print(f"发生错误: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@ -3,14 +3,19 @@ package com.fastbee.iot.platform.adapter.dcp;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.dtflys.forest.http.ForestRequest;
|
||||
import com.dtflys.forest.http.ForestRequestType;
|
||||
import com.fastbee.common.utils.spring.SpringUtils;
|
||||
import com.fastbee.http.model.HttpClientConfig;
|
||||
import com.fastbee.http.service.HttpClientFactory;
|
||||
import com.fastbee.iot.platform.domain.PlatformDeviceMapping;
|
||||
import com.fastbee.iot.platform.domain.PlatformFlow;
|
||||
import com.fastbee.iot.platform.domain.PlatformInstance;
|
||||
import com.fastbee.iot.platform.domain.PlatformPushLog;
|
||||
import com.fastbee.iot.platform.flow.FlowContext;
|
||||
import com.fastbee.iot.platform.flow.PlatformFlowExecutor;
|
||||
import com.fastbee.iot.platform.mapper.PlatformFlowMapper;
|
||||
import com.fastbee.iot.platform.service.IPlatformInstanceService;
|
||||
import com.fastbee.iot.platform.service.IPlatformPushLogService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -18,6 +23,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@ -38,10 +45,10 @@ public class DcpPlatformAdapter {
|
||||
/** 传感器类型 → 推送接口路径 */
|
||||
private static final Map<String, String> PUSH_PATH = new HashMap<>();
|
||||
static {
|
||||
PUSH_PATH.put("inclino", "/api/app/inclino-data/many");
|
||||
PUSH_PATH.put("stress", "/api/app/stress-data/many");
|
||||
PUSH_PATH.put("hyeto", "/api/app/hyeto-data/many");
|
||||
PUSH_PATH.put("static_level", "/api/app/static-level-data/many");
|
||||
PUSH_PATH.put("inclino", "/api/app/inclino-data/many"); // 倾角
|
||||
PUSH_PATH.put("stress", "/api/app/stress-data/many"); // 轴力
|
||||
PUSH_PATH.put("hyeto", "/api/app/hyeto-data/many"); // 雨量
|
||||
PUSH_PATH.put("static_level", "/api/app/static-level-data/many"); // 静力水准
|
||||
}
|
||||
|
||||
/**
|
||||
@ -136,25 +143,19 @@ public class DcpPlatformAdapter {
|
||||
|
||||
JSONObject dcpData = new JSONObject();
|
||||
dcpData.put("deviceId", mapping.getPlatformDeviceId());
|
||||
// 时间处理 时间戳转 ISO8601
|
||||
Instant now = Instant.now();
|
||||
dcpData.put("epoch", now);
|
||||
|
||||
// 按字段映射转换
|
||||
for (int i = 0; i < fieldMappings.size(); i++) {
|
||||
JSONObject fm = fieldMappings.getJSONObject(i);
|
||||
String identifier = fm.getString("identifier");
|
||||
String platformField = fm.getString("platformField");
|
||||
String transform = fm.getString("transform");
|
||||
//String transform = fm.getString("transform");
|
||||
|
||||
Object value = reportData.get(identifier);
|
||||
if (value == null) continue;
|
||||
|
||||
if ("ISO8601".equals(transform)) {
|
||||
// 时间戳转 ISO8601
|
||||
try {
|
||||
long ts = Long.parseLong(value.toString());
|
||||
value = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
|
||||
.format(new Date(ts));
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
dcpData.put(platformField, value);
|
||||
}
|
||||
|
||||
@ -205,18 +206,16 @@ public class DcpPlatformAdapter {
|
||||
|| fresh.getTokenExpiresAt().getTime() - now.getTime() < 5 * 60 * 1000L;
|
||||
if (!StringUtils.hasText(fresh.getAccessToken()) || stillExpired) {
|
||||
// 执行 flow_type=3 的 Token 刷新流程(不是初始化流程)
|
||||
com.fastbee.iot.platform.mapper.PlatformFlowMapper flowMapper =
|
||||
com.fastbee.common.utils.spring.SpringUtils.getBean(
|
||||
com.fastbee.iot.platform.mapper.PlatformFlowMapper.class);
|
||||
com.fastbee.iot.platform.domain.PlatformFlow tokenFlow = flowMapper.selectOne(
|
||||
com.baomidou.mybatisplus.core.toolkit.Wrappers.<com.fastbee.iot.platform.domain.PlatformFlow>lambdaQuery()
|
||||
.eq(com.fastbee.iot.platform.domain.PlatformFlow::getPlatformId, fresh.getId())
|
||||
.eq(com.fastbee.iot.platform.domain.PlatformFlow::getFlowType, 3)
|
||||
.eq(com.fastbee.iot.platform.domain.PlatformFlow::getEnable, "1")
|
||||
PlatformFlowMapper flowMapper =
|
||||
SpringUtils.getBean(PlatformFlowMapper.class);
|
||||
PlatformFlow tokenFlow = flowMapper.selectOne(
|
||||
Wrappers.<PlatformFlow>lambdaQuery()
|
||||
.eq(PlatformFlow::getPlatformId, fresh.getId())
|
||||
.eq(PlatformFlow::getFlowType, 3)
|
||||
.eq(PlatformFlow::getEnable, "1")
|
||||
.last("LIMIT 1"));
|
||||
if (tokenFlow != null) {
|
||||
com.fastbee.iot.platform.flow.FlowContext ctx =
|
||||
new com.fastbee.iot.platform.flow.FlowContext(fresh);
|
||||
FlowContext ctx = new FlowContext(fresh);
|
||||
flowExecutor.execute(tokenFlow, ctx);
|
||||
fresh = instanceService.getById(platform.getId());
|
||||
} else {
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
package com.fastbee.iot.platform.service.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user