Skip to content

Commit

Permalink
rfid and door module(MVP)
Browse files Browse the repository at this point in the history
  • Loading branch information
TMineCola committed Jun 7, 2018
1 parent 7e4f56a commit b5bdda6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 34 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ app.use(bodyParser.json());
rpio.init({mapping: 'physical'});

setInterval(function(){
console.log(rfid.read());
rfid.read();
}, 500);

var server = app.listen(config.main.port || 8080, function() {
Expand Down
79 changes: 52 additions & 27 deletions modules/doorControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,78 @@ module.exports = function (rpio, config) {

var module = {};

module.doorInit = function () {
_doorAttach();
}

// control functions

module.doorPowerSwitch = function () {
rpio.read(config.lock.powerPIN) == 0 ? rpio.write(config.lock.powerPIN, rpio.HIGH) : rpio.write(config.lock.powerPIN, rpio.LOW)

return rpio.read(config.lock.powerPIN);
}

module.doorOpenSwitch = function () {
rpio.read(config.lock.openPIN) == 0 ? rpio.write(config.lock.openPIN, rpio.HIGH) : rpio.write(config.lock.openPIN, rpio.LOW)

return rpio.read(config.lock.openPIN);
}

module.doorPowerState = function () {
return rpio.read(config.lock.powerPIN);
}

module.doorOpenState = function () {
return rpio.read(config.lock.openPIN);
}
var log = new logSystem(config.main.logDirectory, doorControl);
var powerState = false;

function _doorAttach() {
// connect relay and default power open and door close
rpio.open(config.lock.powerPIN, rpio.OUTPUT, rpio.HIGH);
rpio.open(config.lock.openPIN, rpio.OUTPUT, rpio.LOW);
// let module allow be use
powerState = true;
}

function _doorDetach() {
// disconnect rpio
rpio.close(config.lock.powerPIN, rpio.PIN_RESET);
rpio.close(config.lock.openPIN, rpio.PIN_RESET);
// let module disabled
powerState = false;
}

// listen event functions
// event functions
function _doorPowerPush() {
if(powerState == false) {

} else {

}
// push Line API
// logging
}

function _doorStatePush() {
function _doorStatePush(state, message) {
if(state == 0) {

} else {

}
// push Line API
// logging
}

module.doorInit = function () {
_doorAttach();
}

// control functions

module.doorPowerSwitch = function () {
powerState == false ? _doorAttach() : _doorDetach();
_doorPowerPush();

return powerState;
}

module.doorOpenSwitch = function (message) {
if(powerState == true) {
rpio.read(config.lock.openPIN) == 0 ? rpio.write(config.lock.openPIN, rpio.HIGH) : rpio.write(config.lock.openPIN, rpio.LOW);
let currentState = rpio.read(config.lock.openPIN);
_doorStatePush(currentState, message);

return currentState;
} else {
// log failed record
}
}

module.doorPowerState = function () {
return rpio.read(config.lock.powerPIN);
}

module.doorOpenState = function () {
return rpio.read(config.lock.openPIN);
}

return module;
};
37 changes: 31 additions & 6 deletions modules/rfidReader.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const rfid = require('mfrc522-rpi');
const fs = require('fs');
const logSystem = require('./modules/logControl');

module.exports = rfidReader;

function rfidReader(config) {
function rfidReader(config, door) {

var module = {};

var userData = JSON.parse(fs.readFileSync('../offlineData/user.json', 'utf8'));
var log = new logSystem(config.main.logDirectory, rfid);

// listen to SPI channel 0
rfid.initWiringPi(0);

Expand Down Expand Up @@ -34,11 +39,26 @@ function rfidReader(config) {
};
}

module.verify = function() {
function _verify(id) {
for(let studentid in userData) {
for(let index in userData[studentid].card) {
if(userData[studentid].card[index].cardID == id) {
//open
let state = door.doorOpenSwitch("message");
//log.record();
if(state == 0) {

} else {
return true;
}
}
}
}

return false;
}

function read() {
module.read = function () {
// reset card
rfid.reset();

Expand All @@ -57,9 +77,14 @@ function rfidReader(config) {
// If we have the UID, continue
const uid = response.data;

//# Rebuild uid to match card's ID
var cardID = uid[3].toString(16).padStart(2, "0") + uid[2].toString(16).padStart(2, "0") + uid[1].toString(16).padStart(2, "0") + uid[0].toString(16).padStart(2, "0")
return parseInt(cardID, 16);
// Rebuild uid to match card's ID
var cardID = uid[3].toString(16).padStart(2, "0") + uid[2].toString(16).padStart(2, "0") + uid[1].toString(16).padStart(2, "0") + uid[0].toString(16).padStart(2, "0");

return _verify(parseInt(cardID, 16));
}

module.jsonReload = function() {
userData = JSON.parse(fs.readFileSync('../offlineData/user.json', 'utf8'));
}

return module;
Expand Down
Empty file added route/door.js
Empty file.

0 comments on commit b5bdda6

Please sign in to comment.