Skip to content

Commit

Permalink
Update (#35)
Browse files Browse the repository at this point in the history
* let -> const

* let -> const

* Add PTHAFASDomBuilder as global variable

* Remove 'self' because it's never used

* eslint

* eslint + prettier

* Add prettier

* fix test

* Switch to eslint-config-airbnb-base for eslint

* Remove "use strict"
"use strict" is unnecessary inside of modules

* Add space after //

* Simplify methods

* Change string concatenation

* Remove unnecessary variable assignments

* Use typeof

* Prettier

* Add moment as dependency

* Use camel case

* Remove unnecessary 'else' after 'return'

* Change = to +=

* "default-case": "off"

* Use static for methods without this

* Revert "Use static for methods without this"

This reverts commit 95064a9.

* Add eslint rules

* Use this

* Add JSDoc comments

* Raise version to 1.2.0

* Update README.md

* Add a GitHub Actions CI workflow

* Remove Preconditions

* let UserPresence -> var UserPresence
To fix #34

* Bump eslint from 8.7.0 to 8.8.0 (#33)

Bumps [eslint](https://github.com/eslint/eslint) from 8.7.0 to 8.8.0.
- [Release notes](https://github.com/eslint/eslint/releases)
- [Changelog](https://github.com/eslint/eslint/blob/main/CHANGELOG.md)
- [Commits](eslint/eslint@v8.7.0...v8.8.0)

---
updated-dependencies:
- dependency-name: eslint
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Remove dependency from "array-unique".

* 'arrayUnique' was used before it was defined

* Remove dependency from "arr-diff"

* Raise version

* Add configuration option `ignoreRelatedStations`

* Optimize configuration part

* Raise version to 1.3.0

* Fix typo

* Show "plattform" as default
Those who do not need it can deactivate it via `tableHeaderOrder`.

* Optimize configuration part

* Add css for Magdeburg

* Add note for city css files

* Fix line 8

* Use 'stylelint' for css files

* Update lint command

* Use kebab-case for class names

* Optimize

* Add blank

* Raise version to 1.4.0

* npm install --omit=dev

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
KristjanESPERANTO and dependabot[bot] committed Feb 7, 2022
1 parent f00103e commit 5037dba
Show file tree
Hide file tree
Showing 20 changed files with 5,104 additions and 658 deletions.
23 changes: 23 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
env: {
es6: true,
browser: true,
commonjs: true,
es2021: true
},
extends: ["airbnb-base", "prettier"],
parserOptions: {
ecmaVersion: "latest",
sourceType: "module"
},
plugins: ["prettier"],
root: true,
rules: {
"default-case": "off",
"global-require": "off",
"prefer-destructuring": "off",
"prettier/prettier": "error",
"import/no-dynamic-require": "off",
"import/no-unresolved": "off"
}
};
31 changes: 31 additions & 0 deletions .github/workflows/automated-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Run Automated Tests
on:
push:
branches: [main, develop, test]
pull_request:
branches: [main, develop, test]
jobs:
run-lint:
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
matrix:
node-version: ['16', '17']
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v2
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install dependencies and run tests
run: |
npm install
npm run lint
- run: echo "🍏 This job's status is ${{ job.status }}."
6 changes: 6 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
trailingComma: "none",
tabWidth: 2,
semi: true,
arrowParens: "always"
};
9 changes: 9 additions & 0 deletions .stylelintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": ["stylelint-config-standard", "stylelint-config-prettier"],
"plugins": ["stylelint-prettier"],
"root": true,
"rules": {
"prettier/prettier": true,
"rule-empty-line-before": null
}
}
91 changes: 47 additions & 44 deletions MMM-PublicTransportHafas.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"use strict";
/* global PTHAFASDomBuilder Module Log moment */

//UserPresence Management (PIR sensor)
var UserPresence = true; //true by default, so no impact for user without a PIR sensor
// UserPresence Management (PIR sensor)
// (This variable must currently still be declared with var, as several modules use this
// variable. If someone wants to change this, they would have to adapt the other modules as well.)
// eslint-disable-next-line no-var
var UserPresence = true; // true by default, so no impact for user without a PIR sensor

Module.register("MMM-PublicTransportHafas", {
// default values
// prettier-ignore
defaults: {
// Module misc
name: "MMM-PublicTransportHafas",
Expand All @@ -23,6 +27,7 @@ Module.register("MMM-PublicTransportHafas", {
// Departures options
direction: "", // Show only departures heading to this station. (A station ID.)
ignoredLines: [], // Which lines should be ignored? (comma-separated list of line names)
ignoreRelatedStations: false, // For some stations there are related stations. By default, their departures are also displayed.
excludedTransportationTypes: [], // Which transportation types should not be shown on the mirror? (comma-separated list of types) possible values: "tram", "bus", "suburban", "subway", "regional" and "national"
timeToStation: 10, // How long do you need to walk to the Station? (in minutes)
timeInFuture: 40, // Show departures for the next *timeInFuture* minutes.
Expand All @@ -36,7 +41,7 @@ Module.register("MMM-PublicTransportHafas", {
showTableHeaders: true, // Show table headers?
showTableHeadersAsSymbols: true, // Table Headers as symbols or written?
showWarningRemarks: true, // Show warning remarks?
tableHeaderOrder: ["time", "line", "direction"], // In which order should the table headers appear? (add "platform" if you like)
tableHeaderOrder: ["time", "line", "direction", "platform" ], // In which order should the table headers appear?
maxUnreachableDepartures: 0, // How many unreachable departures should be shown?
maxReachableDepartures: 7, // How many reachable departures should be shown?
fadeUnreachableDepartures: true,
Expand All @@ -47,14 +52,14 @@ Module.register("MMM-PublicTransportHafas", {
animationSpeed: 1500 // Refresh animation speed in milliseconds
},

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

this.ModulePublicTransportHafasHidden = false; // By default we display the module (if no carousel or other module)
this.updatesIntervalID = 0; // To stop and start auto update for each module instance
this.lastUpdate = 0; // Timestamp of the last module update. set at 0 at start-up
this.updatesIntervalID = 0; // To stop and start auto update for each module instance
this.lastUpdate = 0; // Timestamp of the last module update. set at 0 at start-up

this.departures = [];
this.initialized = false;
Expand All @@ -63,20 +68,21 @@ Module.register("MMM-PublicTransportHafas", {
this.sanitzeConfig();

if (!this.config.stationID) {
Log.error("stationID not set! " + this.config.stationID);
Log.error(`stationID not set! ${this.config.stationID}`);
this.error.message = this.translate("NO_STATION_ID_SET");

return;
}

let fetcherOptions = {
const fetcherOptions = {
identifier: this.identifier,
hafasProfile: this.config.hafasProfile,
stationID: this.config.stationID,
timeToStation: this.config.timeToStation,
timeInFuture: this.config.timeInFuture,
direction: this.config.direction,
ignoredLines: this.config.ignoredLines,
ignoreRelatedStations: this.config.ignoreRelatedStations,
excludedTransportationTypes: this.config.excludedTransportationTypes,
maxReachableDepartures: this.config.maxReachableDepartures,
maxUnreachableDepartures: this.config.maxUnreachableDepartures
Expand All @@ -85,21 +91,21 @@ Module.register("MMM-PublicTransportHafas", {
this.sendSocketNotification("CREATE_FETCHER", fetcherOptions);
},

suspend: function () {
suspend() {
// Core function called when the module is hidden
this.ModulePublicTransportHafasHidden = true; // Module hidden
// Log.log("Function suspend - Module PublicTransportHafas is hidden " + this.config.stationName);
this.GestionUpdateIntervalHafas(); // Call the function which manages all the cases
},

resume: function () {
resume() {
// Core function called when the module is displayed
this.ModulePublicTransportHafasHidden = false;
// Log.log("Function working - Module PublicTransportHafas is displayed " + this.config.stationName);
this.GestionUpdateIntervalHafas();
},

notificationReceived: function (notification, payload) {
notificationReceived(notification, payload) {
if (notification === "USER_PRESENCE") {
// Notification sent by the MMM-PIR-Sensor module. See its doc.
// Log.log("NotificationReceived USER_PRESENCE = " + payload);
Expand All @@ -108,13 +114,12 @@ Module.register("MMM-PublicTransportHafas", {
}
},

GestionUpdateIntervalHafas: function () {
GestionUpdateIntervalHafas() {
if (
UserPresence === true &&
this.ModulePublicTransportHafasHidden === false
) {
// Make sure to have a user present in front of the screen (PIR sensor) and that the module is displayed
let self = this;
// Log.log(this.config.stationName + " is displayed and user present! Update it");

// Update now and start again the update timer
Expand All @@ -127,8 +132,8 @@ Module.register("MMM-PublicTransportHafas", {
}
},

getDom: function () {
let domBuilder = new PTHAFASDomBuilder(this.config);
getDom() {
const domBuilder = new PTHAFASDomBuilder(this.config);

if (this.hasErrors()) {
return domBuilder.getSimpleDom(this.error.message);
Expand All @@ -138,48 +143,46 @@ Module.register("MMM-PublicTransportHafas", {
return domBuilder.getSimpleDom(this.translate("LOADING"));
}

let headings = {
const headings = {
time: this.translate("PTH_DEPARTURE_TIME"),
line: this.translate("PTH_LINE"),
direction: this.translate("PTH_TO"),
platform: this.translate("PTH_PLATFORM")
};

let noDeparturesMessage = this.translate("PTH_NO_DEPARTURES");
const noDeparturesMessage = this.translate("PTH_NO_DEPARTURES");

let wrapper = domBuilder.getDom(
const wrapper = domBuilder.getDom(
this.departures,
headings,
noDeparturesMessage
);

// display the update time at the end, if defined so by the user config
if (this.config.displayLastUpdate) {
let updateinfo = document.createElement("div");
const updateinfo = document.createElement("div");
updateinfo.className = "xsmall light align-left";
updateinfo.innerText =
"Update: " +
moment
.unix(this.lastUpdate)
.format(this.config.displayLastUpdateFormat);
updateinfo.innerText = `Update: ${moment
.unix(this.lastUpdate)
.format(this.config.displayLastUpdateFormat)}`;
wrapper.appendChild(updateinfo);
}

return wrapper;
},

getStyles: function () {
let styles = [this.file("css/styles.css"), "font-awesome.css"];
getStyles() {
const styles = [this.file("css/styles.css"), "font-awesome.css"];

if (this.config.customLineStyles !== "") {
let customStyle = "css/" + this.config.customLineStyles + "-lines.css";
const customStyle = `css/${this.config.customLineStyles}-lines.css`;
styles.push(this.file(customStyle));
}

return styles;
},

getScripts: function () {
getScripts() {
return [
"moment.js",
this.file("core/PTHAFASDomBuilder.js"),
Expand All @@ -188,18 +191,19 @@ Module.register("MMM-PublicTransportHafas", {
];
},

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

socketNotificationReceived: function (notification, payload) {
socketNotificationReceived(notification, payload) {
if (!this.isForThisStation(payload)) {
return;
}

// eslint-disable-next-line default-case
switch (notification) {
case "FETCHER_INITIALIZED":
this.initialized = true;
Expand All @@ -209,16 +213,15 @@ Module.register("MMM-PublicTransportHafas", {

case "DEPARTURES_FETCHED":
if (this.config.displayLastUpdate) {
this.lastUpdate = Date.now() / 1000; //save the timestamp of the last update to be able to display it
this.lastUpdate = Date.now() / 1000; // save the timestamp of the last update to be able to display it
}

Log.log(
"TransportHafas update OK, station : " +
this.config.stationName +
" at : " +
+moment
.unix(this.lastUpdate)
.format(this.config.displayLastUpdateFormat)
`TransportHafas update OK, station : ${
this.config.stationName
} at : ${+moment
.unix(this.lastUpdate)
.format(this.config.displayLastUpdateFormat)}`
);

// reset error object
Expand All @@ -237,11 +240,11 @@ Module.register("MMM-PublicTransportHafas", {
}
},

isForThisStation: function (payload) {
isForThisStation(payload) {
return payload.identifier === this.identifier;
},

sanitzeConfig: function () {
sanitzeConfig() {
if (this.config.updatesEvery < 30) {
this.config.updatesEvery = 30;
}
Expand All @@ -263,22 +266,22 @@ Module.register("MMM-PublicTransportHafas", {
}
},

startFetchingLoop: function (interval) {
startFetchingLoop(interval) {
// start immediately ...
this.sendSocketNotification("FETCH_DEPARTURES", this.identifier);

// ... and then repeat in the given interval

if (this.updatesIntervalID === 0) {
//if this instance as no auto update defined, then we create one. Otherwise : nothing.
// if this instance as no auto update defined, then we create one. Otherwise : nothing.

this.updatesIntervalID = setInterval(() => {
this.sendSocketNotification("FETCH_DEPARTURES", this.identifier);
}, interval * 1000);
}
},

hasErrors: function () {
hasErrors() {
return Object.keys(this.error).length > 0;
}
});
Loading

0 comments on commit 5037dba

Please sign in to comment.