Fetch cards from Cobot, log access

This commit is contained in:
Dana Woodman
2018-07-10 16:25:05 -07:00
parent 9c11a0506a
commit 710f7953b6
11 changed files with 155 additions and 94 deletions
+4 -1
View File
@@ -15,6 +15,7 @@ module.exports = class Cards {
static write(cards) {
const json = JSON.stringify(this.sortByName(cards))
console.log('WRITING CARDS:', json)
return new Promise((resolve, reject) => {
fs.writeFile(CARDS_PATH, json, err => {
if (err) return reject(err)
@@ -24,7 +25,9 @@ module.exports = class Cards {
}
static validate(number) {
return this.all().then(cards => cards.find(c => c.number === number))
return this.all().then(cards =>
cards.find(c => parseInt(c.number, 10) === parseInt(number, 10))
)
// console.log(':', JSON.stringify(number.toString().trim()))
// const scanned = parseInt(
// number
+13 -17
View File
@@ -1,3 +1,4 @@
const Cards = require('./cards')
const https = require('https')
const {
CARD_UPDATE_INTERVAL,
@@ -7,7 +8,7 @@ const {
COBOT_SCOPE,
COBOT_USER_EMAIL,
COBOT_USER_PASSWORD,
} = require('./constants')
} = require('../../constants')
module.exports = class Cobot {
constructor(token) {
@@ -144,22 +145,17 @@ module.exports = class Cobot {
}
static getCards() {
this.log('Updating cards...')
this.authorize()
console.log('Updating cards...')
return this.authorize()
.then(cobot => cobot.cards())
.then(cards => {
this.log('UPDATED CARDS:', cards.length, 'cards')
this.writeCardsToSDCard(cards)
this.cards = cards
})
.then(() => {
this.log(
'Updating card list in',
CARD_UPDATE_INTERVAL / 1000,
'seconds...'
)
setTimeout(this.fetchCardListFromCobot.bind(this), CARD_UPDATE_INTERVAL)
})
.catch(this.logErrorMessage)
.then(cards => Cards.write(cards))
// .then(() => {
// console.log(
// 'Updating card list in',
// CARD_UPDATE_INTERVAL / 1000,
// 'seconds...'
// )
// setTimeout(this.getCards.bind(this), CARD_UPDATE_INTERVAL)
// })
}
}
+28 -10
View File
@@ -1,14 +1,32 @@
const fs = require('fs')
const path = require('path')
const LOGS_PATH = path.join(process.cwd(), 'logs.json')
module.exports = class Logs {
static all() {
return Promise.all([
{
timestamp: 1531256719431,
card: { name: 'John Smith', number: '1234023423423' },
},
{
timestamp: 1531256756227,
card: { name: 'Jane Doe', number: '2394723984752983' },
},
])
return new Promise((resolve, reject) => {
fs.readFile(LOGS_PATH, (err, data) => {
if (err) return reject(err)
resolve(JSON.parse(data))
})
})
}
static write(logs) {
const json = JSON.stringify(logs)
return new Promise((resolve, reject) => {
fs.writeFile(LOGS_PATH, json, err => {
if (err) return reject(err)
resolve()
})
})
}
static log(access) {
return this.all().then(all => {
all.push(access)
return this.write(all)
})
}
}