• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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 12.2k 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.
  • O Offline
    ooom416354 @KirAsh4
    last edited by Jul 28, 2016, 11:54 AM

    @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
    • K Offline
      KirAsh4 Moderator
      last edited by Jul 28, 2016, 4:07 PM

      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?

      O 1 Reply Last reply Jul 28, 2016, 4:29 PM Reply Quote 0
      • O Offline
        ooom416354 @KirAsh4
        last edited by Jul 28, 2016, 4:29 PM

        @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
        • K Offline
          KirAsh4 Moderator
          last edited by Jul 28, 2016, 4:48 PM

          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?

          O 1 Reply Last reply Jul 28, 2016, 8:11 PM Reply Quote 0
          • E Offline
            EoF
            last edited by Jul 28, 2016, 5:43 PM

            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
            • K Offline
              KirAsh4 Moderator
              last edited by Jul 28, 2016, 6:30 PM

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

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

              E 1 Reply Last reply Jul 29, 2016, 1:56 PM Reply Quote 0
              • O Offline
                ooom416354 @KirAsh4
                last edited by Jul 28, 2016, 8:11 PM

                @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
                • K Offline
                  KirAsh4 Moderator
                  last edited by Jul 28, 2016, 8:14 PM

                  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?

                  O 2 Replies Last reply Jul 28, 2016, 8:32 PM Reply Quote 0
                  • O Offline
                    ooom416354 @KirAsh4
                    last edited by Jul 28, 2016, 8:32 PM

                    @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
                    • O Offline
                      ooom416354 @KirAsh4
                      last edited by Jul 28, 2016, 8:43 PM

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

                      index.php

                      1 Reply Last reply Reply Quote 0
                      • 1
                      • 2
                      • 3
                      • 2 / 3
                      2 / 3
                      • First post
                        11/25
                        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