-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (66 loc) · 1.97 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
73
74
75
76
77
78
79
var config = require('./config')
var axios = require('axios')
var lifxUrl = 'https://api.lifx.com/v1/lights'
var determineEvent = null
var updateRequired = false
axios.defaults.headers.common['Authorization'] = 'Bearer ' + config.token;
getWeatherAlerts();
setInterval(function() {getWeatherAlerts() }, config.refreshRate * 1000)
function getWeatherAlerts() {
axios.get('https://api.weather.gov/alerts?active=true&point=' + config.lat + ',' + config.lon).then(function(response) {
var alerts = response.data.features;
for(var i=0; i < alerts.length; i++) {
computeAlertEvent(alerts[i].properties.event)
if(i == (alerts.length - 1)) {
callLifxApi()
}
}
})
}
function computeAlertEvent(event) {
if(event == 'Tornado Warning') {
determineEvent = 'tor'
updateRequired = true;
} else if (event == 'Severe Thunderstorm Warning') {
if(determineEvent !== 'tor') {
determineEvent = 'tstrm'
updateRequired = true
}
} else if (event == 'Flash Flood Warning') {
if(determineEvent !== 'tstrm' && determineEvent !== 'tor') {
determineEvent = 'ffw'
updateRequired = true
}
}
}
function callLifxApi() {
if(determineEvent == null) {
if(updateRequired) {
setLightsToDefaultState()
updateRequired = false;
}
} else {
console.log('updating lights')
axios.put(lifxUrl + '/all/state', {
power: 'on',
fast: true,
color: config.colors.alerts[determineEvent],
brightness: config.colors.default.brightness,
}).catch(function(response) {
// error turing lights to default state
console.log(response)
})
}
}
function setLightsToDefaultState() {
console.log('setting to default')
axios.put(lifxUrl + '/all/state', {
power: 'on',
fast: true,
color: config.colors.default.color,
brightness: config.colors.default.brightness,
}).catch(function(response) {
// error turing lights to default state
console.log(response)
})
}