-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
185 lines (164 loc) · 4.98 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
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
var express = require('express')
var mysql = require('mysql')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json())
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "iotdb"
})
var seuil =0;
var msg ="";
con.connect(function(err) {
if (err) throw err
console.log("Connected!")
})
app.get('/', function (req, res) {
res.send('Hello Here')
})
/*** ADD ***/
app.get('/addTemp', function (req, res) {
var query = con.query("INSERT INTO temperatures (value) VALUES ("+req.query.value+")", function(err, rows)
{
if (err)
console.log("Error inserting : %s ",err );
else
res.json({msg:"inserted successfully"})
});
})
/*** GET ALL ***/
app.get('/getAll', function (req, res) {
var query = con.query('SELECT * FROM temperatures',function(err,rows)
{
if(err)
console.log("Error Selecting : %s ",err );
else
res.json({rows})
});
})
/*** GET By Id ***/
app.get('/getById', function (req, res) {
var query = con.query('SELECT value FROM temperatures WHERE id='+req.query.id,function(err,result,rows)
{
if(err)
console.log("Error Selecting : %s ",err );
else
res.json(result[0].value)
});
})
/*** GET By Value ***/
app.get('/getByValue', function (req, res) {
var query = con.query('SELECT * FROM temperatures WHERE value='+req.query.value,function(err,result,rows)
{
if(err)
console.log("Error Selecting : %s ",err );
else
res.json({Result:rows})
});
})
/*** DELETE ***/
app.get('/delete', function (req, res) {
var query = con.query("DELETE FROM temperatures WHERE id = "+req.query.id, function(err, rows)
{
if(err)
console.log("Error deleting : %s ",err );
else
res.json({Result:"deleted"})
});
})
/*** UPDATE ***/
app.get('/update', function (req, res) {
var query = con.query("UPDATE temperatures set value="+req.query.value+" WHERE id ="+ req.query.id, function(err, rows)
{
if (err)
console.log("Error Updating : %s ",err );
else
res.json({Result:"updated"})
});
})
/*** SWITCH STATE ***/
app.get('/updateSensor', function (req, res) {
var query = con.query("UPDATE states set status="+req.query.status+" WHERE sensor='"+ req.query.sensor+"'", function(err, rows)
{
if (err)
console.log("Error Updating : %s ",err );
else
//res.send("hhh")
res.json({Result:"updated"})
});
})
/*** ADD MESSAGE ***/
app.get('/sendMessage', function (req, res) {
msg = req.query.content
var query = con.query("INSERT INTO message (content) VALUES ("+req.query.content+")", function(err, rows)
{
if (err)
console.log("Error inserting : %s ",err );
else
res.json({msg:"inserted successfully"})
});
})
/*** ADD VAL ***/
app.get('/updateTemp', function (req, res) {
var query = con.query("UPDATE others set val='"+req.query.currentTmp+"' WHERE etat="+ req.query.etat, function(err, rows)
{
if (err)
console.log("Error Updating : %s ",err );
else
res.json({Result:"updated"})
});
})
/*** ADD SEUIL ***/
app.get('/updateSeuil', function (req, res) {
var query = con.query("UPDATE seuil set value="+req.query.seuil+" WHERE ref=1", function(err, rows)
{
if (err)
console.log("Error Updating : %s ",err );
else{
seuil =req.query.seuil;
res.json({Result:"updated"})
}
});
})
/*** GET SEUIL */
app.get('/getSeuil', function (req, res) {
return res.json(seuil);
/*var query = con.query('SELECT * FROM seuil WHERE ref=1',function(err,result,rows)
{
if(err)
console.log("Error Selecting : %s ",err );
else
// res.json(result[0].value)
});*/
})
/*** GET SEUIL */
app.get('/getSeuil', function (req, res) {
return res.json(seuil);
/*var query = con.query('SELECT * FROM seuil WHERE ref=1',function(err,result,rows)
{
if(err)
console.log("Error Selecting : %s ",err );
else
// res.json(result[0].value)
});*/
})
/*** GET SEUIL */
app.get('/getMsg', function (req, res) {
return res.json(msg);
/*var query = con.query('SELECT * FROM seuil WHERE ref=1',function(err,result,rows)
{
if(err)
console.log("Error Selecting : %s ",err );
else
// res.json(result[0].value)
});*/
})
// Port Number
const port = 4300;
// server address
const address = '172.16.220.137';
app.listen(port, address, () => {
console.log('Server started on address '+address+' and on port '+port);
});