-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressServer.js
executable file
·99 lines (79 loc) · 3.37 KB
/
ExpressServer.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
const express = require('express');
const EventEmitter = require('events')
const path = require('path');
const cookieParser = require('cookie-parser');
const yitLogger = require('./Apis/yitLogger')
const QueryAPN = require("./Apis/MiddleWears/QueryAPN");
const ExpressMiddlewares = require("./Apis/MiddleWears/ExpressMiddlewares");
const {EXPRESS_PORT} = require("./Apis/Config");
const {StationRouters} = require("./routes/StationRouters");
const winston = require('winston');
class ExpressServer extends EventEmitter {
constructor(adminToken) {
super()
this.adminToken = adminToken
this.clientsList = []
this.app = express();
//this.app.use(yitLogger);
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
this.app.once('error', function(err) {
if (err.code === 'EADDRINUSE') {
delete this
}
});
this.app.listen(EXPRESS_PORT, () => {
console.log(`EXPRESS RUNNING on PORT ${EXPRESS_PORT}.`)
});
this.setRouters()
}
configure(){
}
async setRouters(){
this.app.get("/HeartBitExpress", (req, res)=>{res.send({finalResult: true, result: "Test work"})})
this.app.post("/Station/SetServer/:boxId",
(req, res, next) => ExpressMiddlewares.StationLoginValidator(this.clientsList, req, res, next),
(req, res)=>{StationRouters.SetServer(req, res, this.clientsList)}
)
this.app.get(
"/Station/QueryInfo/:boxId",
(req, res, next) =>{ExpressMiddlewares.StationLoginValidator(this.clientsList, req, res, next)},
(req, res)=>{ StationRouters.QueryInfo(req, res, this.clientsList)}
)
this.app.get("/Station/rent/:boxId",
(req, res, next) =>ExpressMiddlewares.StationLoginValidator(this.clientsList, req, res, next),
async (req, res)=>{await StationRouters.rentPowerBank(req, res, this.clientsList, false)})
this.app.get("/Station/adminRent/:boxId",
(req, res, next) =>ExpressMiddlewares.StationLoginValidator(this.clientsList, req, res, next),
async (req, res)=>{await StationRouters.rentPowerBank(req, res, this.clientsList, true)})
this.app.get("/Station/ejectSlot/:boxId/:slotNumber",
(req, res, next) =>ExpressMiddlewares.StationLoginValidator(this.clientsList, req, res, next),
async (req, res)=>{
let { client, boxId, slotNumber } = req
console.log('///////////////////////////')
console.log(slotNumber)
await StationRouters.ejectSlot(req, res, this.clientsList)
})
this.app.get(
"/Station/QueryAPN/:boxId/:APNIndex",
(req, res, next) =>ExpressMiddlewares.StationLoginValidator(this.clientsList, req, res, next),
(req, res, next)=>{QueryAPN.dataValidator(req, res, next)},
(req, res)=>{StationRouters.QueryAPN(req, res, this.clientsList)}
)
this.app.get("/Station/SetVoice/:boxId/:level",
(req, res, next) =>ExpressMiddlewares.StationLoginValidator(this.clientsList, req, res, next),
(req, res)=>{StationRouters.SetVoice(req, res, this.clientsList).then(r => {})})
}
addClient(client){
this.clientsList.push(client)
this.emit("listUpdate")
}
removeClientByConnection(connection){
this.clientsList = this.clientsList.filter(client => {
if(client.connection == connection) return false
return true
})
this.emit("listUpdate")
}
}
module.exports = {ExpressServer}