mirror of
https://github.com/zyphlar/doorlock.git
synced 2024-04-03 21:36:03 +00:00
Add basic RFID reader scanning code.
Other cleanup and docs.
This commit is contained in:
@@ -9,6 +9,7 @@ module.exports = {
|
||||
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,
|
||||
DOOR_OPEN_DELAY: ENV === 'test' ? 1 : 6000,
|
||||
ENV,
|
||||
RFID_PRODUCT_NAME: process.env.RFID_PRODUCT_NAME,
|
||||
}
|
||||
|
||||
@@ -40,13 +40,6 @@ class Cobot {
|
||||
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`,
|
||||
|
||||
28
src/models/rfid-reader.js
Normal file
28
src/models/rfid-reader.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const hid = require('node-hid')
|
||||
const { RFID_PRODUCT_NAME } = require('../constants')
|
||||
|
||||
class RFIDReader {
|
||||
static devices() {
|
||||
return hid.devices() || []
|
||||
}
|
||||
|
||||
static reader() {
|
||||
const device = this.devices().find(d => d.product === RFID_PRODUCT_NAME)
|
||||
|
||||
if (!device) {
|
||||
throw new Error(
|
||||
`no RFID device found with the "RFID_PRODUCT_NAME" matching "${RFID_PRODUCT_NAME}"`
|
||||
)
|
||||
}
|
||||
|
||||
return new hid.HID(device.path)
|
||||
}
|
||||
|
||||
static read() {
|
||||
const device = this.reader()
|
||||
device.read((err, data) => console.log(err, data))
|
||||
return device
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RFIDReader
|
||||
48
src/models/rfid-reader.test.js
Normal file
48
src/models/rfid-reader.test.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const hid = require('node-hid')
|
||||
const RFIDReader = require('./rfid-reader')
|
||||
|
||||
jest.mock('node-hid')
|
||||
|
||||
const rfidDevice = {
|
||||
interface: -1,
|
||||
manufacturer: 'Apple',
|
||||
path:
|
||||
'IOService:/IOResources/IOBluetoothHCIController/AppleBroadcomBluetoothHostController/IOBluetoothDevice/IOBluetoothL2CAPChannel/AppleHSBluetoothDevice/Keyboard / Boot@1/AppleHSBluetoothHIDDriver',
|
||||
product: 'KB800HM Kinesis Freestyle2 for Mac',
|
||||
productId: 615,
|
||||
release: 0,
|
||||
serialNumber: '04-69-f8-c6-d2-c2',
|
||||
usage: 6,
|
||||
usagePage: 1,
|
||||
vendorId: 76,
|
||||
}
|
||||
|
||||
describe('models/rfid-reader', () => {
|
||||
const devices = [rfidDevice]
|
||||
|
||||
beforeAll(() => {
|
||||
hid.devices.mockReturnValue(devices)
|
||||
})
|
||||
|
||||
describe('.devices', () => {
|
||||
test('lists devices', () => {
|
||||
const actual = RFIDReader.devices()
|
||||
expect(actual).toEqual(devices)
|
||||
})
|
||||
})
|
||||
|
||||
describe('.reader', () => {
|
||||
test('should connect to the RFID reader and return it', () => {
|
||||
const actual = RFIDReader.reader()
|
||||
expect(actual).toBeInstanceOf(hid.HID)
|
||||
})
|
||||
})
|
||||
|
||||
describe('.read', () => {
|
||||
test('it should listen for keyboard input', () => {
|
||||
const device = RFIDReader.read()
|
||||
console.log(device)
|
||||
expect(device.read).toBeCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user