node-nfcpy-id
Advanced tools
Comparing version 0.0.3 to 0.0.4
45
app2.js
'use strict'; | ||
const NfcpyId = require('./index'); | ||
const sleep = require('sleep'); | ||
const nfc = new NfcpyId().start(); | ||
const nfc = new NfcpyId({mode: 'loop'}).start(); | ||
nfc.on('touchstart', (card) => { | ||
console.log('Card ID: ' + card.id); | ||
// card.type is the same value as that of nfcpy. | ||
// 2: Mifare | ||
// 3: FeliCa | ||
// 4: Mifare (DESFire) | ||
console.log('Card Type: ' + card.type); | ||
@@ -18,14 +12,6 @@ }); | ||
console.log('Card was away.'); | ||
// Python process will be killed. | ||
// nfc.stop(); | ||
// Python process will be killed at the same time. | ||
// process.exit(); | ||
}); | ||
nfc.on('error', (err) => { | ||
console.error('\u001b[34m'); | ||
console.error(err); | ||
console.error('\u001b[0m'); | ||
console.error('\u001b[34m', err, '\u001b[0m'); | ||
}); | ||
@@ -39,3 +25,3 @@ | ||
if (data.indexOf('stop') > -1) { | ||
nfc.stop(); | ||
nfc.pause(); | ||
} | ||
@@ -46,15 +32,18 @@ // console.log(nfc.isRunning); | ||
} | ||
if (data.indexOf('test') > -1) { | ||
console.log(nfc.pyshell.stdout.listenerCount('data')); | ||
if (data.indexOf('child') > -1) { | ||
nfc.pyshell.childProcess.kill('SIGCHLD'); | ||
} | ||
if (data.indexOf('int') > -1) { | ||
nfc.pyshell.childProcess.kill('SIGINT'); | ||
} | ||
}); | ||
let i = 0; | ||
while(1) { | ||
sleep.usleep(100 * 1000); | ||
nfc.start(); | ||
sleep.usleep(100 * 1000); | ||
nfc.stop(); | ||
i++; | ||
console.log(i); | ||
} | ||
// let i = 0; | ||
// while(1) { | ||
// sleep.usleep(100 * 1000); | ||
// nfc.start(); | ||
// sleep.usleep(100 * 1000); | ||
// nfc.pause(); | ||
// i++; | ||
// console.log(i); | ||
// } |
81
index.js
@@ -9,10 +9,18 @@ 'use strict'; | ||
super(); | ||
this.options = options; | ||
this._options = options; | ||
this._mode = 'mode' in (options || {}) ? options.mode : 'loop'; | ||
this._running = false; | ||
this._exiting = false; | ||
this._firstLaunch = false; | ||
['SIGHUP', 'SIGINT', 'exit'].forEach((event) => { | ||
process.on(event, () => { | ||
this.stop(); | ||
}); | ||
}); | ||
if (this._mode === 'non-loop') { | ||
this._loop = false; | ||
this._non_touchend = false; | ||
} else if (this._mode === 'non-touchend') { | ||
this._loop = false; | ||
this._non_touchend = true; | ||
} else { | ||
this._loop = true; | ||
this._non_touchend = false; | ||
} | ||
@@ -26,17 +34,14 @@ return this; | ||
start() { | ||
if (this.isRunning) { | ||
return this; | ||
} | ||
this._running = true; | ||
once() { | ||
const scriptFile = 'scriptFile' in (this._options || {}) ? | ||
this._options.scriptFile : 'reader.py'; | ||
const scriptPath = 'scriptPath' in (this._options || {}) ? | ||
this._options.scriptPath : __dirname; | ||
const args = [this._mode]; | ||
this.pyshell = new PythonShell(scriptFile, {scriptPath, args}, {mode: 'JSON'}); | ||
const scriptFile = 'scriptFile' in (this.options || {}) ? | ||
this.options.scriptFile : 'reader.py'; | ||
const scriptPath = 'scriptPath' in (this.options || {}) ? | ||
this.options.scriptPath : __dirname; | ||
this.pyshell = new PythonShell(scriptFile, {scriptPath}, {mode: 'JSON'}); | ||
this.pyshell.stdout.on('data', (json) => { | ||
if (this.isRunning) { | ||
const data = JSON.parse(json.split('\n')[0]); | ||
this._running = this._loop || !this._non_touchend && !(data.event === 'touchend'); | ||
this.emit(data.event, data); | ||
@@ -47,11 +52,36 @@ } | ||
this.pyshell.end((err) => { | ||
this.pyshell = null; | ||
this._running = false; | ||
this.emit('error', err); | ||
if (!this._exiting) { | ||
this._running = false; | ||
this._firstLaunch = false; | ||
this.emit('error', err); | ||
} | ||
}); | ||
['SIGHUP', 'SIGINT', 'exit'].forEach((event) => { | ||
process.on(event, () => { | ||
if (!this._exiting) { | ||
this._exiting = true; | ||
this.sendSignal('SIGHUP'); | ||
} | ||
}); | ||
}); | ||
} | ||
start() { | ||
if (this.isRunning) { | ||
return this; | ||
} | ||
this._running = true; | ||
if (!this._firstLaunch) { | ||
this.once(); | ||
this._firstLaunch = true; | ||
} else { | ||
this.sendSignal('SIGCHLD'); | ||
} | ||
return this; | ||
} | ||
stop() { | ||
pause() { | ||
if (!this.isRunning) { | ||
@@ -61,8 +91,9 @@ return this; | ||
this._running = false; | ||
this.pyshell.childProcess.kill('SIGHUP'); | ||
this.pyshell = null; | ||
this.sendSignal('SIGCHLD'); | ||
return this; | ||
} | ||
sendSignal(signal) { | ||
this.pyshell.childProcess.kill(signal); | ||
} | ||
} |
{ | ||
"name": "node-nfcpy-id", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Read the ID of an NFC Tag with nfcpy", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -37,2 +37,3 @@ # node-nfcpy-id | ||
### loop mode | ||
```js | ||
@@ -54,16 +55,39 @@ 'use strict'; | ||
// If the `mode` is `loop` or `non-loop`, event will occur when the card is removed | ||
nfc.on('touchend', () => { | ||
console.log('Card was away.'); | ||
}); | ||
// Python process will be killed. | ||
// nfc.stop(); | ||
nfc.on('error', (err) => { | ||
// standard error output (color is red) | ||
console.error('\u001b[31m', err, '\u001b[0m'); | ||
}); | ||
// Python process will be killed at the same time. | ||
// process.exit(); | ||
``` | ||
### non-loop mode | ||
```js | ||
'use strict'; | ||
const NfcpyId = require('node-nfcpy-id'); | ||
const nfc = new NfcpyId({mode: 'non-loop'}).start(); | ||
nfc.on('touchstart', (card) => { | ||
console.log('Card ID: ' + card.id); | ||
console.log('Card Type: ' + card.type); | ||
}); | ||
// If the `mode` is `loop` or `non-loop`, event will occur when the card is released | ||
nfc.on('touchend', () => { | ||
console.log('Card was away.'); | ||
// Card reading will start five seconds after the card is released | ||
setTimeout(() => { | ||
nfc.start(); | ||
}, 5000); | ||
}); | ||
nfc.on('error', (err) => { | ||
console.error('\u001b[31m'); // RED | ||
console.error(err); | ||
console.error('\u001b[0m'); | ||
// standard error output (color is red) | ||
console.error('\u001b[31m', err, '\u001b[0m'); | ||
}); | ||
@@ -73,4 +97,27 @@ | ||
To stop this script, press Ctrl+C. By this, Python process will be killed at the same time. | ||
### non-touchend mode | ||
```js | ||
'use strict'; | ||
const NfcpyId = require('node-nfcpy-id'); | ||
const nfc = new NfcpyId({mode: 'non-touchend'}).start(); | ||
nfc.on('touchstart', (card) => { | ||
console.log('Card ID: ' + card.id); | ||
console.log('Card Type: ' + card.type); | ||
}); | ||
nfc.on('error', (err) => { | ||
// standard error output (color is red) | ||
console.error('\u001b[31m', err, '\u001b[0m'); | ||
}); | ||
``` | ||
To start (restart) reading cards, use `nfc.start()`. | ||
To pause reading cards, use `nfc.pause()`. | ||
To stop this script, press control+C. By this, Python process will be killed at the same time. | ||
To use this script with other than SONY Pasori RC-S380, it may be necessary to modify `reader.py` and add options to the parameter of constructor. | ||
@@ -77,0 +124,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
26699
23
775
131
1