-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlastupdate-indicator.controller.js
51 lines (45 loc) · 1.38 KB
/
lastupdate-indicator.controller.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
(function () {
'use strict';
angular
.module('app.widgets.lastupdate', [])
.controller('ngLastupdateCtrl', ['$rootScope', '$scope',
function ($rootScope, $scope) {
function getLastupdate() {
try {
var state = $scope.itemState($scope.config.lastupdate_item);
var now = new Date();
var last = new Date(state.slice(0, state.lastIndexOf("+")) + "Z"); //Crappy safari doesnt like timezone format
var state_tzo = last.getTimezoneOffset() //get state timezone
var timeDiff = Math.abs(last.getTime() + state_tzo*60000 - now.getTime());
var diffHours = Math.ceil(timeDiff / (1000 * 3600));
var val = parseInt(diffHours);
if (val > 100) {
val = 100;
} else if (val < 0) {
val = 0;
}
// Create rgb value from val
if (val <= 50) {
var r = Math.floor((255 * (val / 50)));
var g = 255;
var b = 0;
} else {
var r = 255;
var g = Math.floor((100 - val) / 50 * 255);
var b = 0;
}
$scope.bgIndicator = "rgb(" + r + "," + g + "," + b + ")";
$scope.textIndicator = val + "h";
} catch (err) {
$scope.bgIndicator = "rgb(0, 128, 255))";
$scope.textIndicator = "?";
}
};
// Run function when items update
$rootScope.$on('openhab-update', function (event, item) {
getLastupdate();
});
getLastupdate();
}
]);
})();