|
| 1 | +from flask import Flask,Response |
| 2 | +from pimux import function |
| 3 | +from pimux import Sensors |
| 4 | +import subprocess |
| 5 | + |
| 6 | +app=Flask(__name__) |
| 7 | +f=function.misc() |
| 8 | +s=Sensors.sensor() |
| 9 | + |
| 10 | +@app.route('/') |
| 11 | +def welcome(): |
| 12 | + return ''' |
| 13 | + <strong> Welcome to IOT</strong> |
| 14 | + <hr> |
| 15 | + Enter the url methods on your browser: |
| 16 | + <ul> |
| 17 | + <li>/vibrate</li> |
| 18 | + <li>/call/{phone number}</li> |
| 19 | +<li>/torch/{on/off}</li> |
| 20 | +<li>/brightness/{0-255}</li> |
| 21 | +<li>/listsensors</li> |
| 22 | +<li>/battery</li> |
| 23 | + </ul> |
| 24 | + ''' |
| 25 | +@app.route('/battery') |
| 26 | +def battery(): |
| 27 | + out=f.battery() |
| 28 | + return f''' |
| 29 | + <strong>Battery status</strong> |
| 30 | + <br> |
| 31 | + {out} |
| 32 | + ''' |
| 33 | + |
| 34 | +@app.route('/vibrate') |
| 35 | +def vibrate(): |
| 36 | + out=f.vibrate() |
| 37 | + return "Vibrating your phone" |
| 38 | + |
| 39 | +@app.route('/call/<int:pnum>') |
| 40 | +def call(pnum): |
| 41 | + out=f.telephonycall(pnum) |
| 42 | + return f"Making call to {pnum} for you" |
| 43 | + |
| 44 | + |
| 45 | +@app.route('/torch/<string:state>') |
| 46 | +def torch(state): |
| 47 | + if state=="on": |
| 48 | + out=f.torch(True) |
| 49 | + return "The torch on your phone is turned ON" |
| 50 | + elif state=="off": |
| 51 | + out=f.torch(False) |
| 52 | + return "The torch on your phone is turned OFF" |
| 53 | + |
| 54 | + |
| 55 | +@app.route('/listsensors') |
| 56 | +def listS(): |
| 57 | + out=s.listSensor() |
| 58 | + return f''' |
| 59 | + The sensors available on your device are: |
| 60 | + {out} |
| 61 | + ''' |
| 62 | +@app.route('/brightness/<int:bval>') |
| 63 | +def bright(bval): |
| 64 | + out=f.brightness(bval) |
| 65 | + return f''' |
| 66 | + Setting your device brightness to {bval} |
| 67 | + <hr> |
| 68 | + DONE |
| 69 | + ''' |
| 70 | + |
| 71 | +''' |
| 72 | +#TODO: |
| 73 | +
|
| 74 | +from time import sleep |
| 75 | +
|
| 76 | +@app.route('/sensor/<string:sName>') |
| 77 | +def sensordata(sName): |
| 78 | + def display_sensor(sensorN): |
| 79 | + proc = subprocess.Popen( |
| 80 | + s.specificSensors(sensorN), |
| 81 | + shell=True, |
| 82 | + stdout=subprocess.PIPE |
| 83 | + ) |
| 84 | +
|
| 85 | + for line in iter(proc.stdout.readline): |
| 86 | + sleep(1) |
| 87 | + yield line.rstrip() |
| 88 | +
|
| 89 | + return Response(s.specificSensors(sName),mimetype='text/html') |
| 90 | +
|
| 91 | +''' |
| 92 | + |
| 93 | + |
| 94 | +if __name__=="__main__": |
| 95 | + app.run(debug=False) |
0 commit comments