Skip to content

Commit

Permalink
Start of basic robot-controlling http server
Browse files Browse the repository at this point in the history
  • Loading branch information
Oooska committed Jun 13, 2015
1 parent 82e7d9b commit 5493f58
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 0 deletions.
20 changes: 20 additions & 0 deletions drive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import errors
import web

class drive:
def GET(self):
print "VROOOOM!"
raise errors.NotSupportedError
return "This should return a 405 - Not Supported Error"

#POST excepts a single command in plain teext.
#Current supported commands: forward', 'reverse', 'left', or 'right'
def POST(self):
print ("web.data: %s" % web.data()) #'forward', 'reverse', 'left', or 'right'
direction = web.data()
if direction not in ['forward', 'reverse', 'left', 'right']:
raise errors.BadRequestError

#Set robot direction here...

return "Updated direction"
17 changes: 17 additions & 0 deletions errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import web

#405 error when incorrect action is used on a resource
class NotSupportedError(web.HTTPError):
def __init__(self):
status = '405 Method Not Alloweds'
headers = {'Content-Type': 'text/html'}
data = 'That method is not valid for this action.'
web.HTTPError.__init__(self, status, headers, data)

#400 error when invalid data is sent to a resource
class BadRequestError(web.HTTPError):
def __init__(self):
status = '400'
headers = {'Content-Type': 'text/html'}
data = 'Invalid data supplied in request.'
web.HTTPError.__init__(self, status, headers, data)
34 changes: 34 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import web
from drive import drive

urls = (
'/', 'index',
'/drive', 'drive',
)
app = web.application(urls, globals())

#index raises a 301 error pointing to the index.html page
class index:
def GET(self):
raise web.seeother('/static/index.html')

#405 error when incorrect action is used on a resource
class NotSupportedError(web.HTTPError):
def __init__(self):
status = '405 Method Not Alloweds'
headers = {'Content-Type': 'text/html'}
data = 'That method is not valid for this action.'
web.HTTPError.__init__(self, status, headers, data)

#400 error when invalid data is sent to a resource
class BadRequestError(web.HTTPError):
def __init__(self):
status = '400'
headers = {'Content-Type': 'text/html'}
data = 'Invalid data supplied in request.'
web.HTTPError.__init__(self, status, headers, data)



if __name__ == "__main__":
app.run()
Binary file added static/forward.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<html>
<head>
<script type='text/javascript'>
anchorclick = function(dir){
console.log("Anchor click");
postMoveDirection(dir)
return false;
}

postMoveDirection = function(dir){
console.log('Derp')
var request = new XMLHttpRequest();
request.open('POST', '/drive');
request.setRequestHeader('Content-Type', 'text');

request.onload = function(){
if(this.status >= 200 && this.status < 400){
resp = this.response;
console.log("Sent POST request, got back ", resp);
} else {

console.log("Error sending POST request: ", this.response);
}

}
request.send(dir)
return false //Cancels event
}
</script>
</head>

<body>
<h1 align=center >Project Battleaxe Controls</h1>

<br>

<table>
<tr>
<td></td>
<td><a href="#" onclick="postMoveDirection('forward')"><img src='/static/forward.png'/></a></td>
<td></td>
</tr>
<tr>
<td><a href="#" onclick="postMoveDirection('left')"><img src='/static/left.png'/></a></td>
<td align="center"><font size="18"><a href="#" onclick="postMoveDirection('notvalidyet')"><b>A</b></a></font></td>
<td><a href="#" onclick="postMoveDirection('right')"><img src='/static/right.png'/></a></td>
</tr>
<tr>
<td></td>
<td><a href="#" onclick="postMoveDirection('reverse')"><img src='/static/reverse.png'/></a></td>
<td></td>
</tr>
</table>

</body>
</html>
Binary file added static/left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/ocr_pi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/reverse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5493f58

Please sign in to comment.