Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
TMineCola committed May 23, 2018
1 parent 6a4a38b commit a7fcbe4
Show file tree
Hide file tree
Showing 14 changed files with 1,156 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
ENV.json
log/*
offlineData
25 changes: 25 additions & 0 deletions ENV.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"main": {
"port": "3000",
"logDirectory": "/home/pi/raspberryAPI/log",
"linebotAPI": "",
"firebase": {
"apiKey" : "",
"authDomain" : "",
"databaseURL" : "",
"storageBucket" : ""
}
},
"rfid": {

},
"lock": {
"powerPIN": 0,
"openPIN": 0
},
"glass": {
"powerPIN": 0,
"casePIN": 0,
"detectPIN": 0
}
}
4 changes: 4 additions & 0 deletions dataTemplate/permission.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"idNumber": "",
"space": []
}
26 changes: 26 additions & 0 deletions dataTemplate/space.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"spaceID": "",
"space": "",
"spaceRule": [
{
"type": "static",
"method": "",
"period": [
{
"from": "",
"to": ""
}
]
},
{
"type": "schedule",
"method": "",
"period": [
{
"from": "",
"to": ""
}
]
}
]
}
8 changes: 8 additions & 0 deletions dataTemplate/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"idNumber": "",
"name": "",
"identity": "",
"departmentGrade": "",
"email": "",
"lineUserID": ""
}
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const config = require("./ENV.json");
const rpio = require('rpio');
const rfid = require('./modules/rfidReader')(config);
const door = require('./modules/doorControl')(rpio, config);
const logSystem = require('./modules/logControl');

var app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

rpio.init({mapping: 'physical'});

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

var server = app.listen(config.main.port || 8080, function() {
var port = server.address().port;
console.log('API Server is running on port: ' + port + '!');
});
Empty file added middleware/verify.js
Empty file.
54 changes: 54 additions & 0 deletions modules/doorControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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);
}

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);
}

function _doorDetach() {
rpio.close(config.lock.powerPIN, rpio.PIN_RESET);
rpio.close(config.lock.openPIN, rpio.PIN_RESET);
}

// listen event functions
function _doorPowerPush() {
// push Line API
// logging
}

function _doorStatePush() {
// push Line API
// logging
}

return module;
};
32 changes: 32 additions & 0 deletions modules/glassDetect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = function(rpio, config){

var module = {}

module.glassInit = function () {
// connect relay and default power open and door close
rpio.open(config.glass.powerPIN, rpio.OUTPUT, rpio.HIGH);
rpio.open(config.glass.casePIN, rpio.INPUT);
rpio.open(config.glass.detectPIN, rpio.INPUT);
}

// control functions

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

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

// listen event functions
function glassDetectPush() {
// push Line API
// logging
}

function glassCasePush() {
// push Line API
// logging
}

return module;
}();
28 changes: 28 additions & 0 deletions modules/logControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";

const winston = require('winston');
require('winston-daily-rotate-file');

module.exports = class logSystem {
constructor(source, type) {
this.source = source;
this.type = type;
this.transport = new (winston.transports.DailyRotateFile)({
filename: `${type}-%DATE%.log`,
dirname: `${source}/${type}/`,
datePattern: 'YYYY-MMM-DD',
zippedArchive: false,
maxSize: '20m',
maxFiles: '14d'
});
this.logger = new (winston.Logger)({
transports: [
this.transport
]
});
}

record(log) {
this.logger.info(log);
}
};
66 changes: 66 additions & 0 deletions modules/rfidReader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const rfid = require('mfrc522-rpi');

module.exports = rfidReader;

function rfidReader(config) {

var module = {};

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

//*
// @string.padStart(@number, @string)
// padding left
// parameter: @number(total length), @string(padding with which string)
// return @string
//*

// prototype to add left padding
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength,padString) {
targetLength = targetLength>>0; //truncate if number or convert non-number to 0;
padString = String((typeof padString !== 'undefined' ? padString : ' '));
if (this.length > targetLength) {
return String(this);
}
else {
targetLength = targetLength-this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
}
return padString.slice(0,targetLength) + String(this);
}
};
}

module.verify = function() {

}

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

// Scan for cards
var response = rfid.findCard();
if (!response.status) {
return "No Card";
}

// Get the UID of the card
response = rfid.getUid();
if (!response.status) {
return "UID Scan Error";
}

// 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);
}

return module;
};
Empty file added modules/webCam.js
Empty file.
Loading

0 comments on commit a7fcbe4

Please sign in to comment.