diff --git a/cgi-bin/upload.py b/cgi-bin/upload.py index 76a032e..a72de07 100644 --- a/cgi-bin/upload.py +++ b/cgi-bin/upload.py @@ -1,63 +1,63 @@ #!/usr/bin/env python3 """ Created on Sat Mar 28 19:29:28 2020 This module implements a CGI service for uploading voice records in the coughvid web application. @author: T. Teijeiro """ import cgi import datetime import json #TODO disable cgitb in production #import cgitb #cgitb.enable() #Point this variable to the folder where the data will be stored DB_PATH = '/tmp' STAYHOME_PATH = f'{DB_PATH}/stayHome' form = cgi.FieldStorage() #First we check if we are receiving a recording or metadata mimeType = form.getvalue('mimeType') if mimeType == "json": uid = form.getvalue('uuid') if uid is not None and uid != 'null': with open(f'{DB_PATH}/{uid}.json', 'w') as fout: dct = {k:form.getvalue(k) for k in form.keys()} json.dump(dct, fout) message = 'The metadata was properly stored' else: message = 'Not valid UUID' else: # Get filename here. fileitem = form['audio_data'] latitude = form.getvalue('lat') longitude = form.getvalue('lon') mimeType = form.getvalue('mimeType') uid = form.getvalue('uuid') ext = 'wav' if 'ogg' in mimeType: ext = 'ogg' elif 'webm' in mimeType: ext = 'webm' data = fileitem.file.read() #We limit the file size to 2MB, to prevent possible flooding attacks. if 3000 < len(data) < 2000000: # Test if the file was uploaded filename = f'{uid}_{datetime.datetime.now().isoformat()}_{latitude}_{longitude}.{ext}' - outdir = STAYHOME_PATH if len(uid) < 15 else DB_PATH + outdir = STAYHOME_PATH if len(uid) < 25 else DB_PATH with open(f'{outdir}/{filename}', 'wb') as fout: fout.write(data) message = 'The recording was uploaded successfully' else: message = 'Record too long.' print(f"""\ Content-Type: text/html\n

{message}

""") diff --git a/js/upm_app.js b/js/upm_app.js index af95d42..ecc0b12 100644 --- a/js/upm_app.js +++ b/js/upm_app.js @@ -1,138 +1,125 @@ var recorder, gumStream; var lat=null; var lon=null; var uuid=null; var cancelled = false; var noSleep = new NoSleep(); var recordButton = document.getElementById("recordButton"); var recordText = document.getElementById("recordText"); var audio = new Audio('/sounds/bell.mp3'); recordButton.addEventListener("click", toggleRecording); //$(window).on('load',function(){$('#coughModal').modal('show');}); $("#patID").on("input", function () { if (this.value.length > 14) { this.value = this.value.slice(0, 14); } if (this.value.length > 1) { $("#recordButton").removeClass("disabled"); this.value = this.value.toUpperCase(); uuid = "UPM_" + this.value } else { $("#recordButton").addClass("disabled"); } }) //ProgressBar API: https://progressbarjs.readthedocs.io/en/latest/ var bar = new ProgressBar.Line(progressBar, { strokeWidth: 1, easing: 'linear', duration: 20000, color: "#DC1200", trailColor: '#eee', trailWidth: 1, svgStyle: {width: '100%', height: '100%'} }); function toggleRecording() { if ($("#recordButton").hasClass("disabled")) { return; } if (recorder && recorder.state == "recording") { cancelled = true; bar.stop(); bar.set(0); setListStatus(0); finishRecording(); } else { navigator.mediaDevices.getUserMedia({ audio: {echoCancellation:false} }).then(function(stream) { gumStream = stream; recorder = new MediaRecorder(stream); recorder.ondataavailable = function(e) { if (!cancelled) { var xhr=new XMLHttpRequest(); var fd=new FormData(); fd.append("audio_data", e.data, new Date().toISOString()); fd.append("lat", lat); fd.append("lon", lon); fd.append("mimeType", e.data.type); fd.append("uuid", uuid); xhr.open("POST","/cgi-bin/upload.py",true); xhr.send(fd); bar.set(0); - setListStatus(4); + setListStatus(3); audio.play(); $("#thanks").animate({opacity: 1}, 1000, function() { setTimeout(function(){ $("#thanks").animate({opacity:0}, 500, function() {setListStatus(0);}) }, 800); }); } }; recorder.start(); cancelled = false; noSleep.enable(); setListStatus(1); - bar.animate(1.0, {duration: 20000}, changeToDeepBreath); + bar.animate(1.0, {duration: 20000}, changeToCough); recordText.innerHTML = "◻ Cancelar"; recordButton.classList.remove('btn-dark'); recordButton.classList.add('btn-danger'); navigator.geolocation.getCurrentPosition(function(position) { lat = position.coords.latitude; lon = position.coords.longitude; }); }); } } function setListStatus(status) { if (status == 0) { - $("#normal_breath_li, #deep_breath_li, #cough_li").removeClass("active-li"); - $("#normal_breath_li, #deep_breath_li, #cough_li").removeClass("completed-li"); - $("#normal_breath_li, #deep_breath_li, #cough_li").addClass("inactive-li"); + $("#sustained_vocalization_li, #cough_li").removeClass("active-li"); + $("#sustained_vocalization_li, #cough_li").removeClass("completed-li"); + $("#sustained_vocalization_li, #cough_li").addClass("inactive-li"); } else if (status == 1) { - $("#normal_breath_li").removeClass("inactive-li"); - $("#normal_breath_li").addClass("active-li"); + $("#sustained_vocalization_li").removeClass("inactive-li"); + $("#sustained_vocalization_li").addClass("active-li"); } else if (status == 2) { - $("#normal_breath_li").removeClass("active-li"); - $("#normal_breath_li").addClass("completed-li"); - $("#deep_breath_li").removeClass("inactive-li"); - $("#deep_breath_li").addClass("active-li"); - } else if (status == 3) { - $("#deep_breath_li").removeClass("active-li"); - $("#deep_breath_li").addClass("completed-li"); + $("#sustained_vocalization_li").removeClass("active-li"); + $("#sustained_vocalization_li").addClass("completed-li"); $("#cough_li").removeClass("inactive-li"); $("#cough_li").addClass("active-li"); - } else if (status == 4) { + } else if (status == 3) { $("#cough_li").removeClass("active-li"); $("#cough_li").addClass("completed-li"); } } - -function changeToDeepBreath() { - bar.set(0); - setListStatus(2); - bar.animate(1.0, {duration: 15000}, changeToCough); - audio.play(); -} - function changeToCough() { bar.set(0); - setListStatus(3); + setListStatus(2); bar.animate(1.0, {duration: 15000}, finishRecording); audio.play(); } function finishRecording() { recorder.stop(); gumStream.getAudioTracks()[0].stop(); noSleep.disable(); recordText.innerHTML = "◉ Grabar"; recordButton.classList.remove('btn-danger'); recordButton.classList.add('btn-dark'); } diff --git a/upm/index.html b/upm/index.html index e5197ce..47f4494 100644 --- a/upm/index.html +++ b/upm/index.html @@ -1,669 +1,646 @@ Coughvid - UPM
Volver arriba
- -