Add DoorLock controller model

Add more tests, cleanup
This commit is contained in:
Dana Woodman
2018-03-04 16:09:09 -08:00
parent 31ac07bae5
commit de1b182586
10 changed files with 139 additions and 88 deletions
+16 -1
View File
@@ -1,10 +1,25 @@
const axios = require('axios')
const { COBOT_CARDS_API } = require('../constants')
const { CARDS_PATH, COBOT_CARDS_API } = require('../constants')
const SDCard = require('./sd-card')
class Cards {
static update() {
return axios.get(COBOT_CARDS_API)
}
static read() {
return SDCard.read(CARDS_PATH)
}
static write(cards) {
return SDCard.write(CARDS_PATH, cards)
}
// static validate(number) {
// return this.read().then(cards => {
// return cards.find(c => c.number === number)
// })
// }
}
module.exports = Cards
+5 -1
View File
@@ -2,8 +2,12 @@ const Cards = require('./cards')
describe('models/cards', () => {
describe('.update', () => {
test('should fetch list of RFID cards', () => {
test.skip('should fetch list of RFID cards', () => {
return Cards.update().then(cards => expect(cards).toEqual())
})
})
describe('.validate', () => {
test.skip('should check card against list', () => {})
})
})
-44
View File
@@ -2,12 +2,6 @@ const { DOOR_OPEN_DELAY } = require('../constants')
const logger = require('../utils/logger')
const tessel = require('tessel')
// const fs = require('fs')
// const path = require('path')
// const USB_MOUNT_PATH = '/mnt/sda1'
// const CARDS_PATH = path.join(USB_MOUNT_PATH, 'cards.json')
class Door {
static open() {
logger.log('OPEN DOOR')
@@ -31,41 +25,3 @@ class Door {
}
module.exports = Door
// const doorlock = {}
// doorlock.updateCards = () => {
// console.log('fetch cards from API and update file')
// const cards = [{ card: '1234', name: 'John' }, { card: '5566', name: 'Jane' }]
// doorlock.writeCards(cards)
// }
// doorlock.readCards = cb => {
// fs.readFile(CARDS_PATH, function(err, data) {
// if (err) throw err
// console.log('data:', data.toString())
// cb(JSON.parse(data))
// })
// }
// doorlock.writeCards = cards => {
// const text = JSON.stringify(cards)
// fs.writeFile(CARDS_PATH, text, err => {
// if (err) throw err
// console.log('wrote:', cards)
// })
// }
// doorlock.open = () => {
// console.log('open!')
// tessel.led[2].on()
// setTimeout(() => tessel.led[2].off(), 3000)
// }
// doorlock.close = () => {
// console.log('close!')
// tessel.led[3].on()
// setTimeout(() => tessel.led[3].off(), 3000)
// }
// module.exports = doorlock
+28
View File
@@ -0,0 +1,28 @@
const Cobot = require('./cobot')
const Cards = require('./cards')
const logger = require('../utils/logger')
class DoorLock {
static initializeRFIDReader() {
console.log('TODO: initialize RFID reader...')
Cards.read().then(cards => logger.log('EXISTING:', cards.length, 'cards'))
}
static updateCards() {
logger.log('UPDATING CARDS!')
Cobot.authorize().then(cobot => {
cobot.cards().then(cards => {
logger.log('NEW:', cards.length, 'cards')
Cards.write(cards)
})
})
}
static initialize() {
logger.log('INITIALIZING DOORLOCK!')
this.updateCards()
this.initializeRFIDReader()
}
}
module.exports = DoorLock
+14
View File
@@ -25,4 +25,18 @@ class RFIDReader {
}
}
// const RFIDReader = require('./models/rfid-reader')
// console.log(RFIDReader.devices())
// const reader = RFIDReader.reader()
// reader.on('data', data => console.log(data))
// reader.close()
// new Promise(resolve => {
// setTimeout(() => resolve(), 10000)
// })
// console.log('READER:', reader)
module.exports = RFIDReader
+27
View File
@@ -0,0 +1,27 @@
const fs = require('fs')
const logger = require('../utils/logger')
class SDCard {
static read(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err, data) => {
if (err) return reject(err)
// logger.log('READ:', data.toString())
resolve(JSON.parse(data))
})
})
}
static write(filePath, json) {
const text = JSON.stringify(json)
return new Promise((resolve, reject) => {
fs.writeFile(filePath, text, err => {
if (err) return reject(err)
// logger.log('WROTE:', json)
resolve()
})
})
}
}
module.exports = SDCard
+31
View File
@@ -0,0 +1,31 @@
const fs = require('fs')
const SDCard = require('./sd-card')
jest.mock('fs')
describe('models/sd-card', () => {
const filePath = '/path/to/cards.json'
const expected = [{ name: 'John', number: '123' }]
describe('.read', () => {
test('should read and convert file data to JSON', () => {
jest
.spyOn(fs, 'readFile')
.mockImplementationOnce((p, cb) => cb(null, JSON.stringify(expected)))
return SDCard.read(filePath).then(actual => {
expect(actual).toEqual(expected)
})
})
})
describe('.write', () => {
test('should write data to SD card', () => {
jest.spyOn(fs, 'writeFile').mockImplementationOnce((path, text, cb) => {
expect(path).toBe(filePath)
expect(text).toBe(JSON.stringify(expected))
cb()
})
return expect(SDCard.write(filePath, expected)).resolves.toBe()
})
})
})