Skip to content

Commit 8d0f3f5

Browse files
authored
Merge pull request #29 from bieli/add-method-iotext-msg-to-json
add builder method iotext_to_json + update unit tests
2 parents 52c1e5f + dbf01ca commit 8d0f3f5

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/builders/iot_ext_item_data_builder.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import json
22
from decimal import Decimal
3-
from typing import List, Union, Optional
3+
from typing import List, Optional
44

5+
from src.codecs.iot_ext_codec import IoTextCodec
56
from src.codecs.item_codec import ItemCodec
67
from src.codecs.metric_data_item_codec import MetricDataItemCodec
78
from src.tools.crc16 import Tools
@@ -51,8 +52,17 @@ def __str__(self, items_separator=",") -> str:
5152

5253
def to_json(self) -> str:
5354
self.__str__()
55+
return IoTextItemDataBuilder.to_json_from_iotext_struct(self.output)
56+
57+
@staticmethod
58+
def iotext_to_json(iotext_msg) -> str:
59+
iotext_msg_struct = IoTextCodec.decode(iotext_msg)
60+
return IoTextItemDataBuilder.to_json_from_iotext_struct(iotext_msg_struct)
61+
62+
@staticmethod
63+
def to_json_from_iotext_struct(iotext_msg_struct):
5464
data_row = []
55-
for item in self.output:
65+
for item in iotext_msg_struct:
5666
data_item = {
5767
item.kind: item.name,
5868
}

tests/builders/test_iot_ext_item_data_builder.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def test_should_not_convert_from_json_with_wrong_data_type_in_metric(self):
199199
name="v1",
200200
metric=MetricDataItem(
201201
data_type=MetricDataTypes.DECIMALS_LIST,
202-
value="-2+3-1",
202+
value=[-2.0, 3.0, -1.0],
203203
),
204204
),
205205
],
@@ -214,13 +214,13 @@ def test_should_not_convert_from_json_with_wrong_data_type_in_metric(self):
214214
name="v1",
215215
metric=MetricDataItem(
216216
data_type=MetricDataTypes.BOOLS_LIST,
217-
value="10010",
217+
value=[True, False, False, True, False],
218218
),
219219
),
220220
],
221221
),
222222
# (
223-
# '[{"t": "123123123"}, {"d": "D1"}, {"m": "v1", "v": ["a", "b", "c"], "t": "T"}]',
223+
# '[{"t": "123123123"}, {"d": "D1"}, {"m": "v1", "v": "YWJjCg", "t": "T"}]',
224224
# [
225225
# Item(kind=ItemTypes.TIMESTAMP_MILIS, name="123123123", metric=None),
226226
# Item(kind=ItemTypes.DEVICE_ID, name="D1", metric=None),
@@ -229,7 +229,7 @@ def test_should_not_convert_from_json_with_wrong_data_type_in_metric(self):
229229
# name="v1",
230230
# metric=MetricDataItem(
231231
# data_type=MetricDataTypes.TEXTS_LIST,
232-
# value="YWJjCg",
232+
# value=["a", "b", "c"],
233233
# ),
234234
# ),
235235
# ],
@@ -240,4 +240,17 @@ def test_should_convert_from_json_with_data_type_in_metric(
240240
self, iotext_msg_as_json, expected_result
241241
):
242242
result = IoTextItemDataBuilder.from_json(iotext_msg_as_json)
243-
self.assertTrue(result, expected_result)
243+
self.assertEqual(result, expected_result)
244+
245+
def test_should_iotext_to_json(self):
246+
expected_json = """[{"t": "1712829010000"}, {"d": "DEV_NAME_003"}, {"m": "xb", "v": false, "t": "b"}, {"m": "xds", "v": [-547, -542, -553, -540, -542, -522, -539, -522, -549, -539, -542, -556, -539, -555, -529, -561, -548, -567, -532, -554, -540, -521, -568, -541, -542], "t": "I"}, {"m": "battery_lvl", "v": 81, "t": "i"}]"""
247+
248+
iotext_example = (
249+
"t|1712829010000,d|DEV_NAME_003,m|xb=b:0,"
250+
"m|xds=I:-547-542-553-540-542-522-539-522-549-539-542-556-539-555"
251+
"-529-561-548-567-532-554-540-521-568-541-542,m|battery_lvl=i:81"
252+
)
253+
254+
result = IoTextItemDataBuilder.iotext_to_json(iotext_example)
255+
256+
self.assertEqual(result, expected_json)

0 commit comments

Comments
 (0)