fix: 阿里透传-modbus组件

V0.5.x
jay 2023-07-24 16:55:01 +08:00
parent 8df36940b6
commit 0d1da6d928
6 changed files with 22 additions and 43 deletions

View File

@ -127,7 +127,7 @@ function register(payload) {
var pwd = md5(product.getProductSecret() + auth.clientid); var pwd = md5(product.getProductSecret() + auth.clientid);
if (pwd.toLocaleLowerCase() != auth.password.toLocaleLowerCase()) { if (pwd.toLocaleLowerCase() != auth.password.toLocaleLowerCase()) {
throw new Error("incorrect password" + "pwd->" + pwd + " productSecret->" + product.productSecret); throw new Error("incorrect password" + "pwd->" + pwd + " productSecret->" + product.getProductSecret());
} }
return { return {
type: "register", type: "register",
@ -271,7 +271,6 @@ this.onReceive = function (head, type, payload) {
} }
var pk = arr[2]; var pk = arr[2];
var dn = arr[3]; var dn = arr[3];
payload = JSON.parse(payload);
//子设备注册 //子设备注册
if (topic.endsWith('/register')) { if (topic.endsWith('/register')) {
@ -283,12 +282,12 @@ this.onReceive = function (head, type, payload) {
{ {
productKey: pk, productKey: pk,
deviceName: dn, deviceName: dn,
mid: payload.id, // mid: payload.id,
content: { content: {
topic: topic.replace("/s/", "/c/") + "_reply", topic: topic.replace("/s/", "/c/") + "_reply",
payload: JSON.stringify({ payload: JSON.stringify({
id: payload.id, // id: payload.id,
method: payload.method + "_reply", method: "thing.event.property.post",
code: 0, code: 0,
}) })
} }
@ -308,7 +307,7 @@ this.onReceive = function (head, type, payload) {
data: { data: {
productKey: pk, productKey: pk,
deviceName: dn, deviceName: dn,
mid: payload.id, mid: "3",
content: { content: {
topic: topic, topic: topic,
payload: payload payload: payload

View File

@ -1,5 +1,5 @@
var mid = 1; var mid = 1;
var COMMAD_UNKOWN = 0xff; //未知的命令
function getMid() { function getMid() {
mid++; mid++;
if (mid > 10000) { if (mid > 10000) {
@ -11,8 +11,8 @@ function getMid() {
this.decode = function (msg) { this.decode = function (msg) {
var content = msg.content; var content = msg.content;
var topic = content.topic; var topic = content.topic;
var bytes = arrayGroup(content.payload.params,2); var bytes = arrayGroup(content.payload,2);
var byteData=content.payload.params; var byteData=content.payload;
if (topic.endsWith("/thing/model/up_raw")) { if (topic.endsWith("/thing/model/up_raw")) {
var data = arrayGroup(byteData, 2); var data = arrayGroup(byteData, 2);
var params = {}; var params = {};
@ -149,16 +149,6 @@ this.decode = function (msg) {
time: new Date().getTime(), //时间戳,消息上报时间 time: new Date().getTime(), //时间戳,消息上报时间
data: params, data: params,
}; };
} else if (topic.indexOf("/event/") > 0) {
//事件上报
} else if (topic.endsWith("/service/property/set_reply")) {
//属性设置回复
} else if (topic.endsWith("/config/set_reply")) {
//设备配置设置回复
} else if (topic.endsWith("/config/get")) {
//设备配置获取
} else if (topic.endsWith("_reply")) {
//服务回复
} }
return null; return null;
}; };
@ -294,12 +284,12 @@ this.encode = function (service, device) {
// payloadArray = payloadArray.concat(buffer_float32(prop_float)); //属性'prop_float'的值。 // payloadArray = payloadArray.concat(buffer_float32(prop_float)); //属性'prop_float'的值。
}else if (method == 'thing.event.property.post') { //设备上报数据返回结果,如果不需要回复,可以去除该内容 }else if (method == 'thing.event.property.post') { //设备上报数据返回结果,如果不需要回复,可以去除该内容
var code = json['code']; // var code = json['code'];
payloadArray = payloadArray.concat(buffer_uint8(COMMAND_REPORT_REPLY)); //command字段 // payloadArray = payloadArray.concat(buffer_uint8(COMMAND_REPORT_REPLY)); //command字段
payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // ALink JSON格式 'id' // payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // ALink JSON格式 'id'
payloadArray = payloadArray.concat(buffer_uint8(code)); // payloadArray = payloadArray.concat(buffer_uint8(code));
} else { //未知命令,对于有些命令不做处理 } else { //未知命令,对于有些命令不做处理
var code = json['code']; var code = "FF";
payloadArray = payloadArray.concat(buffer_uint8(COMMAD_UNKOWN)); //command字段 payloadArray = payloadArray.concat(buffer_uint8(COMMAD_UNKOWN)); //command字段
payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // ALink JSON格式 'id' payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // ALink JSON格式 'id'
payloadArray = payloadArray.concat(buffer_uint8(code)); payloadArray = payloadArray.concat(buffer_uint8(code));
@ -313,7 +303,7 @@ this.encode = function (service, device) {
payload: JSON.stringify({ payload: JSON.stringify({
id: deviceMid, id: deviceMid,
method: method += "property." + identifier, method: method += "property." + identifier,
params: payloadArray params: ab2hex(payloadArray).toUpperCase()
}) })
} }
} }

View File

@ -23,6 +23,7 @@ import io.vertx.core.net.PemKeyCertOptions;
import io.vertx.mqtt.*; import io.vertx.mqtt.*;
import io.vertx.mqtt.messages.codes.MqttSubAckReasonCode; import io.vertx.mqtt.messages.codes.MqttSubAckReasonCode;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -148,7 +149,7 @@ public class NBVerticle extends AbstractVerticle {
// ack the subscriptions request // ack the subscriptions request
endpoint.unsubscribeAcknowledge(unsubscribe.messageId()); endpoint.unsubscribeAcknowledge(unsubscribe.messageId());
}).publishHandler(message -> { }).publishHandler(message -> {
String payload = message.payload().toString(Charset.defaultCharset()); String payload = Hex.encodeHexString(message.payload().getBytes());
log.info("Received message:{}, with QoS {}", payload, log.info("Received message:{}, with QoS {}", payload,
message.qosLevel()); message.qosLevel());
if (StringUtils.isBlank(payload)) { if (StringUtils.isBlank(payload)) {

View File

@ -271,7 +271,6 @@ this.onReceive = function (head, type, payload) {
} }
var pk = arr[2]; var pk = arr[2];
var dn = arr[3]; var dn = arr[3];
payload = JSON.parse(payload);
//子设备注册 //子设备注册
if (topic.endsWith('/register')) { if (topic.endsWith('/register')) {
@ -283,12 +282,12 @@ this.onReceive = function (head, type, payload) {
{ {
productKey: pk, productKey: pk,
deviceName: dn, deviceName: dn,
mid: payload.id, // mid: payload.id,
content: { content: {
topic: topic.replace("/s/", "/c/") + "_reply", topic: topic.replace("/s/", "/c/") + "_reply",
payload: JSON.stringify({ payload: JSON.stringify({
id: payload.id, // id: payload.id,
method: payload.method + "_reply", method: "thing.event.property.post",
code: 0, code: 0,
}) })
} }
@ -308,7 +307,7 @@ this.onReceive = function (head, type, payload) {
data: { data: {
productKey: pk, productKey: pk,
deviceName: dn, deviceName: dn,
mid: payload.id, mid: "3",
content: { content: {
topic: topic, topic: topic,
payload: payload payload: payload

View File

@ -11,8 +11,8 @@ function getMid() {
this.decode = function (msg) { this.decode = function (msg) {
var content = msg.content; var content = msg.content;
var topic = content.topic; var topic = content.topic;
var bytes = arrayGroup(content.payload.params,2); var bytes = arrayGroup(content.payload,2);
var byteData=content.payload.params; var byteData=content.payload;
if (topic.endsWith("/thing/model/up_raw")) { if (topic.endsWith("/thing/model/up_raw")) {
var data = arrayGroup(byteData, 2); var data = arrayGroup(byteData, 2);
var params = {}; var params = {};
@ -149,16 +149,6 @@ this.decode = function (msg) {
time: new Date().getTime(), //时间戳,消息上报时间 time: new Date().getTime(), //时间戳,消息上报时间
data: params, data: params,
}; };
} else if (topic.indexOf("/event/") > 0) {
//事件上报
} else if (topic.endsWith("/service/property/set_reply")) {
//属性设置回复
} else if (topic.endsWith("/config/set_reply")) {
//设备配置设置回复
} else if (topic.endsWith("/config/get")) {
//设备配置获取
} else if (topic.endsWith("_reply")) {
//服务回复
} }
return null; return null;
}; };