ronds-metadata 1.3.33 → 1.3.34

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
- export declare function transProtobuf2Json(base64: string, protoSchema: string, message: string): any;
2
- export declare function transMqttProbuf2Json(base64: string, variableProtoSchema: string, variableMessage: string, bodyProtoSchema: string, bodyMessage: string): {
1
+ export declare function transProtobuf2Json(pbData: string | Uint8Array, protoSchema: string, message: string): any;
2
+ export declare function transMqttProbuf2Json(base64: string, bodyProtoSchema: string, bodyMessage: string): {
3
3
  formatType: number;
4
4
  compressType: number;
5
5
  sendTime: number;
@@ -4,6 +4,14 @@
4
4
  * @LastEditTime: 2023-11-23 15:48:09
5
5
  */
6
6
  var protobuf = require('protobufjs');
7
+ // Mqtt主题pb解析各字段占用字节
8
+ var cutRuleMap = {
9
+ formatTypeByte: 1,
10
+ compressTypeByte: 1,
11
+ sendTimeByte: 4,
12
+ variableLengthByte: 2,
13
+ contentLengthByte: 4
14
+ };
7
15
  function transBase642byte(base64String) {
8
16
  // 解码 Base64 字符串为原始二进制数据
9
17
  var decodedData = window.atob(base64String);
@@ -14,14 +22,22 @@ function transBase642byte(base64String) {
14
22
  }
15
23
  return uint8Array;
16
24
  }
17
- export function transProtobuf2Json(base64, protoSchema, message) {
25
+ function transUint8Array2Int(uint8Array, start, end) {
26
+ return parseInt(Array.from(uint8Array.slice(start, end)).map(function (byte) {
27
+ return byte.toString(2).padStart(8, '0');
28
+ }).join(''), 2);
29
+ }
30
+ export function transProtobuf2Json(pbData, protoSchema, message) {
18
31
  var root = protobuf.parse(protoSchema, {
19
32
  keepCase: true
20
33
  }).root;
21
34
  // 根据proto文件中的内容对message进行实例化
22
35
  var transferMessage = root.lookupType(message);
23
- var uint8Array = transBase642byte(base64);
24
36
  try {
37
+ var uint8Array = pbData;
38
+ if (typeof pbData === 'string') {
39
+ uint8Array = transBase642byte(pbData);
40
+ }
25
41
  // 解码消息
26
42
  var result = transferMessage.decode(uint8Array);
27
43
  return transferMessage.toObject(result);
@@ -30,58 +46,41 @@ export function transProtobuf2Json(base64, protoSchema, message) {
30
46
  return null;
31
47
  }
32
48
  }
33
- export function transMqttProbuf2Json(base64, variableProtoSchema, variableMessage, bodyProtoSchema, bodyMessage) {
34
- var variableRoot = protobuf.parse(variableProtoSchema, {
35
- keepCase: true
36
- }).root;
37
- var bodyRoot = protobuf.parse(bodyProtoSchema, {
38
- keepCase: true
39
- }).root;
40
- var variableRootTransferMessage = variableRoot.lookupType(variableMessage);
41
- var bodyRootTransferMessage = bodyRoot.lookupType(bodyMessage);
42
- // 解析Mqtt消息
49
+ export function transMqttProbuf2Json(base64, bodyProtoSchema, bodyMessage) {
43
50
  var uint8Array = transBase642byte(base64);
44
51
  try {
45
- // 解码包体格式
46
- var formatValue = uint8Array.slice(0, 1);
47
- var formatType = parseInt(Array.from(formatValue).map(function (byte) {
48
- return byte.toString(2).padStart(8, '0');
49
- }).join(''), 2);
50
- // 解码压缩类型
51
- var compressValue = uint8Array.slice(1, 2);
52
- var compressType = parseInt(Array.from(compressValue).map(function (byte) {
53
- return byte.toString(2).padStart(8, '0');
54
- }).join(''), 2);
55
- // 解码时间戳
56
- var sendTimeValue = uint8Array.slice(2, 6);
57
- var sendTime = parseInt(Array.from(sendTimeValue).map(function (byte) {
58
- return byte.toString(2).padStart(8, '0');
59
- }).join(''), 2);
60
- // 解码可变内容长度、包体内容长度
61
- var variableValue = uint8Array.slice(6, 8);
62
- var bodyValue = uint8Array.slice(8, 12);
63
- var variableLength = parseInt(Array.from(variableValue).map(function (byte) {
64
- return byte.toString(2).padStart(8, '0');
65
- }).join(''), 2);
66
- var bodyLength = parseInt(Array.from(bodyValue).map(function (byte) {
67
- return byte.toString(2).padStart(8, '0');
68
- }).join(''), 2);
52
+ // 解码包体格式、压缩类型、时间戳、可变内容长度、包体长度
53
+ var startByte = 0;
54
+ var formatType = transUint8Array2Int(uint8Array, startByte, startByte + cutRuleMap.formatTypeByte);
55
+ startByte = startByte + cutRuleMap.formatTypeByte;
56
+ var compressType = transUint8Array2Int(uint8Array, startByte, startByte + cutRuleMap.compressTypeByte);
57
+ startByte = startByte + cutRuleMap.compressTypeByte;
58
+ var sendTime = transUint8Array2Int(uint8Array, startByte, startByte + cutRuleMap.sendTimeByte);
59
+ startByte = startByte + cutRuleMap.sendTimeByte;
60
+ var variableLength = transUint8Array2Int(uint8Array, startByte, startByte + cutRuleMap.variableLengthByte);
61
+ startByte = startByte + cutRuleMap.variableLengthByte;
62
+ var contentLength = transUint8Array2Int(uint8Array, startByte, startByte + cutRuleMap.contentLengthByte);
63
+ startByte = startByte + cutRuleMap.contentLengthByte;
69
64
  // 解码可变内容、包体内容
70
- var variableData = uint8Array.slice(12, 12 + variableLength);
71
- var bodyData = uint8Array.slice(12 + variableLength, 12 + variableLength + bodyLength);
72
- var variableContent = variableRootTransferMessage.decode(variableData);
73
- var bodyContent = bodyRootTransferMessage.decode(bodyData);
65
+ var variableData = uint8Array.slice(startByte, startByte + variableLength);
66
+ var data = uint8Array.slice(startByte + variableLength, startByte + variableLength + contentLength);
67
+ var variableContent = transProtobuf2Json(variableData, PROTOSCHEMA, MESSAGE);
68
+ var content = transProtobuf2Json(data, bodyProtoSchema, bodyMessage);
74
69
  return {
75
70
  formatType: formatType,
76
71
  compressType: compressType,
77
72
  sendTime: sendTime,
78
73
  variableLength: variableLength,
79
- contentLength: bodyLength,
74
+ contentLength: contentLength,
80
75
  variableContent: variableContent,
81
- content: bodyContent
76
+ content: content
82
77
  };
83
78
  } catch (err) {
84
79
  console.error('解码失败:', err);
85
80
  return null;
86
81
  }
87
- }
82
+ }
83
+ // MQTT主题可变内容PB
84
+ var PROTOSCHEMA = "syntax = \"proto3\";\npackage com.ronds.iot.handle.proto.app.schema;\noption java_package = \"com.ronds.iot.handle.proto.plc.entity\";\noption java_outer_classname = \"PlcVariableContent\";\nmessage VariableContent\n{\nContentSet TimeStampContentSet = 1; //\u65F6\u95F4\u6807\u8BB0\n}\n//\u53EF\u53D8\u5185\u5BB9\u96C6\u5408\nmessage ContentSet\n{\nrepeated Content Content = 1;\n}\n\n//\u5355\u6761\u53EF\u53D8\u5185\u5BB9\nmessage Content\n{\nrepeated TagInfo TagInfos = 1; //\u6807\u7B7E\u4FE1\u606F\u96C6\u5408\uFF0C\u4E00\u4E2A\u5305\u5934\u4FE1\u606F\uFF0C\u5305\u542B\u4E00\u81F3\u591A\u4E2A\u4E3B\u952E\nuint32 Length = 2; //\u6570\u636E\u5305\u4F53\u957F\u5EA6\n}\nmessage TagInfo\n{\nuint32 Id = 1; //\u6570\u636E\u4E3B\u952E\nuint32 Type = 2; //\u6570\u636E\u7C7B\u578B 0:\u6D4B\u91CF\u5B9A\u4E49 1:\u957F\u6CE2\u5F62 2:\u6307\u6807 3:\u5DE5\u827A 4:\u58F0\u97F3 5:\u56FE\u50CF 6:\u70ED\u50CF 7:\u89C6\u9891 8:\u57FA\u51C6\u56FE\u50CF\nuint32 Time = 3; // \u6570\u636E\u91C7\u96C6\u65F6\u95F4\nrepeated ContentTag Tags = 4; //\u6570\u636E\u65F6\u95F4\u6233\u6807\u7B7E\n}\nmessage ContentTag\n{\nuint32 Key = 1; //\u91C7\u96C6\u7AD9=1 SC\u6807\u51C6\u7248=2 DTS=3 SC\u6807\u51C6\u7248-\u7F51\u95F8\u5916=4 SC\u4E13\u4E1A\u7248=5 \u6570\u636E\u4E2D\u5FC3=9\nuint32 Value = 2; //\u65F6\u95F4\u6233\u6807\u7B7E\u503C\n}";
85
+ // MQTT主题可变内容入口函数
86
+ var MESSAGE = 'VariableContent';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "public": true,
3
3
  "name": "ronds-metadata",
4
- "version": "1.3.33",
4
+ "version": "1.3.34",
5
5
  "scripts": {
6
6
  "start": "dumi dev",
7
7
  "docs:build": "dumi build",