| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import time
- from datetime import datetime
- from common import lora_util
- from common.log_util import MyLog
- from common.read_config import ReadConfig
- from dao import machinery_dao
- my_log = MyLog().my_log
- machinery_all = []
- query_delay = int(ReadConfig().get_constant("query_delay"))
- def load_device():
- global machinery_all
- machinery_all = machinery_dao.query_machinery()
- def lora_db():
- try:
- load_device()
- inter = lora_util.init_port()
- except Exception as e:
- time.sleep(3)
- my_log.error(e)
- lora_db()
- while True:
- try:
- for machinery in machinery_all:
- msg = "0103000A0002E409"
- rs = lora_util.send_msg_back(msg, inter, 1)
- if rs is None:
- my_log.error("通讯失败")
- continue
- res = parser_data(rs)
- machinery_code = machinery['machinery_code']
- machinery_name = machinery['machinery_name']
- today = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
- param = [machinery_code, machinery_name, rs, res, today, today]
- machinery_dao.insert_machinery(param)
- time.sleep(3)
- except Exception as e:
- my_log.error(e)
- time.sleep(query_delay)
- def parser_data(rs):
- res = rs[6:14]
- vls = lora_util.hex_to_float(res.decode())
- vls = round(vls, 2)
- return vls
|