From e693dadb7ad258bdeecde2303d4ac0695808add3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergiusz=20Baza=C5=84ski?= Date: Tue, 1 May 2012 19:02:53 +0000 Subject: [PATCH] Caching IVONA. --- terminal/main.c | 9 +++---- terminal/tts.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++--- terminal/tts.h | 1 + 3 files changed, 67 insertions(+), 7 deletions(-) diff --git a/terminal/main.c b/terminal/main.c index dc0e7ba..dbbbb84 100644 --- a/terminal/main.c +++ b/terminal/main.c @@ -25,7 +25,7 @@ int main(int argc, char **argv) } printf("Barcode initialization successful\n"); - tts_speak("Refryżyrator gotowy do pracy."); + tts_speak_cached("Refryżyrator gotowy do pracy."); char MifareID[128]; char Barcode[14]; @@ -43,14 +43,14 @@ int main(int argc, char **argv) { if (LDAPResult == NO_SUCH_CARD) { - tts_speak("Nieznana karta."); + tts_speak_cached("Nieznana karta."); printf("NFC: Unknown card.\n"); continue; } else { - tts_speak("Błąd eldapa."); + tts_speak_cached("Błąd eldapa."); printf("LDAP: Unknown error %i\n", LDAPResult); continue; } @@ -60,11 +60,12 @@ int main(int argc, char **argv) ldap_uid_from_dn(DN, UID); printf("LDAP uid: %s\n", UID); - tts_speak("Zeskanuj produkt.\n"); + tts_speak_cached("Zeskanuj produkt.\n"); int BarcodeResult = barcode_read(Barcode); if (BarcodeResult == 0) { + tts_speak_cached("bip"); printf("Scanned barcode %s\n", Barcode); } else diff --git a/terminal/tts.c b/terminal/tts.c index 52d36e7..88d9952 100644 --- a/terminal/tts.c +++ b/terminal/tts.c @@ -2,15 +2,73 @@ #include #include #include +#include #include "base64.h" static char *g_IvonaURL = "http://www.ivona.com/online/fileSentence.php?l=en&e=0&v=1&t=%s"; -size_t tts_write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) +void _tts_digest(char *Text, char *DigestOut) { - int written = fwrite(ptr, size, nmemb, stream); - return written; + unsigned char Digest[32]; + + SHA256_CTX SHA; + memset(Digest, 0, 32); + SHA256_Init(&SHA); + SHA256_Update(&SHA, "hf-tts", 6); + SHA256_Update(&SHA, Text, strlen(Text)); + SHA256_Final(Digest, &SHA); + + for (int i = 0; i < 32; i++) + sprintf(DigestOut + 2 * i, "%02x", Digest[i]); + + DigestOut[64] = 0; +} + +void tts_speak_cached(char *Text) +{ + char CachePath[128]; + strcpy(CachePath, "/tmp/hftts-"); + _tts_digest(Text, CachePath + 11); + CachePath[11 + 64] = 0; + + int B64OutputLength; + char *B64Output = (char *)malloc(strlen(Text) * 2); + bin_to_b64(B64Output, Text, strlen(Text)); + + char *URL = (char *)malloc(strlen(B64Output) + strlen(g_IvonaURL)); + sprintf(URL, g_IvonaURL, B64Output); + + FILE *File = fopen(CachePath, "r"); + if (File) + { + printf("Cache hit on \"%s\" [%s]\n", Text, CachePath); + fclose(File); + } + else + { + printf("Cache miss on \"%s\" [%s]\n", Text, CachePath); + char *CommandLine = malloc(256); + sprintf(CommandLine, "curl -L -s -A Mozilla \"%s\" > %s", URL, CachePath); + printf("Executing %s...\n", CommandLine); + system(CommandLine); + + free(CommandLine); + } + + if (!fork()) + { + char *CommandLineChild = malloc(strlen(URL) + 100); + sprintf(CommandLineChild, "mpg123 -2 -q %s", CachePath); + printf("Executing %s...\n", CommandLineChild); + system(CommandLineChild); + free(CommandLineChild); + printf("Child slave exiting.\n"); + exit(0); + } + + free((void *)URL); + free((void *)B64Output); } void tts_speak(char *Text) diff --git a/terminal/tts.h b/terminal/tts.h index 6cdcac0..27d4e79 100644 --- a/terminal/tts.h +++ b/terminal/tts.h @@ -2,5 +2,6 @@ #define __TTS_H__ void tts_speak(char *Text); +void tts_speak_cached(char *Text); #endif