Start fleshing out card update code.

- Use node config,
- Add logger
This commit is contained in:
Dana Woodman 2018-03-03 13:59:07 -08:00
parent 260685e25d
commit a076724f83
12 changed files with 273 additions and 206 deletions

View File

@ -1,6 +0,0 @@
const ENV = process.env.NODE_ENV || 'development'
const DOOR_OPEN_DELAY = ENV === 'test' ? 1 : 6000 // in milliseconds
module.exports = {
DOOR_OPEN_DELAY,
}

View File

@ -0,0 +1,3 @@
module.exports = {
env: 'ENV',
}

5
config/default.js Normal file
View File

@ -0,0 +1,5 @@
module.exports = {
cardApi: 'https://chimera.cobot.me/api/check_in_tokens',
env: 'development',
openDelay: 6000,
}

4
config/test.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
env: 'test',
openDelay: 1,
}

403
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,11 +5,12 @@
"scripts": {
"start": "t2 run src/boot.js",
"test": "jest",
"watch-test": "jest --watch"
"watch-test": "npm test -- --watch"
},
"dependencies": {
"axios": "0.17.1",
"chalk": "2.3.1",
"config": "1.30.0",
"consolidate": "0.15.0",
"eslint": "4.17.0",
"express": "4.16.2",
@ -22,5 +23,8 @@
"babel-eslint": "8.2.2",
"eslint-plugin-react": "7.7.0",
"jest": "22.4.2"
},
"optionalDependencies": {
"fsevents": "*"
}
}

View File

@ -1,5 +1,7 @@
# Chimera Doorlock
> A Tessel powered RFID doorlock for the space, interfacing with Cobot
Powered by a [Tessel][tessel]
## Development

10
src/models/cards.js Normal file
View File

@ -0,0 +1,10 @@
const axios = require('axios')
const config = require('config')
class Cards {
static update() {
return axios.get(config.get('cardApi'))
}
}
module.exports = Cards

9
src/models/cards.test.js Normal file
View File

@ -0,0 +1,9 @@
const Cards = require('./cards')
describe('models/cards', () => {
describe('.update', () => {
test('should fetch list of RFID cards', () => {
return Cards.update().then(cards => expect(cards).toEqual())
})
})
})

View File

@ -1,5 +1,9 @@
const config = require('config')
const logger = require('../utils/logger')
const tessel = require('tessel')
const constants = require('../../config/constants')
const DOOR_OPEN_DELAY = config.get('openDelay')
// const fs = require('fs')
// const path = require('path')
@ -8,21 +12,23 @@ const constants = require('../../config/constants')
class Door {
static open() {
logger.log('OPEN DOOR')
// TODO: trigger door opening...
tessel.led[2].on()
tessel.led[3].on()
return new Promise(resolve => {
setTimeout(() => {
// TODO: trigger door closing
Door.close()
resolve()
}, constants.DOOR_OPEN_DELAY)
}, DOOR_OPEN_DELAY)
})
}
static close() {
console.log('CLOSE DOOR')
tessel.led[2].off()
logger.log('CLOSE DOOR')
tessel.led[3].off()
}
}

View File

@ -7,9 +7,8 @@ describe('models/door', () => {
describe('.open', () => {
test('opens door', () => {
jest.spyOn(Door, 'close')
// Door.close = jest.fn()
return Door.open().then(() => {
expect(tessel.led[2].on).toBeCalled()
expect(tessel.led[3].on).toBeCalled()
expect(Door.close).toBeCalled()
})
})
@ -18,7 +17,7 @@ describe('models/door', () => {
describe('.close', () => {
test('closes door', () => {
Door.close()
expect(tessel.led[2].off).toBeCalled()
expect(tessel.led[3].off).toBeCalled()
})
})
})

10
src/utils/logger.js Normal file
View File

@ -0,0 +1,10 @@
const config = require('config')
const ENV = config.get('env')
class Logger {
log() {
ENV !== 'test' && console.log(...arguments)
}
}
module.exports = new Logger()