Schnitte321
Schnitte321

Reputation: 1

Parse local variable stored in file into javascript

I have a Flask website running. I would like to have a iOS kinda switch to turn my amplifier on and off. I have created the switch as a <div> and I am able to switch the <div> to on with jQuery toggleClass.

Everytime I toggle the switch a shell script runs and turn either on or off the amplifier. Also it stores a "1" (for ON) or "0" (for OFF) in a local file by running a shell script through appRoute in Flask. I hoped it would be easy to implement some javascript reading the state from the local file to toggle the switch - when visiting the website. However I am clueless how to make this if state - getting the parameter from the local file and update it each time I visit the website.

Anyone with good ideas for a solution to this?

Upvotes: 0

Views: 69

Answers (1)

Twisty
Twisty

Reputation: 30893

So I suspect you have something like this:

+-----+        +-------------+        +---------+
| AMP |--------| RPI         |        | iOS     | 
+-----+        | 192.168.1.2 |  ) ) ) | Browser |
               +-------------+        +---------+

I suggest that on your Rasberry Pi, you have a local file, lets say /var/www/html/amp-status.json. Your path might be different depending on your Web Server, but it should be somewhere in your web server path. You may also have a Python script populates this file with the status of the Amp. I suggest the following:

{ "status": 1 }

Or:

{ "status": 0 }

Now, on your iOS Device, you can navigate to 192.168.1.2/amp-status.json to check the status of the Amp. Also you can use jQuery in a webpage to do this via AJAX.

$.getJSON("amp-status.json", function(r){
  $("#amp-status"),val(r.status);
});

This is a simple way to read the status from a Web Server. JSON is a very good way to pass data from server to client as it can be easily interpreted by JavaScrip/jQuery. You can use Text, HTML, or XML if you prefer.

Update - Flask Example

# Get Amp Status via Python, store in 'a'
# Import proper library
from flask import jsonify

@app.route('/_get_amp_status')
def get_amp_status():
    return jsonify(status=a)

See More:

Upvotes: 1

Related Questions