-
Notifications
You must be signed in to change notification settings - Fork 1
/
thl.js
66 lines (53 loc) · 2.08 KB
/
thl.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
const readline = require('readline-promise').default;
const fetch = require('node-fetch');
const {google}=require('googleapis');
const sheets = google.sheets('v4');
require('dotenv').config();
const moment = require('moment');
moment().format();
const { HUE_BRIDGE_IP, HUE_API_KEY, LIGHT_ID, SPREADSHEET_ID, GREEN_HUE, GOOGLE_API_KEY } = process.env;
async function getNewGrav() {
const response2 = await fetch(`https://sheets.googleapis.com/v4/spreadsheets/${SPREADSHEET_ID}/values/Report!D2?key=${GOOGLE_API_KEY}`)
const data = await response2.json()
console.log(`New Gravity = ${data.values[0][0]}`);
return data.values[0][0];
};
function setLight(newHue) {
console.log(`New Hue = ${newHue}`);
fetch(`http://${HUE_BRIDGE_IP}/api/${HUE_API_KEY}/lights/${LIGHT_ID}/state`, {
method: 'PUT',
body: JSON.stringify({
on: true,
hue: newHue,
sat: 254
})
})
}
async function mainLogic(startGrav, endGrav, gravConversion, multFact){
console.log('\n' + moment().format("dddd, MMMM Do YYYY, h:mm:ss a"));
let newGrav = await getNewGrav()
if(newGrav>startGrav)
newGrav = startGrav;
else if (newGrav<endGrav)
newGrav = endGrav;
const hueDifference = (newGrav - endGrav) * gravConversion * multFact;
const newHue = Math.floor(GREEN_HUE - hueDifference);
if(newHue < 0) setLight(0);
else setLight(newHue);
}
(async function(){
const timeToWait = 900000, gravConversion = 1000;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
});
const startGrav = await rl.questionAsync("Starting Gravity? ")
const endGrav = await rl.questionAsync("Ending Gravity? ")
const diff = (startGrav - endGrav)*gravConversion;
const multFact = GREEN_HUE / diff;
mainLogic(startGrav, endGrav, gravConversion, multFact)
setInterval(async function(){
mainLogic(startGrav, endGrav, gravConversion, multFact)
}, timeToWait)
})();