diff eventLogger.mm @ 25:f42a00e3f22d

Logs condensed slightly. Questionnaire button enable. double precision location!!!
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 01 Feb 2013 17:39:19 +0000
parents a4908ad8c78e
children ae4d2c3ce5e0
line wrap: on
line diff
--- a/eventLogger.mm	Fri Feb 01 11:16:56 2013 +0000
+++ b/eventLogger.mm	Fri Feb 01 17:39:19 2013 +0000
@@ -9,15 +9,13 @@
 //---------------------------------------------------------------------------
 #include "eventLogger.h"
 
-
 EventLogger eventLogger;
-extern UsernameAlertViewController *usernameAlertViewController;
 
 //---------------------------------------------------------------------------
 EventLogger::EventLogger(){
     //QuestionnaireViewController * questionnaireViewController;
 
-    
+    consentGiven = true; // unless told otherwise by introView
     loggingEnabled = true;
     serverConnectionOK = false;
     questionnaireCompleted = false;
@@ -27,7 +25,7 @@
     ofxiPhoneDeviceType iOSdeviceType = ofxiPhoneGetDeviceType();
     cout << "Device: " << iOSdeviceType << '\n';
     
-    nextUploadQty = 5000; // amount of data uploaded is always more than 5000 events
+    nextUploadQty = UPLOAD_CHUNK_SIZE; // amount of data uploaded is always more than UPLOAD_CHUNK_SIZE events
 }
 //---------------------------------------------------------------------------
 // draw() - show path of last N scroll events  - can be scrubbed along?
@@ -40,7 +38,7 @@
     
     testConnection();
 
-
+    logEvent(APP_STARTED);
 }
 //---------------------------------------------------------------------------
 void EventLogger::questionnaireAnswersObtained(vector<int> answers){
@@ -130,7 +128,6 @@
     stringstream fileText;
     string line;
     if(!theFile){
-        cout<<"no event log file - first APP open\n";
         
         firstEverAppOpen();
         
@@ -159,7 +156,8 @@
     // now put user deets into variables
     userName = root["userName"].asString();
     deviceID = root["deviceID"].asLargestInt();
-    totalInteractionTime = root["totalInteractionTime"].asLargestInt();
+    nextUploadNumber = root["uploadNumber"].asInt();
+    savedInteractionTime = root["savedInteractionTime"].asLargestInt();
     questionnaireCompleted = root["questionnaireCompleted"].asBool();
     questionnaireUploaded = root["questionnaireUploaded"].asBool();
     
@@ -186,14 +184,7 @@
         uploadEventLog();
     }
     // TODO if the total interaction time is greater than a certain amount && no questions answered - questionnaire time!
-    cout << "Total interaction time: " << totalInteractionTime << '\n';
-    
-    if(totalInteractionTime > 123){
-        //testApp->showQuestionnaire();
-        
-    }
-    // is there logged stuff that hasn't been uploaded yet? - handled automatically
-
+    cout << "Total interaction time: " << savedInteractionTime << '\n';
 
     
 }
@@ -203,16 +194,19 @@
 
 bool EventLogger::uploadEventLog(){
     // show indicator
-    cout << "^^^^^^^^  UPLOADING: " << theEvents.size() << " EVENTS ^^^^^^^^ .\n";
+    
+    cout << "^^^^^^^^  ATTEMPTING TO UPLOAD " << theEvents.size() << " EVENTS ^^^^^^^^ .\n";
     
     bool logUploaded = sendToServer("eventlog", logsToJson());
     if(!logUploaded){
         // try later
-        nextUploadQty += 5000;
+        nextUploadQty += UPLOAD_CHUNK_SIZE;
     }else{
         
         // if success - clear memory
         theEvents.clear();
+        cout << "UPLOAD SUCCESS\n";
+        nextUploadNumber++;
     }
     return logUploaded;
     
@@ -223,16 +217,15 @@
 //---------------------------------------------------------------------------
 
 void EventLogger::firstEverAppOpen(){
+    cout<<"no event log file - first APP open\n";
+    nextUploadNumber = 0;
     deviceID = ofGetSystemTimeMicros();
-    totalInteractionTime = 0;
+    savedInteractionTime = 0;
     questionnaireCompleted = false;
     questionnaireUploaded = false;
     
     ((testApp *)ofGetAppPtr())->showIntro();
-    
-    
-    //[usernameAlertViewController showUserNamePrompt];
-    // then we get userName via setUsername, called from button delegate
+    consentGiven = false;
     
 }
 //---------------------------------------------------------------------------
@@ -282,8 +275,12 @@
         case CHANGE_SLIDER:
             theEvents.push_back(lEvent(evtType,sliderVal , 0.0 , sliderID));
             break;
+        case SWAP_VIEW:
+            theEvents.push_back(lEvent(evtType,0.0 , 0.0 , sliderID)); // slider ID is which view
+            break;
         default:
-            // Code
+            // default is just an event type with no values
+            theEvents.push_back(lEvent(evtType));
             break;
     }
     if(theEvents.size() > nextUploadQty){
@@ -291,23 +288,35 @@
         uploadEventLog();
     }
     //sessionTime = (ofGetSystemTime() - sessionStartTime);
-    
-    
+    totalInteractionTime = savedInteractionTime + (ofGetSystemTime() - sessionStartTime);
+    if(totalInteractionTime > QUESTIONNAIRE_ENABLE_TIME){
+        TopButtonViewController *tbvc = ((testApp *)ofGetAppPtr())->topButtonViewController;
+        [tbvc enableQuestionButton];
+    }
 }
 
 //---------------------------------------------------------------------------
 
 void EventLogger::exitAndSave(){
-    totalInteractionTime = totalInteractionTime + (ofGetSystemTime() - sessionStartTime);
+    
+    if(!consentGiven){
+        logEvent(CONSENT_DENIED);
+        Json::Value jlogs = logsToJson();
+        // try to upload TODO (no - might hang and prevent exit???)
+        uploadEventLog();
+        return;
+    }
+    logEvent(APP_EXITED);
+    savedInteractionTime = savedInteractionTime + (ofGetSystemTime() - sessionStartTime);
     // save user details
     string fname = ofxiPhoneGetDocumentsDirectory() + EVENT_LOG_FILENAME;
 
-    Json::Value jlogs = logsToJson();
-    // try to upload
+    // try to upload TODO (no - might hang and prevent exit???)
     uploadEventLog();
     
     // write to file
-    
+    // json without the logs that were uploaded!
+    Json::Value jlogs = logsToJson();
     ofFile logFile(fname,ofFile::WriteOnly);
     logFile << jlogs;
     
@@ -323,8 +332,9 @@
     root["programVersion"] = PROGRAM_VERSION;
     root["userName"] = userName;
     root["deviceID"] = deviceID;
+    root["uploadNumber"] = nextUploadNumber;
     root["iOSdeviceType"] = iOSdeviceType;
-    root["totalInteractionTime"] = totalInteractionTime;
+    root["savedInteractionTime"] = savedInteractionTime;
     root["questionnaireCompleted"] = questionnaireCompleted;
     root["questionnaireUploaded"] = questionnaireUploaded;
     if(questionnaireCompleted && !questionnaireUploaded){
@@ -336,7 +346,7 @@
         root["events"][i] = (*eventIter).eventToJson();
         i++;
     }
-    
+    root["numEventsHere"] = i;
     return root;
 }