Commit 7c9247e1 authored by Carl Hetherington's avatar Carl Hetherington

Initial.

parents
import SimpleHTTPServer
import SocketServer
import socket
import os
import sys
from ev3.lego import MediumMotor
if len(sys.argv) != 2:
print>>sys.stderr,'Syntax: %s <port>' % (sys.argv[0])
sys.exit(1)
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
parts = self.path[1:].split('/')
print parts
if len(parts) == 4 and parts[0] == 'motor':
print 'Run %s to %s at speed %s' % (parts[1], parts[2], parts[3])
d = MediumMotor(parts[1])
d.run_position_limited(int(parts[2]), int(parts[3]), absolute=False)
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write('OK')
self.wfile.close()
class TCPServer(SocketServer.TCPServer):
def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
httpd = TCPServer(("", int(sys.argv[1])), Handler)
print 'EV3 ready.'
httpd.serve_forever()
import SimpleHTTPServer
import SocketServer
import socket
import os
import urllib2
# Port to listen to Snap on
SNAP_PORT = 1330
# Port for EV3 to listen on
EV3_PORT = 8192
# EV3 user ID and IP address
EV3_CONNECTION = 'root@10.42.0.51'
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def send_head(self):
if self.path == '/snap-ev3':
f = open('snap-ev3.xml', 'rb')
self.send_response(200)
self.send_header("Content-type", 'text/xml')
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
return f
else:
SimpleHTTPServer.SimpleHTTPRequestHandler.send_head(self)
urllib2.urlopen("http://10.42.0.51:%d%s" % (EV3_PORT, self.path), timeout=5).read()
class TCPServer(SocketServer.TCPServer):
def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
print "Starting listener on EV3"
os.system('scp listen.py %s:' % EV3_CONNECTION)
os.system('ssh %s -- nohup python listen.py %d &' % (EV3_CONNECTION, EV3_PORT))
httpd = TCPServer(("", SNAP_PORT), Handler)
print "http://snap.berkeley.edu/snapsource/snap.html#open:http://localhost:1330/snap-ev3"
httpd.serve_forever()
<blocks app="Snap! 4.0, http://snap.berkeley.edu" version="1">
<block-definition category="control" s="move motor %&apos;motor&apos; %&apos;degrees&apos; degrees at %&apos;speed&apos; speed" type="command">
<inputs>
<input type="%txt">a</input>
<input type="%n">360</input>
<input type="%n">100</input>
</inputs>
<script>
<block s="doRun">
<block s="reifyReporter">
<autolambda>
<block s="reportURL">
<block s="reportJoinWords">
<list>
<l>localhost:1330/motor/</l>
<block var="motor"/>
<l>/</l>
<block var="degrees"/>
<l>/</l>
<block var="speed"/>
</list>
</block>
</block>
</autolambda>
</block>
</block>
</script>
</block-definition>
</blocks>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment