Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolai committed Sep 21, 2018
0 parents commit c895f9a
Show file tree
Hide file tree
Showing 18 changed files with 4,381 additions and 0 deletions.
Binary file added .github/screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.nyc_output
coverage
node_modules
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# MMM-RBB-Weather Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).


## [1.0.0] - Release

First public release
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Nikolai Keist

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
debug: console.debug,
log: console.log,
info: console.info,
warn: console.warn,
error: console.error,
};
57 changes: 57 additions & 0 deletions MMM-RBB-Weather.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Magic Mirror
* CSS: MMM-RBB-Weather
*
* By Nikolai Keist (github.com/nkl-kst)
* MIT Licensed.
*/

/*
* ##### Current data #####
*/

div.current {
margin-bottom: 20px;
}

img.weather-icon {
margin-left: 10px;
width: 50px;
}


/*
* ##### Table data #####
*/

table.weather-table td:not(:last-child) {
padding-right: 0.6em;
}

table.weather-table td.day {
text-align: center;
}

table.weather-table img.weather-icon {
padding-top: 0.3em;
padding-right: 0.3em;
width: 1.2em;
}

table.weather-table td.wind {
padding-top: 0.2em;
/* padding-right: 0.4em; */
}

table.weather-table td.wind > span {
font-size: 0.5em;
font-style: italic;
}


/*
* ##### White icons #####
*/

div.white img.weather-icon {
filter: brightness(0) invert(1);
}
243 changes: 243 additions & 0 deletions MMM-RBB-Weather.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/* Magic Mirror
* Module: MMM-RBB-Weather
*
* By Nikolai Keist (github.com/nkl-kst)
* MIT Licensed.
*/

Module.register("MMM-RBB-Weather", {

defaults: {

// Data
id: "18228265", // Berlin
days: 4, // Including current, 0 = only current data

// Times
animationSpeed: 1, // 1 sec.
updateInterval: 600, // 10 min.

// Show / hide flags
showCurrentWindspeed: true,
showRainProbality: true,
showWindspeed: false,

// Styling
tableClass: "small",
whiteIcons: true,
},

requiresVersion: "2.1.0", // Required version of MagicMirror

// Instancevariable
weatherData: null,

getScripts: function() {
return [ "moment.js" ];
},

getStyles: function () {
return [ "weather-icons.css", "MMM-RBB-Weather.css" ];
},

getTranslations: function() {
return {
de: "translations/de.json",
en: "translations/en.json",
};
},

start: function() {
Log.info(`Starting module: ${this.name} ...`);

// Set locale
moment.locale(this.config.language);
},

notificationReceived: function(notification, payload, sender) {

// DOM ready
if (notification === 'MODULE_DOM_CREATED') {

// Initial data load
this.loadData();

// Schedule module refresh
this.scheduleRefresh();
}
},

socketNotificationReceived: function(notification, payload) {

// Data loaded with node helper
if (notification === 'DATA_LOADED') {
this.weatherData = payload;
this.updateDom(this.config.animationSpeed * 1000);
}
},

/**
* getDom - Build and returns the whole module dom, including current data and forecast
* table.
*
* @return {Element} Module dom as div element.
*/
getDom: function() {

// Dom wrapper
let wrapper = document.createElement('div');
if (this.config.whiteIcons) {
wrapper.className = "white";
}

// No data
if (this.weatherData === null || this.weatherData.length === 0) {
wrapper.innerHTML = this.translate('TEXT_NODATA');
return wrapper;
}

// Table with data (without current data)
let table = document.createElement('table');
table.className = this.config.tableClass + " weather-table";

// Current weather
let currentData = this.weatherData[0];
let currentDiv = this.getCurrentDiv(currentData);
wrapper.appendChild(currentDiv);

// Fill table with data
for (let [day, data] of Object.entries(this.weatherData)) {

// Don't create table row for current data
if (day == 0) {
continue;
}

// Create data row
let row = document.createElement('tr');

// Date
let dayCol = document.createElement('td');
dayCol.className = 'day';
dayCol.innerHTML = moment().add(day - 1, 'days').format('ddd');
row.appendChild(dayCol);

// Icon
let iconCol = document.createElement('td');
let icon = document.createElement('img');
icon.className = 'weather-icon';
icon.src = `https://www.rbb24.de/basis/grafik/icons/wetter/${data.nww}.png`;

iconCol.appendChild(icon);
row.appendChild(iconCol);

// Split temparatures
let maxTemp = data.temp.split(';')[0];
let minTemp = data.temp.split(';')[1];

// Max temparature
let maxCol = document.createElement('td');
maxCol.className = 'title bright';
maxCol.innerHTML = `${maxTemp}° <i class='fa fa-fw fa-thermometer-three-quarters'></i>`;
row.appendChild(maxCol);

// Min temparature
let minCol = document.createElement('td');
minCol.innerHTML = `${minTemp}° <i class='fa fa-fw fa-thermometer-quarter'></i>`;
row.appendChild(minCol);

// Wind
if (this.config.showWindspeed) {
let windCol = document.createElement('td');
windCol.innerHTML = `${data.ffkmh} <span>km/h</span>`;
windCol.className = 'wind';
row.appendChild(windCol);
}

// Rain
if (this.config.showRainProbality) {
let rainCol = document.createElement('td');
rainCol.innerHTML = `${data.prr}% <i class='fa fa-fw fa-tint'></i>`;
row.appendChild(rainCol);
}

table.appendChild(row);
}

// Append table to wrapper and return it
wrapper.appendChild(table);
return wrapper;
},

/**
* getCurrentDiv - Builds and returns the special div for current weather informations.
*
* @param {Object} data Data object fetched from RBB with all weather informations
* @return {Element} Special div for current weather informations
*/
getCurrentDiv: function(data) {

// Wrapper
let wrapper = document.createElement('div');
wrapper.className = "current";

// Data wrapper
let dataDiv = document.createElement('div');
dataDiv.className = "large bright";

// Temparature
let temp = document.createElement('span');
temp.innerHTML = data.temp + "°";
dataDiv.appendChild(temp);

// Icon
let icon = document.createElement('img');
icon.className = "weather-icon";
icon.src = `https://www.rbb24.de/basis/grafik/icons/wetter/${data.nww}.png`;
dataDiv.appendChild(icon);

// Append data div (temp and icon) to wrapper
wrapper.appendChild(dataDiv);

// Current header text
let textDiv = document.createElement('div');
textDiv.className = "medium normal";
textDiv.innerHTML = this.translate('TEXT_CURRENT', { text: data.wwtext });
wrapper.appendChild(textDiv);

// Wind
if (this.config.showCurrentWindspeed) {
let wind = document.createElement('div');
wind.className = "small dimmed";
wind.innerHTML = this.translate('TEXT_WINDSPEED', { text: data.ffkmh });
wrapper.appendChild(wind);
}

return wrapper;
},

/**
* loadData - Load weather data via node_helper. This functions sends a socket notification with
* LOAD_DATA as notification name and the place id (config.id) and day count (config.days) as
* payload.
*/
loadData: function() {
Log.info("Send socket notification to load data in node_helper ...");

// Load data via node helper
let dataConfig = { id: this.config.id, days: this.config.days };
this.sendSocketNotification('LOAD_DATA', dataConfig);
},

/**
* scheduleRefresh - Schedules refresh timer depending on config.updateInterval. This function
* calls loadData().
*/
scheduleRefresh: function() {

let self = this;
setTimeout(() => {
self.loadData();
}, this.config.updateInterval * 1000);
},
});
Loading

0 comments on commit c895f9a

Please sign in to comment.