-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py.save
97 lines (73 loc) · 2.2 KB
/
server.py.save
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
pi@raspberrypi:~/camWebServer $ /home/pi/camWebServer/robotfrom flask import Flask, flash, render_template, request, redirect, Response, session, abort
import random, json
import sys
import socket
import signal
sys.path.insert(1, '/home/pi/camWebServer/robot')
import robot
app = Flask(__name__)
app.secret_key = 'X;\xce\xcc\xde\x8f.\x117\x16tO\xfd\x98\n<'
# The local IP and Port of the socket server on the bot
HOST = "192.168.0.55"
PORT = 9999
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connected = False
# def connect():
# global connected
# try:
# sock.connect((HOST, PORT))
# print("Connection with bot established.")
# connected = True
# except Exception as inst:
# print(inst)
# print("Failed to connect to bot.")
# connected = False
# def relay(msg):
# global connected
# if connected:
# try:
# print("Command sent")
# sock.send(msg.encode())
# except Exception:
# print("Failed to send command")
# connected = False
@app.route("/")
def index():
if not session.get('logged_in'):
# Return the log in page
return render_template('login.html')
else:
# Return our main page
return render_template('index.html')
@app.route("/login", methods = ['POST', 'GET'])
def do_login():
if request.method == 'POST':
if request.form['password'] == 'password' and request.form['username'] == 'admin':
session['logged_in'] = True
else:
flash(u'Invalid password or username', 'error')
return index()
return render_template('login.html')
@app.route("/move", methods = ['POST'])
def recieve():
# read the JSON data of the direction and speed issued
data = request.get_json()
direction = str(data['Data']['direction'])
speed = str(data['Data']['speed'])
sendCmd(direction, speed)
return "Robot Moved"
@app.route("/mode", methods = ['POST'])
def rec():
# read the JSON data of the new mode
data = request.get_json()
mode = str(data['Data']['mode'])
changeMode(mode)
return "Mode changed"
# Return the status of the connection with the bot
@app.route("/getStatus", methods = ['GET'])
def returnStatus():
currMode = json.dumps({"status": str(True)})
return currMode
if __name__ == "__main__":
connected = connect()
app.run("0.0.0.0", "8000")