Commit c942bc6e authored by D1plo1d's avatar D1plo1d

Refactoring pronsole logging so that prontserve can send useful errors over websockets

parent 03295e11
...@@ -390,6 +390,9 @@ class pronsole(cmd.Cmd): ...@@ -390,6 +390,9 @@ class pronsole(cmd.Cmd):
def log(self, *msg): def log(self, *msg):
print u"".join(unicode(i) for i in msg) print u"".join(unicode(i) for i in msg)
def logError(self, *msg):
print u"".join(unicode(i) for i in msg)
def promptf(self): def promptf(self):
"""A function to generate prompts so that we can do dynamic prompts. """ """A function to generate prompts so that we can do dynamic prompts. """
if self.in_macro: if self.in_macro:
...@@ -517,7 +520,7 @@ class pronsole(cmd.Cmd): ...@@ -517,7 +520,7 @@ class pronsole(cmd.Cmd):
macro_def += self.cur_macro_def macro_def += self.cur_macro_def
self.save_in_rc(macro_key, macro_def) self.save_in_rc(macro_key, macro_def)
else: else:
self.log("Empty macro - cancelled") self.logError("Empty macro - cancelled")
del self.cur_macro_name, self.cur_macro_def del self.cur_macro_name, self.cur_macro_def
def parseusercmd(self, line): def parseusercmd(self, line):
...@@ -536,7 +539,7 @@ class pronsole(cmd.Cmd): ...@@ -536,7 +539,7 @@ class pronsole(cmd.Cmd):
def compile_macro(self, macro_name, macro_def): def compile_macro(self, macro_name, macro_def):
if macro_def.strip() == "": if macro_def.strip() == "":
self.log("Empty macro - cancelled") self.logError("Empty macro - cancelled")
return return
pycode = "def macro(self,*arg):\n" pycode = "def macro(self,*arg):\n"
if "\n" not in macro_def.strip(): if "\n" not in macro_def.strip():
...@@ -550,7 +553,7 @@ class pronsole(cmd.Cmd): ...@@ -550,7 +553,7 @@ class pronsole(cmd.Cmd):
def start_macro(self, macro_name, prev_definition = "", suppress_instructions = False): def start_macro(self, macro_name, prev_definition = "", suppress_instructions = False):
if not self.processing_rc and not suppress_instructions: if not self.processing_rc and not suppress_instructions:
self.log("Enter macro using indented lines, end with empty line") self.logError("Enter macro using indented lines, end with empty line")
self.cur_macro_name = macro_name self.cur_macro_name = macro_name
self.cur_macro_def = "" self.cur_macro_def = ""
self.onecmd = self.hook_macro # override onecmd temporarily self.onecmd = self.hook_macro # override onecmd temporarily
...@@ -565,7 +568,7 @@ class pronsole(cmd.Cmd): ...@@ -565,7 +568,7 @@ class pronsole(cmd.Cmd):
if not self.processing_rc and not self.processing_args: if not self.processing_rc and not self.processing_args:
self.save_in_rc("macro "+macro_name, "") self.save_in_rc("macro "+macro_name, "")
else: else:
self.log("Macro '"+macro_name+"' is not defined") self.logError("Macro '"+macro_name+"' is not defined")
def do_macro(self, args): def do_macro(self, args):
if args.strip()=="": if args.strip()=="":
self.print_topics("User-defined macros", self.macros.keys(), 15, 80) self.print_topics("User-defined macros", self.macros.keys(), 15, 80)
...@@ -573,7 +576,7 @@ class pronsole(cmd.Cmd): ...@@ -573,7 +576,7 @@ class pronsole(cmd.Cmd):
arglist = args.split(None, 1) arglist = args.split(None, 1)
macro_name = arglist[0] macro_name = arglist[0]
if macro_name not in self.macros and hasattr(self.__class__, "do_"+macro_name): if macro_name not in self.macros and hasattr(self.__class__, "do_"+macro_name):
self.log("Name '"+macro_name+"' is being used by built-in command") self.logError("Name '"+macro_name+"' is being used by built-in command")
return return
if len(arglist) == 2: if len(arglist) == 2:
macro_def = arglist[1] macro_def = arglist[1]
...@@ -610,7 +613,7 @@ class pronsole(cmd.Cmd): ...@@ -610,7 +613,7 @@ class pronsole(cmd.Cmd):
else: else:
self.log("Macro '"+macro_name+"' defined as: '"+macro_def+"'") self.log("Macro '"+macro_name+"' defined as: '"+macro_def+"'")
else: else:
self.log("Macro '"+macro_name+"' is not defined") self.logError("Macro '"+macro_name+"' is not defined")
def set(self, var, str): def set(self, var, str):
try: try:
...@@ -619,9 +622,9 @@ class pronsole(cmd.Cmd): ...@@ -619,9 +622,9 @@ class pronsole(cmd.Cmd):
if not self.processing_rc and not self.processing_args: if not self.processing_rc and not self.processing_args:
self.save_in_rc("set "+var, "set %s %s" % (var, value)) self.save_in_rc("set "+var, "set %s %s" % (var, value))
except AttributeError: except AttributeError:
self.log("Unknown variable '%s'" % var) self.logError("Unknown variable '%s'" % var)
except ValueError, ve: except ValueError, ve:
self.log("Bad value for variable '%s', expecting %s (%s)" % (var, repr(t)[1:-1], ve.args[0])) self.logError("Bad value for variable '%s', expecting %s (%s)" % (var, repr(t)[1:-1], ve.args[0]))
def do_set(self, argl): def do_set(self, argl):
args = argl.split(None, 1) args = argl.split(None, 1)
...@@ -634,7 +637,7 @@ class pronsole(cmd.Cmd): ...@@ -634,7 +637,7 @@ class pronsole(cmd.Cmd):
try: try:
self.log("%s = %s" % (args[0], getattr(self.settings, args[0]))) self.log("%s = %s" % (args[0], getattr(self.settings, args[0])))
except AttributeError: except AttributeError:
self.log("Unknown variable '%s'" % args[0]) self.logError("Unknown variable '%s'" % args[0])
return return
self.set(args[0], args[1]) self.set(args[0], args[1])
...@@ -729,7 +732,7 @@ class pronsole(cmd.Cmd): ...@@ -729,7 +732,7 @@ class pronsole(cmd.Cmd):
#else: #else:
# self.log("Removed '"+key+"' from '"+self.rc_filename+"'") # self.log("Removed '"+key+"' from '"+self.rc_filename+"'")
except Exception, e: except Exception, e:
self.log("Saving failed for ", key+":", str(e)) self.logError("Saving failed for ", key+":", str(e))
finally: finally:
del rci, rco del rci, rco
...@@ -794,11 +797,11 @@ class pronsole(cmd.Cmd): ...@@ -794,11 +797,11 @@ class pronsole(cmd.Cmd):
def _do_load(self, filename): def _do_load(self, filename):
if not filename: if not filename:
self.log("No file name given.") self.logError("No file name given.")
return return
self.log("Loading file: " + filename) self.logError("Loading file: " + filename)
if not os.path.exists(filename): if not os.path.exists(filename):
self.log("File not found!") self.logError("File not found!")
return return
self.fgcode = gcoder.GCode(open(filename)) self.fgcode = gcoder.GCode(open(filename))
self.filename = filename self.filename = filename
...@@ -823,10 +826,10 @@ class pronsole(cmd.Cmd): ...@@ -823,10 +826,10 @@ class pronsole(cmd.Cmd):
filename = names[0] filename = names[0]
targetname = names[1] targetname = names[1]
else: else:
self.log(_("Please enter target name in 8.3 format.")) self.logError(_("Please enter target name in 8.3 format."))
return return
if not self.p.online: if not self.p.online:
self.log(_("Not connected to printer.")) self.logError(_("Not connected to printer."))
return return
self._do_load(filename) self._do_load(filename)
self.log(_("Uploading as %s") % targetname) self.log(_("Uploading as %s") % targetname)
...@@ -850,13 +853,13 @@ class pronsole(cmd.Cmd): ...@@ -850,13 +853,13 @@ class pronsole(cmd.Cmd):
self.log(_("Upload completed. %s should now be on the card.") % targetname) self.log(_("Upload completed. %s should now be on the card.") % targetname)
return return
except: except:
self.log(_("...interrupted!")) self.logError(_("...interrupted!"))
self.p.pause() self.p.pause()
self.p.send_now("M29 "+targetname) self.p.send_now("M29 "+targetname)
time.sleep(0.2) time.sleep(0.2)
self.p.clear = 1 self.p.clear = 1
self.p.startprint(None) self.p.startprint(None)
self.log(_("A partial file named %s may have been written to the sd card.") % targetname) self.logError(_("A partial file named %s may have been written to the sd card.") % targetname)
def complete_upload(self, text, line, begidx, endidx): def complete_upload(self, text, line, begidx, endidx):
s = line.split() s = line.split()
...@@ -879,10 +882,10 @@ class pronsole(cmd.Cmd): ...@@ -879,10 +882,10 @@ class pronsole(cmd.Cmd):
def do_print(self, l): def do_print(self, l):
if not self.fgcode: if not self.fgcode:
self.log(_("No file loaded. Please use load first.")) self.logError(_("No file loaded. Please use load first."))
return return
if not self.p.online: if not self.p.online:
self.log(_("Not connected to printer.")) self.logError(_("Not connected to printer."))
return return
self.log(_("Printing %s") % self.filename) self.log(_("Printing %s") % self.filename)
self.log(_("You can monitor the print with the monitor command.")) self.log(_("You can monitor the print with the monitor command."))
...@@ -893,7 +896,7 @@ class pronsole(cmd.Cmd): ...@@ -893,7 +896,7 @@ class pronsole(cmd.Cmd):
self.p.send_now("M25") self.p.send_now("M25")
else: else:
if not self.p.printing: if not self.p.printing:
self.log(_("Not printing, cannot pause.")) self.logError(_("Not printing, cannot pause."))
return return
self.p.pause() self.p.pause()
self.paused = True self.paused = True
...@@ -903,7 +906,7 @@ class pronsole(cmd.Cmd): ...@@ -903,7 +906,7 @@ class pronsole(cmd.Cmd):
def do_resume(self, l): def do_resume(self, l):
if not self.paused: if not self.paused:
self.log(_("Not paused, unable to resume. Start a print first.")) self.logError(_("Not paused, unable to resume. Start a print first."))
return return
self.paused = False self.paused = False
if self.sdprinting: if self.sdprinting:
...@@ -942,7 +945,7 @@ class pronsole(cmd.Cmd): ...@@ -942,7 +945,7 @@ class pronsole(cmd.Cmd):
def do_ls(self, l): def do_ls(self, l):
if not self.p.online: if not self.p.online:
self.log(_("Printer is not online. Please connect to it first.")) self.logError(_("Printer is not online. Please connect to it first."))
return return
self._do_ls(True) self._do_ls(True)
...@@ -951,7 +954,7 @@ class pronsole(cmd.Cmd): ...@@ -951,7 +954,7 @@ class pronsole(cmd.Cmd):
def waitforsdresponse(self, l): def waitforsdresponse(self, l):
if "file.open failed" in l: if "file.open failed" in l:
self.log(_("Opening file failed.")) self.logError(_("Opening file failed."))
self.recvlisteners.remove(self.waitforsdresponse) self.recvlisteners.remove(self.waitforsdresponse)
return return
if "File opened" in l: if "File opened" in l:
...@@ -1035,7 +1038,7 @@ class pronsole(cmd.Cmd): ...@@ -1035,7 +1038,7 @@ class pronsole(cmd.Cmd):
self.log("SENDING:"+l) self.log("SENDING:"+l)
self.p.send_now(l) self.p.send_now(l)
else: else:
self.log(_("Printer is not online.")) self.logError(_("Printer is not online."))
return return
elif l[0] in self.commandprefixes.lower(): elif l[0] in self.commandprefixes.lower():
if self.p and self.p.online: if self.p and self.p.online:
...@@ -1043,7 +1046,7 @@ class pronsole(cmd.Cmd): ...@@ -1043,7 +1046,7 @@ class pronsole(cmd.Cmd):
self.log("SENDING:"+l.upper()) self.log("SENDING:"+l.upper())
self.p.send_now(l.upper()) self.p.send_now(l.upper())
else: else:
self.log(_("Printer is not online.")) self.logError(_("Printer is not online."))
return return
elif l[0] == "@": elif l[0] == "@":
if self.p and self.p.online: if self.p and self.p.online:
...@@ -1051,7 +1054,7 @@ class pronsole(cmd.Cmd): ...@@ -1051,7 +1054,7 @@ class pronsole(cmd.Cmd):
self.log("SENDING:"+l[1:]) self.log("SENDING:"+l[1:])
self.p.send_now(l[1:]) self.p.send_now(l[1:])
else: else:
self.log("printer is not online.") self.logError("printer is not online.")
return return
else: else:
cmd.Cmd.default(self, l) cmd.Cmd.default(self, l)
...@@ -1093,11 +1096,11 @@ class pronsole(cmd.Cmd): ...@@ -1093,11 +1096,11 @@ class pronsole(cmd.Cmd):
self.p.send_now("M104 S"+l) self.p.send_now("M104 S"+l)
self.log(_("Setting hotend temperature to %s degrees Celsius.") % f) self.log(_("Setting hotend temperature to %s degrees Celsius.") % f)
else: else:
self.log(_("Printer is not online.")) self.logError(_("Printer is not online."))
else: else:
self.log(_("You cannot set negative temperatures. To turn the hotend off entirely, set its temperature to 0.")) self.logError(_("You cannot set negative temperatures. To turn the hotend off entirely, set its temperature to 0."))
except: except:
self.log(_("You must enter a temperature.")) self.logError(_("You must enter a temperature."))
def help_settemp(self): def help_settemp(self):
self.log(_("Sets the hotend temperature to the value entered.")) self.log(_("Sets the hotend temperature to the value entered."))
...@@ -1117,13 +1120,13 @@ class pronsole(cmd.Cmd): ...@@ -1117,13 +1120,13 @@ class pronsole(cmd.Cmd):
if f>=0: if f>=0:
if self.p.online: if self.p.online:
self.p.send_now("M140 S"+l) self.p.send_now("M140 S"+l)
self.log(_("Setting bed temperature to %s degrees Celsius.") % f) self.logError(_("Setting bed temperature to %s degrees Celsius.") % f)
else: else:
self.log(_("Printer is not online.")) self.logError(_("Printer is not online."))
else: else:
self.log(_("You cannot set negative temperatures. To turn the bed off entirely, set its temperature to 0.")) self.logError(_("You cannot set negative temperatures. To turn the bed off entirely, set its temperature to 0."))
except: except:
self.log(_("You must enter a temperature.")) self.logError(_("You must enter a temperature."))
def help_bedtemp(self): def help_bedtemp(self):
self.log(_("Sets the bed temperature to the value entered.")) self.log(_("Sets the bed temperature to the value entered."))
...@@ -1136,13 +1139,13 @@ class pronsole(cmd.Cmd): ...@@ -1136,13 +1139,13 @@ class pronsole(cmd.Cmd):
def do_move(self, l): def do_move(self, l):
if(len(l.split())<2): if(len(l.split())<2):
self.log(_("No move specified.")) self.logError(_("No move specified."))
return return
if self.p.printing: if self.p.printing:
self.log(_("Printer is currently printing. Please pause the print before you issue manual commands.")) self.logError(_("Printer is currently printing. Please pause the print before you issue manual commands."))
return return
if not self.p.online: if not self.p.online:
self.log(_("Printer is not online. Unable to move.")) self.logError(_("Printer is not online. Unable to move."))
return return
l = l.split() l = l.split()
if(l[0].lower()=="x"): if(l[0].lower()=="x"):
...@@ -1158,13 +1161,13 @@ class pronsole(cmd.Cmd): ...@@ -1158,13 +1161,13 @@ class pronsole(cmd.Cmd):
feed = self.settings.e_feedrate feed = self.settings.e_feedrate
axis = "E" axis = "E"
else: else:
self.log(_("Unknown axis.")) self.logError(_("Unknown axis."))
return return
dist = 0 dist = 0
try: try:
dist = float(l[1]) dist = float(l[1])
except: except:
self.log(_("Invalid distance")) self.logError(_("Invalid distance"))
return return
try: try:
feed = int(l[2]) feed = int(l[2])
...@@ -1199,22 +1202,22 @@ class pronsole(cmd.Cmd): ...@@ -1199,22 +1202,22 @@ class pronsole(cmd.Cmd):
length = 5#default extrusion length length = 5#default extrusion length
feed = self.settings.e_feedrate#default speed feed = self.settings.e_feedrate#default speed
if not self.p.online: if not self.p.online:
self.log("Printer is not online. Unable to extrude.") self.logError("Printer is not online. Unable to extrude.")
return return
if self.p.printing: if self.p.printing:
self.log(_("Printer is currently printing. Please pause the print before you issue manual commands.")) self.logError(_("Printer is currently printing. Please pause the print before you issue manual commands."))
return return
ls = l.split() ls = l.split()
if len(ls): if len(ls):
try: try:
length = float(ls[0]) length = float(ls[0])
except: except:
self.log(_("Invalid length given.")) self.logError(_("Invalid length given."))
if len(ls)>1: if len(ls)>1:
try: try:
feed = int(ls[1]) feed = int(ls[1])
except: except:
self.log(_("Invalid speed given.")) self.logError(_("Invalid speed given."))
if override is not None: if override is not None:
length = override length = override
feed = overridefeed feed = overridefeed
...@@ -1239,22 +1242,22 @@ class pronsole(cmd.Cmd): ...@@ -1239,22 +1242,22 @@ class pronsole(cmd.Cmd):
length = 5#default extrusion length length = 5#default extrusion length
feed = self.settings.e_feedrate#default speed feed = self.settings.e_feedrate#default speed
if not self.p.online: if not self.p.online:
self.log(_("Printer is not online. Unable to reverse.")) self.logError(_("Printer is not online. Unable to reverse."))
return return
if self.p.printing: if self.p.printing:
self.log(_("Printer is currently printing. Please pause the print before you issue manual commands.")) self.logError(_("Printer is currently printing. Please pause the print before you issue manual commands."))
return return
ls = l.split() ls = l.split()
if len(ls): if len(ls):
try: try:
length = float(ls[0]) length = float(ls[0])
except: except:
self.log(_("Invalid length given.")) self.logError(_("Invalid length given."))
if len(ls)>1: if len(ls)>1:
try: try:
feed = int(ls[1]) feed = int(ls[1])
except: except:
self.log(_("Invalid speed given.")) self.logError(_("Invalid speed given."))
self.do_extrude("", length*-1.0, feed) self.do_extrude("", length*-1.0, feed)
def help_reverse(self): def help_reverse(self):
...@@ -1288,17 +1291,17 @@ class pronsole(cmd.Cmd): ...@@ -1288,17 +1291,17 @@ class pronsole(cmd.Cmd):
def do_monitor(self, l): def do_monitor(self, l):
interval = 5 interval = 5
if not self.p.online: if not self.p.online:
self.log(_("Printer is not online. Please connect to it first.")) self.logError(_("Printer is not online. Please connect to it first."))
return return
if not (self.p.printing or self.sdprinting): if not (self.p.printing or self.sdprinting):
self.log(_("Printer is not printing. Please print something before monitoring.")) self.logError(_("Printer is not printing. Please print something before monitoring."))
return return
self.log(_("Monitoring printer, use ^C to interrupt.")) self.log(_("Monitoring printer, use ^C to interrupt."))
if len(l): if len(l):
try: try:
interval = float(l) interval = float(l)
except: except:
self.log(_("Invalid period given.")) self.logError(_("Invalid period given."))
self.log(_("Updating values every %f seconds.") % (interval,)) self.log(_("Updating values every %f seconds.") % (interval,))
self.monitoring = 1 self.monitoring = 1
prev_msg_len = 0 prev_msg_len = 0
...@@ -1336,7 +1339,7 @@ class pronsole(cmd.Cmd): ...@@ -1336,7 +1339,7 @@ class pronsole(cmd.Cmd):
def do_skein(self, l): def do_skein(self, l):
l = l.split() l = l.split()
if len(l) == 0: if len(l) == 0:
self.log(_("No file name given.")) self.logError(_("No file name given."))
return return
settings = 0 settings = 0
if(l[0]=="set"): if(l[0]=="set"):
...@@ -1344,7 +1347,7 @@ class pronsole(cmd.Cmd): ...@@ -1344,7 +1347,7 @@ class pronsole(cmd.Cmd):
else: else:
self.log(_("Skeining file: %s") % l[0]) self.log(_("Skeining file: %s") % l[0])
if not(os.path.exists(l[0])): if not(os.path.exists(l[0])):
self.log(_("File not found!")) self.logError(_("File not found!"))
return return
try: try:
if settings: if settings:
...@@ -1359,7 +1362,7 @@ class pronsole(cmd.Cmd): ...@@ -1359,7 +1362,7 @@ class pronsole(cmd.Cmd):
self.log(_("Loading sliced file.")) self.log(_("Loading sliced file."))
self.do_load(l[0].replace(".stl", "_export.gcode")) self.do_load(l[0].replace(".stl", "_export.gcode"))
except Exception, e: except Exception, e:
self.log(_("Skeinforge execution failed: %s") % e) self.logError(_("Skeinforge execution failed: %s") % e)
def complete_skein(self, text, line, begidx, endidx): def complete_skein(self, text, line, begidx, endidx):
s = line.split() s = line.split()
...@@ -1379,10 +1382,10 @@ class pronsole(cmd.Cmd): ...@@ -1379,10 +1382,10 @@ class pronsole(cmd.Cmd):
def do_home(self, l): def do_home(self, l):
if not self.p.online: if not self.p.online:
self.log(_("Printer is not online. Unable to move.")) self.logError(_("Printer is not online. Unable to move."))
return return
if self.p.printing: if self.p.printing:
self.log(_("Printer is currently printing. Please pause the print before you issue manual commands.")) self.logError(_("Printer is currently printing. Please pause the print before you issue manual commands."))
return return
if "x" in l.lower(): if "x" in l.lower():
self.p.send_now("G28 X0") self.p.send_now("G28 X0")
......
...@@ -196,11 +196,10 @@ class ConstructSocketHandler(tornado.websocket.WebSocketHandler): ...@@ -196,11 +196,10 @@ class ConstructSocketHandler(tornado.websocket.WebSocketHandler):
try: try:
if cmd == "set": cmd = "construct_set" if cmd == "set": cmd = "construct_set"
response = getattr(prontserve, "do_%s"%cmd)(*args, **kwargs) response = getattr(prontserve, "do_%s"%cmd)(*args, **kwargs)
print response self.write_message({"ack": response})
if response is not None: self.write_message(response) except Exception as ex:
except:
print traceback.format_exc() print traceback.format_exc()
self.write_message({"error": "bad command."}) self.write_message({"error": str(ex)})
else: else:
self.write_message({"error": "%s command does not exist."%cmd}) self.write_message({"error": "%s command does not exist."%cmd})
...@@ -253,6 +252,7 @@ class EventEmitter(object): ...@@ -253,6 +252,7 @@ class EventEmitter(object):
class Prontserve(pronsole.pronsole, EventEmitter): class Prontserve(pronsole.pronsole, EventEmitter):
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.initializing = True
pronsole.pronsole.__init__(self) pronsole.pronsole.__init__(self)
EventEmitter.__init__(self) EventEmitter.__init__(self)
self.settings.sensor_names = {'T': 'extruder', 'B': 'bed'} self.settings.sensor_names = {'T': 'extruder', 'B': 'bed'}
...@@ -273,6 +273,7 @@ class Prontserve(pronsole.pronsole, EventEmitter): ...@@ -273,6 +273,7 @@ class Prontserve(pronsole.pronsole, EventEmitter):
self._sensor_update_received = True self._sensor_update_received = True
self.init_mdns() self.init_mdns()
self.jobs.listeners.add(self) self.jobs.listeners.add(self)
self.initializing = False
def init_mdns(self): def init_mdns(self):
sdRef = pybonjour.DNSServiceRegister(name = None, sdRef = pybonjour.DNSServiceRegister(name = None,
...@@ -458,6 +459,11 @@ class Prontserve(pronsole.pronsole, EventEmitter): ...@@ -458,6 +459,11 @@ class Prontserve(pronsole.pronsole, EventEmitter):
print msg print msg
self.fire("log", {'msg': msg, 'level': "debug"}) self.fire("log", {'msg': msg, 'level': "debug"})
def logError(self, *msg):
print u"".join(unicode(i) for i in msg)
if self.initializing == False:
raise Exception(u"".join(unicode(i) for i in msg))
def write_prompt(self): def write_prompt(self):
None None
......
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