annotate eventLogger.h @ 0:a223551fdc1f

First commit - copy from tweakathlon.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 10 Oct 2014 11:46:42 +0100
parents
children 7e0a19a538d4
rev   line source
rt300@0 1 //
rt300@0 2 // eventLogger.h
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@0 8 // This class handle everything to do with loggin user actions,
rt300@0 9 // uploading logs to server, and storing locally if not uploaded
rt300@0 10
rt300@0 11 #ifndef __oscSenderExample__eventLogger__
rt300@0 12 #define __oscSenderExample__eventLogger__
rt300@0 13
rt300@0 14
rt300@0 15 #include "ofMain.h"
rt300@0 16 #include "ofxiPhone.h"
rt300@0 17 #include "2dvector.h"
rt300@0 18 #include "ofxiPhoneExtras.h"
rt300@0 19 #include <sys/time.h>
rt300@0 20 #include <iostream>
rt300@0 21 #include <string>
rt300@0 22 #include <cstring>
rt300@0 23 #include <map>
rt300@0 24 #include "2dvector.h"
rt300@0 25 #include "json.h"
rt300@0 26 #import "ServerComms.h"
rt300@0 27 #include "boost/bind.hpp"
rt300@0 28 #include "boost/function.hpp"
rt300@0 29 #include "Poco/Mutex.h"
rt300@0 30
rt300@0 31 #define EVENT_THIN_FACTOR 30
rt300@0 32 #define EVENT_LOG_FILENAME "log.json"
rt300@0 33 #define UPLOAD_CHUNK_SIZE 1000
rt300@0 34 #define APP_CREATION_TIME 0 // ignore this, pointless
rt300@0 35 #define PROGRAM_NAME "TWEAKATHLON"
rt300@0 36 #define PROGRAM_VERSION 0.2
rt300@0 37
rt300@0 38 #define SUPERVISED // this def will save files
rt300@0 39
rt300@0 40
rt300@0 41 // can add but don't change ordering - this will invalidate logs
rt300@0 42 enum leventType {APP_LOADED, // 0
rt300@0 43 INTRO_CONSENTED, // 1
rt300@0 44 APP_EXITED, // 2
rt300@0 45 HELP_PRESSED, // 3
rt300@0 46 QUESTIONNAIRE_COMPLETED, // 4
rt300@0 47
rt300@0 48 NEW_TEST, // 5
rt300@0 49 // al the set up of the new test
rt300@0 50 TARGET_PARAM_SET, // 6 setting up the target sound, param id and value
rt300@0 51 CANDIDATE_PARAM_SET, // 7 setting up the cand sound starting pos, subset of param id and value
rt300@0 52 CANDIDATE_CHANGEABLE_IDX, // 8 list of changeable indexes
rt300@0 53 CANDIDATE_MAPPING_IDS, // 9 which kind of control was mapped to which param
rt300@0 54 CONTROL_LIST, // 10 list of control types
rt300@0 55
rt300@0 56 TEST_TIMER_STARTED, // 11 we're off!!
rt300@0 57
rt300@0 58 CANDIDATE_PARAM_ADJUSTED, // 12 interactions changing params
rt300@0 59 SUBMIT_PRESSED, // 13 this is the suer saying they've found it, along with the actual answer
rt300@0 60 DISTANCE_TIME_SCORE, // 14 list of the results, now we should go back to NEW_TEST
rt300@0 61 COUNTDOWN_INITIATED, // 15 just in case
rt300@0 62 ALL_TESTS_COMPLETED, // 16 ?
rt300@0 63 TARGET_PLAYED, // 17 however this happens it might be good to know when this happens
rt300@0 64 CANDIDATE_PLAYED, // 18
rt300@0 65 START_THE_TESTS, // 19 probably superfluous
rt300@0 66 CRAP_TEST, // eliminate these coords somehow ???
rt300@0 67 EMPTY_EVENT,
rt300@0 68 SPEED_CHANGED, // 22 ms between sounds
rt300@0 69
rt300@0 70 };
rt300@0 71
rt300@0 72 //---------------------------------------------------------------------------
rt300@0 73
rt300@0 74 class lEvent{
rt300@0 75 public:
rt300@0 76 // try and make this as compact as possible.
rt300@0 77 leventType eventType;
rt300@0 78 double val1; // x coord, scale if zoom
rt300@0 79 double val2; // y coord, 0 if zoom
rt300@0 80 int sliderID; // xtra int
rt300@0 81 long long eventTime; // MILLISECONDS since ref date
rt300@0 82 vector<int> eventData; // variable length data int
rt300@0 83
rt300@0 84 // old sonic zoom style event
rt300@0 85 lEvent(leventType eType, double v1 = 0.0, double v2 = 0.0,int sID = 0){
rt300@0 86 eventType = eType;
rt300@0 87 val1 = v1;
rt300@0 88 val2 = v2;
rt300@0 89 sliderID = sID;
rt300@0 90
rt300@0 91 double timemsd = [NSDate timeIntervalSinceReferenceDate]; // MILLISECONDS, but with fractional part (
rt300@0 92 eventTime = (unsigned long long)(timemsd*1000) - APP_CREATION_TIME;
rt300@0 93
rt300@0 94 }
rt300@0 95
rt300@0 96 // new tweakathlon event
rt300@0 97 lEvent(leventType eType, vector<int> theData){
rt300@0 98 eventType = eType;
rt300@0 99 eventData = theData;
rt300@0 100
rt300@0 101 double timemsd = [NSDate timeIntervalSinceReferenceDate]; // MILLISECONDS, but with fractional part (
rt300@0 102 eventTime = (unsigned long long)(timemsd*1000) - APP_CREATION_TIME;
rt300@0 103
rt300@0 104 }
rt300@0 105
rt300@0 106 lEvent(const Json::Value &jevt){
rt300@0 107 // constructor takes "jsonToEvent" readfile function role
rt300@0 108 eventType = (leventType)jevt["eType"].asInt();
rt300@0 109 val1 = jevt["v1"].asDouble();
rt300@0 110 val2 = jevt["v2"].asDouble();
rt300@0 111 sliderID = jevt["sID"].asInt();
rt300@0 112 eventTime = jevt["eTime"].asLargestInt();
rt300@0 113
rt300@0 114 // TODO what happens if we try to read one that isn't there?
rt300@0 115
rt300@0 116 }
rt300@0 117 // new tweak event to json
rt300@0 118 Json::Value eventToJson(){
rt300@0 119 Json::Value jevt;
rt300@0 120 jevt["eType"] = eventType;
rt300@0 121 // convert vector to json array
rt300@0 122 for(auto it = eventData.begin(); it < eventData.end(); it++){
rt300@0 123 jevt["eData"].append(*it);
rt300@0 124 }
rt300@0 125 jevt["eTime"] = eventTime;
rt300@0 126 return jevt;
rt300@0 127 }
rt300@0 128 Json::Value eventToJsonOld(){
rt300@0 129 Json::Value jevt;
rt300@0 130 jevt["eType"] = eventType;
rt300@0 131 // here: should give a certain number of sig figs?
rt300@0 132 jevt["v1"] = val1;
rt300@0 133 jevt["v2"] = val2;
rt300@0 134 jevt["sID"] = sliderID;
rt300@0 135 jevt["eTime"] = eventTime;
rt300@0 136 return jevt;
rt300@0 137 }
rt300@0 138 };
rt300@0 139
rt300@0 140
rt300@0 141 //------------------------------------------
rt300@0 142
rt300@0 143 class EventLogger{
rt300@0 144 public:
rt300@0 145 Poco::Mutex _mutex;
rt300@0 146
rt300@0 147 int nextUploadNumber;
rt300@0 148 int nextUploadQty;
rt300@0 149 bool loggingEnabled;
rt300@0 150
rt300@0 151 bool logUploadInProgress;
rt300@0 152 bool serverConnectionOK;
rt300@0 153 bool consentGiven;
rt300@0 154 bool questionnaireCompleted;
rt300@0 155 bool questionnaireUploaded;
rt300@0 156
rt300@0 157 unsigned int deviceID;
rt300@0 158 unsigned int totalInteractionTime, savedInteractionTime, sessionTime, sessionStartTime;
rt300@0 159 string userName; // not unique
rt300@0 160 string questionnaireComments;
rt300@0 161 // constr
rt300@0 162 EventLogger();
rt300@0 163
rt300@0 164 // public methods:
rt300@0 165 void startLoadAll();
rt300@0 166 void exitAndSave();
rt300@0 167 void setUsername(const char *u);
rt300@0 168 string getUsername(){
rt300@0 169 return userName;
rt300@0 170 }
rt300@0 171 void newUser();
rt300@0 172 void logEvent(const leventType& evtType);
rt300@0 173 void logEvent(const leventType& evtType, const vector<int> eventData);
rt300@0 174
rt300@0 175 void questionnaireAnswersObtained(vector<int> answers, const char* userComments);
rt300@0 176 void urlResponse(ofHttpResponse & response);
rt300@0 177
rt300@0 178 void questionnaireOK();
rt300@0 179 void eventlogOK();
rt300@0 180 void testConnectionOK();
rt300@0 181 void questionnaireNotOK();
rt300@0 182 void eventlogNotOK();
rt300@0 183 void testConnectionNotOK();
rt300@0 184
rt300@0 185 void printAll();
rt300@0 186 void saveSessionToFile();
rt300@0 187
rt300@0 188 private:
rt300@0 189 void thinnedSliderEvent(lEvent newEvent);
rt300@0 190 vector<lEvent> theEvents; // all logged but not uploaded events
rt300@0 191
rt300@0 192 vector<int> questionnaireAnswers;
rt300@0 193
rt300@0 194 // private methods
rt300@0 195 void testConnection();
rt300@0 196 void checkLogFile();
rt300@0 197 void deleteLogs(); // new user
rt300@0 198 bool uploadEventLog(bool async);
rt300@0 199 void firstEverAppOpen();
rt300@0 200 void loadExistingLogFile(const string &jsonFile);
rt300@0 201 void uploadQuestionnaire();
rt300@0 202 bool sendToServer(string functionName, Json::Value jsonData, bool async);
rt300@0 203
rt300@0 204 Json::Value logsToJson();
rt300@0 205 Json::Value questionnaireToJson();
rt300@0 206
rt300@0 207
rt300@0 208 template<class T> bool matchID(T thing);
rt300@0 209 bool matchID2();
rt300@0 210 //
rt300@0 211 ServerComms *serverComms;
rt300@0 212
rt300@0 213 };
rt300@0 214
rt300@0 215
rt300@0 216 //---------------------------------------------------------------------------
rt300@0 217
rt300@0 218
rt300@0 219 #endif /* defined(__oscSenderExample__eventLogger__) */