def write(): status = False text = request.json.get('text') tag_id = None tag_text = None if text is not None: status = True rfid = RFID() tag_id, tag_text = rfid.write(text=text) return { 'id': str(tag_id), 'text': tag_text.strip() if tag_text else tag_text, 'status': status }
def write(self, data): """ Write a 16-byte string of data to the tag """ if len(data) != 16: logger.debug(f"Illegal data length, expected 16, got {len(data)}") return False with self.mutex: rdr = RFID() success = False # check for presence of tag err, _ = rdr.request() if not err: logger.debug("RFIDHandler write: Tag is present") # tag is present, get UID err, uid = rdr.anticoll() if not err: logger.debug("RFIDHandler write: Read UID: " + str(uid)) # write data: RFID lib writes 16 bytes at a time, but for NTAG213 # only the first four are actually written err = False for i in range(4): page = self.page + i page_data = [ord(c) for c in data[4 * i:4 * i + 4]] + [0] * 12 # read data once (necessary for successful writing?) err_read, _ = rdr.read(page) if err: logger.debug( "Error signaled on reading page {:d} before writing" .format(page)) # write data err |= rdr.write(page, page_data) if err: logger.debug( f'Error signaled on writing page {page:d} with data {page_data:s}' ) if not err: logger.debug( "RFIDHandler write: successfully wrote tag data") success = True else: logger.debug( "RFIDHandler write: Error returned from write()") else: logger.debug( "RFIDHandler write: Error returned from anticoll()") # clean up rdr.cleanup() return success