Skip to content

Instantly share code, notes, and snippets.

@erogol
Last active May 25, 2017 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erogol/d47d67c34a8f2dfb0b61dfa89e6cc895 to your computer and use it in GitHub Desktop.
Save erogol/d47d67c34a8f2dfb0b61dfa89e6cc895 to your computer and use it in GitHub Desktop.
import subprocess
from flask import Flask, render_template
app = Flask(__name__)
# keep runnign process global
proc = None
@app.route("/")
def hello():
# return the web interface for raspi
return render_template("index.html")
@app.route("/start", methods=['GET', 'POST'])
def start_talkingraspi():
# start watching code on a different process
global proc
print(" > Start talkingraspi!")
proc = subprocess.Popen(["python", "pi_surveillance.py", "-c", "conf.json"])
print(" > Process id {}".format(proc.pid))
return "Started!"
@app.route("/stop", methods=['GET', 'POST'])
def stop_talkingraspi():
# stop the watching code process
global proc
print(" > Stop talkingraspi!")
# subprocess.call(["kill", "-9", "%d" % proc.pid])
proc.kill()
print(" > Process {} killed!".format(proc.pid))
return "Stopped!"
@app.route("/status", methods=['GET', 'POST'])
def status_talkingraspi():
global proc
if proc is None:
print(" > Talkingraspi is resting")
return "Resting!"
if proc.poll() is None:
print(" > Talking raspi is running (Process {})!".format(proc.pid))
return "Running!"
else:
print(" > Talkingraspi is resting")
return "Stopped!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5555, debug=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment