Skip to content

Commit

Permalink
Updated CSS and refresh rate setting, to update after next upcoming g…
Browse files Browse the repository at this point in the history
…ame + 2 hrs and 45 min.
  • Loading branch information
bureus committed Feb 17, 2019
1 parent f0c4359 commit eb4f78e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
33 changes: 24 additions & 9 deletions MMM-SHL.js
Expand Up @@ -15,11 +15,11 @@ Module.register("MMM-SHL", {
}
},

/*getStyles: function () {
getStyles: function () {
return [
this.file('/css/mmm-shl-main.css')
]
},*/
},

start: function () {
Log.info("Starting module: " + this.name);
Expand Down Expand Up @@ -64,17 +64,21 @@ Module.register("MMM-SHL", {
th.className = 'align-left';
row.appendChild(th);
th = document.createElement("th");
th.className = 'align-left';
th.className = 'align-left stands-colum-left';
th.innerText = "GP"
row.appendChild(th);
th = document.createElement("th");
th.className = 'align-left';
th.className = 'align-left stands-colum';
th.innerText = "+/-"
row.appendChild(th);
th = document.createElement("th");
th.className = 'align-left';
th.className = 'align-left stands-colum';
th.innerText = "P"
row.appendChild(th);
th = document.createElement("th");
th.className = 'align-left';
th.innerText = "Next game"
row.appendChild(th);
table.appendChild(row);
for (let n = 0; n < this.stands.length; n++) {
let team = this.stands[n];
Expand All @@ -92,17 +96,21 @@ Module.register("MMM-SHL", {
th.innerText = team.name;
row.appendChild(th);
th = document.createElement("th");
th.className = 'align-left';
th.className = 'align-left stands-colum-left';
th.innerText = team.gp;
row.appendChild(th);
th = document.createElement("th");
th.className = 'align-left';
th.className = 'align-left stands-colum';
th.innerText = team.diff;
row.appendChild(th);
th = document.createElement("th");
th.className = 'align-left';
th.className = 'align-left stands-colum';
th.innerText = team.points;
row.appendChild(th);
th = document.createElement("th");
th.className = 'align-left';
th.innerText = getNextGameString(team);
row.appendChild(th);
table.appendChild(row);
};

Expand Down Expand Up @@ -133,4 +141,11 @@ Module.register("MMM-SHL", {
this.domObjectCreated = true;
}
}
});
});

function getNextGameString(team) {
let date = new Date(team.nextGame.start_date_time);
let options = { weekday: "long", day: "numeric"};
let playTime = date.toLocaleDateString("sv-SE", options)+" "+date.getHours()+":"+(date.getMinutes() <= 9 ? date.getMinutes()+"0" : date.getMinutes());
return team.team_code == team.nextGame.away_team_code ? "Borta, "+ playTime : "Hemma, "+ playTime;
}
8 changes: 8 additions & 0 deletions css/mmm-shl-main.css
@@ -0,0 +1,8 @@
.stands-colum {
min-width: 30px;
}

.stands-colum-left {
padding-left: 10px;
min-width: 30px;
}
62 changes: 55 additions & 7 deletions node_helper.js
Expand Up @@ -12,11 +12,12 @@ module.exports = NodeHelper.create({
this.started = false;
},
// --------------------------------------- Schedule a stands update
scheduleUpdate: function () {
scheduleUpdate: function (refreshRate) {
let self = this;
log("Sceduled refresh at: "+new Date(Date.now()+refreshRate));
this.updatetimer = setInterval(function () { // This timer is saved in uitimer so that we can cancel it
self.sendStand();
}, 3600000);
}, refreshRate);
},
// --------------------------------------- Get access token
getAccessToken: async function () {
Expand Down Expand Up @@ -111,6 +112,8 @@ module.exports = NodeHelper.create({
let games = response;
debug("Number of games: " + games.length);
resolve(games);
//gamesPerTeams = groupGamesPerTeam(games);
//resolve(gamesPerTeams);
})
.catch(function (error) {
log("getStands failed =" + error);
Expand Down Expand Up @@ -150,19 +153,23 @@ module.exports = NodeHelper.create({
self.sendSocketNotification("SERVICE_FAILURE", "Missing teams..");
}
},
// --------------------------------------- Initiate
initiate: async function(){
// --------------------------------------- update
update: async function(){
let self = this;
if(this.updatetimer){
clearInterval(this.updatetimer);
}
if(!self.accessToken){
self.accessToken = await self.getAccessToken();
if(self.accessToken)
debug("Access token retrived: "+self.accessToken);
}

self.games = await self.getGames();
self.gamePerTeam = groupGamesPerTeam(self.games);
self.stands = await self.getStands();
await self.sendStand();
self.scheduleUpdate();
let timeToNextUpdate = getRefreshTime(sortOnUpcomingGame(new Date(Date.now()),(self.games))[0]);
self.scheduleUpdate(timeToNextUpdate);
},
// --------------------------------------- Create DTO
generateDto: async function(){
Expand All @@ -174,6 +181,8 @@ module.exports = NodeHelper.create({
let teamContent = self.getTeamContent(team.team_code);
team.icon = teamContent.icon;
team.name = teamContent.name;
team.games = sortOnUpcomingGame(new Date(Date.now()), self.gamePerTeam[team.team_code]);
team.nextGame = team.games.length > 0 ? team.games[0] : null;
debug(JSON.stringify(team));
dto.push(team);
}
Expand All @@ -194,7 +203,7 @@ module.exports = NodeHelper.create({
if (!self.accessToken) {
await self.getAccessToken(); // Get inital access token
}
self.initiate();
self.update();
};
}
});
Expand All @@ -209,6 +218,45 @@ function sortByKey(array, key) {
});
}

function getRefreshTime(nextGame){
if(!nextGame){
return 3600000;
}
let estimatedEndTime = new Date(nextGame.start_date_time);
estimatedEndTime.setHours(estimatedEndTime.getHours()+2);
estimatedEndTime.setMinutes(estimatedEndTime.getMinutes()+45);
return estimatedEndTime - new Date(Date.now());
}

function sortOnUpcomingGame(date, array){
array.sort(function(a, b) {
var distancea = Math.abs(date - new Date(a.start_date_time));
var distanceb = Math.abs(date - new Date(b.start_date_time));
return distancea - distanceb; // sort a before b when the distance is smaller
});
let sorted = array.filter(function(d) {
return new Date(d.start_date_time) - date > 0;
});
return sorted;
}

function groupGamesPerTeam(games) {
let teams = {};
for (let i = 0; i < games.length; i++) {
let awayTeam = games[i]["away_team_code"];
let homeTeam = games[i]["home_team_code"];
if (!teams[awayTeam]) {
teams[awayTeam] = [];
}
if(!teams[homeTeam]){
teams[homeTeam] = [];
}
teams[awayTeam].push(games[i]);
teams[homeTeam].push(games[i]);
}
return teams;
}

// --------------------------------------- At beginning of log entries
function logStart() {
return (new Date(Date.now())).toLocaleTimeString() + " MMM-SHL: ";
Expand Down

0 comments on commit eb4f78e

Please sign in to comment.