Read the statement by Michael Teeuw here.
Run JavaScript if a file is created
-
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:
- checks for the existence of that file
- check s to see if the file is newer than the last time it checked
- if the above is true, read the file and parse the JSON and spit it out to the browser
- 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.
-
@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!
-
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.
-
@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!
-
Ok, so if I’m understanding you correctly, your setup is:
- 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
- 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?
-
@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.
-
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