machinery_service.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import time
  2. from datetime import datetime
  3. from common import lora_util
  4. from common.log_util import MyLog
  5. from common.read_config import ReadConfig
  6. from dao import machinery_dao
  7. my_log = MyLog().my_log
  8. machinery_all = []
  9. query_delay = int(ReadConfig().get_constant("query_delay"))
  10. def load_device():
  11. global machinery_all
  12. machinery_all = machinery_dao.query_machinery()
  13. def lora_db():
  14. try:
  15. load_device()
  16. inter = lora_util.init_port()
  17. except Exception as e:
  18. time.sleep(3)
  19. my_log.error(e)
  20. lora_db()
  21. while True:
  22. try:
  23. for machinery in machinery_all:
  24. msg = "0103000A0002E409"
  25. rs = lora_util.send_msg_back(msg, inter, 1)
  26. if rs is None:
  27. my_log.error("通讯失败")
  28. continue
  29. res = parser_data(rs)
  30. machinery_code = machinery['machinery_code']
  31. machinery_name = machinery['machinery_name']
  32. today = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  33. param = [machinery_code, machinery_name, rs, res, today, today]
  34. machinery_dao.insert_machinery(param)
  35. time.sleep(3)
  36. except Exception as e:
  37. my_log.error(e)
  38. time.sleep(query_delay)
  39. def parser_data(rs):
  40. res = rs[6:14]
  41. vls = lora_util.hex_to_float(res.decode())
  42. vls = round(vls, 2)
  43. return vls