Read the statement by Michael Teeuw here.
Run JavaScript if a file is created
-
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 } }
-
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.
-
Not necessarily:
MM² module --> node_helper --> php --> node_helper --> MM² module
-
@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
-
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.
-
@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> } }
-
-
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 anode_helper
which does the background tasksYou could still use PHP to do the file checking for you, but to me that’s a bit of an unnecessary overhead.
-
@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.
-
@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?>