-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
176 lines (121 loc) · 5.42 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* global Module, MMM-ModulePosition */
/* Magic Mirror
* Module: node_helper
*
* By Neil Scott
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var moment = require("moment");
//fs = require('fs');
const fs = require('node:fs/promises');
const Log = require("logger");
//pseudo structures for commonality across all modules
//obtained from a helper file of modules
var LOG = require('../MMM-FeedUtilities/LOG');
var RSS = require('../MMM-FeedUtilities/RSS');
//// get required structures and utilities
//const structures = require("../MMM-ChartUtilities/structures");
//const utilities = require("../MMM-ChartUtilities/common");
//const JSONutils = new utilities.JSONutils();
//const configutils = new utilities.configutils();
module.exports = NodeHelper.create({
start: function () {
this.debug = true;
console.log(this.name + ' is started!');
Log.info(`MMM-ModulePosition node_helper.js started`);
this.consumerstorage = {}; // contains the config and feedstorage
this.currentmoduleinstance = '';
this.logger = {};
},
setconfig: function (aconfig) {
var moduleinstance = aconfig.moduleinstance;
var config = aconfig.config;
//store a local copy so we dont have keep moving it about
this.consumerstorage[moduleinstance] = { config: config, feedstorage: {} };
//additional work to simplify the config for use in the module
},
showstatus: function (moduleinstance) {
//console.log("MMM Module: " + moduleinstance);
console.log('============================ start of status ========================================');
console.log('config for consumer: ' + moduleinstance);
console.log(this.consumerstorage[moduleinstance].config);
console.log('============================= end of status =========================================');
},
showElapsed: function () {
endTime = new Date();
var timeDiff = endTime - startTime; //in ms
// strip the ms
timeDiff /= 1000;
// get seconds
var seconds = Math.round(timeDiff);
return (" " + seconds + " seconds");
},
stop: function () {
console.log("Shutting down node_helper");
},
socketNotificationReceived: function (notification, payload) {
//console.log(this.name + " NODE_HELPER received a socket notification: " + notification + " - Payload: " + payload);
//we will receive a payload with the moduleinstance of the consumerid in it so we can store data and respond to the correct instance of
//the caller - i think that this may be possible!!
if (this.logger[payload.moduleinstance] == null) {
this.logger[payload.moduleinstance] = LOG.createLogger("logfile_" + payload.moduleinstance + ".log", payload.moduleinstance);
};
this.currentmoduleinstance = payload.moduleinstance;
switch (notification) {
case "CONFIG": this.setconfig(payload); break;
case "RESET": this.reset(payload); break;
case "WRITE_THIS": this.writethis(payload); break;
case "STATUS": this.showstatus(payload); break;
}
},
writethis: function (payload) {
//1) create a custom_css set of entries at the module and actual names levels if a duplicate
//using IDs not classes
//ignore all ignorable modules and any not active or amended
var modules = payload.payload;
var css = '';
var modCount = 0;
for (var module in modules) {
var thismod = modules[module];
//may need to have them next to each other not a space, a space means any ?
if (!thismod.ignore && thismod.state.active && thismod.state.amended) {
css = css + '.' + thismod.name +
((thismod.duplicate) ? '#' + module : '') +
' {' +
'\n\tleft:' + thismod.modpos.x + "px;" +
'\n\ttop:' + thismod.modpos.y + "px;" +
'\n\twidth:' + thismod.modpos.w + "px;" +
'\n\theight:' + thismod.modpos.h + "px;" +
'\n\tposition:' + 'absolute' + " !important; /*included to overide the transition static positioning that is added inline */" +
'\n}' + "\r\n"
modCount = modCount + 1;
}
}
// dont check if there are no modpos ! just write it
//create a directory to store these - not under modpos or add the directory as a gitignore
//alert("BANG");
var cssfilename = 'modules/MMM-ModulePosition/css/custom.css.' + new Date().getTime(); //simplest format though smelly
//Log.info(`about to call module writeCSSfile`);
this.writeCSSfile(cssfilename, css, modCount)
//fs.writeFile(cssfilename, css, 'utf8', (err) => {
// if(err) console.error(err);
// console.log('The file has been saved!');
// if (this.consumerstorage[this.currentmoduleinstance].config.showAlerts) this.sendNotificationToMasterModule("ALERT", " FILE:" + cssfilename + " saved with " + modCount + " module" + ((modCount>1) ? 's' : '') +" positioned.");
//});
},
sendNotificationToMasterModule: function (stuff, stuff2) {
this.sendSocketNotification(stuff, stuff2);
},
writeCSSfile: function (filename, filecontent, modcount) {
//Log.info(`In module writeCSSfile`);
try {
fs.writeFile(filename, filecontent);
setTimeout(() => {}, 1000);
Log.info(`The file has been saved: ${filename}`);
if (this.consumerstorage[this.currentmoduleinstance].config.showAlerts) this.sendNotificationToMasterModule("ALERT", " NEW CSS FILE:" + filename + " saved with " + modcount + " module" + ((modcount > 1) ? 's' : '') + " positioned.");
} catch (err) {
Log.error(err);
}
},
});