Compare commits

...

2 Commits

Author SHA1 Message Date
Niko
e73bcc134b added final questions
added ssl certificate and switched from http & ws to https &wss
files to exchange for you are server/cert.pm for the certificate and server/key.pem for the key of the Hochschul certifiacte
2019-09-05 17:09:01 +02:00
Niko
42a704a460 finished questions
question 4 not possible
small refactoring
2019-07-08 19:26:21 +02:00
5 changed files with 174 additions and 93 deletions

2
.vscode/launch.json vendored
View File

@ -5,7 +5,7 @@
"type": "chrome",
"request": "launch",
"name": "Launch Chrome",
"url": "http://localhost:8000",
"url": "https://localhost:8000",
"webRoot": "${workspaceFolder}/client"
},

View File

@ -7,7 +7,8 @@ var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEv
// #region state management
var state = '';
var question = 0;
var rePrompt = false;
// currently disabled feature
// var rePrompt = false;
var partTwo = false;
var questionThreeCount = 0;
var strike = 0;
@ -20,20 +21,25 @@ const QUESTION_TWO = 'Nennen Sie mir bitte so viel Dinge wie möglich, die man i
const QUESTION_THREE = 'Ich werde Ihnen jetzt eine Zahlenreihe nennen, die Sie mir dann bitte in umgekehrter Reihenfolge wiederholen sollen. Wenn ich beispielsweise, vier - fünf sage, dann sagen Sie bitte, fünf - vier.';
const QUESTION_FOUR = 'Wir kommen nun zur vierten Frage. Bitte zählen Sie von 76 an rückwärts. Nennen Sie die Zahlen abwechselnd als ganze Zahlen und als einzelne Ziffern. Zum Beispiel:';
const QUESTION_FOUR_PT2 = ['34', '3, 3', '32', '3, 1'];
const QUESTION_FIVE = 'Ganz am Anfang dieses Tests habe ich Ihnen 10 Worte genannt. Können Sie sich noch an diese Worte erinnern?';
// #endregion
const FINISH = 'Sie sind jetzt fertig mit dem Test, Vielen Dank.';
// #region intents
const WELCOME_INTENT = 'Default Welcome Intent';
const WELCOME_FOLLOWUP_YES = 'Default Welcome Intent - yes';
const WELCOME_FOLLOWUP_NO = 'Default Welcome Intent - no';
const MORE_TIME = 'Add Time Intent';
const MORE_TIME_YES = 'Add Time Intent - yes';
const MORE_TIME_NO = 'Add Time Intent - no';
// currently disabled feature
// const MORE_TIME = 'Add Time Intent';
// const MORE_TIME_YES = 'Add Time Intent - yes';
// const MORE_TIME_NO = 'Add Time Intent - no';
const QUIT_INTENT = 'Quit Intent';
const FALLBACK_INTENT = 'Default Fallback Intent';
const HELP_INTENT = 'Help Intent';
const CHOOSE_QUESTION = 'Frage_Starten';
const NEXT_QUESTION = 'Nächste Frage';
// disabled feature
// const NEXT_QUESTION = 'Nächste Frage';
// #endregion
// #region questions and expected results
@ -93,7 +99,7 @@ var questionNumDisplay = document.querySelector('.quest');
// #endregion
// websocket to communicate with the server
var ws = new WebSocket('ws://' + window.location.host + window.location.pathname + 'ws');
var ws = new WebSocket('wss://' + window.location.host + window.location.pathname + 'ws');
// #region speech recognition initialization
var recognition = new SpeechRecognition();
@ -102,7 +108,7 @@ recognition.lang = 'de-DE';
recognition.maxAlternatives = 1;
recognition.continuous = true;
var answerQuery = '';
var skipRecording = false;
var proceedWithRecording = false;
// #endregion
// #region speech synthesis initialization
@ -125,23 +131,11 @@ window.speechSynthesis.onvoiceschanged = function () {
};
speechsynth.onend = function (event) {
switch (question) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
if (!skipRecording) {
if (proceedWithRecording) {
recognition.start();
console.log('reocgnition started. Question: ' + question);
}
skipRecording = false;
proceedWithRecording = false;
diagnosticPara.textContent = '';
console.log('global speech end');
};
@ -156,7 +150,6 @@ ws.onopen = function () {
ws.onmessage = function (payload) {
var dialogflowResult = JSON.parse(payload.data);
checkIntent(dialogflowResult);
// document.querySelector('h1').innerHTML = dialogflowResult.intent.displayName;
};
// #endregion
@ -165,48 +158,52 @@ function checkIntent (result) {
switch (result.intent.displayName) {
case QUIT_INTENT:
state = 'quit';
if (timerId !== undefined) {
clearTimeout(timerId);
}
skipRecording = true;
stopRecognitionFallback();
speak('Beende die Durchführung.');
break;
case WELCOME_INTENT:
state = 'detect';
proceedWithRecording = true;
speak(result.fulfillmentText);
break;
case WELCOME_FOLLOWUP_YES:
startQuestion(1);
break;
case WELCOME_FOLLOWUP_NO:
skipRecording = true;
speak('Okay, Danke fürs Benutzen.');
speak('Alles klar. Danke fürs Benutzen.');
break;
case MORE_TIME:
state = 'detect';
speak('Brauchen Sie noch etwas Zeit?');
break;
case MORE_TIME_YES:
rePrompt = true;
state = 'answer';
speak('Alles klar');
break;
case MORE_TIME_NO:
skipRecording = true;
state = 'answer';
speak('Verstanden');
/// /// /// currently disabled feature
// case MORE_TIME:
// state = 'detect';
// proceedWithRecording = true;
// speak('Brauchen Sie noch etwas Zeit?');
// break;
// case MORE_TIME_YES:
// rePrompt = true;
// state = 'answer';
// proceedWithRecording = true;
// speak('Alles klar');
// break;
// case MORE_TIME_NO:
// state = 'answer';
// speak('Verstanden');
// recognition.stop();
// ws.send(answerQuery);
// break;
// case CHOOSE_QUESTION:
// question = result.parameters.fields.num.numberValue;
// state = 'answer';
// handleQuestion();
// break;
/// /// ///
case HELP_INTENT:
stopRecognitionFallback();
recognition.stop();
ws.send(answerQuery);
break;
case CHOOSE_QUESTION:
question = result.parameters.fields.num.numberValue;
state = 'answer';
handleQuestion();
startQuestion(question);
break;
case FALLBACK_INTENT:
// if (state === 'answer') {
// handleAnswer(result.queryText)
// }
// fallback what should happen if no intent is detected
// this is handled in the processSpeech() function
break;
default:
break;
@ -217,6 +214,7 @@ function checkIntent (result) {
function startQuestion (number) {
question = number;
state = 'answer';
answerQuery = '';
questionNumDisplay.textContent = 'Question: ' + question;
handleQuestion();
}
@ -224,9 +222,7 @@ function startQuestion (number) {
function handleQuestion () {
switch (question) {
case 1:
skipRecording = true;
speak(QUESTION_ONE);
readQuestionOne();
readQuestionOne(QUESTION_ONE);
break;
case 2:
readQuestionTwo();
@ -238,11 +234,14 @@ function handleQuestion () {
readQuestionFour();
break;
case 5:
proceedWithRecording = true;
speak(QUESTION_FIVE);
break;
}
}
function readQuestionOne () {
function readQuestionOne (questionText) {
speak(questionText);
for (let i = 0; i < QUESTION_ONE_QUESTIONS.length; i++) {
let utterance = new SpeechSynthesisUtterance();
utterance.voice = voices[2];
@ -280,7 +279,6 @@ function readQuestionTwo () {
}
function readQuestionThree () {
skipRecording = true;
speak('Dankeschön, weiter geht es mit der nächsten Frage.');
let utterance = new SpeechSynthesisUtterance();
utterance.voice = voices[2];
@ -288,13 +286,13 @@ function readQuestionThree () {
utterance.rate = 0.88;
window.speechSynthesis.speak(utterance);
utterance.onend = function (event) {
proceedWithRecording = true;
speak(QUESTION_THREE_QUESTIONS_PT1[questionThreeCount]);
};
}
function readQuestionFour () {
// skipRecording = true;
// speak(QUESTION_FOUR);
speak(QUESTION_FOUR);
for (let i = 0; i < QUESTION_FOUR_PT2.length; i++) {
let utterance = new SpeechSynthesisUtterance();
utterance.voice = voices[2];
@ -302,7 +300,7 @@ function readQuestionFour () {
utterance.text = QUESTION_FOUR_PT2[i];
window.speechSynthesis.speak(utterance);
}
// speak('Sie haben hierfür 1 Minute Zeit.');
speak('Sie haben hierfür 1 Minute Zeit.');
}
function handleAnswer (query) {
@ -317,37 +315,33 @@ function handleAnswer (query) {
handleAnswerToThirdQuestion(query);
break;
case 4:
// not possible with the current state of web speech api
// handleAnswerToFourthQuestion(query);
break;
case 5:
handleAnswerToFifthQuestion(query);
break;
}
}
function handleAnswerToFirstQuestion (answer) {
var tokens = answer.split(new RegExp(separators.join('|'), 'g'));
questionPoints[question] += calculatePoints(tokens, QUESTION_ONE_ANSWERS);
deconstructAndCalculateAnswer(answer);
if (partTwo) {
partTwo = false;
console.log('question 1 points: ' + questionPoints[question]);
skipRecording = true;
speak('Vielen Dank, nun geht es weiter mit der nächsten Frage');
startQuestion(2);
// state = 'detect'
} else {
rePrompt = false;
skipRecording = true;
// currently disabled feature
// rePrompt = false;
speak(QUESTION_ONE_PT2);
readQuestionOne(QUESTION_ONE);
readQuestionOne(QUESTION_ONE_PT2);
partTwo = true;
}
}
function handleAnswerToSecondQuestion (answer) {
var tokens = answer.split(new RegExp(separators.join('|'), 'g'));
questionPoints[question] = calculatePoints(tokens, QUESTION_TWO_ANSWERS);
console.log('question 2 points: ' + questionPoints[question]);
deconstructAndCalculateAnswer(answer);
startQuestion(3);
// state = 'detect'
}
function handleAnswerToThirdQuestion (query) {
@ -376,41 +370,58 @@ function handleAnswerToThirdQuestion (query) {
if (strike === 2 || questionThreeCount === 5) {
speechsynth.rate = 0.88;
console.log('question 3 points: ' + questionPoints[question]);
skipRecording = true;
speak('weiter geht es mit der Nächsten Frage');
startQuestion(4);
// Question 4 is not possible to take, because of technical limitation
// startQuestion(4);
startQuestion(5);
return;
}
proceedWithRecording = true;
speak(questionArray[questionThreeCount]);
console.log('count: ' + questionThreeCount + ', strike: ' + strike + ', points: ' + questionPoints[question]);
}
// not possible with the current state of web speech api
// function handleAnswerToFourthQuestion (answer) {
// // In order to evaluate the answer, the recognizer has to distinguish between the number 77 spoken "seventy seven" and 7-7 spoken "seven seven", which it doesnt
// startQuestion(5);
// }
function handleAnswerToFifthQuestion (answer) {
deconstructAndCalculateAnswer(answer);
speak(FINISH);
}
// #endregion
// NOT SUPPORTED
// function recognizeSpeech () {
// if (state === 'answer') {
// var arr;
// switch (question) {
// case 1:
// arr = QUESTION_ONE_QUESTIONS;
// break;
// case 2:
// // arr = QUESTION_TWO_QUESTIONS;
// arr = QUESTION_TWO_QUESTIONS;
// break;
// case 3:
// arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// break;
// case 4:
// arr = ['eins', 'zwei', 'drei', 'vier', 'fünf', 'sechs', 'sieben', 'acht', 'neun'];
// break;
// case 5:
// break;
// }
// // var grammar = '#JSGF V1.0; grammar colors; public <color> = ' + arr.join(' | ') + ' ;';
// // var speechRecognitionList = new SpeechGrammarList();
// // speechRecognitionList.addFromString(grammar, 1);
// // recognition.grammars = speechRecognitionList;
// var grammar = '#JSGF V1.0; grammar arr; public <arr> = ' + arr.join(' | ') + ' ;';
// var speechRecognitionList = new SpeechGrammarList();
// speechRecognitionList.addFromString(grammar, 1);
// recognition.grammars = speechRecognitionList;
// recognition.start();
// }
// #region speech recognition event
@ -506,18 +517,16 @@ function processSpeech (speechResult) {
}
if (state === 'answer') {
if (timerId != undefined) {
clearTimeout(timerId);
}
stopRecognitionFallback();
answerQuery += speechResult;
timerId = window.setTimeout(
function () {
// currently disabled
// if (!rePrompt) {
// ws.send('ich brauche noch etwas Zeit')
// } else {
console.log('recording end. Evaluate: ' + answerQuery);
handleAnswer(answerQuery);
answerQuery = '';
diagnosticPara.textContent = '';
// }
recognition.stop();
@ -539,14 +548,7 @@ function startDemenzScreening () {
}
function testSpeechOut () {
readQuestionFour();
// speechsynth.text = 'test 123';
// speechsynth.volume = 1;
// speechsynth.rate = 1;
// console.log(speechsynth);
// window.speechSynthesis.speak(speechsynth);
// console.log(window.speechSynthesis);
question = 4;
}
function speak (sentence) {
@ -554,6 +556,29 @@ function speak (sentence) {
window.speechSynthesis.speak(speechsynth);
}
function stopRecognitionFallback () {
if (timerId != undefined) {
clearTimeout(timerId);
}
}
function deconstructAndCalculateAnswer (answer) {
let questionAnswers;
switch (question) {
case 1:
questionAnswers = QUESTION_ONE_ANSWERS;
break;
case 2:
questionAnswers = QUESTION_TWO_ANSWERS;
break;
case 5:
questionAnswers = QUESTION_ONE_ANSWERS;
break;
}
var tokens = answer.split(new RegExp(separators.join('|'), 'g'));
questionPoints[question] += calculatePoints(tokens, questionAnswers);
}
function calculatePoints (tokens, d) {
let points = 0;
let dict = {};

23
server/cert.pem Normal file
View File

@ -0,0 +1,23 @@
-----BEGIN CERTIFICATE-----
MIID1zCCAr8CFFQdsOqUqVpXkNcENXj66TeS6crHMA0GCSqGSIb3DQEBCwUAMIGn
MQswCQYDVQQGEwJERTEQMA4GA1UECAwHQkFWQVJJQTESMBAGA1UEBwwJTlVSRU1C
RVJHMQwwCgYDVQQKDANPSE0xDjAMBgNVBAsMBURFTUJBMTMwMQYDVQQDDCpodHRw
czovL21lZGluZi5lZmkudGgtbnVlcm5iZXJnLmRlL2RlbWVuei8xH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1b1X7dRGZigTQqBE7By38yXg4Y++BU
-----END CERTIFICATE-----

27
server/key.pem Normal file
View File

@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA56EWGyHlMUIuMn8YgSafG+a6tMq42o0lD1bYdxepXtzwrAkj
4o+3WBMzA8fhlo1giF2V20gl0lAsB4qj4BBW9RjwFjdxegRIu9bGbmHwYzUgUuGv
UJVNgfxXsIfwFh7umlh5cR2vVR1KfO8doYpXP0VfFdlIILl41G5tvKXkvgB61LE2
D8jkBwX71OLSw5qEJP3gGmvhOdxoOf56KY6tDHvPMLiTrK4W6cZXIcw0IPuHt9VT
VfApk8Amq+QWTup3szdoGfnDANZrbip+SsRneXPgGSvf6uv3+RefBrZNqCLadUjp
jRyqlCxjnvHhUtDzn/dy5DvYw2rY5ylqtgGPPQIDAQABAoIBAHr9tw47JkbMnFkl
0W4qgsu+7R64kgVCcw49D/3Uq5N6f7TpuYtAwpravvjf9rIJaduezca8/ZA6D9cF
T9TW+EV2s20ojXG7up87AOx1xr325K39tih0zs4ZnrpWxC4KyKleF0Xv/DyWrAnA
shwnqAIppL6mPD1cHoO2C0DjWZ1lAzG3pjs5ipEa6Z5csWsHJlv8i12da1Zjhr+4
GoBTyUcQFzegf/RlyJC/5yEyydoD82XDhSEfRu0eijGaktymCbXy0Oop1l7mjdbD
eswhYB8zX1Hht3GoxmSfVZGyhXxBXJO2MRWXuVFUR0HQxvGBjuzZE0B9nn2PTLr6
uYvBIQECgYEA90N9ocDuHwuO19oV6crW8By5+ItlGjrOsFp7Z4PCuj/49GyPFkEC
avcrtNxazszglgsSUX6yAiA0PoDT0SAS1vu2VOgQ6MrloI+PrWu2b0BqhTBTtPmN
AlGdu3GyLY7D2Y5y+Q6rSChbyk7h2qsUGRVl/4WAgOs8zQYtZzymm/0CgYEA79Au
keVg1iIKHmbDOv/HJpLL/Y22E5YqRnpJhWRFGUZPgP8ekYx/U6aQP1FakXCTlK7a
AwkBuijnWKfpuddb8JRWCk70eqibgLLaZPBqKs1VUJnms60MmG/IJXltrtULdORB
WKgat8GtCHgiJZwD2xNIL6vK3ZW8uWcXNtOUBEECgYB8IMF85mGj7CV1buH5T/6+
42zovPGwTT1dgs7YlGi5H0xXR/g8773K/PJQBR8BGhDUwhIcH0I1lv0jM4EiRruu
eQZ7cVSryM+l3uv930jiaLAQ1MXw4CZNt7M7WkRcYoMnQD+vkBGkU75gvWDcaW3Z
eciOkfKExZLsSeGZJfVt8QKBgFhkR4YHIfZLfadPAU7R0Dnribzfauk1REfN8yNz
nfR+8fb+Eepb+uJ+VlTAS72n3P3EpR7RRw2y3fLTkVaqhnzacAxEpeBAHXEGe3ER
HFDCdlvLHjnD4wycxwzpAQCcDij7qQpgi9iHTpv8i7mJE/K/3K+VhqS9+F3uHaRb
wAcBAoGAe58kt4AHnHa67MutpTSBiM9CFqzOOJnp1HcU3eRlVgqoe8okZeaqTSQW
hyte4cpsMLlfiVFU+xxzzSCRDAksmEewdy1/cEovdEEKeBn2UDFpVe9pix73jxnU
roPAtn8PPB7VWRpClVpkOefMM+1mj5U+xdq/mM+LNPQvALENsbE=
-----END RSA PRIVATE KEY-----

View File

@ -3,7 +3,9 @@ const dialogflow = require('dialogflow');
// server and websocket
const WebSocket = require('ws');
var fs = require('fs');
const http = require('http');
const https = require('https');
const path = require('path');
const express = require('express');
@ -20,7 +22,11 @@ app.use(express.static(__dirname + '/../client'));
app.use('/', router);
var server = http.createServer(app);
// var server = http.createServer(app);
var server = https.createServer({
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
}, app);
server.listen(process.env.port || webSocketsServerPort, function () {
console.log((new Date()) + ' Server is listening on port ' +