-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-Cronixie.js
136 lines (131 loc) · 3.42 KB
/
MMM-Cronixie.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Magic Mirror
* Module: MMM-Cronixie
*
* By Timophey Lanevich https://cronixie.ru
*/
Module.register("MMM-Cronixie", {
// Default module config.
defaults: {
width: "90vw",
orientation: "landscape", // landscape | not!portrait
reroll: 1,
rerollDelay: 255,
functions: [1,1,1,1,1,0],
ver: 1,
},
values: [0,0,0,0,0,0], // Current values
valuesTo: [0,0,0,0,0,0], // Current valuesTo
valuesFx: [0,0,0,0,0,0], // Function
valuesPr: [0,0,0,0,0,0], // FunctionProcess
valuesLk: [0,0,0,0,0,0], // Values locked
start: function () {
Log.info("Starting module: " + this.name);
if(this.config.width == this.defaults.width && this.config.orientation == "portrait"){
this.config.width = "calc(90vh * 0.5 / 1.3)";
}
// Schedule update interval.
this.valuesFx = this.config.functions;
Log.info(this);
/*
* Timer
* */
var setValueToRoll = function(i,data){
this.valuesTo[i] = data.ds[i];
this.valuesPr[i] = 1;
this.valuesLk[i] = 0;
}
var notificationTimer = function(){
var data = this.getTemplateData();
// reroll 2
if(this.config.reroll == 2 && data.ds[3] != this.valuesTo[3] && this.valuesLk[3]==0){
Log.info('reroll 2');
for(var j=0; j<3; j++){
for(var k=0; k<2; k++){
var i = k + j*2;
if(this.valuesPr[i] || this.valuesLk[i]) continue;
var timeout = this.config.rerollDelay * (1-k);
this.valuesLk[i]=1;
setTimeout(setValueToRoll.bind(this),timeout,i,data);
}
}
return;
}
// reroll 1
if(this.config.reroll == 1 && data.ds[3] != this.valuesTo[3] && this.valuesLk[3]==0){
Log.info('reroll 1');
for(var i in data.ds){
if(this.valuesPr[i] || this.valuesLk[i]) continue;
if(this.valuesPr[i]) continue;
var timeout = this.config.rerollDelay * (5-i);
this.valuesLk[i]=1;
setTimeout(setValueToRoll.bind(this),timeout,i,data);
}
return;
}
// reroll off
for(var i in data.ds){
if(this.valuesTo[i] == data.ds[i]) continue;
if(this.valuesPr[i] || this.valuesLk[i]) continue;
this.valuesPr[i] = this.valuesFx[i];
this.valuesTo[i] = data.ds[i];
}
//Log.info("self.updateDom");
}
/*
* Frame
* */
var renderFrame = function(){
for(var i=0; i<6; i++){
//if(this.valuesTo[i] != this.values[i]){
switch(this.valuesPr[i]){
case 0: this.nixieSet(i,this.valuesTo[i]); break;
case 1: this.nixieRoll(i); break;
}
}
//}
}
setInterval(notificationTimer.bind(this), 500);
setInterval(renderFrame.bind(this), 50);
},
nixieSet: function(i,v){
this.getNixie(i).dataset.d = v;
this.values[i] = v;
if(this.values[i] == this.valuesTo[i]) this.valuesPr[i]=0;
},
nixieRoll: function(i,v){
var goTo = this.values[i] - 1; if(goTo < 0) goTo = 9;
this.nixieSet(i,goTo);
},
getNixie(i){
return document.querySelector("#"+this.identifier+" .nixie"+i);
},
getTemplate: function () {
return "MMM-Cronixie.njk";
},
getTemplateData: function () {
var h = moment().hour();
var i = moment().minute();
var s = moment().second();
return {
ds: [
Math.floor(h / 10),
Math.floor(h % 10),
Math.floor(i / 10),
Math.floor(i % 10),
Math.floor(s / 10),
Math.floor(s % 10)
],
config: this.config,
identifier: this.identifier,
};
},
getStyles: function() {
return [
this.file("css/style.css")
]
},
// Define required scripts.
getScripts: function () {
return ["moment.js", "moment-timezone.js"];
},
});