-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-Brewdog.js
162 lines (121 loc) · 4.43 KB
/
MMM-Brewdog.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
/* Magic Mirror
* Module: MMM-Brewdog
*
* By jsteel715
*
*/
Module.register("MMM-Brewdog", {
// Module config defaults.
defaults: {
useHeader: true, // false if you don't want a header
header: "Brewdog Beers", // Any text you want
maxWidth: "33%",
animationSpeed: 3000, // fade in and out speed
initialLoadDelay: 4250,
retryDelay: 2500,
updateInterval: 30000 // 60 * 60 * 1000, 2 minutes
},
// Define required scripts.
getScripts: function() {
return [];
},
getStyles: function() {
return ["MMM-Brewdog.css"];
},
start: function() {
Log.info("Starting module: " + this.name);
requiresVersion: "2.1.0",
// Initialize
this.url = "https://api.punkapi.com/v2/beers/random";
this.Beer = [];
this.activeItem = 0;
this.scheduleUpdate(); // <-- When the module updates (see below)
},
getDom: function() {
// creating the wrapper
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.maxWidth = "100%";
// The loading sequence
if (!this.loaded) {
wrapper.innerHTML = "Let's grab a cold one ...";
wrapper.classList.add("bright", "light", "small");
return wrapper;
}
// Setting the data
var Keys = Object.keys(this.Beer);
var Beer = this.Beer[Keys[this.activeItem]];
// creating the header
if (this.config.useHeader != false) {
var header = document.createElement("header");
header.classList.add("xsmall", "bright", "light", "header");
header.innerHTML = this.config.header;
wrapper.appendChild(header);
}
// Creating the div's for your data items
var top = document.createElement("div");
top.classList.add("list-row");
// Name element from data
var getBeerName = document.createElement("div");
getBeerName.classList.add("xsmall", "bright", "name");
getBeerName.innerHTML = "Beer Name: ";
wrapper.appendChild(getBeerName);
var i = 0;
var txt = Beer.name; /* The text */
var speed = 100; /* The speed/duration of the effect in milliseconds */
function typeWriter() {
if (i < txt.length) {
getBeerName.innerHTML += txt.charAt(i);
i++;
setTimeout(typeWriter, speed);
}
};
setTimeout(function() {
typeWriter();
}, 3000);
// Tagline element from data
var beerTagline = document.createElement("div");
beerTagline.classList.add("xsmall", "bright", "tagline");
beerTagline.innerHTML = "Tagline: " + Beer.tagline;
// First Brewed elements from data
var beerBrewed = document.createElement("div");
beerBrewed.classList.add("xsmall", "bright", "brewed");
beerBrewed.innerHTML = "First Brewed: " + Beer.first_brewed;
// Description element from data
var beerDescription = document.createElement("div");
beerDescription.classList.add("xsmall", "bright", "description");
beerDescription.innerHTML = "Description: " + Beer.description;
setTimeout(function() {
wrapper.appendChild(beerTagline);
wrapper.appendChild(beerBrewed);
wrapper.appendChild(beerDescription);
wrapper.style.maxWidth = "45%";
}, 6500);
return wrapper;
}, // <-- closes the getDom function from above
// this processes your data
processBeer: function(data) {
this.Beer = data;
this.loaded = true;
},
// this tells module when to update
scheduleUpdate: function() {
setInterval(() => {
this.getBeer();
}, this.config.updateInterval);
this.getBeer(this.config.initialLoadDelay);
var self = this;
},
// this asks node_helper for data
getBeer: function() {
this.sendSocketNotification('GET_BEER', this.url);
},
// this gets data from node_helper
socketNotificationReceived: function(notification, payload) {
if (notification === "BEER_RESULT") {
this.processBeer(payload);
this.updateDom(this.config.animationSpeed);
}
this.updateDom(this.config.initialLoadDelay);
},
});