-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (119 loc) · 4.14 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
var express = require('express');
var socket = require('socket.io');
fs = require('fs');
// Load json file, might be redundant
let bins_json = JSON.parse(fs.readFileSync('bins.json').toString());
// Set up server
var view = express();
// Listening
var server = view.listen(4000, function () {
console.log('listening to port 4000');
});
// Adds static folder
view.use(express.static('public'));
// Socket
var io = socket(server);
io.on('connection', function (socket) {
// Socket on-load listener
socket.on('loaded', function () {
io.sockets.emit('json_send', bins_json);
});
socket.on('data_req', function () {
bins_json = JSON.parse(fs.readFileSync('bins.json').toString());
io.sockets.emit('json_req', bins_json)
});
//Listener for compress
socket.on('compress', function (data) {
bins_json = JSON.parse(fs.readFileSync('bins.json').toString());
if (data == 'bin1') {
bins_json[data].start_compress = 1;
} else {
bins_json[data].compress = 1;
}
let json = JSON.stringify(bins_json, null, 4);
fs.writeFile('bins.json', json, 'utf8');
});
});
setInterval(function () {
bins_json = JSON.parse(fs.readFileSync('bins.json').toString());
for (var bin in bins_json) {
if (bin == 'bin1') {
} else {
if (bins_json[bin].battery < 1) {
bins_json[bin].battery = Math.random() * 50 + 50;
} else {
bins_json[bin].battery -= Math.random() * 0.5;
}
if (bins_json[bin].compress == 1) {
bins_json[bin].saturation = 0;
bins_json[bin].compress = 0;
bins_json[bin].start_compress = 0;
bins_json[bin].cycles += 1;
event = new Date();
bins_json[bin].timestamp = event.toJSON();
} else {
bins_json[bin].saturation += Math.random() * 0.5;
};
}
}
let json = JSON.stringify(bins_json, null, 4);
fs.writeFile('bins.json', json, 'utf8');
}, 60000);
////////////////BACK-END////////////
let m = JSON.parse(fs.readFileSync('bins.json').toString());
// Updates original object
function update_json(original, incoming) {
Object.keys(incoming).forEach(function (key_1) {
Object.keys(original[key_1]).forEach(function (key_2) {
if (key_2 in incoming[key_1]) {
original[key_1][key_2] = incoming[key_1][key_2];
}
})
});
}
function update_bin(original, incoming) {
Object.keys(incoming).forEach(function (key) {
// Update if previous value is string
if (typeof original[key] === 'string' || original[key] instanceof String) {
original[key] = incoming[key];
}
// Update if previous value is num
else if (!isNaN(incoming[key])) {
original[key] = parseFloat(incoming[key]);
}
});
}
// Check if bin is compressing
function compress_cycle(original, incoming) {
if (original.compress == 1 && parseFloat(incoming.compress) == 0) {
original.start_compress = 0;
// Add compress cycle
original.cycles += 1;
// Resert start_compress
// Add new timestamp
var timestamp = new Date();
original.timestamp = timestamp;
}
}
// Create back-end server
var app = express();
app.get('/send/', function (req, res) {
// Read json
let m = JSON.parse(fs.readFileSync('bins.json').toString());
// Check if compress is changed from active to inactive
compress_cycle(m.bin1, req.query);
// Update data based on query string
update_bin(m.bin1, req.query);
// Send updated start_compress
res.send({ start_compress: m.bin1.start_compress });
let json = JSON.stringify(m, null, 4);
// Write beautified json
fs.writeFile('bins.json', json, 'utf8');
});
app.get('/data/', function (req, res) {
let m = JSON.parse(fs.readFileSync('bins.json').toString());
// Export beautified json
let json = JSON.stringify(m, null, 4);
res.set({ 'Content-Type': 'application/json; charset=utf-8' }).send(json);
});
app.listen(3000);