-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·394 lines (374 loc) · 11 KB
/
app.js
File metadata and controls
executable file
·394 lines (374 loc) · 11 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
var koa = require('koa');
var Router = require('koa-router')
var bodyParser = require('koa-bodyparser');
var MongoClient = require('mongodb').MongoClient;
var app = koa();
app.use(bodyParser());
var router = new Router();
var db;
MongoClient.connect("mongodb://localhost:27017/apple",function(err,pDb){
if(err){
return console.dir(err);
}
db = pDb;
});
router.post('/create/user',createUser);
router.post('/login',login);
router.post('/device/create',createDevice);
router.post('/device/delete',deleteDevice);
router.post('/sensor/create',createSensor);
router.post('/sensor/status',sensorStatus);
router.post('/sensor/update',sensorUpdate);
router.post('/sensor/delete',sensorDelete);
router.post('/customer/opinion',customerOpinion);
router.get('/mbed/:deviceID/:w1/:w2/:w3/:w4/',mbedSensor);
router.get('/popular',popular);
router.get('/customer/opinion',opinion);
function * createUser(){
var request = this.request.body;
var userName = request.userName;
var account = request.account;
var password = request.password;
var collection = db.collection('user');
var judgmentAccount = yield collection.find({account:account}).toArray();
if(judgmentAccount != ""){
this.body = {message:"此信箱已註冊"};
}else{
if(userName != null && account != null && password != null){
var data = {
userName : userName,
account : account,
password : password,
setTime : new Date().getTime()
}
yield collection.insert(data);
this.body = {message:"成功"};
}else{
this.body = {message:"輸入格式錯誤"};
}
}
}
function * login(){
var request = this.request.body;
var account = request.account;
var password = request.password;
var collection = db.collection('user');
var judgmentAccount = yield collection.findOne({account:account,password:password});
if(judgmentAccount != null){
this.body = {
message : "成功",
userName : judgmentAccount.userName,
userID : judgmentAccount._id
}
}else{
this.body = {
message : "查無此用戶",
userName : "",
userID : ""
}
}
}
function * createDevice(){
var request = this.request.body;
var userID = request.userID;
var deviceID = request.deviceID;
var collection = db.collection('device');
var findDev = yield collection.findOne({deviceID:deviceID});
if(findDev == null){
yield collection.insert({'deviceID':deviceID,'setTime':new Date().getTime()});
this.body = {
message : "成功"
}
}else{
this.body = {
message : "此裝置已註冊"
}
}
}
function * deleteDevice(){
var request = this.request.body;
var userID = request.userID;
var deviceID = request.deviceID;
var collectionDevice = db.collection('device');
var collectionSensor = db.collection('sensor');
if(userID != null && deviceID != null){
yield collectionDevice.remove({deviceID:deviceID});
yield collectionSensor.remove({deviceID:deviceID});
this.body = {
message : "成功"
}
}else{
this.body = {
message : "失敗"
}
}
}
function * createSensor(){
var request = this.request.body;
var userID = request.userID;
var deviceID = request.deviceID;
var containerPosition = request.containerPosition;
var containerName = request.containerName;
var brand = request.brand;
var collectionDevice = db.collection('device');
var collectionSensor = db.collection('sensor');
var collectionSensorLog = db.collection('sensorLog');
var deviceStatus = yield collectionDevice.findOne({deviceID:deviceID});
var sensorPosition = yield collectionSensor.findOne({deviceID:deviceID,containerPosition:containerPosition});
if(deviceStatus != null){
if(1 <= parseInt(containerPosition) && parseInt(containerPosition) <= 4){
if(sensorPosition==null){
if(userID != null && deviceID != null && containerPosition != null && containerName != null && brand != null){
var data = {
'userID' : userID,
'deviceID' : deviceID,
'containerPosition' : containerPosition,
'containerName' : containerName,
'brand' : brand,
'weight' : 100,
'setTime' : new Date().getTime()
};
var dataLog = {
'userID':userID,
'deviceID':deviceID,
'containerPosition':containerPosition,
'containerName':containerName,
'brand':brand,
'setTime' : new Date().getTime()
};
yield collectionSensor.insert(data);
yield collectionSensorLog.insert(dataLog);
this.body = {
message : "成功"
}
}else{
this.body = {
message : "失敗"
}
}
}else{
this.body = {
message : "此位子已有容器"
}
}
}else{
this.body = {
message : "位子範圍錯誤"
}
}
}else{
this.body = {
message : "此裝置上未註冊"
}
}
}
function * sensorStatus(){
var request = this.request.body;
var userID = request.userID;
var collection = db.collection('sensor');
var sensorStatus = yield collection.find({userID:userID}).toArray();
var data = new Array();
var device = new Array();
for(var i=0 ; i<sensorStatus.length ; i++){
device.push(sensorStatus[i].deviceID);
}
var result=device.filter(function(element, index, arr){
return arr.indexOf(element)=== index;
});
yield function * (){
for(key in result){
var collection = db.collection('sensor');
var handleData = yield collection.find({deviceID:result[key]}).toArray();
var dataAry = new Array();
for(sensor in handleData){
dataAry.push({
"containerPosition": handleData[sensor].containerPosition,
"containerName": handleData[sensor].containerName,
"brand": handleData[sensor].brand,
"percent": handleData[sensor].weight
})
}
let sensorData = {
"deviceID" : result[key],
"data":dataAry
}
data.push(sensorData);
}
}
this.body = data
}
function * sensorUpdate(){
var request = this.request.body;
var userID = request.userID;
var deviceID = request.deviceID;
var containerPosition = request.containerPosition;
var containerName = request.containerName;
var brand = request.brand;
var collectionSensor = db.collection('sensor');
var collectionSensorLog = db.collection('sensorLog');
var sensorUpdate = yield collectionSensor.findOne({deviceID:deviceID,containerPosition:containerPosition});
if(1 <= parseInt(containerPosition) && parseInt(containerPosition) <= 4){
if(sensorUpdate != null){
if(userID != null && deviceID != null && containerPosition != null && containerName != null && brand != null){
yield collectionSensor.update({deviceID:deviceID,containerPosition:containerPosition},{
'$set': {
'containerName':containerName,
'brand':brand,
'setTime' : new Date().getTime()
}
});
var dataLog = {
'userID':userID,
'deviceID':deviceID,
'containerPosition':containerPosition,
'containerName':containerName,
'brand':brand,
'setTime' : new Date().getTime()
};
yield collectionSensorLog.insert(dataLog);
this.body = {
message : "成功"
}
}else{
this.body = {
message : "失敗"
}
}
}else{
this.body = {
message : "此位子無容器"
}
}
}else{
this.body = {
message : "位子範圍錯誤"
}
}
}
function * sensorDelete(){
var request = this.request.body;
var userID = request.userID;
var deviceID = request.deviceID;
var containerPosition = request.containerPosition;
var collection = db.collection('sensor');
var sensorDelete = yield collection.findOne({userID:userID,deviceID:deviceID,containerPosition:containerPosition});
if(1 <= parseInt(containerPosition) && parseInt(containerPosition) <= 4){
if(sensorDelete != null){
if(userID != null && deviceID != null && containerPosition != null){
yield collection.remove({userID:userID,deviceID:deviceID,containerPosition:containerPosition});
this.body = {
message : "成功"
}
}else{
this.body = {
message : "失敗"
}
}
}else{
this.body = {
message : "此位子無容器"
}
}
}else{
this.body = {
message : "位子範圍錯誤"
}
}
}
function * customerOpinion(){
var request = this.request.body;
var userID = request.userID;
var errMsg = request.errMsg;
var customerMsg = request.customerMsg;
var collection = db.collection('message');
yield collection.insert({
userID : userID,
errMsg : errMsg,
customerMsg : customerMsg,
setTime : new Date().getTime()
});
this.body = {
message : "成功"
}
}
function * mbedSensor(){
var collection = db.collection('sensor');
yield collection.update({deviceID:this.params.deviceID,containerPosition:"1"},{
'$set': {
'weight': this.params.w1/1000*100
}
});
yield collection.update({deviceID:this.params.deviceID,containerPosition:"2"},{
'$set': {
'weight': this.params.w2/1000*100
}
});
yield collection.update({deviceID:this.params.deviceID,containerPosition:"3"},{
'$set': {
'weight': this.params.w3/1000*100
}
});
yield collection.update({deviceID:this.params.deviceID,containerPosition:"4"},{
'$set': {
'weight': this.params.w4/1000*100
}
});
this.body = {
R_1:this.params.w1/1000*100,
R_2:this.params.w2/1000*100,
R_3:this.params.w3/1000*100,
R_4:this.params.w4/1000*100
}
}
function * popular(){
var collection = db.collection('sensorLog');
var data = yield collection.find({}).toArray();
var brand = new Array();
var brandPopular = new Array();
var count = 0;
for(var i=0 ; i<data.length ; i++){
brand.push(data[i].brand);
}
var result=brand.filter(function(element, index, arr){
return arr.indexOf(element)=== index;
});
function * apple(){
var collection = db.collection('sensorLog');
var brandNum = yield collection.find({brand:result[count]}).toArray();
yield function(done){
brandPopular.push(
{
brand:result[count],
count:brandNum.length
}
);
done();
}
if(count<result.length-1){
count++;
yield apple();
}else{
return;
}
}
yield apple();
this.body = brandPopular;
}
function * opinion(){
var collection = db.collection('message');
var data = yield collection.find({}).toArray();
var dataAry = new Array();
for(var i=0 ; i<data.length ; i++){
dataAry.push({
userID : data[i].userID,
errMsg : data[i].errMsg,
customerMsg : data[i].customerMsg,
setTime : data[i].setTime
});
}
this.body = dataAry;
}
app.use(router.middleware());
app.listen(3000,function(){
console.log('listening port on 3000');
});