mirror of
https://github.com/zyphlar/doorlock.git
synced 2024-04-03 21:36:03 +00:00
Add Cobot card fetching, cleanup config setup.
This commit is contained in:
17
src/boot.js
17
src/boot.js
@@ -1,6 +1,19 @@
|
||||
const Door = require('./models/door')
|
||||
const Cobot = require('./models/cobot')
|
||||
|
||||
Door.open()
|
||||
Cobot.authorize().then(cobot => {
|
||||
cobot.cards().then(resp => console.log('RESP:', resp))
|
||||
})
|
||||
|
||||
// const server = require('./server')
|
||||
|
||||
// server()
|
||||
|
||||
// const Cards = require('./models/cards')
|
||||
// const Door = require('./models/door')
|
||||
|
||||
// Door.open()
|
||||
|
||||
// Cards.update()
|
||||
|
||||
// const server = require('./server')
|
||||
|
||||
|
||||
14
src/constants.js
Normal file
14
src/constants.js
Normal file
@@ -0,0 +1,14 @@
|
||||
require('dotenv').config()
|
||||
|
||||
const ENV = process.env.NODE_ENV || 'development'
|
||||
|
||||
module.exports = {
|
||||
COBOT_CARDS_API: process.env.COBOT_CARDS_API,
|
||||
COBOT_CLIENT_ID: process.env.COBOT_CLIENT_ID,
|
||||
COBOT_CLIENT_SECRET: process.env.COBOT_CLIENT_SECRET,
|
||||
COBOT_SCOPE: process.env.COBOT_SCOPE,
|
||||
COBOT_USER_EMAIL: process.env.COBOT_USER_EMAIL,
|
||||
COBOT_USER_PASSWORD: process.env.COBOT_USER_PASSWORD,
|
||||
DOOR_OPEN_DELAY: process.env.DOOR_OPEN_DELAY,
|
||||
ENV,
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
const axios = require('axios')
|
||||
const config = require('config')
|
||||
const { COBOT_CARDS_API } = require('../constants')
|
||||
|
||||
class Cards {
|
||||
static update() {
|
||||
return axios.get(config.get('cardApi'))
|
||||
return axios.get(COBOT_CARDS_API)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
64
src/models/cobot.js
Normal file
64
src/models/cobot.js
Normal file
@@ -0,0 +1,64 @@
|
||||
const axios = require('axios')
|
||||
const {
|
||||
COBOT_CARDS_API,
|
||||
COBOT_CLIENT_ID,
|
||||
COBOT_CLIENT_SECRET,
|
||||
COBOT_SCOPE,
|
||||
COBOT_USER_EMAIL,
|
||||
COBOT_USER_PASSWORD,
|
||||
} = require('../constants')
|
||||
|
||||
class Cobot {
|
||||
constructor(token) {
|
||||
this.token = token
|
||||
}
|
||||
|
||||
cards() {
|
||||
if (!COBOT_CARDS_API)
|
||||
throw new Error('missing "COBOT_CARDS_API" env variable!')
|
||||
return axios
|
||||
.get(COBOT_CARDS_API, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.token}`,
|
||||
},
|
||||
})
|
||||
.then(resp =>
|
||||
resp.data.map(card => ({
|
||||
name: card.membership.name,
|
||||
number: card.token,
|
||||
}))
|
||||
)
|
||||
}
|
||||
|
||||
static authorize() {
|
||||
if (!COBOT_SCOPE) throw new Error('missing "COBOT_SCOPE" env variable!')
|
||||
if (!COBOT_USER_EMAIL)
|
||||
throw new Error('missing "COBOT_USER_EMAIL" env variable!')
|
||||
if (!COBOT_USER_PASSWORD)
|
||||
throw new Error('missing "COBOT_USER_PASSWORD" env variable!')
|
||||
if (!COBOT_CLIENT_ID)
|
||||
throw new Error('missing "COBOT_CLIENT_ID" env variable!')
|
||||
if (!COBOT_CLIENT_SECRET)
|
||||
throw new Error('missing "COBOT_CLIENT_SECRET" env variable!')
|
||||
console.log(
|
||||
COBOT_SCOPE,
|
||||
COBOT_USER_EMAIL,
|
||||
COBOT_USER_PASSWORD,
|
||||
COBOT_CLIENT_ID,
|
||||
COBOT_CLIENT_SECRET
|
||||
)
|
||||
const qs = [
|
||||
`scope=${COBOT_SCOPE}`,
|
||||
`grant_type=password`,
|
||||
`username=${COBOT_USER_EMAIL}`,
|
||||
`password=${COBOT_USER_PASSWORD}`,
|
||||
`client_id=${COBOT_CLIENT_ID}`,
|
||||
`client_secret=${COBOT_CLIENT_SECRET}`,
|
||||
].join('&')
|
||||
return axios
|
||||
.post(`https://www.cobot.me/oauth/access_token?${qs}`)
|
||||
.then(resp => new Cobot(resp.data.access_token))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Cobot
|
||||
41
src/models/cobot.test.js
Normal file
41
src/models/cobot.test.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const axios = require('axios')
|
||||
const Cobot = require('./cobot')
|
||||
|
||||
jest.mock('axios')
|
||||
|
||||
describe('models/cobot', () => {
|
||||
describe('.authorize', () => {
|
||||
test('should fetch a new token', () => {
|
||||
const resp = { data: { access_token: 'meow' } }
|
||||
jest.spyOn(axios, 'post').mockResolvedValue(resp)
|
||||
return Cobot.authorize().then(cobot => {
|
||||
expect(axios.post).toBeCalled()
|
||||
expect(cobot).toBeInstanceOf(Cobot)
|
||||
expect(cobot.token).toBe('meow')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('.cards', () => {
|
||||
test('returns a structured list of cards', () => {
|
||||
const resp = {
|
||||
data: [
|
||||
{ membership: { id: 'foo', name: 'John' }, token: '001' },
|
||||
{ membership: { id: 'bar', name: 'Jane' }, token: '002' },
|
||||
],
|
||||
}
|
||||
const expected = [
|
||||
{ name: 'John', number: '001' },
|
||||
{ name: 'Jane', number: '002' },
|
||||
]
|
||||
jest.spyOn(axios, 'get').mockResolvedValue(resp)
|
||||
|
||||
// TODO: why doesnt this work? It should...
|
||||
// axios.get.mockResolvedValue(resp)
|
||||
|
||||
return new Cobot('fake-value')
|
||||
.cards()
|
||||
.then(actual => expect(actual).toEqual(expected))
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,9 +1,7 @@
|
||||
const config = require('config')
|
||||
const { DOOR_OPEN_DELAY } = require('../constants')
|
||||
const logger = require('../utils/logger')
|
||||
const tessel = require('tessel')
|
||||
|
||||
const DOOR_OPEN_DELAY = config.get('openDelay')
|
||||
|
||||
// const fs = require('fs')
|
||||
// const path = require('path')
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const config = require('config')
|
||||
const ENV = config.get('env')
|
||||
const { ENV } = require('../constants')
|
||||
|
||||
class Logger {
|
||||
log() {
|
||||
|
||||
Reference in New Issue
Block a user