pyztk/zk/finger.py

53 lines
1.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from struct import pack #, unpack
import codecs
2018-12-27 13:57:49 +07:00
class Finger(object):
2018-12-27 13:57:49 +07:00
def __init__(self, uid, fid, valid, template):
self.size = len(template) # template only
2018-07-05 22:52:36 +07:00
self.uid = int(uid)
self.fid = int(fid)
self.valid = int(valid)
self.template = template
#self.mark = str().encode("hex")
2018-05-04 02:58:52 +07:00
self.mark = codecs.encode(template[:8], 'hex') + b'...' + codecs.encode(template[-8:], 'hex')
2018-04-07 07:01:35 +07:00
def repack(self): #full
return pack("HHbb%is" % (self.size), self.size+6, self.uid, self.fid, self.valid, self.template)
def repack_only(self): #only template
return pack("H%is" % (self.size), self.size, self.template)
2018-12-27 13:57:49 +07:00
@staticmethod
def json_unpack(json):
return Finger(
uid=json['uid'],
fid=json['fid'],
valid=json['valid'],
template=codecs.decode(json['template'],'hex')
)
2018-12-27 13:57:49 +07:00
def json_pack(self): #packs for json
return {
"size": self.size,
"uid": self.uid,
"fid": self.fid,
"valid": self.valid,
"template": codecs.encode(self.template, 'hex').decode('ascii')
}
def __eq__(self, other):
2018-05-04 02:58:52 +07:00
return self.__dict__ == other.__dict__
def __str__(self):
2018-05-04 02:58:52 +07:00
return "<Finger> [uid:{:>3}, fid:{}, size:{:>4} v:{} t:{}]".format(self.uid, self.fid, self.size, self.valid, self.mark)
def __repr__(self):
2018-05-04 02:58:52 +07:00
return "<Finger> [uid:{:>3}, fid:{}, size:{:>4} v:{} t:{}]".format(self.uid, self.fid, self.size, self.valid, self.mark)
def dump(self):
return "<Finger> [uid:{:>3}, fid:{}, size:{:>4} v:{} t:{}]".format(self.uid, self.fid, self.size, self.valid, codecs.encode(self.template, 'hex'))
2018-12-27 13:57:49 +07:00