diff --git a/sorteerhoed/central_management.py b/sorteerhoed/central_management.py index 435515e..670a8f9 100644 --- a/sorteerhoed/central_management.py +++ b/sorteerhoed/central_management.py @@ -15,6 +15,7 @@ from sorteerhoed.Signal import Signal import io from PIL import Image import datetime +from shutil import copyfile class CentralManagement(): @@ -99,6 +100,7 @@ class CentralManagement(): time.sleep(.5) finally: + self.logger.warning("Stopping Central Managment") self.isRunning.clear() self.server.stop() @@ -183,7 +185,11 @@ class CentralManagement(): #{'event': {'HITGroupId': '301G7MYOAJ85NEW128ZDGF5DSBW53S', 'EventType': 'AssignmentAbandoned', 'EventTimestamp': '2019-10-23T20:23:06Z', 'HITId': '3JHB4BPSFKKFQ263K4EFULI3LC79QJ', 'AssignmentId': '3U088ZLJVL450PB6MJZUIIUCB6VW0Y', 'WorkerId': 'A1CK46PK9VEUH5', 'HITTypeId': '3EYXOXDEN7RX0YSMN4UMVN01AYKZJ0'}} sqsHit.accept_time = None sqsHit.open_page_at = None - self.reset() + if self.currentHit.id == sqsHit.id: + if not sqsHit.submit_page_at: + self.reset() + else: + sqsHit.submit_hit_at = datetime.datetime.utcnow() # fake submit if updateStatus: self.setLight(False) @@ -191,7 +197,11 @@ class CentralManagement(): self.logger.info(f'Set status progress to returned') sqsHit.accept_time = None sqsHit.open_page_at = None - self.reset() + if self.currentHit.id == sqsHit.id: + if not sqsHit.submit_page_at: + self.reset() + else: + sqsHit.submit_hit_at = datetime.datetime.utcnow() # fake submit if updateStatus: self.setLight(False) # {'event': {'HITGroupId': '301G7MYOAJ85NEW128ZDGF5DSBW53S', 'EventType': 'AssignmentReturned', 'EventTimestamp': '2019-10-23T20:16:47Z', 'HITId': '3IH9TRB0FBAKKZFP3JUD6D9YWQ1I1F', 'AssignmentId': '3BF51CHDTWLN3ZGHRKDUHFKPWIJ0H3', 'WorkerId': 'A1CK46PK9VEUH5', 'HITTypeId': '3EYXOXDEN7RX0YSMN4UMVN01AYKZJ0'}} @@ -308,26 +318,34 @@ class CentralManagement(): """ Run scanimage on scaner and returns a string with the filename """ + + cmd = [ + 'sudo', 'scanimage', '-d', 'epkowa', '--format', 'jpeg', + '--resolution=100', '-l','20','-t','30','-x',str(255), + '-y',str(185) + ] + self.logger.info(f"{cmd}") + filename = self.currentHit.getImagePath() + with self.scanLock: self.eventQueue.put(Signal('scan.start')) - cmd = [ - 'sudo', 'scanimage', '-d', 'epkowa', '--format', 'jpeg', - '--resolution=100', '-l','20','-t','30','-x',str(255), - '-y',str(185) - ] - self.logger.info(f"{cmd}") - filename = self.currentHit.getImagePath() proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # opens connection to scanner, but only starts scanning when output becomes ready: o, e = proc.communicate(80) if e: self.logger.critical(f"Scanner caused: {e.decode()}") #TODO: should clear self.isRunning.clear() ? - - f = io.BytesIO(o) - img = Image.open(f) - img = img.transpose(Image.ROTATE_90) - img.save(filename) + + try: + f = io.BytesIO(o) + img = Image.open(f) + img = img.transpose(Image.ROTATE_90) + img.save(filename) + except Exception as e: + self.logger.critical("Cannot create image from scan. Did scanner work?") + self.logger.exception(e) + # TODO: create + copyfile('www/basic.svg', filename) self.eventQueue.put(Signal('hit.scanned', {'hit_id':self.currentHit.id})) self.eventQueue.put(Signal('scan.finished'))