wxt536_converter.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from logging_config import wxt536_file_logger as logger
  2. import re
  3. from converter import Converter
  4. class WXT536Converter(Converter):
  5. """
  6. 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#'
  7. """
  8. def convert(self, config, data):
  9. logger.info(f"(气象仪)原始接收数据: {data}")
  10. if data:
  11. dict = {}
  12. try:
  13. list = data.decode().split(",")
  14. logger.info(f"(气象仪)解码分割后, 标准长度:9,实际长度:{len(list)}, 内容: {list}, ")
  15. if list[0] == '0R0':
  16. for index in config:
  17. name = 'c' + str(index['serial_number'])
  18. i = int(index['address'])
  19. value = None
  20. if list[i][-1] != "#":
  21. value = re.findall(r"-*\d+\.?\d*", list[i])[0]
  22. dict[name] = format_value(index, value)
  23. logger.info(f"(气象仪)解析后数据:{dict}")
  24. return dict
  25. elif len(list) > 0:
  26. return "pass"
  27. else:
  28. return "error"
  29. except Exception as e:
  30. logger.error(e)
  31. return "error"
  32. def format_value(index, value):
  33. if value:
  34. value = float(value)
  35. divisor = index['divisor']
  36. offset = index['offset']
  37. low_limit = index['low_limit']
  38. up_limit = index['up_limit']
  39. if divisor:
  40. value /= divisor
  41. if offset:
  42. value -= offset
  43. if low_limit <= value <= up_limit:
  44. return value
  45. else:
  46. return ''
  47. else:
  48. return ''