td266_converter.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import json
  2. import re
  3. from logging_config import td266_file_logger as logger
  4. from converter import Converter
  5. class TD266Converter(Converter):
  6. def convert(self, config, data):
  7. # 原始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'
  8. # 去除结尾\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'
  9. # 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
  10. # 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']
  11. try:
  12. data = data.decode('utf-8').split('\t')
  13. logger.info(f"(单点流速仪)原始数据: {data}")
  14. dict = {}
  15. for index in config:
  16. name = 'c' + str(index['serial_number'])
  17. i = int(index['address'])
  18. if index['divisor'] is None:
  19. dict[name] = float(data[i])
  20. else:
  21. dict[name] = round((float(data[i]) / index['divisor']), 2)
  22. logger.info(f"(单点流速仪)解析后数据:{data}")
  23. return dict
  24. except Exception as e:
  25. logger.error(e)
  26. return "error"
  27. """
  28. def convert(self, config, data):
  29. # 原始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'
  30. # 去除结尾\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'
  31. # 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
  32. # 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']
  33. # logger.debug(len(data), time.strftime('%Y-%m-%d %H:%M:%S'), data)
  34. pattern_start = b"4420"
  35. # pattern_end = b"\r\n"
  36. index_start = re.search(pattern_start, data) # 查找字符串得到开始索引
  37. # index_end = re.search(pattern_end, data)
  38. if index_start:
  39. index_start = index_start.span()[0]
  40. # index_end = index_end.span()[1]
  41. data = data[index_start:75] # 只获取流速和流向,大约取25个字符即可
  42. logger.info(data)
  43. data = data.decode('utf-8').split('\t')
  44. logger.info(data)
  45. dict = {}
  46. try:
  47. for index in config:
  48. name = 'c' + str(index['serial_number'])
  49. i = int(index['address'])
  50. if index['divisor'] is None:
  51. dict[name] = float(data[i])
  52. else:
  53. dict[name] = round((float(data[i]) / index['divisor']), 2)
  54. logger.info(dict)
  55. return dict
  56. except Exception as e:
  57. logger.error(e)
  58. return "error"
  59. else:
  60. return 'pass'
  61. """