-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-BinaryClock.js
173 lines (146 loc) · 4.03 KB
/
MMM-BinaryClock.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* global Module */
/* Magic Mirror
* Module: MMM-BinaryClock
*
* By Nik Roberts
* MIT Licensed.
*/
Module.register("MMM-BinaryClock", {
defaults: {
updateInterval: 1000, // set to one second for testing
retryDelay: 5000,
size: 35,
},
requiresVersion: "2.1.0", // Required version of MagicMirror
// Define required scripts. ** moment is for accessing the time **
getScripts: function() {
return ["moment.js"];
},
getStyles: function () {
return [
"MMM-BinaryClock.css",
];
},
start: function() {
var self = this;
var dataRequest = null;
var dataNotification = null;
// load images for the display from the img folder
// to change the images, download image for on and off then rename the below.
this.LEDon = 'img/green-led-on.png';
this.LEDoff = 'img/green-led-off.png';
//Flag for check if module is loaded
this.loaded = false;
this.running = false;
// Schedule update timer.
setInterval(function() {
self.updateBinTime();
}, this.config.updateInterval);
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update.
* If empty, this.config.updateInterval is used.
*/
// calculate time in binary as a string
getBinTime: function(){
var date = moment();
var hours = date.hours();
this.binHours = hours.toString(2).padStart(6,"0");
var minutes = date.minutes();
this.binMins = minutes.toString(2).padStart(6,"0");
var seconds = date.seconds();
this.binSecs = seconds.toString(2).padStart(6,"0");
},
// Update time and refresh DOM
updateBinTime: function(){
this.getBinTime();
var h = this.binHours;
var m = this.binMins;
var s = this.binSecs;
var binH = h.split("");
var binM = m.split("");
var binS = s.split("");
// Loop hour min and secs
for(var a = 0; a < 3 ; a++){
// loop for each string element
for(var j = 5; j>-1 ; j--){
// check which compnent H M S
switch (a) {
case 0: //hours
var img = document.getElementById(`Ho${j}`);
//console.log(img);
binH[j]==1 ? img.style.display = "block" : img.style.display = "none";
break;
case 1: //mins
var img = document.getElementById(`Mo${j}`);
//console.log(img);
binM[j]==1 ? img.style.display = "block" : img.style.display = "none";
break;
case 2: //sec
var img = document.getElementById(`So${j}`);
//console.log(img);
binS[j]==1 ? img.style.display = "block" : img.style.display = "none";
break;
};
}
}
},
/* build dom
*/
getDom: function() {
var LEDoff = this.LEDoff;
var LEDon = this.LEDon;
var wrapper = document.createElement("div");
wrapper.id = 'binary_clock';
var container = document.createElement("div");
container.id = 'container';
var s= this.config.size;
container.style.height = (s*3.5)+"px";
container.style.width = (s*6.5)+"px";
//console.log("build DOM");
//loop to build DOM first load
for(y = 0; y<3 ; y++){
switch(y) {
case 0:
timeIndicator = "H";
break;
case 1:
timeIndicator = "M";
break;
case 2:
timeIndicator = "S";
break;
}
for(var x = 0 ; x < 6 ; x++){
var div = document.createElement("div");
for(var z=0 ; z<2 ; z++){
var a = document.createElement('a');
var img = document.createElement("img");
z==0 ? img.src=this.file(this.LEDoff) : img.src=this.file(this.LEDon);
z==0 ? img.id = timeIndicator+"x"+x : img.id = timeIndicator+"o"+x;
img.style.position = "relative";
img.style.left = x*(s+(s/10))+"px";
img.style.top = y*(s+(s/5))+"px";
img.width = s;
if(z==1){img.style.display = "none"}
a.appendChild(img);
div.appendChild(a);
}
container.appendChild(div);
}
}
this.running = true;
wrapper.appendChild(container);
return wrapper;
},
// Load translations files
getTranslations: function() {
//FIXME: This can be load a one file javascript definition
return {
en: "translations/en.json",
es: "translations/es.json"
};
}
});