-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
140 lines (112 loc) · 3.28 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
const http = require('http')
const express = require('express')
const app = express()
const { BTCMD_GetStatus } = require('./lib/cgiByteCodeMap')
const { config } = require('./config')
const code = 'a'
let logging = true
process.argv.forEach(function (val, index, array) {
if (val == 'no-log') {
logging = false
}
});
function parseOutputStatus(outputStatus) {
let outputs = {
'Heat Elements': [],
Pumps: [],
Valves: [],
};
for (let i = 0; i < 2; i++) {
if (outputStatus & (1 << (11 + i))) {
const heating = ['HTL', 'Boil'];
outputs['Heat Elements'].push(heating[i]);
}
}
for (let i = 0; i < 3; i++) {
if (outputStatus & (1 << (8 + i))) {
const pumps = ['HTL', 'Mash', 'Boil'];
outputs['Pumps'].push(pumps[i]);
}
}
for (let i = 0; i < 8; i++) {
if (outputStatus & (1 << i)) {
outputs['Valves'].push(i + 1);
}
}
return outputs;
}
async function startLog() {
const sendLogItem = (id, logItem) => {
const newLogItem = logItem;
let outputs = parseOutputStatus(logItem['outputStatus']);
console.log('Activated Outputs:');
if (outputs['Heat Elements'].length > 0) {
console.log('Heating Elements: ' + outputs['Heat Elements'].join(', '));
}
if (outputs['Pumps'].length > 0) {
console.log('Pumps: ' + outputs['Pumps'].join(', '));
}
if (outputs['Valves'].length > 0) {
console.log('Valves: ' + outputs['Valves'].join(', '));
}
console.log(`Temperatures:`)
console.log(`HLT: ${newLogItem['HLT_Temperature']} MASH: ${newLogItem['Mash_Temperature']} BOIL: ${newLogItem['Kettle_Temperature']}`)
}
setInterval(() => {
const logItem = {
_type: 'logItem',
_key: new Date().getTime(),
timestamp: new Date()
}
http.get(
`${config.url}?${code}`, (resp) => {
let data = ''
resp.on('data', (chunk) => {
data += chunk;
})
resp.on('end', () => {
BTCMD_GetStatus().rspParams.map((param, i) => {
logItem[param] = JSON.parse(data)[i]
})
if (logItem['responseCode'] === code) {
sendLogItem(lastLog._id, logItem)
}
//else {
// console.log('Got empty status from brewtroller', data)
//}
})
}
).on("error", (err) => {
console.log("Error connecting to Brewtroller " + err.message)
})
}, config.intervalSeconds * 1000 || 10000)
}
if (logging) {
startLog()
} else {
console.log('Logging disabled')
}
app.get('/', function (req, res) {
res.send('Heidrun says hello 🍺')
})
app.get('/api/btnic', function (req, res) {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
const params = req._parsedUrl.query
console.log('parsedUrl.query', req._parsedUrl.query)
http.get(
`${config.url}?${params}`, resp => {
let data = ''
resp.on('data', chunk => {
data += chunk
})
resp.on('end', () => {
res.send(`OK ${data}`)
return JSON.parse(data)
})
}
).on('error', err => {
console.error(`Error connecting to Brewtroller ${err.message}`)
})
})
app.listen(3000, () => console.log('Staring Heidrun Server: port 3000!'))