annotate eventLogger.mm @ 15:e45c3e631d20

View fiddling
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Thu, 17 Jan 2013 13:01:19 +0000
parents 6a9191f5b269
children fb2ef16dd013
rev   line source
rt300@0 1 //
rt300@0 2 // eventLogger.mm
rt300@0 3 // oscSenderExample
rt300@0 4 //
rt300@0 5 // Created by Robert Tubb on 05/11/2012.
rt300@0 6 //
rt300@0 7 //
rt300@4 8
rt300@8 9 //---------------------------------------------------------------------------
rt300@0 10 #include "eventLogger.h"
rt300@1 11
rt300@8 12
rt300@1 13 EventLogger eventLogger;
rt300@8 14 extern IViewController *iViewController;
rt300@1 15
rt300@8 16 //---------------------------------------------------------------------------
rt300@1 17 EventLogger::EventLogger(){
rt300@14 18 //QuestionnaireViewController * questionnaireViewController;
rt300@14 19
rt300@14 20 questionnaireViewController = [[QuestionnaireViewController alloc] initWithNibName:@"QuestionnaireViewController" bundle:nil];
rt300@14 21
rt300@14 22
rt300@8 23
rt300@14 24
rt300@8 25 loggingEnabled = true;
rt300@9 26 internetConnectionOK = false;
rt300@9 27 nextUploadQty = 5000; // amount of data uploaded is always more than 5000 events
rt300@8 28 }
rt300@8 29 //---------------------------------------------------------------------------
rt300@8 30 void EventLogger::init(){
rt300@4 31
rt300@8 32 readJsonToLog(ofxiPhoneGetDocumentsDirectory() + EVENT_LOG_FILENAME);
rt300@8 33 sessionStartTime = ofGetSystemTime();
rt300@9 34
rt300@9 35 testConnection();
rt300@7 36
rt300@14 37 [ofxiPhoneGetGLParentView() addSubview:questionnaireViewController.view];
rt300@8 38 }
rt300@8 39 //---------------------------------------------------------------------------
rt300@9 40 bool EventLogger::testConnection(){
rt300@14 41 string url = "http://127.0.0.1:8080/testservice/testConnection?testtext={%22test%22=%22sometext%22}";
rt300@9 42 bool success = false;
rt300@9 43 ofURLFileLoader fileLoader;
rt300@9 44 ofHttpResponse resp;
rt300@9 45 resp = fileLoader.get(url);
rt300@9 46
rt300@9 47 cout << "HTTP STATUS " << resp.status << "\n";
rt300@9 48 cout << "HTTP ERROR " << resp.error << "\n";
rt300@9 49 cout << "HTTP DATA " << resp.data << "\n";
rt300@9 50
rt300@9 51 if (resp.status == 0){
rt300@9 52 success = true;
rt300@9 53 internetConnectionOK = true;
rt300@9 54 }else{
rt300@9 55 success = false;
rt300@9 56 internetConnectionOK = false;
rt300@9 57 // SHOW AN ALERT TO USER?
rt300@9 58 }
rt300@9 59 return success;
rt300@9 60 }
rt300@9 61 //---------------------------------------------------------------------------
rt300@8 62 void EventLogger::readJsonToLog(const string &jsonFile){
rt300@8 63 Json::Value root;
rt300@8 64 Json::Reader reader;
rt300@5 65
rt300@5 66
rt300@8 67 ifstream theFile(jsonFile.c_str());
rt300@8 68 stringstream fileText;
rt300@8 69 string line;
rt300@8 70 if(!theFile){
rt300@8 71 cout<<"no event log file - first APP open\n";
rt300@8 72
rt300@8 73 firstEverAppOpen();
rt300@8 74
rt300@8 75 return;
rt300@8 76 }else{
rt300@8 77
rt300@8 78 while(theFile){
rt300@8 79 theFile >> line;
rt300@9 80 // cout << line; // lots!!!!
rt300@8 81 fileText << line;
rt300@8 82
rt300@8 83 }
rt300@8 84
rt300@8 85 theFile.close();
rt300@8 86 }
rt300@8 87
rt300@9 88 cout << "size of log JSON string:" << fileText.str().length() << "BYTES \n";
rt300@9 89
rt300@8 90 bool parsingSuccessful = reader.parse( fileText.str(), root );
rt300@8 91
rt300@8 92 if ( !parsingSuccessful )
rt300@8 93 {
rt300@8 94 // report to the user the failure and their locations in the document.
rt300@8 95 std::cout << "Failed to parse preset JSON\n"
rt300@8 96 << reader.getFormattedErrorMessages();
rt300@8 97 return;
rt300@8 98 }
rt300@8 99
rt300@8 100 // now put user deets into variables
rt300@8 101 userName = root["userName"].asString();
rt300@8 102 deviceID = root["deviceID"].asLargestInt();
rt300@8 103 totalInteractionTime = root["totalInteractionTime"].asLargestInt();
rt300@8 104
rt300@8 105 // check for unuploaded evts
rt300@8 106 const Json::Value jlogs = root["events"];
rt300@8 107
rt300@8 108 for ( int index = 0; index < jlogs.size(); ++index ) theEvents.push_back(lEvent(jlogs[index]));
rt300@9 109 if(theEvents.size() > nextUploadQty){
rt300@8 110 //try to upload
rt300@8 111 attemptUpload();
rt300@8 112 }
rt300@8 113 // TODO if the total interaction time is greater than a certain amount && no questions answered - questionnaire time!
rt300@14 114 cout << "Total interaction time: " << totalInteractionTime << '\n';
rt300@8 115
rt300@14 116 if(totalInteractionTime > 0){
rt300@14 117 // questionnaireViewController.show;
rt300@14 118 [questionnaireViewController show:(id)this];
rt300@14 119
rt300@14 120 //IF THE VIEW IS HIDDEN LETS BRING IT BACK!
rt300@14 121 if( questionnaireViewController.view.hidden ){
rt300@14 122 questionnaireViewController.view.hidden = NO;
rt300@14 123 }
rt300@14 124
rt300@14 125 }
rt300@8 126 // is there logged stuff that hasn't been uploaded yet?
rt300@8 127
rt300@8 128 // don't actually need to load old ones unless uploading? or saving...
rt300@8 129 //while(eventLogFile >> nextLine){
rt300@8 130
rt300@8 131 //}
rt300@9 132
rt300@8 133
rt300@9 134 }
rt300@9 135
rt300@9 136
rt300@9 137 //---------------------------------------------------------------------------
rt300@9 138
rt300@9 139 bool EventLogger::attemptUpload(){
rt300@9 140
rt300@9 141 // do simple check of internet connection ?
rt300@9 142 // if not connected return
rt300@9 143 if(!internetConnectionOK){
rt300@9 144 return false;
rt300@9 145 }
rt300@9 146 // show indicator
rt300@9 147 cout << "UPLOADING: " << theEvents.size() << " logs.\n";
rt300@9 148
rt300@9 149 // if numlogs > 500000 show alert...?
rt300@9 150
rt300@9 151 string url = "http://127.0.0.1:8080/testservice/eventlog?jsontext=";
rt300@9 152 url.append(logsToJson().toStyledString());
rt300@9 153
rt300@9 154
rt300@9 155 ofURLFileLoader fileLoader;
rt300@9 156 ofHttpResponse resp;
rt300@9 157 resp = fileLoader.get(url); // Does this sit and wait and arse up interaction?
rt300@9 158
rt300@9 159 cout << "HTTP STATUS " << resp.status << "\n";
rt300@9 160 cout << "HTTP ERROR " << resp.error << "\n";
rt300@9 161 cout << "HTTP DATA " << resp.data << "\n";
rt300@9 162
rt300@9 163 bool uploaded = false;
rt300@9 164 if (resp.status == 0) uploaded = true; // should do a check on server, and report in data ?
rt300@9 165 if(!uploaded){
rt300@9 166 // try later
rt300@9 167 nextUploadQty += 5000;
rt300@9 168 }else{
rt300@9 169
rt300@9 170 // if success - clear memory
rt300@9 171 theEvents.clear();
rt300@9 172 }
rt300@9 173 return uploaded;
rt300@8 174
rt300@1 175 }
rt300@1 176
rt300@8 177 //----------------------------------------------------------------------------
rt300@9 178 //void EventLogger::deleteLogFile(){
rt300@8 179
rt300@8 180 //---------------------------------------------------------------------------
rt300@8 181
rt300@8 182 void EventLogger::firstEverAppOpen(){
rt300@8 183 deviceID = ofGetSystemTimeMicros();
rt300@8 184 totalInteractionTime = 0;
rt300@8 185
rt300@9 186 [iViewController showUserNamePrompt];
rt300@9 187 // then we get userName via setUsername, called from button delegate
rt300@8 188
rt300@8 189 }
rt300@8 190 //---------------------------------------------------------------------------
rt300@8 191 // called from alertView OK in iViewController
rt300@8 192 void EventLogger::setUsername(const char *u){
rt300@8 193 userName = u;
rt300@7 194
rt300@7 195 }
rt300@8 196
rt300@8 197 //---------------------------------------------------------------------------
rt300@8 198 // log zoom event
rt300@5 199 void EventLogger::logEvent(const leventType& evtType,const TwoVector& centre, const double& scale, const int& sliderID, const double& sliderVal){
rt300@3 200 //cout << "log: " << evtType << "\n";
rt300@1 201
rt300@1 202 // scroll has 2 double coords
rt300@1 203 // zoom has 1 double scale
rt300@1 204 // save preset has 2 coords
rt300@1 205 // switch view has view type
rt300@9 206 // slider change has int slider index and 1 float value
rt300@1 207
rt300@4 208 // get time for key index
rt300@4 209
rt300@5 210 // thinFactor
rt300@5 211 if(!loggingEnabled) return;
rt300@4 212 switch ( evtType ) {
rt300@4 213 case SAVE_PRESET:
rt300@5 214 theEvents.push_back(lEvent(evtType,centre.x,centre.y));
rt300@4 215 // Code
rt300@4 216 break;
rt300@4 217 case SAVE_DESET:
rt300@5 218 theEvents.push_back(lEvent(evtType,centre.x,centre.y));
rt300@4 219 break;
rt300@4 220 case SCROLL:
rt300@5 221 theEvents.push_back(lEvent(evtType,centre.x,centre.y));
rt300@5 222 break;
rt300@5 223 case SCROLL_STOPPED:
rt300@5 224 theEvents.push_back(lEvent(evtType,centre.x,centre.y));
rt300@4 225 break;
rt300@4 226 case ZOOM:
rt300@5 227 theEvents.push_back(lEvent(evtType,scale));
rt300@4 228 break;
rt300@4 229 case CHANGE_SLIDER:
rt300@5 230 theEvents.push_back(lEvent(evtType,sliderVal , 0.0 , sliderID));
rt300@4 231 break;
rt300@4 232 default:
rt300@4 233 // Code
rt300@4 234 break;
rt300@1 235 }
rt300@8 236 if(theEvents.size() > nextUploadQty){
rt300@7 237 //try to upload
rt300@7 238 attemptUpload();
rt300@7 239 }
rt300@9 240 //sessionTime = (ofGetSystemTime() - sessionStartTime);
rt300@7 241
rt300@1 242
rt300@1 243 }
rt300@8 244
rt300@8 245 //---------------------------------------------------------------------------
rt300@1 246 void EventLogger::sendHttp(){
rt300@1 247
rt300@5 248 string url = "http://www.rootnot.co.uk/cgi-bin/zoomlogs.cgi";
rt300@1 249 ofURLFileLoader fileLoader;
rt300@1 250 ofHttpResponse resp;
rt300@1 251 resp = fileLoader.get(url);
rt300@1 252 cout << "HTTP STATUS " << resp.status << "\n";
rt300@1 253 cout << "HTTP ERROR " << resp.error << "\n";
rt300@1 254 cout << "HTTP DATA " << resp.data << "\n";
rt300@7 255 }
rt300@7 256
rt300@7 257
rt300@7 258
rt300@8 259 //---------------------------------------------------------------------------
rt300@7 260
rt300@7 261 void EventLogger::exitAndSave(){
rt300@8 262 totalInteractionTime = totalInteractionTime + (ofGetSystemTime() - sessionStartTime);
rt300@7 263 // save user details
rt300@8 264 string fname = ofxiPhoneGetDocumentsDirectory() + EVENT_LOG_FILENAME;
rt300@8 265
rt300@8 266 Json::Value jlogs = logsToJson();
rt300@8 267 // try to upload
rt300@8 268 bool uploaded = attemptUpload();
rt300@7 269
rt300@8 270 // write to file
rt300@8 271
rt300@8 272 ofFile logFile(fname,ofFile::WriteOnly);
rt300@8 273 logFile << jlogs;
rt300@8 274
rt300@8 275 }
rt300@8 276 //---------------------------------------------------------------------------
rt300@8 277
rt300@8 278 Json::Value EventLogger::logsToJson(){
rt300@8 279 // put all logged events into Json formatted string
rt300@8 280 Json::Value root;
rt300@8 281
rt300@8 282 vector<lEvent>::iterator eventIter;
rt300@8 283
rt300@8 284 root["userName"] = userName;
rt300@8 285 root["deviceID"] = deviceID;
rt300@8 286 root["totalInteractionTime"] = totalInteractionTime;
rt300@8 287
rt300@8 288 int i = 0;
rt300@8 289 for(eventIter = theEvents.begin(); eventIter < theEvents.end(); eventIter++){
rt300@8 290 root["events"][i] = (*eventIter).eventToJson();
rt300@8 291 i++;
rt300@7 292 }
rt300@8 293
rt300@8 294 return root;
rt300@8 295 }
rt300@8 296 //---------------------------------------------------------------------------
rt300@8 297 //---------------------------------------------------------------------------
rt300@8 298 //---------------------------------------------------------------------------