Mercurial > hg > soniczoomios
diff eventLogger.mm @ 8:e2c6cfe8c6b7
JSON logs and presets.
author | Robert Tubb <rt300@eecs.qmul.ac.uk> |
---|---|
date | Thu, 10 Jan 2013 18:24:26 +0000 |
parents | 845ea04f8e33 |
children | 346807b47860 |
line wrap: on
line diff
--- a/eventLogger.mm Thu Dec 06 18:26:51 2012 +0000 +++ b/eventLogger.mm Thu Jan 10 18:24:26 2013 +0000 @@ -6,25 +6,111 @@ // // - +//--------------------------------------------------------------------------- #include "eventLogger.h" + EventLogger eventLogger; +extern IViewController *iViewController; +//--------------------------------------------------------------------------- EventLogger::EventLogger(){ + + loggingEnabled = true; + +} +//--------------------------------------------------------------------------- +void EventLogger::init(){ - // if first time opened request a user ID from the server... use time? - loggingEnabled = true; + readJsonToLog(ofxiPhoneGetDocumentsDirectory() + EVENT_LOG_FILENAME); + sessionStartTime = ofGetSystemTime(); +} +//--------------------------------------------------------------------------- +void EventLogger::readJsonToLog(const string &jsonFile){ + Json::Value root; + Json::Reader reader; + ifstream theFile(jsonFile.c_str()); + stringstream fileText; + string line; + if(!theFile){ + cout<<"no event log file - first APP open\n"; + + firstEverAppOpen(); + + return; + }else{ + + while(theFile){ + theFile >> line; + cout << line; + fileText << line; + + } + + theFile.close(); + } + + bool parsingSuccessful = reader.parse( fileText.str(), root ); + + if ( !parsingSuccessful ) + { + // report to the user the failure and their locations in the document. + std::cout << "Failed to parse preset JSON\n" + << reader.getFormattedErrorMessages(); + return; + } + + // now put user deets into variables + userName = root["userName"].asString(); + deviceID = root["deviceID"].asLargestInt(); + totalInteractionTime = root["totalInteractionTime"].asLargestInt(); + + // check for unuploaded evts + const Json::Value jlogs = root["events"]; + + for ( int index = 0; index < jlogs.size(); ++index ) theEvents.push_back(lEvent(jlogs[index])); + if(theEvents.size() > 5000){ + //try to upload + attemptUpload(); + } + // TODO if the total interaction time is greater than a certain amount && no questions answered - questionnaire time! + + // is there logged stuff that hasn't been uploaded yet? + + // don't actually need to load old ones unless uploading? or saving... + //while(eventLogFile >> nextLine){ + + //} + + //printAll(); + } -void EventLogger::init(){ - checkLogFile(); - sessionStartTime = ofGetSystemTime(); +//---------------------------------------------------------------------------- + +//--------------------------------------------------------------------------- + +void EventLogger::firstEverAppOpen(){ + deviceID = ofGetSystemTimeMicros(); + + [iViewController showUserNamePrompt:23]; + + totalInteractionTime = 0; + + +} +//--------------------------------------------------------------------------- +// called from alertView OK in iViewController +void EventLogger::setUsername(const char *u){ + userName = u; } + +//--------------------------------------------------------------------------- +// log zoom event void EventLogger::logEvent(const leventType& evtType,const TwoVector& centre, const double& scale, const int& sliderID, const double& sliderVal){ //cout << "log: " << evtType << "\n"; @@ -62,7 +148,7 @@ // Code break; } - if(theEvents.size() > 5000){ + if(theEvents.size() > nextUploadQty){ //try to upload attemptUpload(); } @@ -70,6 +156,8 @@ } +//--------------------------------------------------------------------------- +// log slider event void EventLogger::logEvent(const leventType& evtType,const int& sliderID, const double& sliderVal){ if(!loggingEnabled) return; // sliderThinFactor @@ -81,19 +169,34 @@ // Code break; } - if(theEvents.size() > 5000){ + if(theEvents.size() > nextUploadQty){ //try to upload - attemptUpload(); + bool uploaded = attemptUpload(); + } } -void EventLogger::attemptUpload(){ +//--------------------------------------------------------------------------- + +bool EventLogger::attemptUpload(){ + bool uploaded; - //if(fail){ + // do simple check of internet connection ? + // if not connected return + + // if numlogs > 500000 show alert... + + if(!uploaded){ // try later - //} + nextUploadQty += 5000; + }else{ - // if success - clear memory + // if success - clear memory + theEvents.clear(); + } + return uploaded; + } +//--------------------------------------------------------------------------- void EventLogger::sendHttp(){ string url = "http://www.rootnot.co.uk/cgi-bin/zoomlogs.cgi"; @@ -105,68 +208,46 @@ cout << "HTTP DATA " << resp.data << "\n"; } -void EventLogger::checkLogFile(){ - string fname = ofxiPhoneGetDocumentsDirectory() + "eventlog.dat"; - ifstream eventLogFile(fname.c_str()); - // have we opened the app before? if so get device ID and userName - //string firstLine, nextLine; - if(!eventLogFile){ - cout<<"no event log file - first APP open\n"; - - firstEverAppOpen(); - - return; - } - eventLogFile >> deviceID; - eventLogFile >> userName; - eventLogFile >> totalInteractionTime; - - cout << "DeviceID" << deviceID << '\n'; - cout << "userName" << userName << '\n'; - cout << "totalInteractionTime" << totalInteractionTime << '\n'; - - - // is there logged stuff that hasn't been uploaded yet? - - // don't actually need to load old ones unless uploading? or saving... - //while(eventLogFile >> nextLine){ - - //} - - eventLogFile.close(); -} - -void EventLogger::firstEverAppOpen(){ - deviceID = ofGetSystemTimeMicros(); - - // somehow prompt for username - userName = "Dennis"; - totalInteractionTime = 0; -} +//--------------------------------------------------------------------------- void EventLogger::exitAndSave(){ + totalInteractionTime = totalInteractionTime + (ofGetSystemTime() - sessionStartTime); // save user details - string fname = ofxiPhoneGetDocumentsDirectory() + "eventlog.dat"; - ofstream logFile(fname.c_str()); - logFile << deviceID << '\n'; - logFile << userName << '\n'; - totalInteractionTime = totalInteractionTime + sessionTime; - logFile << totalInteractionTime << '\n'; + string fname = ofxiPhoneGetDocumentsDirectory() + EVENT_LOG_FILENAME; + + Json::Value jlogs = logsToJson(); + // try to upload + bool uploaded = attemptUpload(); - if(theEvents.size() > 0){ - attemptUpload(); + // write to file + + ofFile logFile(fname,ofFile::WriteOnly); - // if (failed upload) save logged evts to file - cout << "Saving un-uploaded logs\n"; - vector<lEvent>::iterator evtIter; - - for(evtIter = theEvents.begin(); evtIter < theEvents.end(); evtIter++){ - - logFile << *evtIter << "\n"; - } + logFile << jlogs; + +} +//--------------------------------------------------------------------------- + +Json::Value EventLogger::logsToJson(){ + // put all logged events into Json formatted string + Json::Value root; + + vector<lEvent>::iterator eventIter; + + root["userName"] = userName; + root["deviceID"] = deviceID; + root["totalInteractionTime"] = totalInteractionTime; + + int i = 0; + for(eventIter = theEvents.begin(); eventIter < theEvents.end(); eventIter++){ + root["events"][i] = (*eventIter).eventToJson(); + i++; } - logFile.close(); - -} \ No newline at end of file + + return root; +} +//--------------------------------------------------------------------------- +//--------------------------------------------------------------------------- +//---------------------------------------------------------------------------