2013-11-24 15:00:46 +00:00
|
|
|
import os,logging,json
|
2013-11-23 21:25:20 +00:00
|
|
|
|
|
|
|
import bottle
|
|
|
|
from gevent.pywsgi import WSGIServer
|
|
|
|
from geventwebsocket import WebSocketHandler, WebSocketError
|
|
|
|
|
2013-11-23 23:21:26 +00:00
|
|
|
from oven import Oven
|
2013-11-24 15:00:46 +00:00
|
|
|
from ovenWatcher import OvenWatcher
|
2013-11-23 23:21:26 +00:00
|
|
|
|
|
|
|
log_format = '%(asctime)s %(levelname)s %(name)s: %(message)s'
|
|
|
|
logging.basicConfig(level = logging.DEBUG, format = log_format)
|
|
|
|
log = logging.getLogger("picoreflowd")
|
|
|
|
|
2013-11-23 21:25:20 +00:00
|
|
|
app = bottle.Bottle()
|
|
|
|
oven = Oven()
|
|
|
|
ovenWatcher = OvenWatcher(oven)
|
2013-11-24 15:00:46 +00:00
|
|
|
wsocks_control = []
|
2013-11-23 21:25:20 +00:00
|
|
|
|
2013-11-23 22:26:06 +00:00
|
|
|
@app.route('/')
|
|
|
|
def index():
|
|
|
|
return bottle.redirect('/picoreflow/index.html')
|
|
|
|
|
|
|
|
@app.route('/picoreflow/:filename#.*#')
|
|
|
|
def send_static(filename):
|
2013-11-23 23:21:26 +00:00
|
|
|
log.debug("serving %s"%filename)
|
2013-11-23 22:26:06 +00:00
|
|
|
return bottle.static_file(filename, root='./public/')
|
|
|
|
|
2013-11-23 21:25:20 +00:00
|
|
|
@app.route('/run')
|
|
|
|
def start_oven():
|
|
|
|
oven.run_profile("abc")
|
2013-11-23 22:10:51 +00:00
|
|
|
|
2013-11-23 21:25:20 +00:00
|
|
|
return "Starting"
|
|
|
|
|
2013-11-24 15:07:59 +00:00
|
|
|
def get_websocket_from_request():
|
2013-11-23 21:25:20 +00:00
|
|
|
env = bottle.request.environ;
|
|
|
|
wsock = env.get('wsgi.websocket')
|
|
|
|
if not wsock:
|
|
|
|
abort(400, 'Expected WebSocket request.')
|
2013-11-24 15:12:45 +00:00
|
|
|
return wsock
|
2013-11-23 22:10:51 +00:00
|
|
|
|
2013-11-24 15:07:59 +00:00
|
|
|
@app.route('/control')
|
|
|
|
def handle_control():
|
|
|
|
wsock = get_websocket_from_request()
|
2013-11-23 21:25:20 +00:00
|
|
|
wsocks_control.append(wsock)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
message = wsock.receive()
|
|
|
|
wsock.send("Your message was: %r" % message)
|
|
|
|
if message == "start":
|
2013-11-23 23:21:26 +00:00
|
|
|
log.info("Start command received")
|
2013-11-23 21:25:20 +00:00
|
|
|
oven.run_profile("abc")
|
|
|
|
elif message == "stop":
|
2013-11-23 23:21:26 +00:00
|
|
|
log.info("Stop command received")
|
2013-11-23 21:25:20 +00:00
|
|
|
oven.abort_run()
|
|
|
|
except WebSocketError:
|
|
|
|
break
|
2013-11-23 22:10:51 +00:00
|
|
|
|
2013-11-24 14:50:07 +00:00
|
|
|
@app.route('/storage')
|
|
|
|
def handle_storage():
|
2013-11-24 15:07:59 +00:00
|
|
|
wsock = get_websocket_from_request()
|
2013-11-24 14:50:07 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
message = wsock.receive()
|
|
|
|
if message == "GET":
|
|
|
|
log.info("GET command recived")
|
2013-11-24 15:07:59 +00:00
|
|
|
wsock.send(get_profiles())
|
2013-11-24 14:50:07 +00:00
|
|
|
elif message == "PUT":
|
|
|
|
log.info("PUT command received")
|
|
|
|
except WebSocketError:
|
|
|
|
break
|
|
|
|
|
2013-11-23 21:25:20 +00:00
|
|
|
@app.route('/status')
|
2013-11-24 15:07:59 +00:00
|
|
|
def handle_status():
|
|
|
|
wsock = get_websocket_from_request()
|
2013-11-24 15:00:46 +00:00
|
|
|
ovenWatcher.addObserver(wsock)
|
2013-11-23 21:25:20 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
message = wsock.receive()
|
|
|
|
wsock.send("Your message was: %r" % message)
|
|
|
|
except WebSocketError:
|
|
|
|
break
|
|
|
|
|
2013-11-24 15:07:59 +00:00
|
|
|
def get_profiles():
|
2013-11-24 14:50:07 +00:00
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
path = os.path.join(script_dir,"storage","profiles")
|
|
|
|
print path
|
|
|
|
try :
|
|
|
|
profile_files = os.listdir(path)
|
|
|
|
except :
|
|
|
|
profile_files = []
|
|
|
|
profiles = []
|
|
|
|
for filename in profile_files:
|
|
|
|
with open(os.path.join(path,filename), 'r') as f:
|
|
|
|
profiles.append(json.load(f))
|
|
|
|
return json.dumps(profiles)
|
|
|
|
|
2013-11-23 21:25:20 +00:00
|
|
|
def main():
|
2013-11-24 14:20:38 +00:00
|
|
|
log.info("Starting picoreflowd")
|
2013-11-23 23:21:26 +00:00
|
|
|
ip = "0.0.0.0"
|
|
|
|
port = 8080
|
|
|
|
log.info("listening to %s:%d"%(ip,port))
|
2013-11-24 14:20:38 +00:00
|
|
|
|
2013-11-23 23:21:26 +00:00
|
|
|
server = WSGIServer((ip,port), app,
|
2013-11-23 21:25:20 +00:00
|
|
|
handler_class=WebSocketHandler)
|
|
|
|
server.serve_forever()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|