-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
174 lines (149 loc) · 5.04 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
require('dotenv').config();
const express = require('express');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const cron = require('node-cron');
const WebSocket = require('ws');
const http = require('http');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const port = process.env.PORT || 3015;
const dataPath = path.join(__dirname, process.env.DATA_PATH);
// Parse comma-separated environment variables into arrays
const excludedTemperatureKeywords = process.env.EXCLUDED_TEMPERATURE_KEYWORDS.split(',');
const excludedEntities = process.env.EXCLUDED_TEMPERATURE_ENTITIES.split(',');
const temperatureIdentifiers = process.env.TEMPERATURE_IDENTIFIERS.split(',');
const humidityIdentifiers = process.env.HUMIDITY_IDENTIFIERS.split(',');
const airQualityIdentifiers = process.env.AIR_QUALITY_IDENTIFIERS.split(',');
let sensorData = {
temperatureSensors: [],
humiditySensors: [],
airQualitySensors: []
};
function timeAgo(timestamp) {
const now = new Date();
const time = new Date(timestamp);
const diff = Math.floor((now - time) / 1000);
if (diff < 60) return `${diff} seconds ago`;
const minutes = Math.floor(diff / 60);
if (minutes < 60) return `${minutes} minutes ago`;
const hours = Math.floor(minutes / 60);
if (hours < 24) return `${hours} hours ago`;
return `${Math.floor(hours / 24)} days ago`;
}
function broadcastData() {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(sensorData));
}
});
}
function formatSensorData(sensor) {
return {
...sensor,
state: Math.round(Number(sensor.state)),
time_ago: sensor.last_changed ? timeAgo(sensor.last_changed) : 'Unknown'
};
}
function isValidState(state) {
return !['unavailable', 'unknown', ''].includes(state.toLowerCase());
}
function processSensorData(allData) {
// Temperature sensors
sensorData.temperatureSensors = allData
.filter(entity => {
const isTemperature = temperatureIdentifiers.some(id =>
entity.entity_id.toLowerCase().includes(id)) ||
entity.attributes?.device_class === 'temperature';
const notExcluded = !excludedTemperatureKeywords.some(keyword =>
(entity.attributes?.friendly_name || '').toLowerCase().includes(keyword));
const notExcludedEntity = !excludedEntities.includes(entity.entity_id);
return isTemperature && isValidState(entity.state) && notExcluded && notExcludedEntity;
})
.map(formatSensorData);
// Humidity sensors
sensorData.humiditySensors = allData
.filter(entity => {
const isHumidity = humidityIdentifiers.some(id =>
entity.entity_id.toLowerCase().includes(id)) ||
entity.attributes?.device_class === 'humidity';
return isHumidity && isValidState(entity.state);
})
.map(formatSensorData);
// Air quality sensors
sensorData.airQualitySensors = allData
.filter(entity => {
const isAirQuality = airQualityIdentifiers.some(id =>
entity.entity_id.toLowerCase().includes(id)) ||
entity.attributes?.device_class?.includes('air_quality');
return isAirQuality && isValidState(entity.state);
})
.map(formatSensorData);
}
function loadData() {
try {
const rawData = fs.readFileSync(dataPath, 'utf8');
const allData = JSON.parse(rawData);
processSensorData(allData);
broadcastData();
} catch (error) {
console.error('Error loading or parsing data:', error.message);
}
}
function fetchData() {
const curlCommand = `
curl -X GET -H "Authorization: Bearer ${process.env.HA_BEARER_TOKEN}" \
-H "Content-Type: application/json" \
${process.env.HA_API_URL} > ${dataPath}
`;
exec(curlCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error fetching data: ${error.message}`);
return;
}
if (stderr) {
console.error(`Curl stderr: ${stderr}`);
}
loadData();
});
}
// WebSocket connection handling
wss.on('connection', (ws) => {
console.log('New client connected');
ws.send(JSON.stringify(sensorData));
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
ws.on('close', () => {
console.log('Client disconnected');
});
});
// Setup routes and static files
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('index', sensorData);
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
// Schedule data fetching
cron.schedule(process.env.UPDATE_INTERVAL, fetchData);
// Initial fetch
fetchData();
// Start server with error handling
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
}).on('error', (error) => {
console.error('Server error:', error);
});
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
});
process.on('unhandledRejection', (error) => {
console.error('Unhandled Rejection:', error);
});