You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# -*- coding:utf-8 -*-
"""
@Created on : 2023/5/30 8:48
@Author: hxl
@Des:
"""
import asyncio
from itertools import chain
# from conf import setting
from models import DrawerBoard, Drug
from models.terminal import Terminal
async def update_drug_objects(board, drugs, drawer_boards):
"""
更新数据库中药品对象的 hole 字段值
:param board: 当前定位板的 RFID 字典
:param drugs: 当前定位板内所有药品对象的集合
"""
# terminal_obj = await Terminal.get(id=setting.TERMINAL_ID)
# terminal_name = terminal_obj.name
drawer_label = drawer_boards.drawer.label
cabinet_location_label = f"{drawer_boards.cabinet.label}"
result = {v: k for k, v in board.items()} # 反转字典,根据 RFID 查找 hole 编号
for drug in drugs:
hole = result.get(drug.rfid)
if hole:
board_hole = drawer_boards.board.chip_dict.get(str(hole), "") if drawer_boards.board.chip_dict else ""
position_str = f"{cabinet_location_label}-{drawer_label}"
if board_hole:
position_str += f"-{board_hole}"
drug.hole = hole
drug.drawer_board_id = drawer_boards.id
drug.position = position_str
await drug.save(update_fields=['hole', 'drawer_board_id', 'position'])
class InventoryCheck:
"""
将收到的十六进制字符串转换成孔位rfid字典
"""
def __init__(self, receive_data: str):
self.receive_data = receive_data
async def restruct(self):
"""
return: {'A15E1F19': 1, 'C1031119': 2, '811D1619': 3, }key为rfidvalue为position
"""
result = {}
if not self.receive_data:
return result
data_list = self.receive_data.split("7E")
for index, data in enumerate(data_list):
print(data)
print("===")
if data and len(data) > 8:
value = data[-8:]
if value != "00000000":
result.setdefault(value, index)
return result
async def inventory_keys_result(self, cabinet_id: str):
"""
inventory: {'A15E1F19': 1, 'C1031119': 2, '811D1619': 3, }key为rfidvalue为position
"""
inventory = await self.restruct()
# 上报的所有药剂rfid
push_rfid_set = set(rfid for rfid in inventory.keys() if rfid != "00000000")
drugs = await Drug.filter(cabinet_id=cabinet_id, state=1, position__isnull=False,).values('rfid')
# 柜体所有药剂标签
db_rfid_set = {item['rfid'] for item in drugs}
# 放入rfid标签列表
put_in_set = push_rfid_set - db_rfid_set
# 拿出rfid标签列表
take_out_set = db_rfid_set - push_rfid_set
return put_in_set, take_out_set
if __name__ == '__main__':
data = '7E010101120304050607087E010301020304050607987E01090102030405060710'
obj = InventoryCheck(data)
inventory = obj.restruct()
print(inventory)