-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (51 loc) · 1.89 KB
/
index.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
require('dotenv').config();
const express = require('express');
const Nanoleaf = require('./Nanoleaf');
const hexToRgb = require('hex-to-rgb');
const GitHub = require("./GitHub");
const app = express();
async function main() {
const nanoleaf = new Nanoleaf(process.env.NANOLEAF_HOST, process.env.NANOLEAF_TOKEN)
if (await nanoleaf.isOn()) {
// Get panel IDs from Nanoleaf API
const panels = await nanoleaf.panels()
const github = new GitHub();
const days = await github.fetchActivity(process.env.GITHUB_USER, panels.length);
// Get the RGB colors for the recent days
const daysColors = days.map(day => hexToRgb(colors()[day]).join(' '));
// Prepare data for Nanoleaf effect
const data = panels.map((panel, i) => `${panel} 1 ${daysColors[i]} 0 5`);
// Push custom effect to Nanoleaf panels
return await nanoleaf.sendEffect(`${panels.length} ${data.join(' ')}`)
}
return true
}
// Get color codes based on current date from colors.json file
function colors() {
const colorsJSON = require('./colors.json')
const currentDate = new Date();
const month = (currentDate.getMonth() + 1).toString().padStart(2, '0'); // Zero-padding month
const day = currentDate.getDate().toString().padStart(2, '0'); // Zero-padding day
return colorsJSON[`${month}/${day}`] || colorsJSON.default;
}
app.get('/', async (req, res) => {
try {
// Call the main function
const result = await main();
// Return 204 on true, otherwise throw 500 error
result ? res.sendStatus(200) : (() => {
throw new Error('Main function did not return true');
})();
} catch (error) {
console.error('Error:', error);
res.status(500).send('An error occurred');
}
})
app.get('/healthcheck', (req, res) => {
res.sendStatus(204);
})
// Start the Express server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});