Skip to content

Commit

Permalink
Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
soyrubio committed Oct 19, 2020
0 parents commit 19b664e
Show file tree
Hide file tree
Showing 11 changed files with 988 additions and 0 deletions.
32 changes: 32 additions & 0 deletions MMM-idos.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Magic Mirror
* Module: MMM-idos
* Description: Display estimations for public transport stops in Bratislava, Slovakia
*
* By Adrian Matejov https://github.com/Adman
* MIT Licensed.
*/

.MMM-idos {
width : 300px;
}

.MMM-idos tr {
padding-top: 5px;
}

.blinking .blink {
animation: blink 1s ease-in-out infinite alternate;
}

@-webkit-keyframes blink {
from { opacity: 0.2; }
to { opacity: 1; }
}

.idos-padding-left {
padding-left: 5px;
}

.MMM-idos table {
font-size: 1rem;
}
198 changes: 198 additions & 0 deletions MMM-idos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/* Magic Mirror Module
* Module: MMM-idos
* Description: Display estimations for public transport stops in the Czech Republic
*
* By elrubio https://github.com/soyrubio
* MIT Licensed.
*/

Module.register('MMM-idos', {
defaults: {
maximumEntries: 5,
refreshInterval: 60000, // in milliseconds
displaySymbol: true,
displayLineNumber: true,
displayDestination: true,

fadePoint: 0.25, // Start on the 1/4th of the list
fade: true,
blink: true,
torPorts: ['9050', '9052', '9053', '9054']
},

getStyles: function () {
return ['font-awesome.css', this.file('MMM-idos.css')];
},

getTranslations: function () {
return {
en: "translations/en.json",
sk: "translations/sk.json",
cz: "translations/sk.json",
}
},

start: function () {
Log.log('Starting module: ' + this.name);

this.livetable = {};

this.idos_loaded = false;
this.idos_fetch_error = false;

this.scheduleUpdate();
this.updateDom();
},

getDom: function () {
var wrapper = document.createElement('div');

if (this.idos_fetch_error) {
wrapper.innerHTML = this.translate('IDOS_FETCH_ERROR');
wrapper.className = 'dimmed light small';
return wrapper;
}

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

/* process data */
var all_lines = this.livetable;
all_lines = all_lines.slice(0, this.config.maximumEntries);

if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var start_fade = all_lines.length * this.config.fadePoint;
var fade_steps = all_lines.length - start_fade;
}

var table = document.createElement('table');
table.className = 'small';

if (all_lines.length > 0) {
for (var i = 0; i < all_lines.length; i++) {
var line = all_lines[i];

var row = document.createElement('tr');

/* row fading */
if (i + 1 >= start_fade) {
var curr_fade_step = i - start_fade;
row.style.opacity = 1 - (1 / fade_steps * curr_fade_step);
}

/* display symbol */
if (this.config.displaySymbol) {
var w_symbol_td = document.createElement('td');
var w_symbol = document.createElement('span');
w_symbol.className = 'fa fa-fw fa-' + line.type;
w_symbol_td.appendChild(w_symbol);
row.appendChild(w_symbol_td);
}

/* display line number */
if (this.config.displayLineNumber) {
var w_line_num_td = document.createElement('td');
w_line_num_td.className = 'idos-padding-left align-right';
w_line_num_td.innerHTML = line.number;
row.appendChild(w_line_num_td);
}

/* display destination */
if (this.config.displayDestination) {
var w_dest_td = document.createElement('td');
w_dest_td.className = 'idos-padding-left align-left idos-destination';
w_dest_td.innerHTML = line.destination;
row.appendChild(w_dest_td);
}

/* display time left */
var w_time_td = document.createElement('td');
w_time_td.className = 'align-right idos-departure';
if (this.config.blink) {
w_time_td.className += 'blink';
}
var departure_time = this.getDepartureTime(line);
w_time_td.innerHTML = departure_time;
row.appendChild(w_time_td);

/* display delay dot */
var dot_td = document.createElement('td');
dot_td.className = 'idos-delay-dot'
if (this.config.blink) {
w_time_td.className += 'blink';
}
if (line.delay !== "0") {
dot_td.innerHTML = "&bull;";
}
row.appendChild(dot_td);

/* departuring now should blink */
row.className = 'bright'
if (this.config.blink && departure_time === "<1 min")
row.className += ' blinking';

table.appendChild(row);
}
} else {
var row = document.createElement('tr');
table.appendChild(row);

var no_line_cell = document.createElement('td');
no_line_cell.className = 'dimmed light small';
no_line_cell.innerHTML = this.translate('IDOS_NO_LINES');
row.appendChild(no_line_cell);
}

wrapper.appendChild(table);
return wrapper;
},

socketNotificationReceived: function (notification, payload) {
if (payload.module_id == this.identifier) {
if (notification === 'IDOS_UPDATE') {
this.idos_loaded = true;
this.idos_fetch_error = false;
this.livetable = payload.result;
this.updateDom();
} else if (notification === 'IDOS_FETCH_ERROR') {
this.idos_fetch_error = true;
this.updateDom();
}
}
},

scheduleUpdate: function () {
this.sendSocketNotification('IDOS_STOP_INFO', {
module_id: this.identifier,
stop_id: this.config.stopId,
ports: this.config.torPorts
});

var self = this;
setTimeout(function () {
self.scheduleUpdate();
}, this.config.refreshInterval);
},

getDepartureTime: function (line) {
var now = new Date();
var time = line.departure.split(":");
var departure = new Date(now.getFullYear(), now.getMonth(), now.getDate(), time[0], time[1], 0);
var diff = (departure - now) / 60000;
departure.setMinutes(departure.getMinutes() + parseInt(line.delay));
diff = (departure - now) / 60000;
if (diff < 1) {
diff = "<1"
} else {
diff = Math.round(diff);
}
diff += " min"
return diff;
}
});
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# MMM-idos

This is a Magic Mirror Module (https://github.com/MichMich/MagicMirror/), which displays
real time estimated departures for public transport in the Czech Republic.


![screenshoot](img/screenshoot.png)

## Installation

Clone this module into your Magic Mirror modules folder.
1. Go into Magic Mirror's `modules` folder
2. Execute `git clone https://github.com/soyrubio/MMM-idos.git`.

## Using the module

In order to use this module, add the following configuration into Magic Mirror's config file (located in 'config/config.js'):

```js
var config = {
modules: [
{
module: 'MMM-idos',
header: '<header>', // no header will be displayed if ommited
position: 'top_right', // position of the module
config: {
stopId: string, // "https://idos.idnes.cz/vlakyautobusymhdvse/odjezdy/vysledky/?f=the_stop
/* other configurations */
}
}
]
}
```

## Configuration options

| Option | Description
|--------------------- |------------
| `stopId` | *Required* Id of the stop (parameter "z" in stop's livetable url).<br><br> **Type:** `int` <br> **Default value:** `none`
| `maximumEntries` | *Optional* The maximum entries shown. <br><br> **Type:** `int` <br> **Default value:** `10`
| `refreshInterval` | *Optional* How often to check for the next lines. <br><br> **Type:** `int`<br> **Default value:** `30000` milliseconds (60 seconds)
| `fade` | *Optional* Fade the future lines to black. (Gradient) <br><br> **Type:** `boolean`<br> **Default value:** `true`
| `fadePoint` | *Optional* Where to start fade? <br><br> **Type:** `float`<br> **Default value:** `0.25` (start on the 1/4 th of the list)
| `blink` | *Optional* Whether departures should blink when departure time is <1 min. > <br><br> **Type:** `boolean`<br> **Default value:** `true`
| `displaySymbol` | *Optional* Whether to display bus/tram symbols. <br><br> **Type:** `boolean`<br> **Default value:** `true`
| `displayLineNumber` | *Optional* Whether to display line number. <br><br> **Type:** `boolean`<br> **Default value:** `true`
| `displayDestination` | *Optional* Whether to display destination stop. <br><br> **Type:** `boolean`<br> **Default value:** `true`

## Credits
The module is based on the [MMM-imhdsk By Adman](https://github.com/Adman/MMM-imhdsk), his module is far better than my own would be so this is basically the MMM-imhdsk module remade to fetch departures data from the idos.cz website. Thanks Adman!

*Beware that by using this you might be violating idos.cz ToS*
Binary file added img/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 19b664e

Please sign in to comment.