Core treatment functions.

This commit is contained in:
Mikaël Ates 2013-05-24 17:05:17 +02:00
parent fd0ea39922
commit 5ad3f4b590
1 changed files with 57 additions and 0 deletions

57
core_standard.py Normal file
View File

@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
'''
librfid - RFID EPC Class 1 Gen2 / ISO/IEC 18000-6C compliant library
Copyright (C) 2013 Entr'ouvert
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Functions to parse standard response content.
'''
from _exceptions import MalformedDataError
def inventory(response):
'''
Parse response content of 'Inventory' command.
Return a list of dict describing tags.
'''
tags = list()
if not response.data:
return tags
nb_tags = int(response.data[0].encode('hex'), 16)
end = len(response.data)
index = 1
for _ in range(0, nb_tags):
tag = dict()
if index + 5 <= end:
tag['EPCLen'] = int(response.data[index].encode('hex'), 16)
index += 1
else :
raise MalformedDataError('Data content is malformed.')
if index + 3 + tag['EPCLen'] <= end:
tag['EPC'] = response.data[index:index+tag['EPCLen']]
index += tag['EPCLen']
tag['AntID'] = int(response.data[index].encode('hex'), 16)
index += 1
tag['NbRead'] = int(response.data[index:index+2].encode('hex'), 16)
index += 2
else:
raise MalformedDataError('Data content is malformed.')
tags.append(tag)
return tags