def _update_db_data(self, package): eq_id = package['equipment_id'] sensor_addr = package['data']['addr_num'] total_weight = package['data']['total'] grid = Grid.by_eqid_sensor(eq_id=eq_id, sensor_addr=sensor_addr) if grid: grid.total = total_weight grid.save() print('\033[1;34m', 'update Gravity--(%s, %d)' % (sensor_addr, total_weight), '\033[0m')
def _get_list_need_borrow(self): """ 查询待借格子对应的显示模块 :return: {(ip, port, add_num), } """ goods = Toolkit.by_user(self.user_id) grids = {good[1].grid_id for good in goods} lcd_modules = Grid.by_id_list(id_list=grids) lcd_addr_list = {(lcd.led_id, lcd.led_addr) for lcd in lcd_modules} lcd_list = list() for lcd in lcd_addr_list: if lcd[0] is not None and lcd[1] is not None: indicator = Indicator.by_id(lcd[0]) if indicator: lcd_list.append((indicator.ip, indicator.port, lcd[1])) return lcd_list
def _get_list_need_return(self): """ 查询待还格子对应的显示模块 :return: {(ip, port, add_num), } """ history = History_inbound_outbound.by_user_need_return(self.user_id) grids = {h.grid_id for h in history} lcd_modules = Grid.by_id_list(id_list=grids) lcd_addr_list = {(lcd.led_id, lcd.led_addr) for lcd in lcd_modules} lcd_list = list() for lcd in lcd_addr_list: if lcd[0] is not None and lcd[1] is not None: indicator = Indicator.by_id(lcd[0]) if indicator: lcd_list.append((indicator.ip, indicator.port, lcd[1])) return lcd_list
def _get_gravity_grid(self, eq_id, sensor_addr): grid = Grid.by_eqid_sensor(eq_id=eq_id, sensor_addr=sensor_addr) return grid
def _save_rfid_data(self, rfid_history: list): """ 1、逐个判断借出或者归还; 2、借出:若耗材则直接保存已还,否则直接保存未还并设置goods出库; 3、归还:是否在未还列表,是则修改已还并设置goods入库,否则新增记录设置错误用户已还并设置goods入库; :param history: :return: """ try: # print('rfid history--', history) current_dt = datetime.datetime.now() # 先把self.rfid_goods中的key从bytes转为str; rfid_current = {k.hex(): v for k, v in self.rfid_goods.items()} goods_registered = Goods.by_epc_list(epcs=list(rfid_current.keys())) goods_epc_db = {g.epc: g for g in goods_registered} epc_grid_history = {h.epc: h for h in rfid_history} if rfid_history is not None else {} for epc, v in rfid_current.items(): grid_current = Grid.by_eqid_antenna(eq_id=v[0], antenna_num=v[1].hex(), addr_num=v[3].hex()) grid_id_current = grid_current.id if grid_current is not None else None if epc in goods_epc_db.keys(): # 存在于DB的EPC if v[2] is True: # 为归还 if epc in epc_grid_history.keys(): # 该EPC在未还列表 goods_grid_id = goods_epc_db[epc].grid_id wrong_place_gid = grid_id_current if grid_id_current != goods_grid_id else None status = 3 if wrong_place_gid else 0 record = epc_grid_history[epc] if record: record.update('status', status) record.update('inbound_datetime', current_dt) record.update('wrong_place_gid', wrong_place_gid) else: if self.user_role == 1 or self.user_role == 2: # 管理员新增物资 record = History_inbound_outbound(user_id=self.user_id, grid_id=grid_id_current, epc=epc, count=1, inbound_datetime=current_dt, status=5) record.save() mylogger.info('EPC(%s) was increased by administrator--%s' % (epc, self.user_code)) else: # 普通用户代还 record = History_inbound_outbound(user_id=self.user_id, grid_id=grid_id_current, epc=epc, count=1, inbound_datetime=current_dt, status=4) record.save() mylogger.info('EPC(%s) was returned by wrong user--%s' % (epc, self.user_code)) else: # 为借出 goods = goods_epc_db[epc] status = 0 if goods.type == 2 else 1 record = History_inbound_outbound(user_id=self.user_id, grid_id=grid_id_current, epc=epc, count=1, outbound_datetime=current_dt, status=status) record.save() goods.update('is_in_store', 0) else: # 不存在于DB的EPC record = History_inbound_outbound(user_id=self.user_id, grid_id=grid_id_current, epc=epc, count=1, inbound_datetime=current_dt, status=6, wrong_place_gid=grid_id_current) record.save() mylogger.warning('EPC(%s) was not registered but token in by user--%s' % (epc, self.user_code)) except Exception as e: mylogger.warning(e)
def _save_rfid_data(self, history: list): """ 1、逐个判断借出或者归还; 2、借出:若耗材则直接保存已还,否则直接保存未还并设置goods出库; 3、归还:是否在未还列表,是则修改已还并设置goods入库,否则新增记录设置错误用户已还并设置goods入库; :param history: :return: """ try: # print('rfid history--', history) current_dt = datetime.datetime.now() # 先把self.rfid_goods中的key从bytes转为str; rfid_current = {k.hex(): v for k, v in self.rfid_goods.items()} goods_db = Goods.by_epc_list(epcs=rfid_current.keys()) goods_epc_db = [g.epc for g in goods_db] epc_grid_id = {h.epc: h.grid_id for h in history} if history is not None else {} for epc, v in rfid_current.items(): print('epc--', epc) print('goods_epc_db--', goods_epc_db) grid_current = Grid.by_eqid_antenna(eq_id=v[0], antenna_num=v[1].hex(), addr_num=v[3].hex()) grid_id_current = grid_current.id if grid_current is not None else None if epc in goods_epc_db: # 存在于DB的EPC if v[2] is True: # 为归还 wrong_place_gid = grid_current if grid_id_current != epc_grid_id[ epc] else None record = History_inbound_outbound.by_epc_need_return( epc=epc) if record: wrong_return_user = self.user_id if self.user_id != record.user_id else None record.update('status', 0) record.update('inbound_datetime', current_dt) record.update('wrong_place_gid', wrong_place_gid) record.update('wrong_return_user', wrong_return_user) goods = [g for g in goods_db if g.epc == epc] if goods: goods[0].update('is_in_store', 1) else: record = History_inbound_outbound( user_id=self.user_id, grid_id=grid_id_current, epc=epc, count=1, inbound_datetime=current_dt, status=0, wrong_place_gid=grid_id_current, wrong_return_uid=self.user_id) record.save() mylogger.warning( 'get no history by_epc_need_return() EPC(%s) but still was returned' % epc) else: # 为借出 record = History_inbound_outbound( user_id=self.user_id, grid_id=grid_id_current, epc=epc, count=1, outbound_datetime=current_dt, status=1) record.save() goods = [g for g in goods_db if g.epc == epc] if goods: goods[0].update('is_in_store', 0) else: # 不存在于DB的EPC record = History_inbound_outbound( user_id=self.user_id, grid_id=grid_id_current, epc=epc, count=1, inbound_datetime=current_dt, status=0, wrong_place_gid=grid_id_current) record.save() mylogger.warning( 'get no goods in database by EPC(%s) but still was returned' % epc) except Exception as e: mylogger.warning(e)
def _modify_db_eq_status(self, eq_type: str, addr: tuple, is_online: bool): """ 设备的类型包括:['entrance_zk', 'code_scane', 'channel_machine', 'led', 'gravity', 'rfid2000', 'rfid2000fh'] :param eq_type: :return: """ try: if eq_type == 'entrance_zk' or eq_type == 'entrance_hk': entrance = Entrance.by_addr(ip=addr[0], port=addr[1]) if entrance is not None: entrance.update('status', int(is_online)) if not is_online: cur_dt = str(datetime.datetime.now()) entrance.update('last_offline_time', cur_dt) else: mylogger.warning( 'Not found object(%s,%d) from DB-entrance while updating' % addr) elif eq_type in ['gravity', 'rfid2000', 'rfid2000fh']: collector = Collector.by_addr(ip=addr[0], port=addr[1]) if collector is not None: collector.update('status', int(is_online)) # shelf = collector.shelf # map(lambda grid: grid.update('status', int(is_online)), shelf.grids) if not is_online: cur_dt = str(datetime.datetime.now()) collector.update('last_offline_time', cur_dt) grids = Grid.by_collector_id(collector_id=collector.id) if grids: for grid in grids: grid.update('status', int(is_online)) else: mylogger.warning( 'Not found object(%s,%d) from DB-collector while updating' % addr) elif eq_type == 'led': indicator = Indicator.by_addr(ip=addr[0], port=addr[1]) if indicator is not None: indicator.update('status', int(is_online)) if not is_online: cur_dt = str(datetime.datetime.now()) indicator.update('last_offline_time', cur_dt) else: mylogger.warning( 'Not found object(%s,%d) from DB-indicator while updating' % addr) elif eq_type == 'channel_machine': cm = ChannelMachine.by_addr(ip=addr[0], port=addr[1]) if cm is not None: cm.update('status', int(is_online)) if not is_online: cur_dt = str(datetime.datetime.now()) cm.update('last_offline_time', cur_dt) else: mylogger.warning( 'Not found object(%s,%d) from DB-indicator while updating' % addr) else: pass print('\033[1;33m', str(addr), 'is online' if is_online else 'is offline', '\033[0m') except Exception as e: print('_modify_db_eq_status', e)