-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
72 lines (59 loc) · 1.93 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
62
63
64
65
66
67
68
69
70
71
72
const express = require('express');
const multer = require('multer');
const dotenv = require('dotenv');
const got = require('got');
const SunCalc = require('suncalc');
const { addDays, addHours, isWithinRange } = require('date-fns');
dotenv.config({
path: process.env.ENV_FILE || '.env',
});
const toggleLights = require('./lights');
var app = express();
var upload = multer({ dest: '/tmp/' });
// Allows to easily modify the current time to test things
const now = () => new Date();
function isDarkOutside() {
const latlng = process.env.SUN_LATLNG;
if (!latlng) {
return true;
}
const [latitude, longitude] = latlng.split(',');
const sunriseDate = SunCalc.getTimes(now(), latitude, longitude).sunrise;
const sunsetDate = SunCalc.getTimes(now(), latitude, longitude).sunset;
return !isWithinRange(now(), sunriseDate, addHours(sunsetDate, 1));
}
// Plex webhook event constants
const PLAY = 'media.play';
const PAUSE = 'media.pause';
const RESUME = 'media.resume';
const STOP = 'media.stop';
const PLEX_RELEVANT_TYPES = ['movie', 'episode'];
app.post('/plex_webhook', upload.single('thumb'), function(req, res, next) {
var payload = JSON.parse(req.body.payload);
const { event, Player, Metadata } = payload;
console.log(
'Plex webhook:',
Metadata.type,
event,
'player uuid:',
Player.uuid
);
if (
process.env.PLEX_PLAYER_UUID === Player.uuid && // Event came from the correct player
PLEX_RELEVANT_TYPES.includes(Metadata.type) && // Event type is from a movie
[PLAY, STOP, PAUSE, RESUME].includes(event) // Event is a valid type
) {
const turnOff = event === PLAY || event === RESUME;
if (turnOff) {
toggleLights(true);
return res.sendStatus(200);
}
if (!isDarkOutside()) {
console.log('Skip toggling lights because it is not yet dark outside');
return res.sendStatus(200);
}
toggleLights(false);
}
res.sendStatus(200);
});
app.listen(process.env.PORT);