td266_converter.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from tools.format_value import format_value
  2. from logging_config import td266_converter as logger
  3. from converter import Converter
  4. class TD266Converter(Converter):
  5. def __init__(self, name):
  6. self._name = name
  7. def convert(self, config, data):
  8. # 原始data: data = b'4420\t1194\t29.823\t104.507\t-7.471\t28.872\t253.153\t9.369\t1.816\t91.491\t-59.593\t100\t9.542\t9.589\t0.015\r\n'
  9. # 去除结尾\r\n: data = b'4420\t1194\t29.823\t104.507\t-7.471\t28.872\t253.153\t9.369\t1.816\t91.491\t-59.593\t100\t9.542\t9.589\t0.015'
  10. # decode('utf-8'): data = 4420 1194 29.823 104.507 -7.471 28.872 253.153 9.369 1.816 91.491 -59.593 100 9.542 9.589 0.015
  11. # split('\t'): data = ['4420', '1194', '29.823', '104.507', '-7.471', '28.872', '253.153', '9.369', '1.816', '91.491', '-59.593', '100', '9.542', '9.589', '0.015']
  12. try:
  13. logger.info(f"[{self._name}]原始接收数据: len: {len(data)}, values: {data}")
  14. data = data[:90].decode('utf-8').split('\t')
  15. logger.info(f"[{self._name}]解码分割: len: {len(data)}, values: {data}")
  16. dict = {}
  17. for index in config:
  18. name = 'c' + str(index['serial_number'])
  19. i = int(index['address'])
  20. dict[name] = format_value(index, data[i])
  21. logger.info(f"[{self._name}]返回数据: len: {len(dict)}, values: {dict}")
  22. return dict
  23. except Exception as e:
  24. logger.error(e)
  25. return "error"
  26. """
  27. def convert(self, config, data):
  28. # 原始data: data = b'4420\t1194\t29.823\t104.507\t-7.471\t28.872\t253.153\t9.369\t1.816\t91.491\t-59.593\t100\t9.542\t9.589\t0.015\r\n'
  29. # 去除结尾\r\n: data = b'4420\t1194\t29.823\t104.507\t-7.471\t28.872\t253.153\t9.369\t1.816\t91.491\t-59.593\t100\t9.542\t9.589\t0.015'
  30. # decode('utf-8'): data = 4420 1194 29.823 104.507 -7.471 28.872 253.153 9.369 1.816 91.491 -59.593 100 9.542 9.589 0.015
  31. # split('\t'): data = ['4420', '1194', '29.823', '104.507', '-7.471', '28.872', '253.153', '9.369', '1.816', '91.491', '-59.593', '100', '9.542', '9.589', '0.015']
  32. # logger.debug(len(data), time.strftime('%Y-%m-%d %H:%M:%S'), data)
  33. pattern_start = b"4420"
  34. # pattern_end = b"\r\n"
  35. index_start = re.search(pattern_start, data) # 查找字符串得到开始索引
  36. # index_end = re.search(pattern_end, data)
  37. if index_start:
  38. index_start = index_start.span()[0]
  39. # index_end = index_end.span()[1]
  40. data = data[index_start:75] # 只获取流速和流向,大约取25个字符即可
  41. logger.info(data)
  42. data = data.decode('utf-8').split('\t')
  43. logger.info(data)
  44. dict = {}
  45. try:
  46. for index in config:
  47. name = 'c' + str(index['serial_number'])
  48. i = int(index['address'])
  49. if index['divisor'] is None:
  50. dict[name] = float(data[i])
  51. else:
  52. dict[name] = round((float(data[i]) / index['divisor']), 2)
  53. logger.info(dict)
  54. return dict
  55. except Exception as e:
  56. logger.error(e)
  57. return "error"
  58. else:
  59. return 'pass'
  60. """