.txt file include



  • Hello, i have a .txt file,
    this file I want to embed to the mirror. So the
    Content is displayed on the mirror.
    In addition , the file should be up-dated after a time= x.

    can someone help me?

    Thanks



  • @dominic this should give you an idea how to solve your problem, I just wrote it down maybe you have to adjust something a little bit and it’s not a finished solution

    • nodehelper:
    const fs = require('fs');
    ...
    
    socketNotificationReceived: function(notification, payload) {
        if(notification === 'START'){
            this.config = payload;
            this.readData();
            setInterval(() => {
                this.readData();
            }, this.config.updateInterval);
        }
    },
    
    readData: function(){
        //to read a file to do the following
        fs.readFile('YOUR FILE PATH', (err, data) => {
            if (err) throw err;
            this.sendSocketNotification('DATA', data);
        });
    }
    
    • module:
    defaults: {
        updateInterval: 30*60*1000 //reads the file every 30 mins
    },
    
    start: function(){
        this.sendSocketNotification('START', this.config);
    },
    
    socketNotificationReceived: function(notification, payload) {
        if(notification === 'DATA'){
            this.dataFile = payload;
            this.updateDom();
        }
    },
    
    getDom: function(){
        var wrapper = document.createElement('div');
        if(this.dataFile){
            wrapper.innerHTML = this.dataFile;
        } else {
            wrapper.innerHTML = 'No data';
        }
        return wrapper;
    }
    


  • @strawberry-3.141 Hi thank you.
    Sorry for the questions, i´m new in .js.

    Where do I insert the nodehelper und the module and and what I need to modify?
    I have insert the nodehelper code in the file: MagicMirror/modules/node_modules/node_helper/index.js.
    And the module code in /MagicMirror/modules/sensor/sensor.js.

    on the mirror is displayed : Sensor module_2_sensor…



  • there is a general design your module have to look like

    you have to create a directory in ~/MagicMirror/modules/YOUR_MODULE_NAME

    then create a file YOUR_MODULE_NAME.js

    Module.register("YOUR_MODULE_NAME",{
        //here comes the rest of the code for module I posted above
    });
    

    then create a file node_helper.js

    const NodeHelper = require("node_helper");
    const fs= require("fs");
    
    module.exports = NodeHelper.create({
        //here comes the part of the nodehelper after the 3 dots as posted above
    });
    


  • @strawberry-3.141 Thanks for the fast answer :)

    I did it , just as you said it. on the mirror is displayed : NO DATA

    Here is the code from the nodehelper:
    Is the path specified correctly or I need to change something?

    • readData: function(){
      //to read a file to do the following
      fs.readFile(’/home/pi/MagicMirror/Test-temp.txt’, (err, data) => {
      if (err) throw err;
      this.sendSocketNotification(‘DATA’, data);
      });


  • i think this should do the job

    fs.readFile('Test-temp.txt', (err, data) => {
    


  • @strawberry-3.141
    hi, now i get " [object ArryBuffer]"
    what did i worng?



  • sry wasn’t aware of that a buffer gets returned instead of a string if there is no encoding specified

    fs.readFile('Test-temp.txt', 'utf8', (err, data) => {
    


  • @strawberry-3.141 said in .txt file include:

    fs.readFile(‘Test-temp.txt’, ‘utf8’, (err, data) => {

    Hi, sorry.
    It didn´t work :/

    Here is the node_helper.js code

    const NodeHelper = require("node_helper");
    const fs= require("fs");
    
    module.exports = NodeHelper.create({
        //here comes the part of the nodehelper after the 3 dots as posted above
    	
    	socketNotificationReceived: function(notification, payload) {
        if(notification === 'START'){
            this.config = payload;
            this.readData();
            setInterval(() => {
                this.readData();
            }, this.config.updateInterval);
        }
    },
    
    readData: function(){
        //to read a file to do the following
       fs.readFile('Test-temp.txt', 'utf8', (err, data) => {
            if (err) throw err;
            this.sendSocketNotification('DATA', data);
        });
    }
    });
    
    
    
    and here is the sensor.js code: 
    
    Module.register("sensor",{
       defaults: {
        updateInterval: 30*60*1000 //reads the file every 30 mins
    },
    
    start: function(){
        this.sendSocketNotification('START', this.config);
    },
    
    socketNotificationReceived: function(notification, payload) {
        if(notification === 'DATA'){
            this.dataFile = payload;
            this.updateDom();
        }
    },
    
    getDom: function(){
        var wrapper = document.createElement('div');
        if(this.dataFile){
            wrapper.innerHTML = this.dataFile;
        } else {
            wrapper.innerHTML = 'No data';
        }
        return wrapper;
    }
    });
    

    Waht did i wrong? :D
    I hope you can help me.


    Note from admin: Please use Markdown on code snippets for easier reading!



  • this works for me you had everywhere weird quotes and the updateinterval was also wrong

    Module.register("sensor",{
    	defaults: {
    		updateInterval: 30 * 60 * 1000 //reads the file every 30 mins
    	},
    
    	start: function(){
    		this.sendSocketNotification("START", this.config);
    	},
    
    	socketNotificationReceived: function(notification, payload) {
    		if(notification === "DATA"){
    			this.dataFile = payload;
    			this.updateDom();
    		}
    	},
    
    	getDom: function(){
    		var wrapper = document.createElement("div");
    		if(this.dataFile){
    			wrapper.innerHTML = this.dataFile;
    		} else {
    			wrapper.innerHTML = "No data";
    		}
    		return wrapper;
    	}
    });
    
    const NodeHelper = require("node_helper");
    const fs= require("fs");
    
    module.exports = NodeHelper.create({
    //here comes the part of the nodehelper after the 3 dots as posted above
    
    	socketNotificationReceived: function(notification, payload) {
    		if(notification === "START"){
    			this.config = payload;
    			this.readData();
        			setInterval(() => {
            			this.readData();
        			}, this.config.updateInterval);
    		}
    	},
    
    	readData: function(){
    		//to read a file to do the following
    		fs.readFile("Test-temp.txt", "utf8", (err, data) => {
    			if (err) throw err;
    			this.sendSocketNotification("DATA", data);
    		});
    	}
    });
    

Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.