Read the statement by Michael Teeuw here.
Editing files outside of MM
-
I’m learning JS, just to develop a module, but I’m not sure how to edit a file outside magic mirror. I’ve seen people use fs and request, but I’m not sure what those are if someone could help.
-
@aareben not quite sure what u mean ‘edit’ a file outside of mm
see my sample module https://github.com/sdetweil/SampleModule
fs reads files from the local disk, request sends a ‘get’ http request to some remote (outside the pi usually) machine to get data
the nodejs libraries are documented online
https://nodejs.org/api/modules.htmlgood search for how to do ?/? in javascript will get u info…
1st thing to do is write down the steps YOU would have to do manually, in increasing detail,
get data read file from get line of data get second word from line
once u have the work, its then fairly easy to ask google how would I do x
I search ALL the time… i cannot remember it all…
-
also, from a development environment, if u have a pi, get winscp or bitvise ssh clients, each will give you a file manager window over the pi files from your pc, so you can doubleclick edit, using your favorite pc editor (I use notepad++)
-
@sdetweil so I would probably be using fs. I’m trying to edit a file on the in the /sys/class. so with research, it would probably be:
fs.writeFile(/sys/class/___.txt, '18', function (err) { if (err) console.log(err); });)
this was taken from a website so I assume its right. I’ve been using vnc viewer to do everything from the pi but I will look into those other file managers. Thanks so much
-
@aareben problem is files in /sys, /etc, /opt
are all protected. so you would have to be in root user authority, which is not really possible inside mm.if your userid was in the sudoers group, you could exec sudo node scriptname.js, outside/under mm to run the script without requiring a prompt
-
@sdetweil that makes sense, my module is just a way to control the brightness of the official ras pi display. all the ones out there do it by sunrise and sunset and I want to be able to do it from on-screen buttons. I was planning on using this, https://github.com/tosti007/MMM-TouchNavigation for the buttons and just using notifications to control the module but this seems to be getting complicated. I just want to be able to edit the number within /sys/class/backlight/rpi_backlight/brightness. I was using the info in here https://www.raspberrypi.org/forums/viewtopic.php?t=214086 to figure it out. Do you have any suggestions for how to do it
-
@aareben try use sudo echo to push the number into the file.
so, to do the sudo echo is the same work as doing sudo node scriptname.
you would use the process.spawn library call
user has to be in the sudoers file or sudo group to have this work without prompt
-
@sdetweil how would i get sudo echo into my MMM-Brightness.js? this Is my module rn. Ik its probably very wrong but i don’t actually know javascript so…
Module.register("MMM-Brightness", { defaults: { startingBrightness: 130, jump: 10, path_to_backlight: '/sys/class/backlight/rpi_backlight/brightness', }, start: function () { self = this; var brightness = this.config.startingBrightness; }, notificationReceived: function() { if (notification === 'BRIGHTNESS_UP') { var brightness = brightness-this.config.jump;} if (notification === 'BRIGHTNESS_DOWN') { var brightness = brightness+this.config.jump;} }, loaded: function(callback) { this.finishLoading(); Log.log(this.name + ' is loaded!'); callback(); } })
-
@aareben again u can’t do it from the modulename.js as this runs inside the browser, you must use the node_helper to execute commands, call databases, access pins on pi board…
the browsers restrict all these things for security against hackers reading files, running programs to steal your info, etc
-
@aareben if u haven’t made any progress, see my sample module, which provides a node_helper and uses socket notifications for communications with the modulename.js