├── README.md
├── SampleModule.js
└── node_helper.js
/README.md:
--------------------------------------------------------------------------------
1 | # SampleModule
2 | sample MM module:
3 |
4 | this module shows the basics as documented in the MM development spec.
5 |
6 | add this to the modules list in config/config.js
7 |
8 | ```
9 | {
10 | module:"SampleModule",
11 | position:"middle_center",
12 | config:{
13 | message:"some message to be displayed by this module, this is optional as the module provides a default"
14 | }
15 | }
16 | ```
17 | the module naming rules are
18 |
19 | everything in MM is case sensitive
20 | Test is not the same as test
21 | module name = folder name in MagicMirror/modules = filename of the main js (modulename.js, here SampleModule.js) = the name used in the register statement
22 | ```
23 | Module.register("SampleModule", {
24 | ```
25 |
--------------------------------------------------------------------------------
/SampleModule.js:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | sample module structure
4 |
5 |
6 | */
7 |
8 |
9 | Module.register("SampleModule", {
10 | // define variables used by module, but not in config data
11 | some_variable: true,
12 | some_other_variable: "a string",
13 |
14 | // holder for config info from module_name.js
15 | config:null,
16 |
17 | // anything here in defaults will be added to the config data
18 | // and replaced if the same thing is provided in config
19 | defaults: {
20 | message: "default message if none supplied in config.js"
21 | },
22 |
23 | init: function(){
24 | Log.log(this.name + " is in init!");
25 | },
26 |
27 | start: function(){
28 | Log.log(this.name + " is starting!");
29 | },
30 |
31 | loaded: function(callback) {
32 | Log.log(this.name + " is loaded!");
33 | callback();
34 | },
35 |
36 | // return list of other functional scripts to use, if any (like require in node_helper)
37 | getScripts: function() {
38 | return [
39 | // sample of list of files to specify here, if no files,do not use this routine, or return empty list
40 |
41 | //'script.js', // will try to load it from the vendor folder, otherwise it will load is from the module folder.
42 | //'moment.js', // this file is available in the vendor folder, so it doesn't need to be available in the module folder.
43 | //this.file('anotherfile.js'), // this file will be loaded straight from the module folder.
44 | //'https://code.jquery.com/jquery-2.2.3.min.js', // this file will be loaded from the jquery servers.
45 | ]
46 | },
47 |
48 | // return list of stylesheet files to use if any
49 | getStyles: function() {
50 | return [
51 | // sample of list of files to specify here, if no files, do not use this routine, , or return empty list
52 |
53 | //'script.css', // will try to load it from the vendor folder, otherwise it will load is from the module folder.
54 | //'font-awesome.css', // this file is available in the vendor folder, so it doesn't need to be avialable in the module folder.
55 | //this.file('anotherfile.css'), // this file will be loaded straight from the module folder.
56 | //'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css', // this file will be loaded from the bootstrapcdn servers.
57 | ]
58 | },
59 |
60 | // return list of translation files to use, if any
61 | /*getTranslations: function() {
62 | return {
63 | // sample of list of files to specify here, if no files, do not use this routine, , or return empty list
64 |
65 | // en: "translations/en.json", (folders and filenames in your module folder)
66 | // de: "translations/de.json"
67 | }
68 | }, */
69 |
70 |
71 |
72 | // only called if the module header was configured in module config in config.js
73 | getHeader: function() {
74 | return this.data.header + " Foo Bar";
75 | },
76 |
77 | // messages received from other modules and the system (NOT from your node helper)
78 | // payload is a notification dependent data structure
79 | notificationReceived: function(notification, payload, sender) {
80 | // once everybody is loaded up
81 | if(notification==="ALL_MODULES_STARTED"){
82 | // send our config to our node_helper
83 | this.sendSocketNotification("CONFIG",this.config)
84 | }
85 | if (sender) {
86 | Log.log(this.name + " received a module notification: " + notification + " from sender: " + sender.name);
87 | } else {
88 | Log.log(this.name + " received a system notification: " + notification);
89 | }
90 | },
91 |
92 | // messages received from from your node helper (NOT other modules or the system)
93 | // payload is a notification dependent data structure, up to you to design between module and node_helper
94 | socketNotificationReceived: function(notification, payload) {
95 | Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
96 | if(notification === "message_from_helper"){
97 | this.config.message = payload;
98 | // tell mirror runtime that our data has changed,
99 | // we will be called back at GetDom() to provide the updated content
100 | this.updateDom(1000)
101 | }
102 |
103 | },
104 |
105 | // system notification your module is being hidden
106 | // typically you would stop doing UI updates (getDom/updateDom) if the module is hidden
107 | suspend: function(){
108 |
109 | },
110 |
111 | // system notification your module is being unhidden/shown
112 | // typically you would resume doing UI updates (getDom/updateDom) if the module is shown
113 | resume: function(){
114 |
115 | },
116 |
117 | // this is the major worker of the module, it provides the displayable content for this module
118 | getDom: function() {
119 | var wrapper = document.createElement("div");
120 |
121 | // if user supplied message text in its module config, use it
122 | if(this.config.hasOwnProperty("message")){
123 | // using text from module config block in config.js
124 | wrapper.innerHTML = this.config.message;
125 | }
126 | else{
127 | // use hard coded text
128 | wrapper.innerHTML = "Hello world!";
129 | }
130 |
131 | // pass the created content back to MM to add to DOM.
132 | return wrapper;
133 | },
134 |
135 | })
136 |
--------------------------------------------------------------------------------
/node_helper.js:
--------------------------------------------------------------------------------
1 | var NodeHelper = require("node_helper");
2 |
3 | // add require of other javascripot components here
4 | // var xxx = require('yyy') here
5 |
6 | module.exports = NodeHelper.create({
7 |
8 | init(){
9 | console.log("init module helper SampleModule");
10 | },
11 |
12 | start() {
13 | console.log('Starting module helper:' +this.name);
14 | },
15 |
16 | stop(){
17 | console.log('Stopping module helper: ' +this.name);
18 | },
19 |
20 | // handle messages from our module// each notification indicates a different messages
21 | // payload is a data structure that is different per message.. up to you to design this
22 | socketNotificationReceived(notification, payload) {
23 | console.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
24 | // if config message from module
25 | if (notification === "CONFIG") {
26 | // save payload config info
27 | this.config=payload
28 | // wait 15 seconds, send a message back to module
29 | setTimeout(()=> { this.sendSocketNotification("message_from_helper"," this is a test_message")}, 15000)
30 | }
31 | else if(notification === "????2") {
32 | }
33 |
34 | },
35 |
36 | });
--------------------------------------------------------------------------------