Skip to content

Commit

Permalink
Merge pull request #16 from gmichaela/master
Browse files Browse the repository at this point in the history
added flipDirection/flipHour settings, stopname and filterDirection fix
  • Loading branch information
edward-shen committed Nov 27, 2018
2 parents 147be12 + 1466a32 commit b33af7d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
64 changes: 53 additions & 11 deletions MMM-MBTA.js
Expand Up @@ -5,7 +5,7 @@ Module.register("MMM-MBTA", {
baseUrl: "https://api-v3.mbta.com/",
stations: [ "Northeastern University" ],
direction: [ ],
predictedTimes: true,
predictedTimes: true, // false - scheduled times may not work yet
doAnimation: false,
animationSpeed: 1000,
formatETA: true,
Expand All @@ -20,8 +20,11 @@ Module.register("MMM-MBTA", {
fadePoint: 0.25, // Start on 1/4th of the list.
showFullName: false,
colorIcons: false,
showAlerts: false,
hideEmptyAlerts: false
showAlerts: false, // works but styling needs help
hideEmptyAlerts: false,
flipDirection: false, // if set to true, it will flip direction filter flag,
flipHour: 12,
directionFlipped: false
},

getStyles: function() {
Expand Down Expand Up @@ -71,7 +74,8 @@ Module.register("MMM-MBTA", {
}

this.filterDirection = [];

this.directionFlipped = false;

switch (this.config.direction) {
case "Southbound":
case "Westbound":
Expand Down Expand Up @@ -124,7 +128,7 @@ Module.register("MMM-MBTA", {
row.appendChild(symbolCell);

var descCell = document.createElement("td");
descCell.innerHTML = "No vehicles are scheduled";
descCell.innerHTML = "Nothing coming soon...";
descCell.className = "align-left bright";
row.appendChild(descCell);
} else {
Expand Down Expand Up @@ -198,8 +202,22 @@ Module.register("MMM-MBTA", {

// Description
var descCell = document.createElement("td");
descCell.innerHTML = this.stationData[i].tripSign;
var direction = "";
switch (this.stationData[i].directionId) {
case "0":
direction = " Out";
break;
case "1":
direction = " In";
break;
}

//TODO: logic to display stopName for Commuter Rail
// T
descCell.innerHTML = this.stationData[i].tripSign + direction;
// CR
//descCell.innerHTML = this.stationData[i].stopName + direction;

//Change routeId to public route name
if ($.isNumeric(this.stationData[i].routeId)) {
switch (this.stationData[i].routeId) {
Expand Down Expand Up @@ -235,7 +253,6 @@ Module.register("MMM-MBTA", {
if (this.config.showETATime) {
var preETACell = document.createElement("td");
var preETATime = this.stationData[i].preETA;

if (preETATime == null) {
preETACell.innerHTML = "No ETA"
} else if (preETATime < 10) { // Better to display single digits as "now"
Expand Down Expand Up @@ -433,12 +450,12 @@ Module.register("MMM-MBTA", {
url += "&filter[stop]=" + stopId;
url += "&include=stop,route,trip,alerts&sort=arrival_time";

// Gets (maxEntries + 10) schedules or all schedules up to 3 hours from now, whichever is lower
// Gets (maxEntries + 10) schedules or all schedules up to 5 hours from now, whichever is lower
// Page and time limits necessary because otherwise "schedules" endpoint gets every single schedule
if (!this.config.predictedTimes) {
url += "&page[limit]=" + (maxEntries + 10) + '"';
url += "&page[limit]=" + (this.config.maxEntries + 10) + '"';
url += "&filter[min_time]=" + moment().format("HH:mm");
url += "&filter[max_time]=" + moment().add(3, 'h').format("HH:mm");
url += "&filter[max_time]=" + moment().add(5, 'h').format("HH:mm");
}
return url;
},
Expand Down Expand Up @@ -580,6 +597,7 @@ Module.register("MMM-MBTA", {
preDt = parseInt(moment(data.data[pred].attributes.departure_time).format("X"));
preArr = parseInt(moment(data.data[pred].attributes.arrival_time).format("X"));
preETA = moment(data.data[pred].attributes.arrival_time).diff(moment(), 'seconds') - 30; //Better safe than sorry?
stopName = data.data[pred].relationships.stop.data.id;

rawData.push({
routeType: routeType,
Expand All @@ -589,7 +607,8 @@ Module.register("MMM-MBTA", {
alerts: alertsArray,
preDt: preDt,
preArr: preArr,
preETA: preETA
preETA: preETA,
stopName: stopName
});
},

Expand All @@ -613,6 +632,29 @@ Module.register("MMM-MBTA", {
// Sorts them according to ETA time
this.stationData.sort((a,b) => (a.preETA - b.preETA));

if (this.config.flipDirection && this.config.direction.length > 0) {
//after flipTime, invert direction
var flipTime = moment().toDate();
flipTime.setHours(this.config.flipHour);
flipTime.setMinutes(0);

if (moment().isSameOrAfter(flipTime)) {
if (!this.directionFlipped) {
this.filterDirection[0] = (1 - this.filterDirection[0]).toString();
this.directionFlipped = true;
}
}
else {
this.directionFlipped = false;
}

}

// Applys directional filter
if (this.filterDirection.length > 0) {
this.stationData = this.stationData.filter((obj) => obj.directionId = this.filterDirection);
}

// Remove trips beyond maxTime
if (this.config.maxTime !== 0) {
this.stationData = this.stationData.filter(obj => (obj.preETA <= this.config.maxTime * 60));
Expand Down
11 changes: 9 additions & 2 deletions readme.md
Expand Up @@ -67,12 +67,17 @@ Option|Description
`colorIcons`|Display the vehicle icons in their respective color.<br/>**Expected Value type:** `boolean`.
`showAlerts`|Shows alerts near the specified station.<br/>**Expected Value type:** `boolean`.<br/>**WARNING**: Alerts are only properly shown when in a full-width position, such as `top_bar`, `middle_center` or `lower_third`.
`hideEmptyAlerts`|Automatically hide the alerts section when there are no alerts.<br/>**Expected Value type:** `boolean`.
`direction`| Sets the default direction you care about.<br/>Example: `direction: "Inbound" `.<br/>**Expected Value type:** `String`.
`flipDirection`| When set to `true`, MMM-MBTA will the flip the direction filter for incoming vehicles at the station, i.e. Inbound in the AM and Outbound in PM<br/>**Expected Value type:** `boolean`.
`flipHour`| Specifies the hour to flip the direction, if `flipDirection` is equal to `true`. Default value is 12.**Expected Value type:** `int`.



More options will be added as this module becomes feature-rich.

## Planned Features
- [x] Alert tickers **Partial support**
- [ ] Filter displayed trips by direction (i.e. Northbound, Eastbound)
- [x] Filter displayed trips by direction (i.e. Northbound, Eastbound)
- [ ] ETA countdown when vehicle is close to arriving
- [ ] Time filter to display certain trips during specified times of the day

Expand All @@ -87,7 +92,9 @@ This list was last updated on 2018-01-29.

Unfortunately, that's due to an issue with MagicMirror² core. I may do a pull request in the future, but as of right now, this is (unfortunate) intented behavior.

This list was last updated on 2017-06-13.
- Using Scheduled times instead of predicted times.

This list was last updated on 2018-11-26.

## Thanks

Expand Down

0 comments on commit b33af7d

Please sign in to comment.