mirror of
https://github.com/zyphlar/doorlock.git
synced 2024-04-03 21:36:03 +00:00
Fetch cards from Cobot, log access
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
// })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const Cards = require('../models/cards')
|
||||
const Logs = require('../models/logs')
|
||||
|
||||
module.exports = (req, res) => {
|
||||
const rfid = req.body.rfid.trim().toLowerCase()
|
||||
@@ -9,6 +10,9 @@ module.exports = (req, res) => {
|
||||
// TODO: add to log if success
|
||||
if (card) {
|
||||
res.redirect('/success?name=' + card.name)
|
||||
Logs.log({ timestamp: new Date().getTime(), card }).then(() =>
|
||||
console.log('Logged!')
|
||||
)
|
||||
} else {
|
||||
res.redirect('/failure')
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
const Logs = require('../models/logs')
|
||||
|
||||
module.exports = (req, res) => {
|
||||
Logs.all().then(logs => res.render('logs', { logs }))
|
||||
Logs.all().then(logs => {
|
||||
logs = logs.sort((a, b) => a.timestamp < b.timestamp)
|
||||
res.render('logs', { logs })
|
||||
})
|
||||
}
|
||||
|
||||
10
src/routes/update.js
Normal file
10
src/routes/update.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const Cobot = require('../models/cobot')
|
||||
|
||||
module.exports = (req, res) => {
|
||||
Cobot.getCards()
|
||||
.then(cards => {
|
||||
console.log('GOT CARDS:', cards)
|
||||
res.redirect('/')
|
||||
})
|
||||
.catch(console.error)
|
||||
}
|
||||
@@ -37,6 +37,7 @@ app.get('/success', require('./routes/success'))
|
||||
app.get('/failure', require('./routes/failure'))
|
||||
app.get('/cards', require('./routes/cards'))
|
||||
app.get('/logs', require('./routes/logs'))
|
||||
app.get('/update', require('./routes/update'))
|
||||
app.get('/', (req, res) => res.render('home', {}))
|
||||
|
||||
app.listen(PORT, () => console.log('Example app listening on port 3000!'))
|
||||
|
||||
@@ -5,7 +5,11 @@ block title
|
||||
| Cards
|
||||
|
||||
block content
|
||||
h1.page-heading Cards
|
||||
a.button.button-sm.fr(href='/update')
|
||||
i.fas.fa-sync.mr-sm
|
||||
| Update
|
||||
h1.page-heading
|
||||
| Cards
|
||||
table.collapse.w-100
|
||||
thead
|
||||
tr
|
||||
|
||||
@@ -14,5 +14,5 @@ block content
|
||||
a(href='/') ← Back
|
||||
|
||||
script.
|
||||
//- setTimeout(function () { window.location.href = '/' }, 6000)
|
||||
setTimeout(function () { window.location.href = '/' }, 4000)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user