Merge branch 'master' of gitlab.com:rubenvandeven/sorteerhoed

This commit is contained in:
mt 2019-11-01 18:58:49 +01:00
commit 08939eeccd
2 changed files with 50 additions and 29 deletions

View file

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

View file

@ -27,6 +27,7 @@ spec_names.forEach(function(name){
let request_time = timeStamp(), let request_time = timeStamp(),
hit_started = false,
elapsed_time, elapsed_time,
hit_finished = false hit_finished = false
@ -36,15 +37,6 @@ let request_time = timeStamp(),
let ws = new ReconnectingWebSocket('ws://localhost:8888/status/ws') let ws = new ReconnectingWebSocket('ws://localhost:8888/status/ws')
let test = `{"property": "os", "value": "213.127.20.197"}`
// fakeWs(test)
//
// function fakeWs(json){
//
//
// }
ws.addEventListener('open', () => { ws.addEventListener('open', () => {
// ws.send('hi server') // ws.send('hi server')
@ -56,14 +48,21 @@ ws.addEventListener('message', (event) => {
let data = JSON.parse(event.data) let data = JSON.parse(event.data)
if(data.property === 'hit_opened') { if(data.property === 'hit_opened') {
if(data.value != null){
hit_started = true
hit_finished = false hit_finished = false
request_time = timeStamp() request_time = timeStamp()
divs[data.property].innerHTML = `${request_time.format('dd mmm HH:MM:ss')}` divs[data.property].innerHTML = `${request_time.format('dd mmm HH:MM:ss')}`
}else{
hit_started = false
}
} }
else if(data.property === 'hit_submitted'){ else if(data.property === 'hit_submitted'){
hit_finished = true; hit_finished = true;
} }
else if(divs[data.property]) divs[data.property].innerHTML = `${data.value}` else if(divs[data.property]){
data.value === null ? divs[data.property].innerHTML = `—` : divs[data.property].innerHTML = `${data.value}`
}
}) })
@ -103,8 +102,11 @@ function makeAnimation(){
function update(step){ function update(step){
if(!hit_finished) elapsed_time = `${new Date((Date.now() - request_time)).format('MM"m "ss"s"')}` if(!hit_finished) elapsed_time = `${new Date((Date.now() - request_time)).format('MM"m "ss"s"')}`
if(hit_started){
divs['elapsed_time'].innerHTML = elapsed_time divs['elapsed_time'].innerHTML = elapsed_time
}else{
divs['elapsed_time'].innerHTML = `—`
}
} }
@ -112,3 +114,4 @@ makeAnimation()
function timeStamp(){return window.performance && window.performance.now ? window.performance.now() : new Date().getTime()} function timeStamp(){return window.performance && window.performance.now ? window.performance.now() : new Date().getTime()}