MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Run JavaScript if a file is created

    Scheduled Pinned Locked Moved General Discussion
    25 Posts 3 Posters 15.1k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ooom416354O Offline
      ooom416354
      last edited by

      Thanks for your reply, I’m more of a database developer so I’m really not sure how to do certain things using PHP or JavaScript. So I have a PHP page which I load and in that page it calls references some JS files. Would I add the file_exists check in the php page directly, then if it does exist, call the new JS file which will contain the info I want displayed if the file exists? Again sorry for these questions, I’m trying to learn as I go.

      1 Reply Last reply Reply Quote 0
      • KirAsh4K Offline
        KirAsh4 Moderator
        last edited by

        I’d need more information to be able to give you a better answer. Not sure why you’re using JS to store data. Traditionally when data is being transmitted from one device to another, or even stored, one does that in something designed for it, with keys and values, just like a database does. I would store stuff in JSON files. Then you can write a very simple routine in PHP that:

        1. checks for the existence of that file
        2. check s to see if the file is newer than the last time it checked
        3. if the above is true, read the file and parse the JSON and spit it out to the browser
        4. if the time stamp on the file hasn’t changed, either do nothing, or display something else

        One could assume if the last read found a valid file, and the next read it’s the same file, just display the same information. Unless you don’t want to display stale information, then display something else.

        A Life? Cool! Where can I download one of those from?

        ooom416354O 1 Reply Last reply Reply Quote 0
        • ooom416354O Offline
          ooom416354 @KirAsh4
          last edited by

          @KirAsh4 I’m actually not storing any data into the file, it’s more like an “OK” file. What I’m trying to do is output a file to the Pi via Tasker when I hit a specific location in my commute home (I have this part working). From there I want to call a function that I have already written in JavaScript which looks up the train status from the MTA (NYC’s transit system). The idea is to run this function only when the file is output so that my wife can see which train I’m on and what the ETA will be. Currently I have the function running whenever there are delays but the problem is that it lists every train and not the specific one I am on. I’d like to have it so it just appears once the text file is output. Thanks again for your help!

          1 Reply Last reply Reply Quote 0
          • KirAsh4K Offline
            KirAsh4 Moderator
            last edited by

            What specifically is the ‘OK’ file’s contents? If nothing, and all the script is doing is ‘does file xyz exist’, then how is your wife to know what train you’re on?

            Personal opinion: what you are trying to do is so easily done using an IFTTT recipe: ‘IF THEN ’ Or do a number of other things.

            I’m a huge proponent of using the right tools for the task. While I can certainly see the desire to do it the way you are wanting to, it relies on her needing to watch the mirror, whereas something like a simple text she can receive anywhere.

            A Life? Cool! Where can I download one of those from?

            ooom416354O 1 Reply Last reply Reply Quote 0
            • ooom416354O Offline
              ooom416354 @KirAsh4
              last edited by

              @KirAsh4 the file wouldn’t have anything in it, it simply would trigger the mirror to display the current train status. The only reason I haven’t just sent a text directly from my phone when I hit a location is because there is no way for me to send the actual trains ETA (and if you’ve rode the MTA before it is always late). I was thinking I could display the results of my mta JS module (which does a JSON pull to their api getting the trains ETA) on the screen only when the file becomes available, thus having to avoid her looking at the mirror showing every trains ETA and only displaying my train. If you have any other suggestions I’d definitely like to try them out; like I said I’m doing this more to learn as well. Thanks!

              1 Reply Last reply Reply Quote 0
              • KirAsh4K Offline
                KirAsh4 Moderator
                last edited by

                Ok, so if I’m understanding you correctly, your setup is:

                1. you have a JS script that, which given a trigger, will reach out to the MTA’s API and pull in the train’s ETA
                2. your phone, through Tasker, will send a notification (in the form of a file creation) to the rpi (which is exposed to the internet?)

                What you want is, once that file is created, a message gets displayed on the mirror with the train’s ETA.

                What am I missing?

                A Life? Cool! Where can I download one of those from?

                ooom416354O 1 Reply Last reply Reply Quote 0
                • ooom416354O Offline
                  ooom416354 @KirAsh4
                  last edited by

                  @KirAsh4 Yup! That’s exactly what the setup is. The Pi is exposed to the internet via SSH on port 22, I changed the password to something pretty obscure but may look into additional security.

                  Right now the JS script is running every 5 minutes and presenting the ETA for the train that is currently running closest to the actual time of the day. Problem here is that information clearly isn’t important because I’m at work and not in transit. I was trying to make it more robust in that I am able to use Tasker to ping cell towers and once I am in a specific location, output the file to the Pi to then trigger the script. Of course, if there is a better way I’d absolutely look into that, it was just my first thought on how to accomplish it.

                  1 Reply Last reply Reply Quote 0
                  • KirAsh4K Offline
                    KirAsh4 Moderator
                    last edited by

                    Have the script check for the date stamp on the file. Have Tasker just 'touch' the existing file. The script needs to store the date stamp when it checks the file. Something like this:

                    // pseudo code, it won't brew coffee
                    var prev_datestamp = 0;
                    var expire_time = 15 * 60 * 1000; // 15 minutes
                    
                    // run every five minutes
                    if (file exists) {
                      if (file.datestamp > previous_datestamp) {
                        // train is on the way, quick, hide the money, hide the poker table
                        // oh, refresh the mirror too
                        previous_datestamp = file.datestamp;
                      } else if (now > file.datestamp + expire_time) {
                        // it's been 15 minutes, be afraid, be very afraid
                        // clear mirror message
                      }
                    }
                    

                    A Life? Cool! Where can I download one of those from?

                    ooom416354O 1 Reply Last reply Reply Quote 0
                    • EoFE Offline
                      EoF
                      last edited by

                      Might be wise to incorporate some AJAX into this, no? Having PHP check the file, etc and then pass the results / message back to JS.

                      1 Reply Last reply Reply Quote 0
                      • KirAsh4K Offline
                        KirAsh4 Moderator
                        last edited by

                        Not necessarily:
                        MM² module --> node_helper --> php --> node_helper --> MM² module

                        A Life? Cool! Where can I download one of those from?

                        EoFE 1 Reply Last reply Reply Quote 0
                        • ooom416354O Offline
                          ooom416354 @KirAsh4
                          last edited by

                          @KirAsh4 This is awesome!

                          I would just put this in my JS file, right? I’m currently sending the following command through Tasker to the Pi so I think I’m good.

                          touch curr-train.txt

                          1 Reply Last reply Reply Quote 0
                          • KirAsh4K Offline
                            KirAsh4 Moderator
                            last edited by

                            Earlier you mentioned you using PHP, so I kept it in the loop. Use node_helper to call the PHP script. Let the PHP script check for the file and report back to node_helper which in turn reports to the module.

                            A Life? Cool! Where can I download one of those from?

                            ooom416354O 2 Replies Last reply Reply Quote 0
                            • ooom416354O Offline
                              ooom416354 @KirAsh4
                              last edited by

                              @KirAsh4 OK, so my PHP script is actually index.php, it’s effectively the page that loads and is my Mirror. In that file I have a reference to the JS file which runs the mta code.

                              <script src="js/mm-mta.js"></script>

                              So would I put the following in the PHP page? (sorry if I sound dumb here)

                              var prev_datestamp = 0;
                              var expire_time = 15 * 60 * 1000; // 15 minutes
                              
                              // run every five minutes
                              if (file exists) {
                                if (file.datestamp > previous_datestamp) {
                              
                                 <script src="js/mm-mta.js"></script>
                              
                                  previous_datestamp = file.datestamp;
                                } else if (now > file.datestamp + expire_time) {
                              
                                <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
                                <script> 
                                    $("#mta").empty();
                                </script>
                              }
                              }
                              1 Reply Last reply Reply Quote 0
                              • ooom416354O Offline
                                ooom416354 @KirAsh4
                                last edited by

                                @KirAsh4 for reference here’s the full index.php file

                                index.php

                                1 Reply Last reply Reply Quote 0
                                • KirAsh4K Offline
                                  KirAsh4 Moderator
                                  last edited by

                                  Oy, why do it that way … your index.php will never get updated if there’s an upstream change in the code (because we use index.html). You should use the existing infrastructure in MM². Create a new module, have it display in a specific region on the mirror. Every 5 minutes, have the module refresh by doing a call to its node_helper. The node_helper can be a simply python script that checks the specific file in question and reports back to the module.

                                  I thought the PHP script was doing something … it’s not in this case. You don’t need PHP at all for this.

                                  Some places to look for coding ideas:
                                  MM² Module Development Documentation
                                  Look at how modules such as the calendar and newsfeed implement a node_helper which does the background tasks

                                  You could still use PHP to do the file checking for you, but to me that’s a bit of an unnecessary overhead.

                                  A Life? Cool! Where can I download one of those from?

                                  ooom416354O 2 Replies Last reply Reply Quote 0
                                  • ooom416354O Offline
                                    ooom416354 @KirAsh4
                                    last edited by

                                    @KirAsh4 LOL - I’m just lost these days with all of this stuff. I should stick to my DB work.

                                    Let me see what I can do with the module documentation, it would be a good experience anyway to learn.

                                    1 Reply Last reply Reply Quote 0
                                    • ooom416354O Offline
                                      ooom416354 @KirAsh4
                                      last edited by

                                      @KirAsh4 Just out of curiosity, if I were to add the code you provided into the PHP file, would i need to bracket it with <?php then add the code here?>

                                      1 Reply Last reply Reply Quote 0
                                      • KirAsh4K Offline
                                        KirAsh4 Moderator
                                        last edited by

                                        This is hard to answer because I don’t know what your PHP file looks like. I don’t know if you are echo-ing HTML data, or using HEREDOC? I’m flying blind here … (as much fun as that could be)

                                        A Life? Cool! Where can I download one of those from?

                                        ooom416354O 1 Reply Last reply Reply Quote 0
                                        • ooom416354O Offline
                                          ooom416354 @KirAsh4
                                          last edited by

                                          @KirAsh4 I had actually uploaded that file a few posts back. For reference here’s the entire thing (forgive the ugliness)

                                          <head>
                                          	<title>Magic Mirror</title>
                                          <html>
                                          	<style type="text/css">
                                          		<?php include('css/main.css') ?>
                                          		<?php include('css//mbta-icons.css') ?>
                                          	</style>
                                          	<link rel="stylesheet" type="text/css" href="css/weather-icons.css">
                                          	<script type="text/javascript">
                                          		var gitHash = '<?php echo trim(`git rev-parse HEAD`) ?>';
                                          	</script>
                                          	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
                                          	<meta name="google" value="notranslate" />
                                           
                                          </head>
                                          <body>
                                          
                                              <div class="backgroundTint"></div>
                                          
                                          	<div class="top left">
                                          		<div class="date small dimmed"></div>
                                          		<div class="time"></div>
                                          		<div class="timeWarning xxsmall"></div>
                                          		<div class="holidays xxsmall dimmed"></div>
                                          		<div class="calendar xxsmall"></div>
                                          		<div class="mbta xxxsmall dimmed"></div>
                                          	</div>
                                          
                                          	<div class="top right">
                                          	    <div class="windsun small dimmed"></div>
                                          		<div class="temp"></div>
                                          		<div class="tempfeelslike xxsmall"></div>
                                          		<div class="spacer"></div>
                                          		<div class="tempgraph"></div>
                                          		<div class="weekgraph"></div>
                                          		<div class="summary xxsmall2 dimmed"></div>
                                          		<div class="weatheralerts xxsmall2 dimmed"></div>
                                          	</div>
                                          
                                          	<div class="lower-third center-hor">
                                          		<div class="compliment light"></div>
                                          		<div class="weatherCompliment small light"></div>
                                          	</div>
                                          
                                          	<div class="bottom center-hor">
                                          		<div class="newsTitle xsmall xxdimmed"></div>
                                          		<div class="news medium"></div>
                                          	</div>
                                          
                                          	<div class="farbottom center-hor">
                                          		<div class="newsDots xxxsmall"></div>
                                          	</div>
                                          
                                          	<div class="farbottom right">
                                          		<div class="lastupdated luWeather xxxsmall xxdimmed"></div>
                                          		<div class="lastupdated luMBTA xxxsmall xxdimmed"></div>
                                          		<div class="lastupdated luRSS xxxsmall xxdimmed"></div>
                                          		<div class="lastupdated luHolidays xxxsmall xxdimmed"></div>
                                          	</div>
                                          
                                          </div>
                                          
                                          <!-- Third Parties -->
                                          <script src="js/jquery.js"></script>
                                          <script src="js/ical_parser.js"></script>
                                          <script src="js/moment-with-locales.js"></script>
                                          <script src="js/config.js"></script>
                                          <script src="js/rrule.js"></script>
                                          <script src="js/d3.js"></script>
                                          
                                          <!-- Our Stuff -->
                                          <script src="js/main.js?nocache=<?php echo md5(microtime()) ?>"></script>
                                          <script src="js/mm-weather.js?nocache=<?php echo md5(microtime()) ?>"></script>
                                          <script src="js/mm-mbta.js?nocache=<?php echo md5(microtime()) ?>"></script>
                                          <script src="js/mm-mta.js?nocache=<?php echo md5(microtime()) ?>"></script>
                                          
                                          </html>
                                          </body>
                                          1 Reply Last reply Reply Quote 0
                                          • KirAsh4K Offline
                                            KirAsh4 Moderator
                                            last edited by

                                            Then yes, write the proper PHP code and stick it in <?php ... ?> tags.

                                            A Life? Cool! Where can I download one of those from?

                                            ooom416354O 1 Reply Last reply Reply Quote 0

                                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                            With your input, this post could be even better 💗

                                            Register Login
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post
                                            Enjoying MagicMirror? Please consider a donation!
                                            MagicMirror created by Michael Teeuw.
                                            Forum managed by Sam, technical setup by Karsten.
                                            This forum is using NodeBB as its core | Contributors
                                            Contact | Privacy Policy