Skip to content

Commit

Permalink
feat: enable multiple module instances (#40)
Browse files Browse the repository at this point in the history
* implement

* improve logging

* change

* fix
  • Loading branch information
CFenner authored Jan 4, 2024
1 parent f789d7d commit ef6f9ac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
19 changes: 10 additions & 9 deletions MMM-AirQuality.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Module.register('MMM-AirQuality', {
// Default module config.
defaults: {
initialDelay: 0,
lang: '',
location: '',
showLocation: true,
showIndex: true,
Expand Down Expand Up @@ -38,12 +37,12 @@ Module.register('MMM-AirQuality', {
self.loaded = false

setTimeout(function () {
self.sendSocketNotification(self.notifications.DATA, self.config)
self.sendSocketNotification(self.notifications.DATA, { identifier: self.identifier, config: self.config })
}, this.config.initialDelay * 1000)

// set auto-update
setInterval(function () {
self.sendSocketNotification(self.notifications.DATA, self.config)
self.sendSocketNotification(self.notifications.DATA, { identifier: self.identifier, config: self.config })
}, this.config.updateInterval * 60 * 1000 + this.config.initialDelay * 1000)
},
render: function (response) {
Expand Down Expand Up @@ -138,12 +137,14 @@ Module.register('MMM-AirQuality', {
Log.debug('received ' + notification)
switch (notification) {
case self.notifications.DATA_RESPONSE:
if (payload.status === 'OK') {
console.log('Data %o', payload.payloadReturn)
self.render(payload.payloadReturn)
self.updateDom(this.animationSpeed)
} else {
console.log('DATA FAILED ' + payload.message)
if (payload.identifier === this.identifier) {
if (payload.status === 'OK') {
console.log('Data %o', payload.payloadReturn)
self.render(payload.payloadReturn)
self.updateDom(this.animationSpeed)
} else {
console.log('DATA FAILED ' + payload.message)
}
}
break
}
Expand Down
9 changes: 5 additions & 4 deletions helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ module.exports = {
start: function () {
console.log('AirQuality helper started ...')
},
loadData: async function (config) {
loadData: async function (payload) {
const self = this
self.config = config
const url = `https://${self.config.apiBase}${self.config.dataEndpoint}${self.config.location}/?token=${this.config.token}`
console.log(`AirQuality loaded: ${url}`)
const url = `https://${payload.config.apiBase}${payload.config.dataEndpoint}${payload.config.location}/?token=${payload.config.token}`
console.log(`AirQuality-Fetcher: ${url}`)

const result = await fetch(url)
.then(response => response.json())

self.sendSocketNotification(self.notifications.DATA_RESPONSE, {
payloadReturn: result,
status: 'OK',
identifier: payload.identifier,
})
},
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case this.notifications.DATA:
console.log(`AirQuality-Fetcher: Loading data of ${payload.config.location} for module ${payload.identifier}`)
this.loadData(payload)
break
}
Expand Down

0 comments on commit ef6f9ac

Please sign in to comment.