kilnController/picoreflowd.py

154 wiersze
4.6 KiB
Python
Czysty Zwykły widok Historia

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-29 23:13:06 +00:00
try:
import config
except:
print "Could not import config file."
print "Copy config.pyEXAMPLE to config.py and adapt it for your setup."
exit(1)
logging.basicConfig(level = config.log_level, format = config.log_format)
2013-11-23 23:21:26 +00:00
log = logging.getLogger("picoreflowd")
2013-11-24 15:18:42 +00:00
log.info("Starting picoreflowd")
2013-11-24 17:35:08 +00:00
from oven import Oven, Profile
2013-11-24 15:18:42 +00:00
from ovenWatcher import OvenWatcher
2013-11-23 23:21:26 +00:00
2013-11-23 21:25:20 +00:00
app = bottle.Bottle()
oven = Oven()
ovenWatcher = OvenWatcher(oven)
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-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-24 15:07:59 +00:00
@app.route('/control')
def handle_control():
wsock = get_websocket_from_request()
2013-11-24 15:50:16 +00:00
log.info("websocket (control) opened")
2013-11-23 21:25:20 +00:00
while True:
try:
message = wsock.receive()
wsock.send("Your message was: %r" % message)
2013-11-24 17:35:08 +00:00
log.info("Received (control): %s"% message)
msgdict = json.loads(message)
if msgdict.get("cmd") == "RUN":
log.info("RUN command received")
profile_obj = msgdict.get('profile')
if profile_obj:
profile_json = json.dumps(profile_obj)
profile = Profile(profile_json)
oven.run_profile(profile)
2013-11-29 23:47:35 +00:00
ovenWatcher.record(profile_json)
2013-11-24 17:35:08 +00:00
elif msgdict.get("cmd") == "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-24 15:50:16 +00:00
log.info("websocket (control) closed")
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 15:50:16 +00:00
log.info("websocket (storage) opened")
2013-11-24 14:50:07 +00:00
while True:
try:
message = wsock.receive()
2013-11-24 15:50:16 +00:00
if not message:
break
2013-11-24 19:01:33 +00:00
print message
try:
msgdict = json.loads(message)
except:
msgdict = {}
2013-11-24 14:50:07 +00:00
if message == "GET":
log.info("GET command recived")
2013-11-24 15:07:59 +00:00
wsock.send(get_profiles())
2013-11-24 19:01:33 +00:00
elif msgdict.get("cmd") == "PUT":
2013-11-24 14:50:07 +00:00
log.info("PUT command received")
2013-11-24 19:01:33 +00:00
profile_obj = msgdict.get('profile')
2013-11-25 00:12:58 +00:00
force = msgdict.get('force',False)
2013-11-24 19:01:33 +00:00
if profile_obj:
2013-11-25 00:12:58 +00:00
#del msgdict["cmd"]
if save_profile(profile_obj,force):
msgdict["resp"]="OK"
else:
msgdict["resp"]="FAIL"
print "sending:" +str(msgdict)
wsock.send(json.dumps(msgdict))
2013-11-25 00:24:20 +00:00
wsock.send(get_profiles())
2013-11-24 14:50:07 +00:00
except WebSocketError:
break
2013-11-24 15:50:16 +00:00
log.info("websocket (storage) closed")
2013-11-24 14:50:07 +00:00
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-29 23:47:35 +00:00
ovenWatcher.add_observer(wsock)
2013-11-24 15:50:16 +00:00
log.info("websocket (status) opened")
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:50:16 +00:00
log.info("websocket (status) closed")
2013-11-23 21:25:20 +00:00
2013-11-24 19:01:33 +00:00
script_dir = os.path.dirname(os.path.realpath(__file__))
profile_path = os.path.join(script_dir,"storage","profiles")
2013-11-24 15:07:59 +00:00
def get_profiles():
2013-11-24 14:50:07 +00:00
try :
2013-11-24 19:01:33 +00:00
profile_files = os.listdir(profile_path)
2013-11-24 14:50:07 +00:00
except :
profile_files = []
profiles = []
for filename in profile_files:
2013-11-24 19:01:33 +00:00
with open(os.path.join(profile_path,filename), 'r') as f:
2013-11-24 14:50:07 +00:00
profiles.append(json.load(f))
return json.dumps(profiles)
2013-11-25 00:12:58 +00:00
def save_profile(profile, force=False):
2013-11-24 19:01:33 +00:00
profile_json = json.dumps(profile)
filename = profile['name']+".json"
filepath = os.path.join(profile_path,filename)
2013-11-25 00:12:58 +00:00
if not force and os.path.exists(filepath):
print "Didnt write"
return False
2013-11-24 19:01:33 +00:00
with open(filepath, 'w+') as f:
print filepath
f.write(profile_json)
f.close()
2013-11-25 00:12:58 +00:00
print "Did write"
return True
2013-11-24 19:01:33 +00:00
2013-11-23 21:25:20 +00:00
def main():
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()