Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
re-write to using OpenWeatherMap.org , since DarkSky is decommissioning.
  • Loading branch information
FlatPepsi17 authored Jun 17, 2022
1 parent 89c01cf commit aaf3e29
Showing 1 changed file with 123 additions and 54 deletions.
177 changes: 123 additions & 54 deletions MMM-forecast-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ Module.register("MMM-forecast-io", {

defaults: {
apiKey: "",
apiBase: "https://api.darksky.net/forecast",
// apiBase: "https://api.darksky.net/forecast",
apiBase: "http://api.openweathermap.org/data/2.5/onecall?",
units: config.units,
language: config.language,
updateInterval: 6 * 60 * 1000, // every 5 minutes
Expand All @@ -23,27 +24,47 @@ Module.register("MMM-forecast-io", {
showWind: true,
showSunrise: true,
unitTable: {
'default': 'auto',
'metric': 'si',
'imperial': 'us'
// 'default': 'auto', // DarkSky
// 'metric': 'si',
// 'imperial': 'us'
'default': 'standard', // OpenWeather
'metric': 'metric',
'imperial': 'imperial'
},
// iconTable: {
// 'clear-day': 'wi-day-sunny',
// 'clear-night': 'wi-night-clear',
// 'rain': 'wi-rain',
// 'snow': 'wi-snow',
// 'sleet': 'wi-rain-mix',
// 'wind': 'wi-cloudy-gusts',
// 'fog': 'wi-fog',
// 'cloudy': 'wi-cloudy',
// 'partly-cloudy-day': 'wi-day-cloudy',
// 'partly-cloudy-night': 'wi-night-cloudy',
// 'hail': 'wi-hail',
// 'thunderstorm': 'wi-thunderstorm',
// 'tornado': 'wi-tornado'
// },

// https://openweathermap.org/weather-conditions#Weather-Condition-Codes-2
iconTable: {
'clear-day': 'wi-day-sunny',
'clear-night': 'wi-night-clear',
'rain': 'wi-rain',
'snow': 'wi-snow',
'sleet': 'wi-rain-mix',
'wind': 'wi-cloudy-gusts',
'fog': 'wi-fog',
'cloudy': 'wi-cloudy',
'partly-cloudy-day': 'wi-day-cloudy',
'partly-cloudy-night': 'wi-night-cloudy',
'hail': 'wi-hail',
'thunderstorm': 'wi-thunderstorm',
'tornado': 'wi-tornado'
'Clear': 'wi-day-sunny',
'clear night': 'wi-night-clear',
'few clouds': 'wi-day-cloudy',
'Clouds': 'wi-cloudy',
'broken clouds': 'wi-day-cloudy',
'Drizzle': 'wi-rain',
'Rain': 'wi-rain',
'Thunderstorm': 'wi-thunderstorm',
'Snow': 'wi-snow',
'Mist': 'wi-fog',
'Haze': 'wi-fog',
'Fog': 'wi-fog',
'Tornado': 'wi-tornado'
},

debug: false
debug: true
},

getTranslations: function () {
Expand Down Expand Up @@ -86,7 +107,16 @@ Module.register("MMM-forecast-io", {

var units = this.config.unitTable[this.config.units] || 'auto';

var url = this.config.apiBase+'/'+this.config.apiKey+'/'+this.config.latitude+','+this.config.longitude+'?units='+units+'&lang='+this.config.language;
// DarkSky
// var url = this.config.apiBase+'/'+this.config.apiKey+'/'+this.config.latitude+','+this.config.longitude+'?units='+units+'&lang='+this.config.language;

// OpenWeather
var url = this.config.apiBase+'appid='+this.config.apiKey+'&lat='+this.config.latitude+'&lon='+this.config.longitude+'&units='+units+'&lang='+this.config.language;

if (this.config.debug) {
console.log('Query URL: ', url);
}

if (this.config.data) {
// for debugging
this.processWeather(this.config.data);
Expand All @@ -101,7 +131,8 @@ Module.register("MMM-forecast-io", {
}
this.loaded = true;
this.weatherData = data;
this.temp = this.roundTemp(this.weatherData.currently.temperature);
// this.temp = this.roundTemp(this.weatherData.currently.temperature);
this.temp = this.roundTemp(this.weatherData.current.temp);
this.updateDom(this.config.animationSpeed);
this.scheduleUpdate();
},
Expand All @@ -125,7 +156,7 @@ Module.register("MMM-forecast-io", {
var wrapper = document.createElement("div");

if (this.config.apiKey === "") {
wrapper.innerHTML = "Please set the correct forcast.io <i>apiKey</i> in the config for module: " + this.name + ".";
wrapper.innerHTML = "Please set the correct OpenWeatherMarp.org <i>apiKey</i> in the config for module: " + this.name + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
Expand All @@ -137,12 +168,13 @@ Module.register("MMM-forecast-io", {
}

if (!this.loaded) {
wrapper.innerHTML = this.translate('LOADING');
wrapper.innerHTML = this.translate('LOADING WEATHER');
wrapper.className = "dimmed light small";
return wrapper;
}

var currentWeather = this.weatherData.currently;
// var currentWeather = this.weatherData.currently;
var currentWeather = this.weatherData.current;
var hourly = this.weatherData.hourly;
var minutely = this.weatherData.minutely;
var daily = this.weatherData.daily;
Expand All @@ -151,8 +183,10 @@ Module.register("MMM-forecast-io", {
var large = document.createElement("div");
large.className = "large light";

var icon = minutely ? minutely.icon : hourly.icon;
var iconClass = this.config.iconTable[hourly.icon];
// var icon = minutely ? minutely.icon : hourly.icon;
// var iconClass = this.config.iconTable[hourly.icon];
var iconClass = this.config.iconTable[this.weatherData.current.weather[0].main];

var icon = document.createElement("span");
icon.className = 'big-icon wi ' + iconClass;
large.appendChild(icon);
Expand All @@ -175,7 +209,8 @@ Module.register("MMM-forecast-io", {

var wind = document.createElement("span");
wind.className = "dim";
wind.innerHTML = " " + Math.round(this.weatherData.currently.windSpeed) + " ";
// wind.innerHTML = " " + Math.round(this.weatherData.currently.windSpeed) + " ";
wind.innerHTML = " " + Math.round(this.weatherData.current.wind_speed) + " ";
large.appendChild(wind);
}

Expand All @@ -184,14 +219,23 @@ Module.register("MMM-forecast-io", {
var midText = document.createElement("div");
midText.className = "light";

var today = this.weatherData.daily.data[0];
// var today = this.weatherData.daily.data[0];
var today = this.weatherData.daily[0];
var now = new Date();

if (today.sunriseTime*1000 < now && today.sunsetTime*1000 > now) {
var sunset = new moment.unix(today.sunsetTime).format( "h:mm a" );
// if (today.sunriseTime*1000 < now && today.sunsetTime*1000 > now) {
// var sunset = new moment.unix(today.sunsetTime).format( "h:mm a" );
// sunString = '<span class="wi wi-sunset xdimmed"></span> ' + sunset;
// } else {
// var sunrise = new moment.unix(today.sunriseTime).format( "h:mm a" );
// sunString = '<span class="wi wi-sunrise xdimmed"></span> ' + sunrise;
// }

if (today.sunrise*1000 < now && today.sunset*1000 > now) {
var sunset = new moment.unix(today.sunset).format( "h:mm a" );
sunString = '<span class="wi wi-sunset xdimmed"></span> ' + sunset;
} else {
var sunrise = new moment.unix(today.sunriseTime).format( "h:mm a" );
var sunrise = new moment.unix(today.sunrise).format( "h:mm a" );
sunString = '<span class="wi wi-sunrise xdimmed"></span> ' + sunrise;
}

Expand All @@ -204,7 +248,8 @@ Module.register("MMM-forecast-io", {

// ========= summary text
if (this.config.showSummary) {
var summaryText = minutely ? minutely.summary : hourly.summary;
// var summaryText = minutely ? minutely.summary : hourly.summary;
var summaryText = this.weatherData.current.weather[0].description;
var summary = document.createElement("div");
summary.className = "small dimmed summary";
summary.innerHTML = summaryText;
Expand Down Expand Up @@ -245,8 +290,10 @@ Module.register("MMM-forecast-io", {

context.save();
for (i = 0; i < 3; i++) { // 3 days ([0]..[2])
timeUnilSunrise = (this.weatherData.daily.data[i].sunriseTime - now);
timeUnilSunset = (this.weatherData.daily.data[i].sunsetTime - now);
// timeUnilSunrise = (this.weatherData.daily.data[i].sunriseTime - now);
// timeUnilSunset = (this.weatherData.daily.data[i].sunsetTime - now);
timeUnilSunrise = (this.weatherData.daily[i].sunrise - now);
timeUnilSunset = (this.weatherData.daily[i].sunset - now);

if ((timeUnilSunrise < 0) && (i == 0)) {
timeUnilSunrise = 0; // sunrise has happened already today
Expand Down Expand Up @@ -285,7 +332,9 @@ Module.register("MMM-forecast-io", {


// ====== graph of precipIntensity (inches of liquid water per hour)
var data = this.weatherData.hourly.data;

// var data = this.weatherData.hourly.data;
var data = this.weatherData.hourly;

context.save();

Expand All @@ -294,8 +343,16 @@ Module.register("MMM-forecast-io", {
var intensity;
for (i = 0; i < data.length; i++) {
intensity = 0;
if (data[i].precipIntensity > 0) {
intensity = (data[i].precipIntensity * height * 5) + 4; // make trace stand out
// if (data[i].precipIntensity > 0) {
// intensity = (data[i].precipIntensity * height * 5) + 4; // make trace stand out
// }

// console.log('hourly index,pop: ', i, ' ', this.weatherData.hourly[i].pop);
// console.log('rain: ', this.weatherData.hourly[i].rain);

// if (this.weatherData.hourly[i].pop > 0) {
if (this.weatherData.hourly[i]?.rain ) {
intensity = (this.weatherData.hourly[i].rain["1h"] * height * 5) + 4; // make trace stand out
}
context.lineTo(i * stepSize, height - intensity);
}
Expand All @@ -310,19 +367,20 @@ Module.register("MMM-forecast-io", {
context.restore();

// ====== graph of snow
// var data = this.weatherData.hourly.data;

context.save();

context.beginPath();
context.moveTo(0, height);
var intensity;
for (i = 0; i < data.length; i++) {
intensity = 0;
if (data[i].precipType == "snow") {
if (data[i].precipIntensity > 0) {
intensity = (data[i].precipIntensity * height * 5) + 4; // make trace stand out
}
// if (data[i].precipType == "snow") {
// if (data[i].precipIntensity > 0) {
// intensity = (data[i].precipIntensity * height * 5) + 4; // make trace stand out
// }
// }
if (data[i].snow > 0) {
intensity = (data[i].snow * height * 5) + 4; // make trace stand out
}
context.lineTo(i * stepSize, height - intensity);
}
Expand Down Expand Up @@ -368,7 +426,8 @@ Module.register("MMM-forecast-io", {

for (i = 0; i < (24+12+1); i++) {
tempX = i * stepSizeTemp;
tempY = height - (this.weatherData.hourly.data[i].temperature + 10);
// tempY = height - (this.weatherData.hourly.data[i].temperature + 10);
tempY = height - (this.weatherData.hourly[i].temp + 10);

context.lineTo( tempX, tempY ); // line from last hour to this hour
context.stroke();
Expand All @@ -384,8 +443,10 @@ Module.register("MMM-forecast-io", {
for (i = 0; i < (24+12+1); i++) { // text label for temperature on graph
if ((i % 2) == 1) {
tempX = (i * stepSizeTemp) - 5;
tempY = height - (this.weatherData.hourly.data[i].temperature + 10 + 5);
tempTemp = Math.round( this.weatherData.hourly.data[i].temperature );
// tempY = height - (this.weatherData.hourly.data[i].temperature + 10 + 5);
// tempTemp = Math.round( this.weatherData.hourly.data[i].temperature );
tempY = height - (this.weatherData.hourly[i].temp + 10 + 5);
tempTemp = Math.round( this.weatherData.hourly[i].temp );

context.beginPath();
context.font = "10px Arial";
Expand Down Expand Up @@ -416,7 +477,8 @@ Module.register("MMM-forecast-io", {

for (i = 0; i < (24+12+1); i++) {
tempX = i * stepSizeTemp;
tempY = height - ((this.weatherData.hourly.data[i].windSpeed * windGraphScale) + 5);
// tempY = height - ((this.weatherData.hourly.data[i].windSpeed * windGraphScale) + 5);
tempY = height - ((this.weatherData.hourly[i].wind_speed * windGraphScale) + 5);

context.lineTo( tempX, tempY ); // line from last hour to this hour
context.stroke();
Expand All @@ -432,8 +494,10 @@ Module.register("MMM-forecast-io", {
for (i = 0; i < (24+12+1); i++) { // text label for wind on graph
if ((i % 2) == 1) {
tempX = (i * stepSizeTemp) - 5;
tempY = height - ((this.weatherData.hourly.data[i].windSpeed * windGraphScale) + 5 + 3);
tempWind = Math.round( this.weatherData.hourly.data[i].windSpeed );
// tempY = height - ((this.weatherData.hourly.data[i].windSpeed * windGraphScale) + 5 + 3);
// tempWind = Math.round( this.weatherData.hourly.data[i].windSpeed );
tempY = height - ((this.weatherData.hourly[i].wind_speed * windGraphScale) + 5 + 3);
tempWind = Math.round( this.weatherData.hourly[i].wind_speed );

context.beginPath();
context.font = "10px Arial";
Expand All @@ -456,15 +520,18 @@ Module.register("MMM-forecast-io", {
renderForecastRow: function (data, min, max) {
var total = max - min;
var interval = 100 / total;
var rowMinTemp = this.roundTemp(data.temperatureMin);
var rowMaxTemp = this.roundTemp(data.temperatureMax);
// var rowMinTemp = this.roundTemp(data.temperatureMin);
// var rowMaxTemp = this.roundTemp(data.temperatureMax);
var rowMinTemp = this.roundTemp(data.temp.min);
var rowMaxTemp = this.roundTemp(data.temp.max);

var row = document.createElement("tr");
row.className = "forecast-row";

var dayTextSpan = document.createElement("span");
dayTextSpan.className = "forecast-day"
dayTextSpan.innerHTML = this.getDayFromTime(data.time);
// dayTextSpan.innerHTML = this.getDayFromTime(data.time);
dayTextSpan.innerHTML = this.getDayFromTime(data.dt);
var iconClass = this.config.iconTable[data.icon];
var icon = document.createElement("span");
icon.className = 'wi weathericon ' + iconClass;
Expand Down Expand Up @@ -511,15 +578,17 @@ Module.register("MMM-forecast-io", {
var numDays = 7;
var i;

// var filteredDays =
// this.weatherData.daily.data.filter( function(d, i) { return (i < numDays); });
var filteredDays =
this.weatherData.daily.data.filter( function(d, i) { return (i < numDays); });
this.weatherData.daily.filter( function(d, i) { return (i < numDays); });

var min = Number.MAX_VALUE;
var max = -Number.MAX_VALUE;
for (i = 0; i < filteredDays.length; i++) {
var day = filteredDays[i];
min = Math.min(min, day.temperatureMin);
max = Math.max(max, day.temperatureMax);
min = Math.min(min, day.temp.min);
max = Math.max(max, day.temp.max);
}
min = Math.round(min);
max = Math.round(max); // this week's min & max, for graph scaling
Expand Down

0 comments on commit aaf3e29

Please sign in to comment.