-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Bosch-BME680-sensor.js
executable file
·157 lines (142 loc) · 4.86 KB
/
MMM-Bosch-BME680-sensor.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
/**
* This MagicMirror² module is designed for a Bosch BME680 sensor and can display temperature/humidity/pressure/IAQ.
* @module MMM-Bosch-BME680-sensor
* @class Module
* @see `README.md`
* @author Sébastien Mazzon
* @license MIT - @see `LICENCE.txt`
*/
"use strict";
Module.register("MMM-Bosch-BME680-sensor", {
/**
* Default properties of the module
* @see `module.defaults`
* @see <https://docs.magicmirror.builders/development/core-module-file.html#defaults>
*/
defaults: {
/* Display configuration */
updateInterval: 30 * 1000,
animationSpeed: 1000,
decimalSymbol: ".", // Symbol of decimal separator
showAqiAtLevel: 0, // Show Air Quality only if AQI is worst or equals to a level (0 to always displaying, undefined to never displaying)
/* Driver configuration */
mock: false, // Use a true BME680 sensor or mock data?
i2cAddress: 0x76, // I²C address of BME680
offsetTemperature: 0, // Offset to apply to sensor temperature
gasLimitLow: 5000, // Bad air quality limit (value from Bosch specs)
gasLimitHigh: 50000, // Good air quality limit (value from Bosch specs)
},
/**
* Initializes module
* @see `module.start`
* @see <https://docs.magicmirror.builders/development/core-module-file.html#start>
*/
start: function () {
// Add custom filters for Nunjucks
this.addFilters();
// Send config to initialize node
this.sendSocketNotification("CONFIG", this.config);
},
/**
* Returns the title of the module
* @see <https://docs.magicmirror.builders/development/core-module-file.html#getheader>
* @returns {string}
*/
getHeader: function () {
return this.data.header || this.translate("TITLE");
},
/**
* Returns the template file used by Nunjucks
* @see `module.getTemplate`
* @returns {string} Path to Nunjucks template file
*/
getTemplate: function () {
return `${this.name}.njk`;
},
/**
* Returns the data to be used in the template
* @see `module.getTemplateData`
* @returns {indoor: {temperature: float, humidity: float, pressure: float, gas_resistance: float, aqi: integer, aqi_level: float}} or {} if no data
*/
getTemplateData: function () {
return {
config: this.config,
indoor: this.dataSensors,
};
},
/**
* Returns the CSS files used by Nunjucks
* @see `module.getStyles`
* @see <https://docs.magicmirror.builders/development/core-module-file.html#getstyles>
* @returns {Array}
*/
getStyles: function () {
return [`${this.name}.css`];
},
/**
* Returns the json files used for string translation
* @see `module.getTranslations`
* @see <https://docs.magicmirror.builders/development/core-module-file.html#gettranslations>
* @returns {Array}
*/
getTranslations: function () {
return {
en: "translations/en.json",
fr: "translations/fr.json",
};
},
/**
* Handles sensor data from node
* @see `module.socketNotificationReceived`
* @see <https://docs.magicmirror.builders/development/core-module-file.html?#socketnotificationreceived-function-notification-payload>
* @param {temperature: float, humidity: float, pressure: float, gas_resistance: float, aqi: integer, aqi_level: float} data Sensor data
*/
processData: function (data) {
// Send notifications with sensor data to others modules
this.sendNotification("INDOOR_TEMPERATURE", data.temperature);
this.sendNotification("INDOOR_HUMIDITY", data.humidity);
this.sendNotification("INDOOR_PRESSURE", data.pressure);
this.sendNotification("INDOOR_GAS", data.gas_resistance);
this.sendNotification("INDOOR_AQI_LEVEL", data.aqi_level);
this.sendNotification("INDOOR_AQI", data.aqi);
// Store data into module
this.dataSensors = data;
// Update DOM (only if module is visible)
if (!this.hidden) {
this.updateDom(this.config.animationSpeed);
}
},
/**
* Called when a socket notification arrives.
* @see `module.socketNotificationReceived`
* @see <https://docs.magicmirror.builders/development/core-module-file.html?#socketnotificationreceived-function-notification-payload>
* @param {string} notification The identifier of the notification.
* @param {*} payload The payload of the notification.
*/
socketNotificationReceived: function (notification, payload) {
if (notification === "DATA") {
// Sensor data from node
this.processData(payload);
}
},
/**
* Adds functions used in Nunjucks template to Nunjucks environment
*/
addFilters: function () {
// roundValue1dec: rounds values to max 1 decimal and replace "-0" by "0"
this.nunjucksEnvironment().addFilter(
"roundValue1dec",
function (value) {
const roundValue = parseFloat(value).toFixed(1);
return roundValue === "-0" ? 0 : roundValue;
}
);
// decimalSymbol: replaces decimal separator by the one defined in config
this.nunjucksEnvironment().addFilter(
"decimalSymbol",
function (value) {
return value.toString().replace(/\./g, this.config.decimalSymbol);
}.bind(this)
);
},
});