This repository was archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (94 loc) · 2.29 KB
/
server.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
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const db = require("./db/db");
const app = express();
// by requiring the engine, we initialize and kick it off to
// run
// const dataGenEngine = require('./data_gen/engine');
app.use(express.static(path.join(__dirname, "/public")));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get("/getList", (req, res) => {
var list = ["item1", "item2", "item3"];
res.json(list);
console.log("Sent list of items");
});
// test for database access
app.get("/getRoomList", (req, res) => {
db.query(
`
select *
from room
`
)
.then(model => {
console.log(model);
res.json(model);
})
.catch(err => {
// res.send(`${err}`);
});
});
app.get("/getMonthlyBilling", (req, res) => {
db.monthlyBilling()
.then(model => {
res.json(model);
});
});
app.get("/expensesLog", (req, res) => {
db.expensesLog()
.then(model => {
res.json(model);
});
});
app.get("/adminPageModel", (req, res) => {
db.adminPageModel()
.then(model => {
res.json(model);
});
});
app.get("/dashboardModel", (req, res) => {
db.dashboardModel()
.then(model => {
res.json(model);
});
});
app.get("/powerConsumptionByCategory", (req, res) => {
db.powerConsumptionByCategory()
.then(model => {
res.json(model);
});
});
app.get("/runningMonthlyPowerTotal", (req, res) => {
db.runningMonthlyPowerTotal()
.then(total => {
res.json(total);
});
});
app.post("/turnOn", (req, res) => {
db.turnOn(req.body.objId);
res.end('OK');
});
app.post("/turnOff", (req, res) => {
db.turnOff(req.body.objId);
res.end('OK');
});
/**
* This endpoint expects 2 parameters:
* setting:numeric (degrees Farenheit to set thermostat for)
* head:boolean (true for heat, false for AC)
*/
app.post("/setThermostat", (req, res) => {
if (req.body.setting === undefined || (req.body.heat === undefined)) {
res.end('BAD REQUEST');
return;
}
db.setThermostat(req.body.setting, req.body.heat);
res.end('OK');
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + "/public/index.html"));
});
const myPort = process.env.PORT || 8080;
app.listen(myPort, () => console.log(`Listening on port ${myPort}...`));