wxt536_converter.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import re
  2. from converter import Converter
  3. from tools.format_value import format_value
  4. from logging_config import wxt536_converter as logger
  5. class WXT536Converter(Converter):
  6. def __init__(self, name):
  7. self._name = name
  8. def convert(self, config, data):
  9. """
  10. data: b'0R0,Dm=267D,Sm=1.2M,Ta=-25.0C,Ua=87.1P,Pa=1001.9H,Rc=-0.00M,Th=28.3C,Vh=0.0#'
  11. """
  12. logger.info(f"原始接收数据[{self._name}]: {data}")
  13. if data:
  14. dict = {}
  15. try:
  16. list = data.decode().split(",")
  17. logger.info(f"({self._name})解码分割后, 标准长度:9,实际长度:{len(list)}, 内容: {list}, ")
  18. if list[0] == '0R0':
  19. for index in config:
  20. name = 'c' + str(index['serial_number'])
  21. i = int(index['address'])
  22. value = None
  23. if list[i][-1] != "#":
  24. value = re.findall(r"-*\d+\.?\d*", list[i])[0]
  25. dict[name] = format_value(index, value)
  26. logger.info(f"({self._name})解析后数据:{dict}")
  27. return dict
  28. elif len(list) > 0:
  29. return "pass"
  30. else:
  31. return "error"
  32. except Exception as e:
  33. logger.error(f"({self._name}):{repr(e)}")
  34. return "error"