-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This reverts commit 24054da.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
NODE_ENV=production | ||
#express server config | ||
|
||
PORT=8080 | ||
HOST=localhost | ||
HOST_URL=http://localhost:8080 | ||
|
||
#firebase database config | ||
#Never Use | ||
|
||
API_KEY=AIzaSyCIl6Mrk6PggGNbah22tETIQK27Eh68aVw | ||
AUTH_DOMAIN=nodewithfirebase-af6e9.firebaseapp.com | ||
PROJECT_ID=nodewithfirebase-af6e9 | ||
STORAGE_BUCKET=nodewithfirebase-af6e9.appspot.com | ||
MESSAGING_SENDER_ID=154374084104 | ||
APP_ID=1:154374084104:web:7b39a844cc07f5b18563bd |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
const dotenv = require('dotenv'); | ||
const assert = require('assert'); | ||
|
||
dotenv.config(); | ||
|
||
const { | ||
PORT, | ||
HOST, | ||
HOST_URL | ||
} = process.env; | ||
|
||
assert(PORT, 'PORT is required'); | ||
assert(HOST, 'HOST is required'); | ||
|
||
module.exports = { | ||
port: PORT, | ||
host: HOST, | ||
url: HOST_URL, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
'use strict'; | ||
|
||
const Table = require('../models/Table'); | ||
const admin = require('firebase-admin'); | ||
|
||
// 수정 부분 | ||
var SerialPort = require('serialport'); | ||
|
||
// Firestore 등록 | ||
var serviceAccount = require("../path/kpu-summerproject-firebase-adminsdk-1gekz-81fd6cf343.json"); | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount) | ||
}); | ||
const db = admin.firestore(); | ||
|
||
// 아두이노 데이터 받을 준비 | ||
|
||
|
||
// 아두이노에서 압력 데이터 받아서 Firestore로 넘기기 | ||
const addArduinoData = async (req, res, next) => { | ||
try { | ||
const parsers = SerialPort.parsers; | ||
const parser = new parsers.Readline( | ||
{ | ||
delimiters: '\r\n' | ||
} | ||
); | ||
|
||
var port = new SerialPort('COM10',{ | ||
baudRate: 9600, | ||
dataBits: 8, | ||
parity: 'none', | ||
stopBits: 1, | ||
flowControl: false | ||
}); | ||
|
||
const data = parser.on('pulseData', function(pulseData){ | ||
console.log(pulseData); | ||
req.body = pulseData; | ||
console.log(req.body); | ||
data = req.body; | ||
console.log(data); | ||
}); | ||
await db.collection('Table_Use_Information').doc('Table_1').set(data); | ||
res.send(data); | ||
console.log(data); | ||
} catch(error) { | ||
res.status(400).send(error.message); | ||
console.log("Failed with error: " + error); | ||
} | ||
} | ||
|
||
// 기존 구현 | ||
const addTable = async (req, res, next) => { | ||
try { | ||
const data = req.body; | ||
await db.collection('Table_Use_Information').doc().set(data); | ||
res.send('Record saved successfuly'); | ||
console.log('Record saved successfuly'); | ||
} catch(error) { | ||
res.status(400).send(error.message); | ||
console.log("Failed with error: " + error); | ||
} | ||
} | ||
|
||
const getAllTableInfo = async (req, res, next) => { | ||
try { | ||
const tableInfo = await db.collection('Table_Use_Information'); | ||
const data = await tableInfo.get(); | ||
const tableInfoArray = []; | ||
if(data.empty) { | ||
res.status(404).send('No Table record found'); | ||
}else { | ||
data.forEach(doc => { | ||
const table = new Table( | ||
doc.id, | ||
doc.data().firstName, | ||
doc.data().lastName, | ||
doc.data().fatherName, | ||
doc.data().class, | ||
doc.data().age, | ||
doc.data().phoneNumber, | ||
doc.data().subject, | ||
doc.data().year, | ||
doc.data().semester, | ||
doc.data().status | ||
); | ||
tableInfoArray.push(table); | ||
}); | ||
res.send(tableInfoArray); | ||
} | ||
} catch (error) { | ||
res.status(400).send(error.message); | ||
} | ||
} | ||
|
||
const getTable = async (req, res, next) => { | ||
try { | ||
const id = req.params.id; | ||
const table = await db.collection('Table_Use_Information').doc(id); | ||
const data = await table.get(); | ||
if(!data.exists) { | ||
res.status(404).send('Table with the given ID not found'); | ||
}else { | ||
res.send(data.data()); | ||
} | ||
} catch (error) { | ||
res.status(400).send(error.message); | ||
} | ||
} | ||
|
||
const updateTable = async (req, res, next) => { | ||
try { | ||
const id = req.params.id; | ||
const data = req.body; | ||
const table = await db.collection('Table_Use_Information').doc(id); | ||
await table.update(data); | ||
res.send('Table record updated successfuly'); | ||
} catch (error) { | ||
res.status(400).send(error.message); | ||
} | ||
} | ||
|
||
const deleteTable = async (req, res, next) => { | ||
try { | ||
const id = req.params.id; | ||
await db.collection('Table_Use_Information').doc(id).delete(); | ||
res.send('Record deleted successfuly'); | ||
} catch (error) { | ||
res.status(400).send(error.message); | ||
} | ||
} | ||
|
||
module.exports = { | ||
addArduinoData, | ||
addTable, | ||
getAllTableInfo, | ||
getTable, | ||
updateTable, | ||
deleteTable | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
const cors = require('cors'); | ||
const bodyParser = require('body-parser'); | ||
const config = require('./config'); | ||
//const routes = require('./routes/routes'); | ||
|
||
const app = express(); | ||
|
||
app.use(express.json()); | ||
app.use(cors()); | ||
app.use(bodyParser.json()); | ||
|
||
//app.use('/api', routes.routes); | ||
|
||
app.listen(config.port, () => console.log('App is listening on url http://localhost:' + config.port)); | ||
|
||
var SerialPort = require('serialport'); | ||
|
||
// Firestore 등록 | ||
const admin = require('firebase-admin'); | ||
|
||
var serviceAccount = require("./path/kpu-summerproject-firebase-adminsdk-1gekz-81fd6cf343.json"); | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount) | ||
}); | ||
const db = admin.firestore(); | ||
|
||
|
||
const parsers = SerialPort.parsers; | ||
const parser = new parsers.Readline( | ||
{ | ||
delimiters: '\r\n' | ||
} | ||
); | ||
|
||
var port = new SerialPort('COM10',{ | ||
baudRate: 9600, | ||
dataBits: 8, | ||
parity: 'none', | ||
stopBits: 1, | ||
flowControl: false | ||
}); | ||
|
||
port.pipe(parser); | ||
|
||
const tableData = { | ||
userId: '', | ||
useInfo: false | ||
}; | ||
|
||
parser.on('data', function(data){ | ||
console.log(data); | ||
const promise = new Promise((resolve, reject)=>{ | ||
if(data.startsWith('T')){ | ||
resolve('자리비움'); | ||
}else{ | ||
reject('이용중'); | ||
} | ||
}); | ||
|
||
promise | ||
.then((message) => { | ||
try { | ||
const table = db.collection('Table_Use_Information').doc(data.substring(0, data.length-1)); | ||
table.update(tableData); | ||
console.log("Success"); | ||
} catch (error) { | ||
console.log("Fail"); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error(`${data}: 자리 이용중`); | ||
}) | ||
.finally(() => { | ||
//console.log('promise 작동중'); | ||
}) | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Table { | ||
constructor(userId, useInfo) { | ||
this.userId = userId; | ||
this.useInfo = useInfo; | ||
} | ||
} | ||
|
||
module.exports = Table; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.