-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-AirQuality.js
More file actions
98 lines (85 loc) · 2.36 KB
/
Copy pathMMM-AirQuality.js
File metadata and controls
98 lines (85 loc) · 2.36 KB
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
/*
* Magic Mirror
* Module: MMM-AirQuality
*
* By Smart'Gic
* MIT Licensed.
*/
Module.register('MMM-AirQuality', {
result: {
metrics: 'loading metrics...'
},
defaults: {
abstractApiUrl: 'http://192.168.1.66:8123',
mycroftApiUrl: 'http://192.168.1.94:8000',
mycroftApiToken: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJteWNyb2Z0IiwiZXhwIjoxNjQzMjQzMDM5LCJpYXQiOjE2NDMyNDEyMzksImlzcyI6Im15Y3JvZnQtYXBpIiwic2NvcGUiOiJhY2Nlc3MifQ.Ilz3hVvcv76CRSWjjsEJSaxiyi-xYBf7GkcK8mT5jZY',
symbol: 'fa fa-wind',
interval: 15000,
fadeSpeed: 4*1000,
sensor: 'mq2',
speechNotification: true,
speech: 'alert, high ppm level detected in the air, check for gas leak or smoke!',
speechLang: 'en-us',
ledNotification: true,
ledGpio: 20,
ledFrequency: 1000,
ledSpeed: 0.01,
ledStep: 3,
ledDuration: 10
},
start() {
this.sendSocketNotification('INIT_MQ2', this.config)
this.getMetrics();
this.scheduleUpdate();
},
scheduleUpdate() {
setInterval(() => {
this.getMetrics();
}, this.config.interval);
},
getDom() {
var wrapper = document.createElement('div');
var metrics = document.createElement('div');
var symbol = document.createElement('span');
var text = document.createElement('span');
if (this.result['alert']) {
this.sendAlert();
symbol.classList = 'symbol symbol-alert ' + this.config.symbol;
metrics.appendChild(symbol);
text.classList = 'alert';
text.innerHTML = this.translate('ALERT');
metrics.appendChild(text);
} else {
symbol.classList = 'symbol symbol-good ' + this.config.symbol;
metrics.appendChild(symbol);
text.classList = 'good';
text.innerHTML = this.translate('GOOD');
metrics.appendChild(text);
}
wrapper.appendChild(metrics);
return wrapper;
},
sendAlert() {
this.sendSocketNotification('ALERT_MQ2')
},
getMetrics() {
this.sendSocketNotification('GET_MQ2');
},
socketNotificationReceived(notification, payload) {
if (notification == 'DATA_MQ2') {
if (this.result != payload) {
this.result = payload;
this.updateDom(this.config.fadeSpeed);
}
}
},
getStyles () {
return ['MMM-AirQuality.css'];
},
getTranslations() {
return {
en: 'translations/en.json',
fr: 'translations/fr.json'
};
},
});