2022-01-26 23:09:26 +01:00
|
|
|
/* MagicMirror²
|
2019-12-31 19:31:13 +00:00
|
|
|
* Node Helper Superclass
|
|
|
|
*
|
2020-04-28 23:05:28 +02:00
|
|
|
* By Michael Teeuw https://michaelteeuw.nl
|
2019-12-31 19:31:13 +00:00
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
2020-05-02 10:39:09 +02:00
|
|
|
const express = require("express");
|
Release 2.23.0 (#3078)
## [2.23.0] - 2023-04-04
Thanks to: @angeldeejay, @buxxi, @CarJem, @dariom, @DaveChild, @dWoolridge, @grenagit, @Hirschberger, @KristjanESPERANTO, @MagMar94, @naveensrinivasan, @nfogal, @psieg, @rajniszp, @retroflex, @SkySails and @tomzt.
Special thanks to @khassel, @rejas and @sdetweil for taking over most (if not all) of the work on this release as project collaborators. This version would not be there without their effort. Thank you guys! You are awesome!
### Added
- Added increments for hourly forecasts in weather module (#2996)
- Added tests for hourly weather forecast
- Added possibility to ignore MagicMirror repo in updatenotification module
- Added Pirate Weather as new weather provider (#3005)
- Added possibility to use your own templates in Alert module
- Added error message if `<modulename>.js` file is missing in module folder to get a hint in the logs (#2403)
- Added possibility to use environment variables in `config.js` (#1756)
- Added option `pastDaysCount` to default calendar module to control of how many days past events should be displayed
- Added thai language to alert module
- Added option `sendNotifications` in clock module (#3056)
### Removed
- Removed darksky weather provider
- Removed unneeded (and unwanted) '.' after the year in calendar repeatingCountTitle (#2896)
### Updated
- Use develop as target branch for dependabot
- Update issue template, contributing doc and sample config
- The weather modules clearly separates precipitation amount and probability (risk of rain/snow)
- This requires all providers that only supports probability to change the config from `showPrecipitationAmount` to `showPrecipitationProbability`.
- Update tests for weather and calendar module
- Changed updatenotification module for MagicMirror repo only: Send only notifications for `master` if there is a tag on a newer commit
- Update dates in Calendar widgets every minute
- Cleanup jest coverage for patches
- Update `stylelint` dependencies, switch to `stylelint-config-standard` and handle `stylelint` issues, update `main.css` matching new rules
- Update Eslint config, add new rule and handle issue
- Convert lots of callbacks to async/await
- Revise require imports (#3071 and #3072)
### Fixed
- Fix wrong day labels in envcanada forecast (#2987)
- Fix for missing default class name prefix for customEvents in calendar
- Fix electron flashing white screen on startup (#1919)
- Fix weathergov provider hourly forecast (#3008)
- Fix message display with HTML code into alert module (#2828)
- Fix typo in french translation
- Yr wind direction is no longer inverted
- Fix async node_helper stopping electron start (#2487)
- The wind direction arrow now points in the direction the wind is flowing, not into the wind (#3019)
- Fix precipitation css styles and rounding value
- Fix wrong vertical alignment of calendar title column when wrapEvents is true (#3053)
- Fix empty news feed stopping the reload forever
- Fix e2e tests (failed after async changes) by running calendar and newsfeed tests last
- Lint: Use template literals instead of string concatenation
- Fix default alert module to render HTML for title and message
- Fix Open-Meteo wind speed units
2023-04-04 20:44:32 +02:00
|
|
|
const Log = require("logger");
|
|
|
|
const Class = require("./class");
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2021-01-05 18:44:36 +01:00
|
|
|
const NodeHelper = Class.extend({
|
|
|
|
init() {
|
2020-05-11 07:25:42 +02:00
|
|
|
Log.log("Initializing new module helper ...");
|
2019-12-31 19:31:13 +00:00
|
|
|
},
|
|
|
|
|
Release 2.23.0 (#3078)
## [2.23.0] - 2023-04-04
Thanks to: @angeldeejay, @buxxi, @CarJem, @dariom, @DaveChild, @dWoolridge, @grenagit, @Hirschberger, @KristjanESPERANTO, @MagMar94, @naveensrinivasan, @nfogal, @psieg, @rajniszp, @retroflex, @SkySails and @tomzt.
Special thanks to @khassel, @rejas and @sdetweil for taking over most (if not all) of the work on this release as project collaborators. This version would not be there without their effort. Thank you guys! You are awesome!
### Added
- Added increments for hourly forecasts in weather module (#2996)
- Added tests for hourly weather forecast
- Added possibility to ignore MagicMirror repo in updatenotification module
- Added Pirate Weather as new weather provider (#3005)
- Added possibility to use your own templates in Alert module
- Added error message if `<modulename>.js` file is missing in module folder to get a hint in the logs (#2403)
- Added possibility to use environment variables in `config.js` (#1756)
- Added option `pastDaysCount` to default calendar module to control of how many days past events should be displayed
- Added thai language to alert module
- Added option `sendNotifications` in clock module (#3056)
### Removed
- Removed darksky weather provider
- Removed unneeded (and unwanted) '.' after the year in calendar repeatingCountTitle (#2896)
### Updated
- Use develop as target branch for dependabot
- Update issue template, contributing doc and sample config
- The weather modules clearly separates precipitation amount and probability (risk of rain/snow)
- This requires all providers that only supports probability to change the config from `showPrecipitationAmount` to `showPrecipitationProbability`.
- Update tests for weather and calendar module
- Changed updatenotification module for MagicMirror repo only: Send only notifications for `master` if there is a tag on a newer commit
- Update dates in Calendar widgets every minute
- Cleanup jest coverage for patches
- Update `stylelint` dependencies, switch to `stylelint-config-standard` and handle `stylelint` issues, update `main.css` matching new rules
- Update Eslint config, add new rule and handle issue
- Convert lots of callbacks to async/await
- Revise require imports (#3071 and #3072)
### Fixed
- Fix wrong day labels in envcanada forecast (#2987)
- Fix for missing default class name prefix for customEvents in calendar
- Fix electron flashing white screen on startup (#1919)
- Fix weathergov provider hourly forecast (#3008)
- Fix message display with HTML code into alert module (#2828)
- Fix typo in french translation
- Yr wind direction is no longer inverted
- Fix async node_helper stopping electron start (#2487)
- The wind direction arrow now points in the direction the wind is flowing, not into the wind (#3019)
- Fix precipitation css styles and rounding value
- Fix wrong vertical alignment of calendar title column when wrapEvents is true (#3053)
- Fix empty news feed stopping the reload forever
- Fix e2e tests (failed after async changes) by running calendar and newsfeed tests last
- Lint: Use template literals instead of string concatenation
- Fix default alert module to render HTML for title and message
- Fix Open-Meteo wind speed units
2023-04-04 20:44:32 +02:00
|
|
|
loaded() {
|
2021-01-05 18:44:36 +01:00
|
|
|
Log.log(`Module helper loaded: ${this.name}`);
|
2019-12-31 19:31:13 +00:00
|
|
|
},
|
|
|
|
|
2021-01-05 18:44:36 +01:00
|
|
|
start() {
|
|
|
|
Log.log(`Starting module helper: ${this.name}`);
|
2019-12-31 19:31:13 +00:00
|
|
|
},
|
|
|
|
|
2021-10-18 14:36:31 +02:00
|
|
|
/**
|
2022-01-26 23:47:51 +01:00
|
|
|
* Called when the MagicMirror² server receives a `SIGINT`
|
2019-12-31 19:31:13 +00:00
|
|
|
* Close any open connections, stop any sub-processes and
|
|
|
|
* gracefully exit the module.
|
|
|
|
*/
|
2021-01-05 18:44:36 +01:00
|
|
|
stop() {
|
|
|
|
Log.log(`Stopping module helper: ${this.name}`);
|
2019-12-31 19:31:13 +00:00
|
|
|
},
|
|
|
|
|
2021-10-18 14:36:31 +02:00
|
|
|
/**
|
2019-12-31 19:31:13 +00:00
|
|
|
* This method is called when a socket notification arrives.
|
2021-10-18 14:36:31 +02:00
|
|
|
* @param {string} notification The identifier of the notification.
|
|
|
|
* @param {*} payload The payload of the notification.
|
2019-12-31 19:31:13 +00:00
|
|
|
*/
|
2021-01-05 18:44:36 +01:00
|
|
|
socketNotificationReceived(notification, payload) {
|
|
|
|
Log.log(`${this.name} received a socket notification: ${notification} - Payload: ${payload}`);
|
2019-12-31 19:31:13 +00:00
|
|
|
},
|
|
|
|
|
2021-10-18 14:36:31 +02:00
|
|
|
/**
|
2019-12-31 19:31:13 +00:00
|
|
|
* Set the module name.
|
2021-10-18 14:36:31 +02:00
|
|
|
* @param {string} name Module name.
|
2019-12-31 19:31:13 +00:00
|
|
|
*/
|
2021-01-05 18:44:36 +01:00
|
|
|
setName(name) {
|
2019-12-31 19:31:13 +00:00
|
|
|
this.name = name;
|
|
|
|
},
|
|
|
|
|
2021-10-18 14:36:31 +02:00
|
|
|
/**
|
2019-12-31 19:31:13 +00:00
|
|
|
* Set the module path.
|
2021-10-18 14:36:31 +02:00
|
|
|
* @param {string} path Module path.
|
2019-12-31 19:31:13 +00:00
|
|
|
*/
|
2021-01-05 18:44:36 +01:00
|
|
|
setPath(path) {
|
2019-12-31 19:31:13 +00:00
|
|
|
this.path = path;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* sendSocketNotification(notification, payload)
|
|
|
|
* Send a socket notification to the node helper.
|
|
|
|
*
|
|
|
|
* argument notification string - The identifier of the notification.
|
|
|
|
* argument payload mixed - The payload of the notification.
|
|
|
|
*/
|
2021-01-05 18:44:36 +01:00
|
|
|
sendSocketNotification(notification, payload) {
|
2019-12-31 19:31:13 +00:00
|
|
|
this.io.of(this.name).emit(notification, payload);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* setExpressApp(app)
|
|
|
|
* Sets the express app object for this module.
|
|
|
|
* This allows you to host files from the created webserver.
|
|
|
|
*
|
|
|
|
* argument app Express app - The Express app object.
|
|
|
|
*/
|
2021-01-05 18:44:36 +01:00
|
|
|
setExpressApp(app) {
|
2019-12-31 19:31:13 +00:00
|
|
|
this.expressApp = app;
|
|
|
|
|
2021-01-05 18:44:36 +01:00
|
|
|
app.use(`/${this.name}`, express.static(`${this.path}/public`));
|
2019-12-31 19:31:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/* setSocketIO(io)
|
|
|
|
* Sets the socket io object for this module.
|
|
|
|
* Binds message receiver.
|
|
|
|
*
|
|
|
|
* argument io Socket.io - The Socket io object.
|
|
|
|
*/
|
2021-01-05 18:44:36 +01:00
|
|
|
setSocketIO(io) {
|
|
|
|
this.io = io;
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2021-01-05 18:44:36 +01:00
|
|
|
Log.log(`Connecting socket for: ${this.name}`);
|
|
|
|
|
|
|
|
io.of(this.name).on("connection", (socket) => {
|
2019-12-31 19:31:13 +00:00
|
|
|
// add a catch all event.
|
2021-01-05 18:44:36 +01:00
|
|
|
const onevent = socket.onevent;
|
2020-05-11 22:22:32 +02:00
|
|
|
socket.onevent = function (packet) {
|
2021-01-05 18:44:36 +01:00
|
|
|
const args = packet.data || [];
|
2020-03-15 15:49:34 +01:00
|
|
|
onevent.call(this, packet); // original call
|
2019-12-31 19:31:13 +00:00
|
|
|
packet.data = ["*"].concat(args);
|
2020-03-15 15:49:34 +01:00
|
|
|
onevent.call(this, packet); // additional call to catch-all
|
2019-12-31 19:31:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// register catch all.
|
2021-01-05 18:44:36 +01:00
|
|
|
socket.on("*", (notification, payload) => {
|
2019-12-31 19:31:13 +00:00
|
|
|
if (notification !== "*") {
|
2021-01-05 18:44:36 +01:00
|
|
|
this.socketNotificationReceived(notification, payload);
|
2019-12-31 19:31:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-24 09:14:08 +02:00
|
|
|
NodeHelper.checkFetchStatus = function (response) {
|
|
|
|
// response.status >= 200 && response.status < 300
|
|
|
|
if (response.ok) {
|
|
|
|
return response;
|
|
|
|
} else {
|
|
|
|
throw Error(response.statusText);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-15 14:49:04 +02:00
|
|
|
/**
|
|
|
|
* Look at the specified error and return an appropriate error type, that
|
|
|
|
* can be translated to a detailed error message
|
|
|
|
* @param {Error} error the error from fetching something
|
|
|
|
* @returns {string} the string of the detailed error message in the translations
|
|
|
|
*/
|
2021-05-02 10:24:22 +02:00
|
|
|
NodeHelper.checkFetchError = function (error) {
|
|
|
|
let error_type = "MODULE_ERROR_UNSPECIFIED";
|
|
|
|
if (error.code === "EAI_AGAIN") {
|
|
|
|
error_type = "MODULE_ERROR_NO_CONNECTION";
|
2021-05-02 14:43:12 +02:00
|
|
|
} else if (error.message === "Unauthorized") {
|
|
|
|
error_type = "MODULE_ERROR_UNAUTHORIZED";
|
2021-05-02 10:24:22 +02:00
|
|
|
}
|
|
|
|
return error_type;
|
|
|
|
};
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
NodeHelper.create = function (moduleDefinition) {
|
2019-12-31 19:31:13 +00:00
|
|
|
return NodeHelper.extend(moduleDefinition);
|
|
|
|
};
|
|
|
|
|
2021-01-05 18:44:36 +01:00
|
|
|
module.exports = NodeHelper;
|