def init(card,aid,pix,deposit): from transport_card import validate,set_deposit,register_contract,clear validate(card) set_deposit(card,deposit) clear(card,[(STATIC,KEY,'dynamic'),(DYNAMIC,KEY,'dynamic')]) static_sector = card.sector(num = STATIC, key = 0, enc = ENC, method = 'full', read = False) term_init(p(static_sector.data),p(TERM_INIT(aid,pix))) static_sector.data.crc16_calc(low_endian = 1) event = EVENT_CONTRACT(card, AID = aid, PIX = pix, TransactionType = 0) try: static_sector.write() static_sector.set_trailer(KEY, mode = 'dynamic') dynamic_sector = card.sector(num = DYNAMIC, key = 0, enc = 0xFF, method = 'full', read = False) dynamic_sector.data.cast(DYNAMIC_A).__init__(TERM_DYNAMIC) dynamic_sector.write() dynamic_sector.set_trailer(KEY, mode = 'dynamic') register_contract(card,STATIC,aid,pix) except Exception as e: event.set_error_code(e); raise finally: event.save(card)
def refill(self,begin,end): ''' Tries to change contents of current TERM_STATIC structure to represent contract refilled to be active during begin-end period. Requires `begin` and `end` parameters to be datetime objects. ''' if DATE(uint16 = self.validity_limit_date).expired(): raise TimeError('This contract has been expired') # if current end_date for contract is greater than proposed end date # then TimeError happened if self.status == self.VALID_STATUS and \ DATE(uint16 = self.validity_end_date).to_datetime() >= end: raise TimeError('There is no need to refill this contract') validity = [ DATE(date = begin).to_int() if self.expired() else self.validity_begin_date, DATE(date = end).to_int() ] #using native function due to bug in ctypes handling 8-byte long bit fields #for now, only changes begin and end dates without meddling with other parameters term_set_validity(p(self),*validity) self.status = self.VALID_STATUS