Skip to content

Commit

Permalink
improved Reader
Browse files Browse the repository at this point in the history
added .npmignore
updated usage example script
  • Loading branch information
pokusew committed May 15, 2016
1 parent ed28202 commit 7ee0396
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

!dist/
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,47 @@ A simple wrapper around [santigimeno/node-pcsclite](https://github.com/santigime
```javascript
import NFC from './src/NFC';

const nfc = new NFC();
const nfc = new NFC(logger);

let readers = [];

nfc.on('reader', reader => {

console.log(`NFC (${reader.reader.name}): device attached`);

readers.push(reader);

console.log(readers);

reader.on('card', card => {

// card uid is hex string
console.log('card detected', card.uid);
console.log(`NFC (${reader.reader.name}): card detected`, card.uid);

});

reader.on('error', err => {

console.log(`NFC (${reader.reader.name}): an error occurred`, err);

});

reader.on('end', () => {

console.log(`NFC (${reader.reader.name}): device removed`);

delete readers[readers.indexOf(reader)];

console.log(readers);

});

});

nfc.on('error', err => {

console.log('NFC: an error occurred', err);

});
```

Expand Down
9 changes: 9 additions & 0 deletions src/Reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,24 @@ class Reader extends EventEmitter {

this.logger.info(err);

this.emit('error', err);
return;

}
else {

this.logger.info('Data received', data);

if (data.length !== 9) {
this.emit('error', 'Invalid data.');
return;
}

let error = data.readUInt16BE(7);

if (error !== 0x9000) {
// an error occurred
this.emit('error', 'Error reading UID.');
return;
}

Expand Down
18 changes: 17 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,39 @@ let readers = [];

nfc.on('reader', reader => {

console.log(`NFC (${reader.reader.name}): device attached`);

readers.push(reader);

console.log(readers);

reader.on('card', card => {

// card uid is hex string
console.log('card detected', card.uid);
console.log(`NFC (${reader.reader.name}): card detected`, card.uid);

});

reader.on('error', err => {

console.log(`NFC (${reader.reader.name}): an error occurred`, err);

});

reader.on('end', () => {

console.log(`NFC (${reader.reader.name}): device removed`);

delete readers[readers.indexOf(reader)];

console.log(readers);

});

});

nfc.on('error', err => {

console.log('NFC: an error occurred', err);

});

0 comments on commit 7ee0396

Please sign in to comment.