annotate eventLogger.mm @ 16:fb2ef16dd013

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