Read the statement by Michael Teeuw here.
read csv-data and put it in an array
-
this is the getDom-section where the data finally should be placed (perlchamp is for testing) :
getDom: function() { if ((moment() > this.midnight) || (!this.loaded)) { var month = moment().month(); var year = moment().year(); var monthName = moment().format("MMMM"); var monthLength = moment().daysInMonth(); // tabelle erstellen var wrapper = document.createElement("table"); wrapper.className = 'table'; wrapper.id = 'birthday-table'; // tabellenkopf erstellen var tHeader = document.createElement("thead"); tHeader.className = "thead"; tHeader.id = "birthday-thead"; var headerTR = document.createElement("tr"); headerTR.className = "tr-head"; headerTR.id = "birthday-tr-head"; var headerTH = document.createElement("th"); headerTH.colSpan = "2"; headerTH.scope = "col"; headerTH.className = "th-head"; headerTH.id = "birthday-th-head"; headerTH.innerHTML = this.translate(this.config.title); headerTR.appendChild(headerTH); tHeader.appendChild(headerTR); wrapper.appendChild(tHeader); // Create TFOOT section -- currently used for debugging only var tFooter = document.createElement('tfoot'); tFooter.className = "tfoot"; tFooter.id = "birthday-tfoot"; var footerTR = document.createElement("tr"); footerTR.className = "tr-foot"; footerTR.id = "birthday-tr-foot"; var footerTD = document.createElement("td"); footerTD.colSpan ="2"; footerTD.className = "td-foot"; footerTD.id = "birthday-td-foot"; if (this.config.debugging) { footerTD.innerHTML = "Birthdaylist is currently in DEBUG mode!<br />Please see console log."; } else { footerTD.innerHTML = ""; } footerTR.appendChild(footerTD); tFooter.appendChild(footerTR); wrapper.appendChild(tFooter); // Create TBODY section with day names var eintraege = 3; for (var i=1; i<=eintraege; i++) { var tBody = document.createElement("tBody"); tBody.className = "tbody"; tBody.id = "birthday-tbody"; var bodyTR = document.createElement("tr"); bodyTR.className = "tr-body"; bodyTR.id = "birthday-tr-body"; var bodyTDimage = document.createElement("td"); bodyTDimage.className = "td-image"; bodyTDimage.id = "birthday-td-image"; bodyTDimage.innerHTML = "23"; var bodyTD = document.createElement("td"); bodyTD.className = "td-body"; bodyTD.id = "birthday-td-body"; bodyTD.innerHTML = "Bettina Zimmermann <span class=\"age-span\">46</span></br>Frank Dubiel <span class=\"age-span\">54</span></br>Jürgen Gruse <span class=\"age-span\">52</span>"; /* var bodyAgeSpan = document.createElement("span"); bodyAgeSpan.className = "age-span"; bodyAgeSpan.id = "birthday-age-span"; bodyAgeSpan.innerHTML = " 46"; bodyTD.appendChild(bodyAgeSpan); */ bodyTR.appendChild(bodyTDimage); bodyTR.appendChild(bodyTD); tBody.appendChild(bodyTR); wrapper.appendChild(tBody); } this.loaded = true; return wrapper; }
the data here are text, ifilles in, no variables …
-
@Perlchamp ok, couple problems… in teh notification handler we write to active_birthdays,
but it is a variable local to this routine… so, when this routine ends, it goes awauneed to move its definition to the top
defaults: { language: "de", dimmEntries: true // true: dims entries and the associated // symbol when the date has expired. // false: delete entries and the associated // symbol when the date has expired. }, active_birthdays : {},
then add ‘this.’ in front of it in the notification handler
(and remove the var activebirthdays)now it is an instance variable (one per browser)
also, I spelled substring wrong (I had it subString, its substring )
gotta run to store, back in a little bit
-
ok, be carefully. keep corona away. use mouth and nose protection and wear disposable gloves !
thanks, but doesn’t work. it seems, that i’m too stupid for this ?
modified perlchamp.js:Module.register("perlchamp", { defaults: { language: "de", dimmEntries: true // true: dims entries and the associated // symbol when the date has expired. // false: delete entries and the associated // symbol when the date has expired. }, active_birthdays: { }, init: function(){ Log.log(this.name + " is in init!"); }, start: function(){ Log.log("Starting module: " + this.name); // Set locale moment.locale(config.language); }, loaded: function(callback) { Log.log(this.name + " is loaded!"); callback(); }, // return list of other functional scripts to use, if any (like require in node_helper) getScripts: function() { return ["moment.js"]; }, // return list of stylesheet files to use if any getStyles: function() { return [this.data.path + "/css/perlchamp.css"]; }, // only called if the module header was configured in module config in config.js getHeader: function() { return this.data.header; }, // messages received from other modules and the system (NOT from your node helper) // payload is a notification dependent data structure notificationReceived: function(notification, payload, sender) { // once everybody is loaded up if(notification==="ALL_MODULES_STARTED"){ // send our config to our node_helper this.sendSocketNotification("CONFIG",this.config); } if (sender) { Log.log(this.name + " received a module notification: " + notification + " from sender: " + sender.name); } else { Log.log(this.name + " received a system notification: " + notification); } }, // messages received from from your node helper (NOT other modules or the system) // payload is a notification dependent data structure, up to you to design between module and node_helper socketNotificationReceived: function(notification, payload) { if(notification === "have data"){ Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload); var now = moment(); for(var birthday of payload) { // get 1st 5 chars of birthdate, thru month // we will use this as the key in the hash var birth_date = birthday.birth.substring(0,4); // get the birthday as a moment in this year, for comparing var birth_date_moment = moment(birth_date + now.getYear(),"DD.MMYYYY"); // u can add days to a moment object and then compare // if the birthdate is before that date (and after now) // so within the next xx days // if the date is the same or later, don't use time of day if(birth_date.startOf('day').isSame(now.startOf('day'))) { // birthday is in this month // check the hash if we've seen anything for today yet // if we haven't see this date yet if(this.active_birthdays[birth_date] == undefined){ // create the holder for its info (array of // names) in the hash this.active_birthdays[birth_date] = []; } // save the persons name on the list this.active_birthdays[birth_date].push(birthday.name); } } // tell MM to call and get our content self.updateDom(); } }, // system notification your module is being hidden // typically you would stop doing UI updates (getDom/updateDom) if the module is hidden suspend: function(){ }, // system notification your module is being unhidden/shown // typically you would resume doing UI updates (getDom/updateDom) if the module is shown resume: function(){ }, // this is the major worker of the module, it provides the displayable content for this module getDom: function() { var wrapper = document.createElement("div"); for(var birthday of Object.keys(this.active_birthdays)){ for(var name of this.active_birthdays[birthday]){ var m = document.createElement('div'); m.innerText = birthday + ' '+ name; wrapper.appendChild(m); } } // pass the created content back to MM to add to DOM. return wrapper; } })
in node_helper.js was nothing to be changed. maybe I put a few semicolons where none have to go?
-
sorry, forgot one thing i saw in node helper,… need to move everything in the start function to the socketNotification section… at start time the module is not up yet, so the message goes nowhere
socketNotificationReceived(notification, payload) { var self = this // if config message from module if (notification === "CONFIG") { // save payload config info this.config = payload; var self = this; console.log("Starting module helper: " + this.name); // convert the csv-file into a JSON-String const csvFilePath = this.path + '/birthdays.csv'; csv() .fromFile(csvFilePath) .then((jsonObj)=>{ birthdaysArray = JSON.stringify(jsonObj); // for debugging only console.log("birthdaysArray: ", JSON.stringify(birthdaysArray)); // send data to [modulname].js self.sendSocketNotification("have data", jsonObj); }) } else if(notification === "????2") { } },
also, by moving it here, all you have to do is fix the code, save and then refresh the page… don’t have to restart MM
start is now empty
start() { },
-
ok, nothing changed and i don’t see any messages of “HAVEDATA” (changed it in both files to big letter without space). i only see the header “Geburtstage” … :-(
node_helper.js:var NodeHelper = require("node_helper"); var moment = require("moment"); // add require of other javascripot components here // var xxx = require('yyy'); here const csv = require("csvtojson"); var birthdaysArray = []; module.exports = NodeHelper.create({ /* init(){ console.log("init module helper perlchamp"); }, */ start() { }, /* stop(){ console.log("Stopping module helper: " + this.name); }, */ // handle messages from our module// each notification indicates a different messages // payload is a data structure that is different per message.. up to you to design this socketNotificationReceived(notification, payload) { var self = this; console.log("Starting module helper: " + this.name); // convert the csv-file into a JSON-String const csvFilePath = this.path + '/data/birthdays.csv'; csv() .fromFile(csvFilePath) .then((jsonObj)=>{ birthdaysArray = JSON.stringify(jsonObj); // send data to [modulname].js self.sendSocketNotification("HAVEDATA", jsonObj); }) // if config message from module if (notification === "CONFIG") { // save payload config info this.config = payload; // wait 15 seconds, send a message back to module setTimeout(()=> {this.sendSocketNotification("message_from_helper"," this is a test_message")}, 15000); } else if(notification === "????2") { } }, });
maye change this part, cause i never need this variable birthdaysArray:
.then((jsonObj)=>{ birthdaysArray = JSON.stringify(jsonObj);
perlchamp.js:
Module.register("perlchamp", { defaults: { language: "de", dimmEntries: true // true: dims entries and the associated // symbol when the date has expired. // false: delete entries and the associated // symbol when the date has expired. }, active_birthdays: { }, init: function(){ Log.log(this.name + " is in init!"); }, start: function(){ Log.log("Starting module: " + this.name); // Set locale moment.locale(config.language); }, loaded: function(callback) { Log.log(this.name + " is loaded!"); callback(); }, // return list of other functional scripts to use, if any (like require in node_helper) getScripts: function() { return ["moment.js"]; }, // return list of stylesheet files to use if any getStyles: function() { return [this.data.path + "/css/perlchamp.css"]; }, // only called if the module header was configured in module config in config.js getHeader: function() { return this.data.header; }, // messages received from other modules and the system (NOT from your node helper) // payload is a notification dependent data structure notificationReceived: function(notification, payload, sender) { // once everybody is loaded up if(notification==="ALL_MODULES_STARTED"){ // send our config to our node_helper this.sendSocketNotification("CONFIG",this.config); } if (sender) { Log.log(this.name + " received a module notification: " + notification + " from sender: " + sender.name); } else { Log.log(this.name + " received a system notification: " + notification); } }, // messages received from from your node helper (NOT other modules or the system) // payload is a notification dependent data structure, up to you to design between module and node_helper socketNotificationReceived: function(notification, payload) { if(notification === "HAVEDATE"){ Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload); var now = moment(); for(var birthday of payload) { // get 1st 5 chars of birthdate, thru month // we will use this as the key in the hash var birth_date = birthday.birth.substring(0,4); // get the birthday as a moment in this year, for comparing var birth_date_moment = moment(birth_date + now.getYear(),"DD.MMYYYY"); // u can add days to a moment object and then compare // if the birthdate is before that date (and after now) // so within the next xx days // if the date is the same or later, don't use time of day if(birth_date.startOf('day').isSame(now.startOf('day'))) { // birthday is in this month // check the hash if we've seen anything for today yet // if we haven't see this date yet if(this.active_birthdays[birth_date] == undefined){ // create the holder for its info (array of // names) in the hash this.active_birthdays[birth_date] = []; } // save the persons name on the list this.active_birthdays[birth_date].push(birthday.name); } } // tell MM to call and get our content self.updateDom(); } }, // system notification your module is being hidden // typically you would stop doing UI updates (getDom/updateDom) if the module is hidden suspend: function(){ }, // system notification your module is being unhidden/shown // typically you would resume doing UI updates (getDom/updateDom) if the module is shown resume: function(){ }, // this is the major worker of the module, it provides the displayable content for this module getDom: function() { var wrapper = document.createElement("div"); for(var birthday of Object.keys(this.active_birthdays)){ for(var name of this.active_birthdays[birthday]){ var m = document.createElement('div'); m.innerText = birthday + ' '+ name; wrapper.appendChild(m); } } // pass the created content back to MM to add to DOM. return wrapper; } })
please have an eye on the getDom-sektion.
-
no… it will do the thig every time a notifcation comes in, only want it when the module sends down a message (say it is alive)
in node_help you are sending this “HAVEDATA”
and in module you are looking for this if(notification === “HAVEDATE”){add a var self = this to start of socketNotificationReceived() and getDom()
-
@Perlchamp said in read csv-data and put it in an array:
if(birth_date.startOf(‘day’)
also should be
if(birth_date_moment.startOf('day')
comparing moments, not strings! oops
-
only the header i see, BUT now i see the terminal-message “HAVEDATA” but an error, too:
-
@Perlchamp open the developers window, ctrl-shift-i on the mirror screen,
select console tab,
put perl in the filter fieldselect the sources tab, navigate the left nav tree,. to modules/perlchamp/perlcmap.js
find the socketnotificationreceived
click the number in the left column, hit f5 (refresh) on keyboard
code should stop at the line you clicked
in the right pane, top there is a blue arrow (resume executing) and a top half of a globe, stop on next )instruction)
-
before looking in dev: i saw the following:
var birth_date_moment = moment(birth_date + now.getYear(),"DD.MMYYYY");
change to DD.MM.YYYY (point between MM and YYYY) ?