/
app.js
executable file
·210 lines (182 loc) · 5.77 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var express = require('express');
var path = require('path')
var fs = require('fs');
var app = express();
var engine = require('ejs-locals');
var bodyParser = require('body-parser');
var admin = require("firebase-admin");
const Multer = require('multer');
const {format} = require('util');
const multer = Multer({
storage: Multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
},
});
const swaggerJsDoc = require('swagger-jsdoc')
const swaggerUI = require('swagger-ui-express')
const fileUpload = require('express-fileupload');
const cors = require('cors');
const morgan = require('morgan');
const _ = require('lodash');
const {Storage} = require('@google-cloud/storage');
const storage = new Storage({
projectId: "test-a9217",
keyFilename: './test-a9217-firebase-adminsdk-tppo7-7d982aa13e.json'
});
const bucket = storage.bucket("gs://test-a9217.appspot.com");
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(fileUpload({
createParentPath: true,
limits: {
fileSize: 2 * 1024 * 1024 * 1024 //2MB max file(s) size
},
}));
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(morgan('dev'));
// var multer = require('multer')
// var upload = multer({ dest: 'upload/' });
const swaggerOptions = {
swaggerDefinition:{
info:{
title:"Customer APi",
description:"API for SeatMap",
contact:{
name:"andy"
},
servers:["http://localhost:3000"]
}
},
//
apis:["app.js"]
}
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use('/api-docs',swaggerUI.serve,swaggerUI.setup(swaggerDocs));
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
var serviceAccount = require("./test-a9217-firebase-adminsdk-tppo7-7d982aa13e.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://test-a9217.firebaseio.com"
});
var fireData = admin.database();
// console.log(fireData);
app.engine('ejs',engine);
app.set('views','./views');
app.set('view engine','ejs');
//增加靜態檔案的路徑
app.use(express.static('public'))
// 增加 body 解析
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extendedc:false}))
//路由
app.get('/',function(req,res){
fireData.ref('todos').once('value',function(snapshot){
var data = snapshot.val();
return res.render('index',{"todolist":data})
})
})
// getTime function
function getTime(){
let date_ob = new Date();
// current date
// adjust 0 before single digit date
let date = ("0" + date_ob.getDate()).slice(-2);
// current month
let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
// current year
let year = date_ob.getFullYear();
// current hours
let hours = date_ob.getHours();
// current minutes
let minutes = date_ob.getMinutes();
// current seconds
let seconds = date_ob.getSeconds();
// prints date & time in YYYY-MM-DD HH:MM:SS format
let nowTime = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
return nowTime;
}
// 溫濕度
app.post('/updateHumiture',function(req,res){
var humidity = req.body.humidity;
var temperature = req.body.temperature;
var volume = req.body.ao;
var location = req.body.location;
// let date_ob = new Date();
var time = getTime();
// 更新當前溫度
fireData.ref('/locations/'+location).update({"curHum":humidity,"curTemp":temperature});
// 存入歷史資料
var Ref = fireData.ref('/locations/'+location +'/historicalTH').push();
Ref.set({"humidity":humidity,"temperature":temperature,"time":time}).then(function(){
fireData.ref('locations').once('value',function(snapshot){
res.send({
"sucess":true,
"result":snapshot.val(),
"message":"資料讀取成功"
});
})
})
});
// 人數
app.post('/updatePersonCount',function(req,res){
var count = req.body.count;
var location = req.body.location;
// let date_ob = new Date();
var time = getTime();
// 更新當前溫度
fireData.ref('/locations/'+location).update({"curPersonCount":count});
// 存入歷史資料
var Ref = fireData.ref('/locations/'+location+'/historicalPersonCount').push();
Ref.set({"count":count,"time":time}).then(function(){
fireData.ref('locations').once('value',function(snapshot){
res.send({
"sucess":true,
"result":snapshot.val(),
"message":"資料讀取成功"
});
})
})
});
app.get('/images/:img_name',function(req,res){
img_path = path.resolve('./image_uploads/'+req.params.img_name+'.jpg');
console.log(img_path)
if(fs.existsSync(img_path)){
res.send(img_path)
}else{
res.send("不存在")
}
});
app.post('/uploadImage',multer.single('file'), (req, res, next) => {
try {
if(!req.files) {
res.send({
status: false,
message: 'No file uploaded'
});
} else {
//Use the name of the input field (i.e. "avatar") to retrieve the uploaded file
let avatar = req.files.file;
//Use the mv() method to place the file in upload directory (i.e. "uploads")
avatar.mv('./image_uploads/' + avatar.name);
//send response
res.send({
status: true,
message: 'File is uploaded',
data: {
name: avatar.name,
mimetype: avatar.mimetype,
size: avatar.size
}
});
}
} catch (err) {
res.status(500).send(err);
}
})
// 監聽 port
var port = process.env.PORT || 3000;
app.listen(port);