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
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
"""
with self.scanLock:
self.eventQueue.put(Signal('scan.start'))
cmd = [
'sudo', 'scanimage', '-d', 'epkowa', '--format', 'jpeg',
cmd = [
'sudo', 'scanimage', '-d', 'epkowa', '--format', 'jpeg',
'--resolution=100', '-l','20','-t','30','-x',str(185),
'-y',str(245)
]
self.logger.info(f"{cmd}")
filename = self.currentHit.getImagePath()
]
self.logger.info(f"{cmd}")
filename = self.currentHit.getImagePath()
with self.scanLock:
self.eventQueue.put(Signal('scan.start'))
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'))

View file

@ -27,6 +27,7 @@ spec_names.forEach(function(name){
let request_time = timeStamp(),
hit_started = false,
elapsed_time,
hit_finished = false
@ -36,15 +37,6 @@ let request_time = timeStamp(),
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.send('hi server')
@ -56,14 +48,21 @@ ws.addEventListener('message', (event) => {
let data = JSON.parse(event.data)
if(data.property === 'hit_opened') {
hit_finished = false
request_time = timeStamp()
divs[data.property].innerHTML = `${request_time.format('dd mmm HH:MM:ss')}`
if(data.value != null){
hit_started = true
hit_finished = false
request_time = timeStamp()
divs[data.property].innerHTML = `${request_time.format('dd mmm HH:MM:ss')}`
}else{
hit_started = false
}
}
else if(data.property === 'hit_submitted'){
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){
if(!hit_finished) elapsed_time = `${new Date((Date.now() - request_time)).format('MM"m "ss"s"')}`
divs['elapsed_time'].innerHTML = elapsed_time
if(hit_started){
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()}