|
|
|
@ -1,10 +1,19 @@ |
|
|
|
|
import { NFC } from 'nfc-pcsc' |
|
|
|
|
import fetch from 'node-fetch' |
|
|
|
|
import fs from 'fs' |
|
|
|
|
|
|
|
|
|
const CARDS_LOCATION = '/home/snufkin/Repos/apps/YACards/' |
|
|
|
|
|
|
|
|
|
console.log('[ ][INF][EABU]: Starting up (if this hangs check if pcscd is running)') |
|
|
|
|
const nfc = new NFC() |
|
|
|
|
const status = { |
|
|
|
|
card: null |
|
|
|
|
card: null, |
|
|
|
|
confirmed: false, |
|
|
|
|
newCardRegistration: false, |
|
|
|
|
guestPlay: false, |
|
|
|
|
readyCard: false, |
|
|
|
|
countdown: 0, |
|
|
|
|
newCard: false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
console.log('[ ][INF][EABU]: NFC Initialized') |
|
|
|
@ -12,41 +21,111 @@ nfc.on('error', err => { |
|
|
|
|
console.error('[!][ERR][PCSC]:', err); |
|
|
|
|
}); |
|
|
|
|
nfc.on('reader', reader => { |
|
|
|
|
reader.aid = 'F222222222'; |
|
|
|
|
// reader.aid = 'F222222222';
|
|
|
|
|
reader.on('card', card => { |
|
|
|
|
console.log(`[C][INF][PCSC]: Card ${card.uid} scanned`) |
|
|
|
|
if (status.card === null) { |
|
|
|
|
console.log(`[C][INF][PCSC] Card ${card.uid} scanned`) |
|
|
|
|
if (status.card === null && !status.guestPlay) { |
|
|
|
|
status.card = card.uid |
|
|
|
|
console.log(`[C][INF][YACE]: Telling YACE to insert ${card.uid}`) |
|
|
|
|
fetch('http://localhost:8082/api/v1/insertCard?cardname=' + card.uid, { method: 'POST' }) |
|
|
|
|
status.confirmed = false |
|
|
|
|
status.countdown = 5 |
|
|
|
|
status.newCard = !fs.existsSync(CARDS_LOCATION + card.uid + '.track_0') |
|
|
|
|
console.log(`[C][INF][YACE] Telling YACE to insert ${card.uid}`) |
|
|
|
|
if (status.newCard) { |
|
|
|
|
console.log(`[C][INF][EABU] New card!`) |
|
|
|
|
status.newCardRegistration = true |
|
|
|
|
status.countdown = 300 // seconds
|
|
|
|
|
fetch('http://localhost:8082/api/v1/loadedCard?cardname=' + card.uid, { method: 'POST' }) |
|
|
|
|
} else { |
|
|
|
|
console.log(`[C][INF][EABU] Existing card!`) |
|
|
|
|
if (status.readyCard) { |
|
|
|
|
console.log(`[C][INF][EABU] Inserting...`) |
|
|
|
|
fetch('http://localhost:8082/api/v1/insertedCard?cardname=' + card.uid, { method: 'POST' }) |
|
|
|
|
} else { |
|
|
|
|
console.log(`[C][INF][EABU] Not inserting as game isn't ready for it...`) |
|
|
|
|
status.card = null |
|
|
|
|
status.confirmed = false |
|
|
|
|
status.countdown = 0 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
console.log(`[C][INF][EABU]: Ignoring as we already have a card...`) |
|
|
|
|
console.log(`[C][INF][EABU] Ignoring as we already have a card or GUEST is playing...`) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
reader.on('error', err => { |
|
|
|
|
console.error('[!][ERR][PCSC]:', err); |
|
|
|
|
console.error('[!][ERR][PCSC] ', err); |
|
|
|
|
}); |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const pollYACE = () => { |
|
|
|
|
// console.debug('[ ][DEB][EABU] STATUS ' + JSON.stringify(status))
|
|
|
|
|
if (status.card !== null) { |
|
|
|
|
fetch('http://localhost:8082/api/v1/insertedCard') |
|
|
|
|
.then(res => res.json()) |
|
|
|
|
.then(bool => console.log('[ ][INF][YACE] Emulator card inserted: ' + bool.inserted)) |
|
|
|
|
// fetch('http://localhost:8082/api/v1/insertedCard')
|
|
|
|
|
// .then(res => res.json())
|
|
|
|
|
// .then(bool => console.log('[ ][INF][YACE] Emulator card inserted: ' + bool.inserted))
|
|
|
|
|
fetch('http://localhost:8082/api/v1/hasCard') |
|
|
|
|
.then(res => res.json()) |
|
|
|
|
.then(bool => { |
|
|
|
|
console.log('[ ][INF][YACE] Emulator card present: ' + bool) |
|
|
|
|
if (!bool) { |
|
|
|
|
// console.debug('[ ][DEB][YACE] Emulator card present: ' + bool)
|
|
|
|
|
if (status.countdown > 0) { |
|
|
|
|
// counting down trying to confirm insertion
|
|
|
|
|
status.countdown-- |
|
|
|
|
status.confirmed = bool |
|
|
|
|
if (bool) status.countdown = 0 |
|
|
|
|
setTimeout(pollYACE, 1000) |
|
|
|
|
} else if (!status.confirmed) { |
|
|
|
|
// couldn't confirm insertion
|
|
|
|
|
status.card = null |
|
|
|
|
fetch('http://localhost:8082/api/v1/insertCard?cardname=dummy', { method: 'POST' }) |
|
|
|
|
status.newCard = false |
|
|
|
|
console.error('[!][ERR][YACE] Could not confirm card presence with emulator!') |
|
|
|
|
console.error('[!][INF][YACE] Telling YACE to load dummy just in case') |
|
|
|
|
fetch('http://localhost:8082/api/v1/loadedCard?cardname=dummy', { method: 'POST' }) |
|
|
|
|
setTimeout(pollYACE, 1000) |
|
|
|
|
} else { |
|
|
|
|
// insertion confirmed! waiting for ejection
|
|
|
|
|
if (!bool) { |
|
|
|
|
status.card = null |
|
|
|
|
status.newCard = false |
|
|
|
|
console.error('[!][INF][YACE] Telling YACE to load dummy') |
|
|
|
|
fetch('http://localhost:8082/api/v1/loadedCard?cardname=dummy', { method: 'POST' }) |
|
|
|
|
} |
|
|
|
|
setTimeout(pollYACE, 500) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
.finally(() => setTimeout(pollYACE, 100)) |
|
|
|
|
.catch(err => { |
|
|
|
|
console.error('[!][ERR][EABU] Failure polling YACE') |
|
|
|
|
setTimeout(pollYACE, 2000) |
|
|
|
|
}) |
|
|
|
|
} else { |
|
|
|
|
setTimeout(pollYACE, 200) |
|
|
|
|
fetch('http://localhost:8082/api/v1/hasCard') |
|
|
|
|
.then(res => res.json()) |
|
|
|
|
.then(bool => { |
|
|
|
|
if (status.guestPlay) { |
|
|
|
|
console.debug('[ ][DEB][YACE] Emulator GUEST card present: ' + bool) |
|
|
|
|
} |
|
|
|
|
if (bool) { |
|
|
|
|
status.guestPlay = true |
|
|
|
|
} |
|
|
|
|
if (status.guestPlay && !bool) { |
|
|
|
|
status.guestPlay = false |
|
|
|
|
} |
|
|
|
|
setTimeout(pollYACE, 200) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const pollYACE2 = () => { |
|
|
|
|
fetch('http://localhost:8082/api/v1/readyCard') |
|
|
|
|
.then(res => res.json()) |
|
|
|
|
.then(bool => { |
|
|
|
|
status.readyCard = bool |
|
|
|
|
setTimeout(pollYACE2, 500) |
|
|
|
|
}) |
|
|
|
|
.catch(err => { |
|
|
|
|
console.error('[!][ERR][EABU] Failure polling YACE (loop2)') |
|
|
|
|
setTimeout(pollYACE2, 2000) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pollYACE() |
|
|
|
|
pollYACE2() |
|
|
|
|