-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglux-autopsn.js
77 lines (65 loc) · 2.12 KB
/
glux-autopsn.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
require('sugar');
var config = require('./glux-config');
var PSNjs = require('PSNjs');
var restify = require('restify');
var util = require('util');
var gluxHostname = config.gluxHostname;
var gluxClient = restify.createJsonClient(gluxHostname);
var gluxKey = 'AUTOPSN';
var DimmingAnimationLength = 20*1000; // 20 seconds
var UnDimmingAnimationLength = 10*1000; // 10 seconds
var dimTo = 0.2;
var psn = new PSNjs({
email: config.psnUsername,
password: config.psnPassword,
debug: true,
authfile: '.psnAuth'
});
function getPsnState(cb) {
psn.getProfile(config.psnMonitorName, function(error, data) {
// check for an error
if (error)
{
console.log("Error fetching profile: " + error);
return;
}
console.dir(data);
cb(data.presence.primaryInfo.onlineStatus);
});
}
function getGluxState(cb) {
gluxClient.get('/', function (err, req, res, obj) {
var currentBrightnessState = 0.0;
console.log(obj);
if (Object.has(obj.states, gluxKey)) {
currentBrightnessState = obj.states[gluxKey].cachedBrightness;
}
cb(currentBrightnessState);
});
}
function checkStateAndAnimate(cb) {
getPsnState(function(state) {
getGluxState(function (brightnessState) {
console.log(state);
if (state === 'offline' && brightnessState < 1.0) {
console.log('(%s) animating to 1.0: %s', state, brightnessState);
// offline
var animDiff = (brightnessState - dimTo) / (1.0 - dimTo)
var calcTime = UnDimmingAnimationLength - (animDiff * UnDimmingAnimationLength);
var apiCall = util.format('/setModifiedBrightness/%s/%d/%d', gluxKey, 1.0, calcTime);
gluxClient.get(apiCall, function() {});
}
else if (state !== 'offline' && brightnessState > dimTo) {
console.log('(%s) animating to %s: %s', state, dimTo, brightnessState);
var animDiff = (brightnessState - dimTo) / (1.0 - dimTo)
var calcTime = DimmingAnimationLength * animDiff;
var apiCall = util.format('/setModifiedBrightness/%s/%d/%d', gluxKey, dimTo, calcTime);
gluxClient.get(apiCall, function () {} );
}
cb();
});
});
}
checkStateAndAnimate(function() {
console.log('Nothing more to do.');
});