LudovicRousseau / pyscard

pyscard smartcard library for python
http://pyscard.sourceforge.net/
GNU Lesser General Public License v2.1
383 stars 110 forks source link

CardMonitoring: cards can be seen multiple times #30

Closed lburg closed 7 years ago

lburg commented 7 years ago

Add a newcardonly argument to CardMonitor and CardMonitoringThread so that observers can get notified several times about the same card. Defaults to the current implementation of not notifying several times.

I need to check that a RFID reader keeps working under certain conditions (temperature, magnetic field, etc.), and it's not convenient (if not dangerous) to have a human perform these tasks. Thus the need for continuous notifications to detect if the reader stops working.

Is it something you can see go in the core of pyscard?

Also, I modified some tests but couldn't get them to run. How can I get them to run? I tried simply calling python path/to/testcase_CardMonitor.py but had errors when importing local_config, then I had error importing configcheck when trying to run the test suite. I also tried with the python -m smartcard.test.testcase_CardMonitor syntax but to no avail. I didn't find any doc around running tests either.

Thanks!

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.03%) to 23.947% when pulling a8ffd01a9971ac028f5bc6e200a737a7ac91ddc2 on lburg:lburg/notify_continuous_card_detection into 7776e55b54ce1ac8e261caa71abf22fae792ce4b on LudovicRousseau:master.

LudovicRousseau commented 7 years ago

I am not sure you can easily run a single test. To run the framework test suite use:

cd smartcard/test/framework
python testsuite_framework.py
LudovicRousseau commented 7 years ago

I am not familiar with CardMonitor and CardMonitoringThread. Can you describe what you get now and what you want to get instead?

lburg commented 7 years ago

With the current implementation if I keep a RFID card close to the reader I only get notified once, i.e. notifyObservers is called once, and so is the update(self, observable, actions) function from my custom Observer. What I'd like is for this function to be called several times as long as the reader sees the card.

The CardMonitoringThread class keeps track of the list of cards it's already seen (the cards attribute) and only notifies observers when the card read is not in this list. The newcardonly argument I introduce disables this tracking when set to False (defaults to True), making CardMonitoringThread always think the card is new and notify all observers.

Thanks for the help on running the tests, I'll get them working :)

LudovicRousseau commented 7 years ago

Is the program also notified when the card is "removed"?

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.02%) to 23.956% when pulling 6b262f03bc98be083d9b8c381cdf986b7b75f748 on lburg:lburg/notify_continuous_card_detection into 7776e55b54ce1ac8e261caa71abf22fae792ce4b on LudovicRousseau:master.

lburg commented 7 years ago

Nice catch, I juste fixed that behaviour!

Regarding the tests, whenever I try to run them I get an error :

» python testsuite_framework.py
regenerating local_config.py...
Traceback (most recent call last):
  File "testsuite_framework.py", line 59, in <module>
    configcheck.checklocalconfig()
  File "../configcheck.py", line 62, in checklocalconfig
    expectedATRs.append(getATR(reader))
  File "../configcheck.py", line 38, in getATR
    cc.connect()
  File "/home/lburg/dev/pyscard/smartcard/CardConnectionDecorator.py", line 54, in connect
    self.component.connect(protocol, mode, disposition)
  File "/home/lburg/dev/pyscard/smartcard/pcsc/PCSCCardConnection.py", line 128, in connect
    SCardGetErrorMessage(hresult))
smartcard.Exceptions.CardConnectionException: Unable to connect with protocol: T0 or T1. Card is unpowered.

The reader is powered on and the pcsc service is running, do I need some kind of smartcard to be able to run tests?

LudovicRousseau commented 7 years ago

Maybe you should debug your code and run the test suite using a normal (contact) reader.

Note that I will not accept your first patch with the newcardonly parameter. A card event should be notified once. Otherwise it is not an event.

lburg commented 7 years ago

Alright, I can understand that. We'll replicate this behaviour from our software then, I'm closing this PR :)

Thank you for your help!