Simplified state updates
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Thu, 22 Feb 2018 08:47:01 +0000 (00:47 -0800)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Thu, 22 Feb 2018 08:47:01 +0000 (00:47 -0800)
src/py/bbctrl/Comm.py
src/py/bbctrl/Mach.py

index 4422c1b18cb3bdcb8197148086ac729ced2744e2..3be60bd72549a01f31e41cac43406e1127fb0de5 100644 (file)
@@ -42,12 +42,11 @@ class Comm():
     def __init__(self, ctrl):
         self.ctrl = ctrl
 
-        self.listeners = []
         self.queue = deque()
         self.in_buf = ''
         self.command = None
 
-        ctrl.state.add_listener(self._update_state)
+        ctrl.state.add_listener(self._update)
 
         try:
             self.sp = serial.Serial(ctrl.args.serial, ctrl.args.baud,
@@ -65,10 +64,6 @@ class Comm():
         self.i2c_addr = ctrl.args.avr_addr
 
 
-    def add_listener(self, listener):
-        self.listeners.append(listener)
-
-
     def start_sending_gcode(self, path):
         self.ctrl.planner.load(path)
         self.set_write(True)
@@ -108,7 +103,7 @@ class Comm():
         self.ctrl.ioloop.update_handler(self.sp, flags)
 
 
-    def _update_state(self, update):
+    def _update(self, update):
         if 'xx' in update and update['xx'] == 'ESTOPPED':
             self.stop_sending_gcode()
 
@@ -181,8 +176,6 @@ class Comm():
         except Exception as e:
             log.warning('%s: %s', e, data)
 
-        update = {}
-
         # Parse incoming serial data into lines
         while True:
             i = self.in_buf.find('\n')
@@ -202,20 +195,15 @@ class Comm():
 
                 if 'variables' in msg:
                     self._update_vars(msg)
-                    continue
 
-                if 'msg' in msg:
+                elif 'msg' in msg:
                     self._log_msg(msg)
-                    continue
 
-                if 'firmware' in msg:
+                elif 'firmware' in msg:
                     log.warning('firmware rebooted')
                     self.connect()
 
-                update.update(msg)
-
-        if update:
-            for listener in self.listeners: listener(update)
+                else: self.ctrl.state.update(msg)
 
 
     def _serial_handler(self, fd, events):
index 367d7cfc0b949d3fa82eb5d8b15a495bb29d0e91..9670b7690ee313d2e1c15c8953eed2bedce9d453 100644 (file)
@@ -58,11 +58,9 @@ class Mach():
     def __init__(self, ctrl):
         self.ctrl = ctrl
         self.comm = bbctrl.Comm(ctrl)
-
         self.stopping = False
 
-        self.comm.add_listener(self._comm_update)
-        self.comm.add_listener(self.ctrl.state.update)
+        ctrl.state.add_listener(self._update)
 
         self.comm.queue_command(Cmd.REBOOT)
 
@@ -70,7 +68,7 @@ class Mach():
     def _is_busy(self): return self.ctrl.planner.is_running()
 
 
-    def _comm_update(self, update):
+    def _update(self, update):
         if self.stopping and 'xx' in update and update['xx'] == 'HOLDING':
             self.comm.stop_sending_gcode()
             # Resume once current queue of GCode commands has flushed
@@ -140,10 +138,7 @@ class Mach():
 
     def estop(self): self.comm.i2c_command(Cmd.ESTOP)
     def clear(self): self.comm.i2c_command(Cmd.CLEAR)
-
-
-    def start(self, path):
-        if path: self.comm.start_sending_gcode(path)
+    def start(self, path): self.comm.start_sending_gcode(path)
 
 
     def step(self, path):