def reset_card(self,card): if not card.simulation_mode : to_reset = card.reset_card() if card.RESET_RESPONSE_SIZE : cicles = 0 self.write(to_reset) time_before = time.time() while self.serial.inWaiting() <> card.RESET_RESPONSE_SIZE and (time.time() - time_before) < 3 : cicles += 1 if self.serial.inWaiting() <> card.RESET_RESPONSE_SIZE : e = CardTimeoutException( "Se produjo un timeout al resetear la targeta %s cicles %d" % (card.get_id(),cicles) ) e.operation = CardException.RESET_OPERATION #completar e.card_id = card.get_id() raise e else: response = self.read(self.serial.inWaiting()) card.reset_card(response) else: self.logger_instance.debug("La placa %s tiene excribe %s para el reset" % (card.card_id, to_reset.encode('hex'))) self.write(to_reset) time.sleep(card.RESET_WAIT_TIME) self.read(self.serial.inWaiting())
def test_card_speed(self, card_id, mode='read'): self.logger_instance.debug("entro en test_card_speed con card_id %s" % card_id ) card = self.cards[card_id] self.reset_card(card) to_send = card.test_speed(None,mode) if not card.simulation_mode : self.logger_instance.debug("en test_card_speed escribo %s " % to_send.encode('hex')) self.reset() cicles = 0 time_before = time.time() self.write(to_send) card_response_size = card.READ_RESPONSE_SIZE if mode == 'read' else card.WRITE_RESPONSE_SIZE #pongo como maximo tiempo de respuesta para el test 2 segundos while self.serial.inWaiting() <> card_response_size and (time.time() - time_before) < 2 : cicles += 1 if self.serial.inWaiting() <> card_response_size : e = CardTimeoutException( "Se produjo un timeout al testear la targeta %s, mode = %s cicles %d" % (card.get_id(),mode,cicles) ) e.card_id = card.get_id() raise e else: response = self.read(self.serial.inWaiting()) try : card.test_speed(response,mode) except DomoticaException, e: e.message = "Se recibieron datos incorrectos durante un\ test_card_speed de la placa %d, mode = %s, (%s)" % ( card.get_id(),mode,e.message) raise e except Exception, e: d_e = DomoticaException("Se produjo un error durante el test card\ speed de la placa %d, mode = %s,(%s)" % ( card.get_id(),mode,e.message)) raise d_e new_cote = cicles + int(cicles * 0.30) if not card.timeout_cote or card.timeout_cote < new_cote: card.timeout_cote = new_cote self.logger_instance.debug("La placa %s tiene timeout_cote = %d " % (card_id, card.timeout_cote))
def write_card(self,card): to_send = card.write_card() if card.simulation_mode or self.simulation_mode : r = random.random() if r < self.timeout_probability : e = CardTimeoutException( "Se produjo un timeout al escribir la targeta %s random(%f<%f)" % (card.get_id(),r,self.timeout_probability) ) e.operation = CardException.WRITE_OPERATION e.card_id = card.get_id() raise e write_response = True else: self.write(to_send) # cicles = 0 # while self.serial.inWaiting() <> card.WRITE_RESPONSE_SIZE and cicles < card.timeout_cote : # cicles += 1 send_time = time.time() while (time.time() - send_time) < 2 and self.serial.inWaiting() <> card.WRITE_RESPONSE_SIZE : pass inWaiting = self.serial.inWaiting() write_response = self.read(inWaiting) if inWaiting <> card.WRITE_RESPONSE_SIZE : e = CardTimeoutException( "Se produjo un timeout al escribir la targeta %s,\ response ( %s ) " % ( card.get_id(), write_response.encode('hex') ) ) e.operation = CardException.WRITE_OPERATION e.card_id = card.get_id() raise e card.write_card(write_response)
e.message = "Se recibieron datos incorrectos durante un\ test_card_speed de la placa %d, mode = %s, (%s)" % ( card.get_id(),mode,e.message) raise e except Exception, e: d_e = DomoticaException("Se produjo un error durante el test card\ speed de la placa %d, mode = %s,(%s)" % ( card.get_id(),mode,e.message)) raise d_e new_cote = cicles + int(cicles * 0.30) if not card.timeout_cote or card.timeout_cote < new_cote: card.timeout_cote = new_cote self.logger_instance.debug("La placa %s tiene timeout_cote = %d " % (card_id, card.timeout_cote)) else: if random.random() < self.timeout_probability : e = CardTimeoutException( "Se produjo un timeout al testear la targeta %s " % card.get_id() ) e.operation = CardException.TEST_OPERATION e.card_id = card.get_id() raise e count = random.randint(2500,3700) if card.timeout_cote < count : card.timeout_cote = count def work(self): self.read_cards() self.write_cards() def reset(self): self.serial.flushOutput() self.serial.flushInput()